瀏覽代碼

Allow subclasses of NavigationApplication to provide a custom ReactNativeHost. (#2552)

Currently the default NavigationReactNativeHost only adds the NavigationPackage. Users of React Native Navigation may need to override additional methods in ReactNativeHost. This change allows advanced users to provide their own implemenation of ReactNativeHost, with the requirement that they add the NavigationPackage themselves.
Alec B. Plumb 6 年之前
父節點
當前提交
f8fa7341a1

+ 18
- 1
lib/android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java 查看文件

@@ -19,9 +19,22 @@ public abstract class NavigationApplication extends Application implements React
19 19
 	public void onCreate() {
20 20
 		super.onCreate();
21 21
         instance = this;
22
-        reactGateway = new ReactGateway(this, isDebug(), createAdditionalReactPackages());
22
+        reactGateway = createReactGateway();
23 23
 	}
24 24
 
25
+    /**
26
+     * Subclasses of NavigationApplication may override this method to create the singleton instance
27
+     * of {@link ReactGateway}. For example, subclasses may wish to provide a custom {@link ReactNativeHost}
28
+     * with the ReactGateway. This method will be called exactly once, in the application's {@link #onCreate()} method.
29
+     *
30
+     * Custom {@link ReactNativeHost}s must be sure to include {@link com.reactnativenavigation.react.NavigationPackage}
31
+     *
32
+     * @return a singleton {@link ReactGateway}
33
+     */
34
+	protected ReactGateway createReactGateway() {
35
+	    return new ReactGateway(this, isDebug(), createAdditionalReactPackages());
36
+    }
37
+
25 38
 	public ReactGateway getReactGateway() {
26 39
 		return reactGateway;
27 40
 	}
@@ -33,6 +46,10 @@ public abstract class NavigationApplication extends Application implements React
33 46
 
34 47
 	public abstract boolean isDebug();
35 48
 
49
+    /**
50
+     * Create a list of additional {@link ReactPackage}s to include. This method will only be called by
51
+     * the default implementation of {@link #createReactGateway()}
52
+     */
36 53
 	@Nullable
37 54
 	public abstract List<ReactPackage> createAdditionalReactPackages();
38 55
 }

+ 6
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationReactNativeHost.java 查看文件

@@ -9,6 +9,10 @@ import com.facebook.react.shell.MainReactPackage;
9 9
 import java.util.ArrayList;
10 10
 import java.util.List;
11 11
 
12
+/**
13
+ * Default implementation of {@link ReactNativeHost} that includes {@link NavigationPackage}
14
+ * and user-defined additional packages.
15
+ */
12 16
 public class NavigationReactNativeHost extends ReactNativeHost {
13 17
 
14 18
 	private final boolean isDebug;
@@ -27,6 +31,8 @@ public class NavigationReactNativeHost extends ReactNativeHost {
27 31
 
28 32
 	@Override
29 33
 	protected List<ReactPackage> getPackages() {
34
+		// Please note that users may provide their own implementations of ReactNativeHost. Any additional
35
+		// package requirements should be documented in the changelog.
30 36
 		List<ReactPackage> packages = new ArrayList<>();
31 37
 		packages.add(new MainReactPackage());
32 38
 		packages.add(new NavigationPackage(this));

+ 9
- 3
lib/android/app/src/main/java/com/reactnativenavigation/react/ReactGateway.java 查看文件

@@ -1,5 +1,7 @@
1 1
 package com.reactnativenavigation.react;
2 2
 
3
+import android.app.Application;
4
+
3 5
 import com.facebook.react.ReactNativeHost;
4 6
 import com.facebook.react.ReactPackage;
5 7
 import com.facebook.soloader.SoLoader;
@@ -10,13 +12,17 @@ import java.util.List;
10 12
 
11 13
 public class ReactGateway {
12 14
 
13
-	private final NavigationReactNativeHost reactNativeHost;
15
+	private final ReactNativeHost reactNativeHost;
14 16
 	private final NavigationReactInitializer initializer;
15 17
 	private final JsDevReloadHandler jsDevReloadHandler;
16 18
 
17
-	public ReactGateway(final NavigationApplication application, final boolean isDebug, final List<ReactPackage> additionalReactPackages) {
19
+	public ReactGateway(final Application application, final boolean isDebug, final List<ReactPackage> additionalReactPackages) {
20
+		this(application, isDebug, new NavigationReactNativeHost(application, isDebug, additionalReactPackages));
21
+	}
22
+
23
+	public ReactGateway(final Application application, final boolean isDebug, final ReactNativeHost reactNativeHost) {
18 24
 		SoLoader.init(application, false);
19
-		reactNativeHost = new NavigationReactNativeHost(application, isDebug, additionalReactPackages);
25
+		this.reactNativeHost = reactNativeHost;
20 26
 		initializer = new NavigationReactInitializer(reactNativeHost.getReactInstanceManager(), isDebug);
21 27
 		jsDevReloadHandler = new JsDevReloadHandler(reactNativeHost.getReactInstanceManager());
22 28
 	}