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 4 years ago
parent
commit
ab2fa632c2

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

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

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

8
 import com.facebook.react.ReactPackage;
8
 import com.facebook.react.ReactPackage;
9
 import com.facebook.soloader.SoLoader;
9
 import com.facebook.soloader.SoLoader;
10
 import com.reactnativenavigation.NavigationActivity;
10
 import com.reactnativenavigation.NavigationActivity;
11
+import com.reactnativenavigation.utils.Functions.FuncR;
11
 
12
 
12
 import java.util.List;
13
 import java.util.List;
13
 
14
 
22
 		this(application, isDebug, new NavigationReactNativeHost(application, isDebug, additionalReactPackages));
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
         SoLoader.init(application, false);
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
         if (host instanceof BundleDownloadListenerProvider) {
36
         if (host instanceof BundleDownloadListenerProvider) {
31
             ((BundleDownloadListenerProvider) host).setBundleLoaderListener(jsDevReloadHandler);
37
             ((BundleDownloadListenerProvider) host).setBundleLoaderListener(jsDevReloadHandler);
32
         }
38
         }
33
-	}
39
+    }
34
 
40
 
35
 	public ReactNativeHost getReactNativeHost() {
41
 	public ReactNativeHost getReactNativeHost() {
36
 		return host;
42
 		return host;

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

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