From af2bb2089327ab4a07dc71dae31b84d2e6a089c2 Mon Sep 17 00:00:00 2001 From: Felix Oghina Date: Tue, 16 Aug 2016 07:33:50 -0700 Subject: [PATCH] make main component name nullable Summary: Sometimes the main component name is not known at activity construction time and depends on e.g. reading shared preferences. To support this use case, make `(Fragment)ReactActivity#getMainComponentName()` nullable and return `null` by default. In this case, the app will not be loaded in `onCreate` by default and the user has to call `loadApp` manually once the component name is known. Reviewed By: andreicoman11 Differential Revision: D3722517 fbshipit-source-id: 062eed158798606e4160f1c142b23fd98ca618c8 --- .../com/facebook/react/ReactActivity.java | 10 ++++++++- .../facebook/react/ReactActivityDelegate.java | 21 ++++++++++++++----- .../facebook/react/ReactFragmentActivity.java | 10 ++++++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java b/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java index 75f2b694b..926b665e6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java @@ -9,6 +9,8 @@ package com.facebook.react; +import javax.annotation.Nullable; + import android.app.Activity; import android.content.Intent; import android.os.Bundle; @@ -35,7 +37,9 @@ public abstract class ReactActivity extends Activity * This is used to schedule rendering of the component. * e.g. "MoviesApp" */ - protected abstract String getMainComponentName(); + protected @Nullable String getMainComponentName() { + return null; + } /** * Called at construction time, override if you have a custom delegate implementation. @@ -120,4 +124,8 @@ public abstract class ReactActivity extends Activity protected final ReactInstanceManager getReactInstanceManager() { return mDelegate.getReactInstanceManager(); } + + protected final void loadApp(String appKey) { + mDelegate.loadApp(appKey); + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java b/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java index a5b0da68e..5afb72203 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java @@ -33,19 +33,21 @@ public class ReactActivityDelegate { private final @Nullable Activity mActivity; private final @Nullable FragmentActivity mFragmentActivity; - private final String mMainComponentName; + private final @Nullable String mMainComponentName; private @Nullable ReactRootView mReactRootView; private @Nullable DoubleTapReloadRecognizer mDoubleTapReloadRecognizer; private @Nullable PermissionListener mPermissionListener; - public ReactActivityDelegate(Activity activity, String mainComponentName) { + public ReactActivityDelegate(Activity activity, @Nullable String mainComponentName) { mActivity = activity; mMainComponentName = mainComponentName; mFragmentActivity = null; } - public ReactActivityDelegate(FragmentActivity fragmentActivity, String mainComponentName) { + public ReactActivityDelegate( + FragmentActivity fragmentActivity, + @Nullable String mainComponentName) { mFragmentActivity = fragmentActivity; mMainComponentName = mainComponentName; mActivity = null; @@ -85,13 +87,22 @@ public class ReactActivityDelegate { } } + if (mMainComponentName != null) { + loadApp(mMainComponentName); + } + mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer(); + } + + protected void loadApp(String appKey) { + if (mReactRootView != null) { + throw new IllegalStateException("Cannot loadApp while app is already running."); + } mReactRootView = createRootView(); mReactRootView.startReactApplication( getReactNativeHost().getReactInstanceManager(), - mMainComponentName, + appKey, getLaunchOptions()); getPlainActivity().setContentView(mReactRootView); - mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer(); } protected void onPause() { diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactFragmentActivity.java b/ReactAndroid/src/main/java/com/facebook/react/ReactFragmentActivity.java index a966a29bd..9671b0a1d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactFragmentActivity.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactFragmentActivity.java @@ -9,6 +9,8 @@ package com.facebook.react; +import javax.annotation.Nullable; + import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; @@ -36,7 +38,9 @@ public abstract class ReactFragmentActivity extends FragmentActivity implements * This is used to schedule rendering of the component. * e.g. "MoviesApp" */ - protected abstract String getMainComponentName(); + protected @Nullable String getMainComponentName() { + return null; + } /** * Called at construction time, override if you have a custom delegate implementation. @@ -121,4 +125,8 @@ public abstract class ReactFragmentActivity extends FragmentActivity implements protected final ReactInstanceManager getReactInstanceManager() { return mDelegate.getReactInstanceManager(); } + + protected final void loadApp(String appKey) { + mDelegate.loadApp(appKey); + } }