From a8449c14ac375b003a66670ad47903dd45860512 Mon Sep 17 00:00:00 2001 From: August Lilleaas Date: Mon, 25 Feb 2019 00:37:21 -0800 Subject: [PATCH] Allow ReactActivityDelegate subclass to overrride mainComponentName (#19814) Summary: Currently, the final field mMainComponentName is used. This field is initialized in the constructor of ReactActivityDelegate, and the ReactActivityDelegate itself is initialized in the constructor of ReactActivity. This means that the only way you can pass a main component name to ReactActivityDelegate, is when your ReactActivity subclass is constructed. At this point in the lifecycle of an activity, the getIntent() value that the activity was initialized by returns null, making it impossible to set the mainComponentName dynamically based on data passed to the activity via an intent. The mMainComponentName final field is also only used in onCreate of the delegate, so it's not actually needed by the ReactActivityDelegate at construction time. So the above limitation is not fundamental, it's just a side effect of the API design. By allowing subclasses of ReactActivityDelegate to implement a getMainComponentName method, the subclass then has full control of how to initialize the value. So an implementation of getMainComponentName could be: public String getMainComponentName() { return getIntent().getStringExtra("reactMainComponentName"); } This commit doesn't remove anything and only adds a new method, so it should be fully backwards compatible. Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. Pull Request resolved: https://github.com/facebook/react-native/pull/19814 Differential Revision: D14206644 Pulled By: cpojer fbshipit-source-id: 8532560f944362fe0cce263a7f9a503df8ae539f --- .../java/com/facebook/react/ReactActivityDelegate.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java b/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java index 99811b7fefc10d..26a95665654d32 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java @@ -70,9 +70,14 @@ public ReactInstanceManager getReactInstanceManager() { return getReactNativeHost().getReactInstanceManager(); } + public String getMainComponentName() { + return mMainComponentName; + } + protected void onCreate(Bundle savedInstanceState) { - if (mMainComponentName != null) { - loadApp(mMainComponentName); + String mainComponentName = getMainComponentName(); + if (mainComponentName != null) { + loadApp(mainComponentName); } mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer(); }