Browse Source

Create react host after SoLoader.init (#5707)

Under certain configurations, creating ReactNativeHost before SoLoader.init throws an exception.
This commit changes the behaviour of the main ReactGateway constructor to ensures host is created after SoLoader.init.
Guy Carmeli 5 years ago
parent
commit
ab2fa632c2

+ 4
- 3
lib/android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java View File

@@ -1,8 +1,6 @@
1 1
 package com.reactnativenavigation;
2 2
 
3 3
 import android.app.Application;
4
-import androidx.annotation.Nullable;
5
-import androidx.annotation.NonNull;
6 4
 
7 5
 import com.facebook.react.ReactApplication;
8 6
 import com.facebook.react.ReactNativeHost;
@@ -15,6 +13,9 @@ import java.util.HashMap;
15 13
 import java.util.List;
16 14
 import java.util.Map;
17 15
 
16
+import androidx.annotation.NonNull;
17
+import androidx.annotation.Nullable;
18
+
18 19
 public abstract class NavigationApplication extends Application implements ReactApplication {
19 20
 
20 21
 	private ReactGateway reactGateway;
@@ -38,7 +39,7 @@ public abstract class NavigationApplication extends Application implements React
38 39
      * @return a singleton {@link ReactGateway}
39 40
      */
40 41
 	protected ReactGateway createReactGateway() {
41
-	    return new ReactGateway(this, isDebug(), createReactNativeHost());
42
+	    return new ReactGateway(this, isDebug(), this::createReactNativeHost);
42 43
     }
43 44
 
44 45
     protected ReactNativeHost createReactNativeHost() {

+ 11
- 5
lib/android/app/src/main/java/com/reactnativenavigation/react/ReactGateway.java View File

@@ -8,6 +8,7 @@ import com.facebook.react.ReactNativeHost;
8 8
 import com.facebook.react.ReactPackage;
9 9
 import com.facebook.soloader.SoLoader;
10 10
 import com.reactnativenavigation.NavigationActivity;
11
+import com.reactnativenavigation.utils.Functions.FuncR;
11 12
 
12 13
 import java.util.List;
13 14
 
@@ -22,15 +23,20 @@ public class ReactGateway {
22 23
 		this(application, isDebug, new NavigationReactNativeHost(application, isDebug, additionalReactPackages));
23 24
 	}
24 25
 
25
-	public ReactGateway(final Application application, final boolean isDebug, final ReactNativeHost host) {
26
+    @SuppressWarnings("WeakerAccess")
27
+    public ReactGateway(final Application application, final boolean isDebug, final ReactNativeHost host) {
28
+        this(application, isDebug, () -> host);
29
+    }
30
+
31
+    public ReactGateway(final Application application, final boolean isDebug, FuncR<ReactNativeHost> hostCreator) {
26 32
         SoLoader.init(application, false);
27
-		this.host = host;
28
-		initializer = new NavigationReactInitializer(host.getReactInstanceManager(), isDebug);
29
-		jsDevReloadHandler = new JsDevReloadHandler(host.getReactInstanceManager().getDevSupportManager());
33
+        this.host = hostCreator.run();
34
+        initializer = new NavigationReactInitializer(host.getReactInstanceManager(), isDebug);
35
+        jsDevReloadHandler = new JsDevReloadHandler(host.getReactInstanceManager().getDevSupportManager());
30 36
         if (host instanceof BundleDownloadListenerProvider) {
31 37
             ((BundleDownloadListenerProvider) host).setBundleLoaderListener(jsDevReloadHandler);
32 38
         }
33
-	}
39
+    }
34 40
 
35 41
 	public ReactNativeHost getReactNativeHost() {
36 42
 		return host;

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/Functions.java View File

@@ -13,6 +13,10 @@ public class Functions {
13 13
         void run(T param);
14 14
     }
15 15
 
16
+    public interface FuncR<T> {
17
+        T run();
18
+    }
19
+
16 20
     public interface FuncR1<T, S> {
17 21
         S run(T param);
18 22
     }