55import android .os .AsyncTask ;
66import android .os .Handler ;
77import android .os .Looper ;
8- import android .provider .Settings ;
98import android .view .View ;
109
1110import com .facebook .react .ReactApplication ;
2221import com .facebook .react .bridge .ReactMethod ;
2322import com .facebook .react .bridge .ReadableMap ;
2423import com .facebook .react .bridge .WritableMap ;
24+ import com .facebook .react .devsupport .interfaces .DevSupportManager ;
2525import com .facebook .react .modules .core .ChoreographerCompat ;
2626import com .facebook .react .modules .core .DeviceEventManagerModule ;
2727import com .facebook .react .modules .core .ReactChoreographer ;
3636import java .util .ArrayList ;
3737import java .util .Date ;
3838import java .util .HashMap ;
39- import java .util .List ;
4039import java .util .Map ;
4140import 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