Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.view.View;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
Expand All @@ -29,6 +31,7 @@
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

Method[] methods = jsBundleLoaderClass.getDeclaredMethods();
for (Method method : methods) {
Expand Down Expand Up @@ -157,6 +160,11 @@ private void loadBundle() {
@Override
public void run() {
try {
// This workaround has been implemented in order to fix https://github.com/facebook/react-native/issues/14533
// resetReactRootViews allows to call recreateReactContextInBackground without any exceptions
// This fix also relates to https://github.com/Microsoft/react-native-code-push/issues/878
resetReactRootViews(instanceManager);

instanceManager.recreateReactContextInBackground();
mCodePush.initializeUpdateAfterRestart();
} catch (Exception e) {
Expand All @@ -174,6 +182,17 @@ public void run() {
}
}

private void resetReactRootViews(ReactInstanceManager instanceManager) throws NoSuchFieldException, IllegalAccessException {
Field mAttachedRootViewsField = instanceManager.getClass().getDeclaredField("mAttachedRootViews");
mAttachedRootViewsField.setAccessible(true);
List<ReactRootView> mAttachedRootViews = (List<ReactRootView>)mAttachedRootViewsField.get(instanceManager);
for (ReactRootView reactRootView : mAttachedRootViews) {
reactRootView.removeAllViews();
reactRootView.setId(View.NO_ID);
}
mAttachedRootViewsField.set(instanceManager, mAttachedRootViews);
}

private void clearLifecycleEventListener() {
// Remove LifecycleEventListener to prevent infinite restart loop
if (mLifecycleEventListener != null) {
Expand Down