diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java index 6002cff9ef6..e61f26a2dd6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java @@ -257,6 +257,11 @@ public class SurfaceMountingManager { new Runnable() { @Override public void run() { + // We must call `onDropViewInstance` on all remaining Views + for (ViewState viewState : mTagToViewState.values()) { + onViewStateDeleted(viewState); + } + // Evict all views from cache and memory mLastSuccessfulQueryTime = System.currentTimeMillis(); mTagSetForStoppedSurface = mTagToViewState.keySet(); @@ -794,6 +799,21 @@ public class SurfaceMountingManager { mJSResponderHandler.setJSResponder(initialReactTag, view.getParent()); } + @UiThread + private void onViewStateDeleted(ViewState viewState) { + // Destroy state immediately instead of waiting for Java GC. + if (viewState.mStateWrapper != null) { + viewState.mStateWrapper.destroyState(); + viewState.mStateWrapper = null; + } + + // For non-root views we notify viewmanager with {@link ViewManager#onDropInstance} + ViewManager viewManager = viewState.mViewManager; + if (!viewState.mIsRoot && viewManager != null) { + viewManager.onDropViewInstance(viewState.mView); + } + } + @UiThread public void deleteView(int reactTag) { UiThreadUtil.assertOnUiThread(); @@ -816,17 +836,7 @@ public class SurfaceMountingManager { // or StopSurface being called, so we do not handle deleting descendents of the View. mTagToViewState.remove(reactTag); - // Destroy state immediately instead of waiting for Java GC. - if (viewState.mStateWrapper != null) { - viewState.mStateWrapper.destroyState(); - viewState.mStateWrapper = null; - } - - // For non-root views we notify viewmanager with {@link ViewManager#onDropInstance} - ViewManager viewManager = viewState.mViewManager; - if (!viewState.mIsRoot && viewManager != null) { - viewManager.onDropViewInstance(viewState.mView); - } + onViewStateDeleted(viewState); } @UiThread