@@ -105,67 +105,64 @@ private void loadBundle() {
105105 // The currentActivity can be null if it is backgrounded / destroyed, so we simply
106106 // no-op to prevent any null pointer exceptions.
107107 return ;
108- } else if (!ReactActivity .class .isInstance (currentActivity )) {
109- // Our preferred reload logic relies on the user's Activity inheriting
110- // from the core ReactActivity class, so if it doesn't, we fallback
111- // early to our legacy behavior.
112- loadBundleLegacy (currentActivity );
113- } else {
114- try {
115- ReactActivity reactActivity = (ReactActivity )currentActivity ;
116- ReactInstanceManager instanceManager ;
108+ }
117109
118- // #1) Get the ReactInstanceManager instance, which is what includes the
119- // logic to reload the current React context.
120- try {
121- // In RN 0.29, the "mReactInstanceManager" field yields a null value, so we try
122- // to get the instance manager via the ReactNativeHost, which only exists in 0.29.
123- Method getApplicationMethod = ReactActivity .class .getMethod ("getApplication" );
124- Object reactApplication = getApplicationMethod .invoke (reactActivity );
125- Class <?> reactApplicationClass = tryGetClass (REACT_APPLICATION_CLASS_NAME );
126- Method getReactNativeHostMethod = reactApplicationClass .getMethod ("getReactNativeHost" );
127- Object reactNativeHost = getReactNativeHostMethod .invoke (reactApplication );
128- Class <?> reactNativeHostClass = tryGetClass (REACT_NATIVE_HOST_CLASS_NAME );
129- Method getReactInstanceManagerMethod = reactNativeHostClass .getMethod ("getReactInstanceManager" );
130- instanceManager = (ReactInstanceManager )getReactInstanceManagerMethod .invoke (reactNativeHost );
131- } catch (Exception e ) {
132- // The React Native version might be older than 0.29, so we try to get the
133- // instance manager via the "mReactInstanceManager" field.
134- Field instanceManagerField = ReactActivity .class .getDeclaredField ("mReactInstanceManager" );
135- instanceManagerField .setAccessible (true );
136- instanceManager = (ReactInstanceManager )instanceManagerField .get (reactActivity );
137- }
110+ try {
111+ ReactInstanceManager instanceManager ;
112+ // #1) Get the ReactInstanceManager instance, which is what includes the
113+ // logic to reload the current React context.
114+ try {
115+ // In RN >=0.29, the "mReactInstanceManager" field yields a null value, so we try
116+ // to get the instance manager via the ReactNativeHost, which only exists in 0.29.
117+ Method getApplicationMethod = ReactActivity .class .getMethod ("getApplication" );
118+ Object reactApplication = getApplicationMethod .invoke (currentActivity );
119+ Class <?> reactApplicationClass = tryGetClass (REACT_APPLICATION_CLASS_NAME );
120+ Method getReactNativeHostMethod = reactApplicationClass .getMethod ("getReactNativeHost" );
121+ Object reactNativeHost = getReactNativeHostMethod .invoke (reactApplication );
122+ Class <?> reactNativeHostClass = tryGetClass (REACT_NATIVE_HOST_CLASS_NAME );
123+ Method getReactInstanceManagerMethod = reactNativeHostClass .getMethod ("getReactInstanceManager" );
124+ instanceManager = (ReactInstanceManager )getReactInstanceManagerMethod .invoke (reactNativeHost );
125+ } catch (Exception e ) {
126+ // The React Native version might be older than 0.29, or the activity does not
127+ // extend ReactActivity, so we try to get the instance manager via the
128+ // "mReactInstanceManager" field.
129+ Class instanceManagerHolderClass = currentActivity instanceof ReactActivity
130+ ? ReactActivity .class
131+ : currentActivity .getClass ();
132+ Field instanceManagerField = instanceManagerHolderClass .getDeclaredField ("mReactInstanceManager" );
133+ instanceManagerField .setAccessible (true );
134+ instanceManager = (ReactInstanceManager )instanceManagerField .get (currentActivity );
135+ }
138136
139- String latestJSBundleFile = mCodePush .getJSBundleFileInternal (mCodePush .getAssetsBundleFileName ());
137+ String latestJSBundleFile = mCodePush .getJSBundleFileInternal (mCodePush .getAssetsBundleFileName ());
140138
141- // #2) Update the locally stored JS bundle file path
142- Field jsBundleField = instanceManager .getClass ().getDeclaredField ("mJSBundleFile" );
143- jsBundleField .setAccessible (true );
144- jsBundleField .set (instanceManager , latestJSBundleFile );
139+ // #2) Update the locally stored JS bundle file path
140+ Field jsBundleField = instanceManager .getClass ().getDeclaredField ("mJSBundleFile" );
141+ jsBundleField .setAccessible (true );
142+ jsBundleField .set (instanceManager , latestJSBundleFile );
145143
146- // #3) Get the context creation method and fire it on the UI thread (which RN enforces)
147- final Method recreateMethod = instanceManager .getClass ().getMethod ("recreateReactContextInBackground" );
144+ // #3) Get the context creation method and fire it on the UI thread (which RN enforces)
145+ final Method recreateMethod = instanceManager .getClass ().getMethod ("recreateReactContextInBackground" );
148146
149- final ReactInstanceManager finalizedInstanceManager = instanceManager ;
150- reactActivity .runOnUiThread (new Runnable () {
151- @ Override
152- public void run () {
153- try {
154- recreateMethod .invoke (finalizedInstanceManager );
155- mCodePush .initializeUpdateAfterRestart ();
156- }
157- catch (Exception e ) {
158- // The recreation method threw an unknown exception
159- // so just simply fallback to restarting the Activity
160- loadBundleLegacy (currentActivity );
161- }
147+ final ReactInstanceManager finalizedInstanceManager = instanceManager ;
148+ currentActivity .runOnUiThread (new Runnable () {
149+ @ Override
150+ public void run () {
151+ try {
152+ recreateMethod .invoke (finalizedInstanceManager );
153+ mCodePush .initializeUpdateAfterRestart ();
162154 }
163- });
164- } catch (Exception e ) {
165- // Our reflection logic failed somewhere
166- // so fall back to restarting the Activity
167- loadBundleLegacy (currentActivity );
168- }
155+ catch (Exception e ) {
156+ // The recreation method threw an unknown exception
157+ // so just simply fallback to restarting the Activity
158+ loadBundleLegacy (currentActivity );
159+ }
160+ }
161+ });
162+ } catch (Exception e ) {
163+ // Our reflection logic failed somewhere
164+ // so fall back to restarting the Activity
165+ loadBundleLegacy (currentActivity );
169166 }
170167 }
171168
0 commit comments