diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index 320b3c3fcf75b3..217d2c117b53bc 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -1044,7 +1044,7 @@ private void attachRootViewToInstance( UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class); final int rootTag = uiManagerModule.addRootView(rootView); rootView.setRootViewTag(rootTag); - rootView.runApplication(); + rootView.invokeJSEntryPoint(); Systrace.beginAsyncSection( TRACE_TAG_REACT_JAVA_BRIDGE, "pre_rootView.onAttachedToReactInstance", diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java b/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java index 05cfc8d776ed80..045dab14fdb917 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java @@ -87,6 +87,7 @@ public interface ReactRootViewEventListener { private boolean mWasMeasured = false; private int mWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); private int mHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); + private @Nullable Runnable mJSEntryPoint; public ReactRootView(Context context) { super(context); @@ -379,7 +380,7 @@ public void setAppProperties(@Nullable Bundle appProperties) { UiThreadUtil.assertOnUiThread(); mAppProperties = appProperties; if (getRootViewTag() != 0) { - runApplication(); + invokeJSEntryPoint(); } } @@ -387,34 +388,61 @@ public void setAppProperties(@Nullable Bundle appProperties) { * Calls into JS to start the React application. Can be called multiple times with the * same rootTag, which will re-render the application from the root. */ - /* package */ void runApplication() { - Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactRootView.runApplication"); - try { - if (mReactInstanceManager == null || !mIsAttachedToInstance) { - return; - } + /*package */ void invokeJSEntryPoint() { + if (mJSEntryPoint == null) { + defaultJSEntryPoint(); + } else { + mJSEntryPoint.run(); + } + } - ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); - if (reactContext == null) { - return; - } + /** + * Set a custom entry point for invoking JS. By default, this is AppRegistry.runApplication + * @param jsEntryPoint + */ + public void setJSEntryPoint(Runnable jsEntryPoint) { + mJSEntryPoint = jsEntryPoint; + } - CatalystInstance catalystInstance = reactContext.getCatalystInstance(); + public void invokeDefaultJSEntryPoint(@Nullable Bundle appProperties) { + UiThreadUtil.assertOnUiThread(); + if (appProperties != null) { + mAppProperties = appProperties; + } + defaultJSEntryPoint(); + } - WritableNativeMap appParams = new WritableNativeMap(); - appParams.putDouble("rootTag", getRootViewTag()); - @Nullable Bundle appProperties = getAppProperties(); - if (appProperties != null) { - appParams.putMap("initialProps", Arguments.fromBundle(appProperties)); - } + /** + * Calls the default entry point into JS which is AppRegistry.runApplication() + */ + private void defaultJSEntryPoint() { + Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactRootView.runApplication"); + try { + if (mReactInstanceManager == null || !mIsAttachedToInstance) { + return; + } - mShouldLogContentAppeared = true; + ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); + if (reactContext == null) { + return; + } - String jsAppModuleName = getJSModuleName(); - catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams); - } finally { - Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); - } + CatalystInstance catalystInstance = reactContext.getCatalystInstance(); + + WritableNativeMap appParams = new WritableNativeMap(); + appParams.putDouble("rootTag", getRootViewTag()); + @Nullable Bundle appProperties = getAppProperties(); + if (appProperties != null) { + appParams.putMap("initialProps", Arguments.fromBundle(appProperties)); + } + + mShouldLogContentAppeared = true; + + String jsAppModuleName = getJSModuleName(); + catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams); + } finally { + Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); + } } /**