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

Commit d62f3df

Browse files
add support for RN 0.45.x (#901)
* add support for RN 0.45.x * add support for RN 0.45.x * to last * remove additional calls * remove support for RN 0.46.x that has been added due to as of now it is not compatibale with RN 0.45.x
1 parent 658e81a commit d62f3df

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import android.os.Handler;
66
import android.os.Looper;
77
import android.provider.Settings;
8+
import android.view.View;
89

910
import com.facebook.react.ReactApplication;
1011
import com.facebook.react.ReactInstanceManager;
12+
import com.facebook.react.ReactRootView;
1113
import com.facebook.react.bridge.Arguments;
1214
import com.facebook.react.bridge.LifecycleEventListener;
1315
import com.facebook.react.bridge.Promise;
@@ -29,6 +31,7 @@
2931
import java.lang.reflect.Method;
3032
import java.util.Date;
3133
import java.util.HashMap;
34+
import java.util.List;
3235
import java.util.Map;
3336

3437
public class CodePushNativeModule extends ReactContextBaseJavaModule {
@@ -101,7 +104,7 @@ private void setJSBundle(ReactInstanceManager instanceManager, String latestJSBu
101104
Class<?> jsBundleLoaderClass = Class.forName("com.facebook.react.cxxbridge.JSBundleLoader");
102105
Method createFileLoaderMethod = null;
103106
String createFileLoaderMethodName = latestJSBundleFile.toLowerCase().startsWith("assets://")
104-
? "createAssetLoader" : "createFileLoader";
107+
? "createAssetLoader" : "createFileLoader";
105108

106109
Method[] methods = jsBundleLoaderClass.getDeclaredMethods();
107110
for (Method method : methods) {
@@ -157,6 +160,11 @@ private void loadBundle() {
157160
@Override
158161
public void run() {
159162
try {
163+
// This workaround has been implemented in order to fix https://github.com/facebook/react-native/issues/14533
164+
// resetReactRootViews allows to call recreateReactContextInBackground without any exceptions
165+
// This fix also relates to https://github.com/Microsoft/react-native-code-push/issues/878
166+
resetReactRootViews(instanceManager);
167+
160168
instanceManager.recreateReactContextInBackground();
161169
mCodePush.initializeUpdateAfterRestart();
162170
} catch (Exception e) {
@@ -174,6 +182,17 @@ public void run() {
174182
}
175183
}
176184

185+
private void resetReactRootViews(ReactInstanceManager instanceManager) throws NoSuchFieldException, IllegalAccessException {
186+
Field mAttachedRootViewsField = instanceManager.getClass().getDeclaredField("mAttachedRootViews");
187+
mAttachedRootViewsField.setAccessible(true);
188+
List<ReactRootView> mAttachedRootViews = (List<ReactRootView>)mAttachedRootViewsField.get(instanceManager);
189+
for (ReactRootView reactRootView : mAttachedRootViews) {
190+
reactRootView.removeAllViews();
191+
reactRootView.setId(View.NO_ID);
192+
}
193+
mAttachedRootViewsField.set(instanceManager, mAttachedRootViews);
194+
}
195+
177196
private void clearLifecycleEventListener() {
178197
// Remove LifecycleEventListener to prevent infinite restart loop
179198
if (mLifecycleEventListener != null) {

0 commit comments

Comments
 (0)