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
This commit is contained in:
Felix Oghina 2016-08-16 07:33:50 -07:00 committed by Facebook Github Bot 8
parent 2f78852411
commit af2bb20893
3 changed files with 34 additions and 7 deletions

View File

@ -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);
}
}

View File

@ -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() {

View File

@ -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);
}
}