Skip to content

Commit

Permalink
Create react host after SoLoader.init (#5707)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
guyca authored and yogevbd committed Dec 1, 2019
1 parent 2cd4752 commit ab2fa63
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.reactnativenavigation;

import android.app.Application;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
Expand All @@ -15,6 +13,9 @@
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public abstract class NavigationApplication extends Application implements ReactApplication {

private ReactGateway reactGateway;
Expand All @@ -38,7 +39,7 @@ public void onCreate() {
* @return a singleton {@link ReactGateway}
*/
protected ReactGateway createReactGateway() {
return new ReactGateway(this, isDebug(), createReactNativeHost());
return new ReactGateway(this, isDebug(), this::createReactNativeHost);
}

protected ReactNativeHost createReactNativeHost() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import com.reactnativenavigation.NavigationActivity;
import com.reactnativenavigation.utils.Functions.FuncR;

import java.util.List;

Expand All @@ -22,15 +23,20 @@ public ReactGateway(final Application application, final boolean isDebug, final
this(application, isDebug, new NavigationReactNativeHost(application, isDebug, additionalReactPackages));
}

public ReactGateway(final Application application, final boolean isDebug, final ReactNativeHost host) {
@SuppressWarnings("WeakerAccess")
public ReactGateway(final Application application, final boolean isDebug, final ReactNativeHost host) {
this(application, isDebug, () -> host);
}

public ReactGateway(final Application application, final boolean isDebug, FuncR<ReactNativeHost> hostCreator) {
SoLoader.init(application, false);
this.host = host;
initializer = new NavigationReactInitializer(host.getReactInstanceManager(), isDebug);
jsDevReloadHandler = new JsDevReloadHandler(host.getReactInstanceManager().getDevSupportManager());
this.host = hostCreator.run();
initializer = new NavigationReactInitializer(host.getReactInstanceManager(), isDebug);
jsDevReloadHandler = new JsDevReloadHandler(host.getReactInstanceManager().getDevSupportManager());
if (host instanceof BundleDownloadListenerProvider) {
((BundleDownloadListenerProvider) host).setBundleLoaderListener(jsDevReloadHandler);
}
}
}

public ReactNativeHost getReactNativeHost() {
return host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public interface Func1<T> {
void run(T param);
}

public interface FuncR<T> {
T run();
}

public interface FuncR1<T, S> {
S run(T param);
}
Expand Down

0 comments on commit ab2fa63

Please sign in to comment.