@@ -40,38 +40,8 @@ public abstract class ReactActivity extends Activity
40
40
private @ Nullable PermissionListener mPermissionListener ;
41
41
private @ Nullable ReactInstanceManager mReactInstanceManager ;
42
42
private @ Nullable ReactRootView mReactRootView ;
43
- private LifecycleState mLifecycleState = LifecycleState .BEFORE_RESUME ;
44
43
private DoubleTapReloadRecognizer mDoubleTapReloadRecognizer ;
45
-
46
- /**
47
- * Returns the name of the bundle in assets. If this is null, and no file path is specified for
48
- * the bundle, the app will only work with {@code getUseDeveloperSupport} enabled and will
49
- * always try to load the JS bundle from the packager server.
50
- * e.g. "index.android.bundle"
51
- */
52
- protected @ Nullable String getBundleAssetName () {
53
- return "index.android.bundle" ;
54
- };
55
-
56
- /**
57
- * Returns a custom path of the bundle file. This is used in cases the bundle should be loaded
58
- * from a custom path. By default it is loaded from Android assets, from a path specified
59
- * by {@link getBundleAssetName}.
60
- * e.g. "file://sdcard/myapp_cache/index.android.bundle"
61
- */
62
- protected @ Nullable String getJSBundleFile () {
63
- return null ;
64
- }
65
-
66
- /**
67
- * Returns the name of the main module. Determines the URL used to fetch the JS bundle
68
- * from the packager server. It is only used when dev support is enabled.
69
- * This is the first file to be executed once the {@link ReactInstanceManager} is created.
70
- * e.g. "index.android"
71
- */
72
- protected String getJSMainModuleName () {
73
- return "index.android" ;
74
- }
44
+ private boolean mDoRefresh = false ;
75
45
76
46
/**
77
47
* Returns the launchOptions which will be passed to the {@link ReactInstanceManager}
@@ -92,48 +62,31 @@ protected String getJSMainModuleName() {
92
62
protected abstract String getMainComponentName ();
93
63
94
64
/**
95
- * Returns whether dev mode should be enabled. This enables e.g. the dev menu.
96
- */
97
- protected abstract boolean getUseDeveloperSupport ();
98
-
99
- /**
100
- * Returns a list of {@link ReactPackage} used by the app.
101
- * You'll most likely want to return at least the {@code MainReactPackage}.
102
- * If your app uses additional views or modules besides the default ones,
103
- * you'll want to include more packages here.
65
+ * A subclass may override this method if it needs to use a custom {@link ReactRootView}.
104
66
*/
105
- protected abstract List <ReactPackage > getPackages ();
67
+ protected ReactRootView createRootView () {
68
+ return new ReactRootView (this );
69
+ }
106
70
107
71
/**
108
- * A subclass may override this method if it needs to use a custom instance.
72
+ * Get the {@link ReactNativeHost} used by this app. By default, assumes {@link #getApplication()}
73
+ * is an instance of {@link ReactApplication} and calls
74
+ * {@link ReactApplication#getReactNativeHost()}. Override this method if your application class
75
+ * does not implement {@code ReactApplication} or you simply have a different mechanism for
76
+ * storing a {@code ReactNativeHost}, e.g. as a static field somewhere.
109
77
*/
110
- protected ReactInstanceManager createReactInstanceManager () {
111
- ReactInstanceManager .Builder builder = ReactInstanceManager .builder ()
112
- .setApplication (getApplication ())
113
- .setJSMainModuleName (getJSMainModuleName ())
114
- .setUseDeveloperSupport (getUseDeveloperSupport ())
115
- .setInitialLifecycleState (mLifecycleState );
116
-
117
- for (ReactPackage reactPackage : getPackages ()) {
118
- builder .addPackage (reactPackage );
119
- }
120
-
121
- String jsBundleFile = getJSBundleFile ();
122
-
123
- if (jsBundleFile != null ) {
124
- builder .setJSBundleFile (jsBundleFile );
125
- } else {
126
- builder .setBundleAssetName (getBundleAssetName ());
127
- }
128
-
129
- return builder .build ();
78
+ protected ReactNativeHost getReactNativeHost () {
79
+ return ((ReactApplication ) getApplication ()).getReactNativeHost ();
130
80
}
131
81
132
82
/**
133
- * A subclass may override this method if it needs to use a custom {@link ReactRootView}.
83
+ * Get whether developer support should be enabled or not. By default this delegates to
84
+ * {@link ReactNativeHost#getUseDeveloperSupport()}. Override this method if your application
85
+ * class does not implement {@code ReactApplication} or you simply have a different logic for
86
+ * determining this (default just checks {@code BuildConfig}).
134
87
*/
135
- protected ReactRootView createRootView () {
136
- return new ReactRootView ( this );
88
+ protected boolean getUseDeveloperSupport () {
89
+ return (( ReactApplication ) getApplication ()). getReactNativeHost (). getUseDeveloperSupport ( );
137
90
}
138
91
139
92
@ Override
@@ -150,9 +103,11 @@ protected void onCreate(Bundle savedInstanceState) {
150
103
}
151
104
}
152
105
153
- mReactInstanceManager = createReactInstanceManager ();
154
106
mReactRootView = createRootView ();
155
- mReactRootView .startReactApplication (mReactInstanceManager , getMainComponentName (), getLaunchOptions ());
107
+ mReactRootView .startReactApplication (
108
+ getReactNativeHost ().getReactInstanceManager (),
109
+ getMainComponentName (),
110
+ getLaunchOptions ());
156
111
setContentView (mReactRootView );
157
112
mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer ();
158
113
}
@@ -161,62 +116,57 @@ protected void onCreate(Bundle savedInstanceState) {
161
116
protected void onPause () {
162
117
super .onPause ();
163
118
164
- mLifecycleState = LifecycleState .BEFORE_RESUME ;
165
-
166
- if (mReactInstanceManager != null ) {
167
- mReactInstanceManager .onHostPause ();
119
+ if (getReactNativeHost ().hasInstance ()) {
120
+ getReactNativeHost ().getReactInstanceManager ().onHostPause ();
168
121
}
169
122
}
170
123
171
124
@ Override
172
125
protected void onResume () {
173
126
super .onResume ();
174
127
175
- mLifecycleState = LifecycleState .RESUMED ;
176
-
177
- if (mReactInstanceManager != null ) {
178
- mReactInstanceManager .onHostResume (this , this );
128
+ if (getReactNativeHost ().hasInstance ()) {
129
+ getReactNativeHost ().getReactInstanceManager ().onHostResume (this , this );
179
130
}
180
131
}
181
132
182
133
@ Override
183
134
protected void onDestroy () {
184
135
super .onDestroy ();
185
136
186
- mReactRootView .unmountReactApplication ();
187
- mReactRootView = null ;
188
-
189
- if (mReactInstanceManager != null ) {
190
- mReactInstanceManager .destroy ();
137
+ if (mReactRootView != null ) {
138
+ mReactRootView .unmountReactApplication ();
139
+ mReactRootView = null ;
191
140
}
141
+ getReactNativeHost ().clear ();
192
142
}
193
143
194
144
@ Override
195
145
public void onActivityResult (int requestCode , int resultCode , Intent data ) {
196
- if (mReactInstanceManager != null ) {
197
- mReactInstanceManager .onActivityResult (requestCode , resultCode , data );
146
+ if (getReactNativeHost ().hasInstance ()) {
147
+ getReactNativeHost ().getReactInstanceManager ()
148
+ .onActivityResult (requestCode , resultCode , data );
198
149
}
199
150
}
200
151
201
152
@ Override
202
153
public boolean onKeyUp (int keyCode , KeyEvent event ) {
203
- if (mReactInstanceManager != null &&
204
- mReactInstanceManager .getDevSupportManager ().getDevSupportEnabled ()) {
154
+ if (getReactNativeHost ().hasInstance () && getUseDeveloperSupport ()) {
205
155
if (keyCode == KeyEvent .KEYCODE_MENU ) {
206
- mReactInstanceManager .showDevOptionsDialog ();
156
+ getReactNativeHost (). getReactInstanceManager () .showDevOptionsDialog ();
207
157
return true ;
208
158
}
209
159
if (mDoubleTapReloadRecognizer .didDoubleTapR (keyCode , getCurrentFocus ())) {
210
- mReactInstanceManager .getDevSupportManager ().handleReloadJS ();
160
+ getReactNativeHost (). getReactInstanceManager () .getDevSupportManager ().handleReloadJS ();
211
161
}
212
162
}
213
163
return super .onKeyUp (keyCode , event );
214
164
}
215
165
216
166
@ Override
217
167
public void onBackPressed () {
218
- if (mReactInstanceManager != null ) {
219
- mReactInstanceManager .onBackPressed ();
168
+ if (getReactNativeHost (). hasInstance () ) {
169
+ getReactNativeHost (). getReactInstanceManager () .onBackPressed ();
220
170
} else {
221
171
super .onBackPressed ();
222
172
}
@@ -229,8 +179,8 @@ public void invokeDefaultOnBackPressed() {
229
179
230
180
@ Override
231
181
public void onNewIntent (Intent intent ) {
232
- if (mReactInstanceManager != null ) {
233
- mReactInstanceManager .onNewIntent (intent );
182
+ if (getReactNativeHost (). hasInstance () ) {
183
+ getReactNativeHost (). getReactInstanceManager () .onNewIntent (intent );
234
184
} else {
235
185
super .onNewIntent (intent );
236
186
}
0 commit comments