Skip to content

Commit 93872ff

Browse files
committed
fix(Native): freezing that occurs when reloading the app in debug
1 parent 64e3141 commit 93872ff

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.os.AsyncTask;
66
import android.os.Handler;
77
import android.os.Looper;
8-
import android.provider.Settings;
98
import android.view.View;
109

1110
import com.facebook.react.ReactApplication;
@@ -22,6 +21,7 @@
2221
import com.facebook.react.bridge.ReactMethod;
2322
import com.facebook.react.bridge.ReadableMap;
2423
import com.facebook.react.bridge.WritableMap;
24+
import com.facebook.react.devsupport.interfaces.DevSupportManager;
2525
import com.facebook.react.modules.core.ChoreographerCompat;
2626
import com.facebook.react.modules.core.DeviceEventManagerModule;
2727
import com.facebook.react.modules.core.ReactChoreographer;
@@ -36,7 +36,6 @@
3636
import java.util.ArrayList;
3737
import java.util.Date;
3838
import java.util.HashMap;
39-
import java.util.List;
4039
import java.util.Map;
4140
import java.util.UUID;
4241

@@ -166,6 +165,9 @@ public void run() {
166165
if (reactDelegate == null) {
167166
throw new NoSuchMethodException("ReactDelegate doesn't have reload method in RN < 0.74");
168167
}
168+
169+
resetReactRootViewsIfDebug(instanceManager, reactDelegate);
170+
169171
Method reloadMethod = reactDelegate.getClass().getMethod("reload");
170172
reloadMethod.invoke(reactDelegate);
171173
} catch (NoSuchMethodException e) {
@@ -189,18 +191,22 @@ public void run() {
189191
}
190192
}
191193

192-
// This workaround has been implemented in order to fix https://github.com/facebook/react-native/issues/14533
193-
// resetReactRootViews allows to call recreateReactContextInBackground without any exceptions
194-
// This fix also relates to https://github.com/microsoft/react-native-code-push/issues/878
195-
private void resetReactRootViews(ReactInstanceManager instanceManager) throws NoSuchFieldException, IllegalAccessException {
196-
Field mAttachedRootViewsField = instanceManager.getClass().getDeclaredField("mAttachedRootViews");
197-
mAttachedRootViewsField.setAccessible(true);
198-
List<ReactRootView> mAttachedRootViews = (List<ReactRootView>)mAttachedRootViewsField.get(instanceManager);
199-
for (ReactRootView reactRootView : mAttachedRootViews) {
200-
reactRootView.removeAllViews();
201-
reactRootView.setId(View.NO_ID);
194+
// Fix freezing that occurs when reloading the app in debug mode (RN >= 0.77.1 Old Architecture)
195+
// - "Trying to add a root view with an explicit id (11) already set.
196+
// React Native uses the id field to track react tags and will overwrite this field.
197+
// If that is fine, explicitly overwrite the id field to View.NO_ID before calling addRootView."
198+
private void resetReactRootViewsIfDebug(ReactInstanceManager instanceManager, ReactDelegate reactDelegate) {
199+
DevSupportManager devSupportManager = instanceManager.getDevSupportManager();
200+
if (devSupportManager.getDevSupportEnabled()) {
201+
ReactActivity currentActivity = (ReactActivity) getCurrentActivity();
202+
if (currentActivity != null) {
203+
ReactRootView reactRootView = reactDelegate.getReactRootView();
204+
if (reactRootView != null) {
205+
reactRootView.removeAllViews();
206+
reactRootView.setId(View.NO_ID);
207+
}
208+
}
202209
}
203-
mAttachedRootViewsField.set(instanceManager, mAttachedRootViews);
204210
}
205211

206212
private void clearLifecycleEventListener() {

0 commit comments

Comments
 (0)