From bb075d785deb3732689bf6aacdc5247187e81391 Mon Sep 17 00:00:00 2001 From: Will Schurman Date: Wed, 29 Nov 2023 04:53:31 -0800 Subject: [PATCH] Fix android root view group removal during instance re-creation (#41678) Summary: This is my proposed solution to https://github.com/facebook/react-native/issues/41677. Fixes https://github.com/facebook/react-native/issues/41677. ## Changelog: [ANDROID] [FIXED] - Fix android root view group removal during instance re-creation Pull Request resolved: https://github.com/facebook/react-native/pull/41678 Test Plan: Both with fabric enabled and disabled (new architecture): 1. Clone repro repo: https://github.com/wschurman/rn-reload-repro 2. Build and run on android (I use android studio) 3. Click reload button, see timestamp doesn't change (indicating that the view is not removed) 4. Apply this PR as a patch. 5. Re-build and run. 6. Click reload button, see view is correctly disposed of and the new view is set. Reviewed By: cortinico Differential Revision: D51658524 Pulled By: javache fbshipit-source-id: d9a026cde677ad1ec113230bc31bd9297bca8bfc --- .../main/java/com/facebook/react/ReactInstanceManager.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index be0ca24b62f2ad..a1801d5627c933 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -1296,14 +1296,12 @@ private void detachRootViewFromInstance(ReactRoot reactRoot, ReactContext reactC uiManager.stopSurface(surfaceId); } else { FLog.w(ReactConstants.TAG, "Failed to stop surface, UIManager has already gone away"); - reactRoot.getRootViewGroup().removeAllViews(); } } else { ReactSoftExceptionLogger.logSoftException( TAG, new RuntimeException( "detachRootViewFromInstance called with ReactRootView with invalid id")); - reactRoot.getRootViewGroup().removeAllViews(); } } else { reactContext @@ -1312,8 +1310,7 @@ private void detachRootViewFromInstance(ReactRoot reactRoot, ReactContext reactC .unmountApplicationComponentAtRootTag(reactRoot.getRootViewTag()); } - // The view is no longer attached, so mark it as such by resetting its ID. - reactRoot.getRootViewGroup().setId(View.NO_ID); + clearReactRoot(reactRoot); } @ThreadConfined(UI)