From 57b0039ce1c89423cd3038e411a12df7e572a46b Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Tue, 25 Apr 2017 10:11:08 -0700 Subject: [PATCH] Add appProperty to ReactRootView MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: To get on par with iOS this PR adds an `appProperty` to the `ReactRootView`. The documentation on the iOS side is here: [https://facebook.github.io/react-native/docs/communication-ios.html#properties.](https://facebook.github.io/react-native/docs/communication-ios.html#properties.) You can pass in initial props with the `startReactApplication` method: ```java … Bundle initialProps = new Bundle(); 
bundle.putString(“initialKey”, “initialValue”); mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", initialProps); setContentView(mReactRootView); … ``` And later on properties can be updated this way: ```java … // Update props Bundle updatedProps = mReactRootView.getAppProperties();
 updatedProps.putString(“someOtherKey”, “someOtherValue”);
 mReactRootView.setAppProperties(updatedProps); // Replace props Bundle newProps = new Bundle();
 newProps.putString(“someKey”, “someValue”);
 mReactRootView.setAppProperties(newProps); … Closes https://github.com/facebook/react-native/pull/13430 Reviewed By: AaaChiuuu Differential Revision: D4896483 Pulled By: javache fbshipit-source-id: 7c752d6bbf5dc500874b49dcff80db772e83915f --- .../facebook/react/ReactInstanceManager.java | 9 +--- .../com/facebook/react/ReactRootView.java | 46 +++++++++++++++++-- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index 859d4c8d16..4b0a84f5a0 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -839,14 +839,7 @@ public class ReactInstanceManager { UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class); int rootTag = uiManagerModule.addMeasuredRootView(rootView); rootView.setRootViewTag(rootTag); - @Nullable Bundle launchOptions = rootView.getLaunchOptions(); - WritableMap initialProps = Arguments.makeNativeMap(launchOptions); - String jsAppModuleName = rootView.getJSModuleName(); - - WritableNativeMap appParams = new WritableNativeMap(); - appParams.putDouble("rootTag", rootTag); - appParams.putMap("initialProps", initialProps); - catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams); + rootView.runApplication(); UiThreadUtil.runOnUiThread(new Runnable() { @Override public void run() { diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java b/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java index 8babff13d1..ba3f752e5b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java @@ -25,11 +25,14 @@ import android.view.WindowManager; import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.CatalystInstance; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.bridge.WritableMap; +import com.facebook.react.bridge.WritableNativeMap; import com.facebook.react.common.ReactConstants; import com.facebook.react.common.annotations.VisibleForTesting; +import com.facebook.react.modules.appregistry.AppRegistry; import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.modules.deviceinfo.DeviceInfoModule; import com.facebook.react.uimanager.DisplayMetricsHolder; @@ -68,7 +71,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView private @Nullable ReactInstanceManager mReactInstanceManager; private @Nullable String mJSModuleName; - private @Nullable Bundle mLaunchOptions; + private @Nullable Bundle mAppProperties; private @Nullable CustomGlobalLayoutListener mCustomGlobalLayoutListener; private @Nullable ReactRootViewEventListener mRootViewEventListener; private int mRootViewTag; @@ -197,7 +200,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView public void startReactApplication( ReactInstanceManager reactInstanceManager, String moduleName, - @Nullable Bundle launchOptions) { + @Nullable Bundle initialProperties) { UiThreadUtil.assertOnUiThread(); // TODO(6788889): Use POJO instead of bundle here, apparently we can't just use WritableMap @@ -209,7 +212,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView mReactInstanceManager = reactInstanceManager; mJSModuleName = moduleName; - mLaunchOptions = launchOptions; + mAppProperties = initialProperties; if (!mReactInstanceManager.hasStartedCreatingInitialContext()) { mReactInstanceManager.createReactContextInBackground(); @@ -249,8 +252,41 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView return Assertions.assertNotNull(mJSModuleName); } - /* package */ @Nullable Bundle getLaunchOptions() { - return mLaunchOptions; + public @Nullable Bundle getAppProperties() { + return mAppProperties; + } + + public void setAppProperties(@Nullable Bundle appProperties) { + UiThreadUtil.assertOnUiThread(); + mAppProperties = appProperties; + runApplication(); + } + + /** + * 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() { + if (mReactInstanceManager == null || !mIsAttachedToInstance) { + return; + } + + ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); + if (reactContext == null) { + return; + } + + 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)); + } + + String jsAppModuleName = getJSModuleName(); + catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams); } /**