Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 807f4cf

Browse files
committed
remove ReactActivity dependency
1 parent 93c3e61 commit 807f4cf

File tree

1 file changed

+49
-55
lines changed

1 file changed

+49
-55
lines changed

android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -105,67 +105,61 @@ 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+
Field instanceManagerField = currentActivity.getClass().getDeclaredField("mReactInstanceManager");
130+
instanceManagerField.setAccessible(true);
131+
instanceManager = (ReactInstanceManager)instanceManagerField.get(currentActivity);
132+
}
138133

139-
String latestJSBundleFile = mCodePush.getJSBundleFileInternal(mCodePush.getAssetsBundleFileName());
134+
String latestJSBundleFile = mCodePush.getJSBundleFileInternal(mCodePush.getAssetsBundleFileName());
140135

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);
136+
// #2) Update the locally stored JS bundle file path
137+
Field jsBundleField = instanceManager.getClass().getDeclaredField("mJSBundleFile");
138+
jsBundleField.setAccessible(true);
139+
jsBundleField.set(instanceManager, latestJSBundleFile);
145140

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");
141+
// #3) Get the context creation method and fire it on the UI thread (which RN enforces)
142+
final Method recreateMethod = instanceManager.getClass().getMethod("recreateReactContextInBackground");
148143

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-
}
144+
final ReactInstanceManager finalizedInstanceManager = instanceManager;
145+
currentActivity.runOnUiThread(new Runnable() {
146+
@Override
147+
public void run() {
148+
try {
149+
recreateMethod.invoke(finalizedInstanceManager);
150+
mCodePush.initializeUpdateAfterRestart();
162151
}
163-
});
164-
} catch (Exception e) {
165-
// Our reflection logic failed somewhere
166-
// so fall back to restarting the Activity
167-
loadBundleLegacy(currentActivity);
168-
}
152+
catch (Exception e) {
153+
// The recreation method threw an unknown exception
154+
// so just simply fallback to restarting the Activity
155+
loadBundleLegacy(currentActivity);
156+
}
157+
}
158+
});
159+
} catch (Exception e) {
160+
// Our reflection logic failed somewhere
161+
// so fall back to restarting the Activity
162+
loadBundleLegacy(currentActivity);
169163
}
170164
}
171165

0 commit comments

Comments
 (0)