diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java index e48192e896..2f7f893b38 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java @@ -224,7 +224,7 @@ public class MountingManager { } @UiThread - public void removeViewAt(int parentTag, int index) { + public void removeViewAt(int tag, int parentTag, int index) { UiThreadUtil.assertOnUiThread(); ViewState viewState = getNullableViewState(parentTag); @@ -244,6 +244,22 @@ public class MountingManager { ViewGroupManager viewGroupManager = getViewGroupManager(viewState); + // Verify that the view we're about to remove has the same tag we expect + if (tag != -1) { + View view = viewGroupManager.getChildAt(parentView, index); + if (view != null && view.getId() != tag) { + throw new IllegalStateException( + "Tried to delete view [" + + tag + + "] of parent [" + + parentTag + + "] at index " + + index + + ", but got view tag " + + view.getId()); + } + } + try { viewGroupManager.removeViewAt(parentView, index); } catch (RuntimeException e) { @@ -397,6 +413,13 @@ public class MountingManager { } View view = viewState.mView; + ViewParent parentView = view.getParent(); + + if (parentView != null) { + throw new IllegalStateException( + "Cannot delete view that is still attached to parent: [" + reactTag + "]"); + } + if (view != null) { dropView(view); } else { diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/RemoveDeleteMultiMountItem.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/RemoveDeleteMultiMountItem.java index c11cb4e441..daf3ed5ca7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/RemoveDeleteMultiMountItem.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/RemoveDeleteMultiMountItem.java @@ -49,8 +49,9 @@ public class RemoveDeleteMultiMountItem implements MountItem { int flags = mMetadata[i + FLAGS_INDEX]; if ((flags & REMOVE_FLAG) != 0) { int parentTag = mMetadata[i + PARENT_TAG_INDEX]; + int tag = mMetadata[i + TAG_INDEX]; int index = mMetadata[i + VIEW_INDEX_INDEX]; - mountingManager.removeViewAt(parentTag, index); + mountingManager.removeViewAt(tag, parentTag, index); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/RemoveMountItem.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/RemoveMountItem.java index f09f91e3f9..3b6fd92110 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/RemoveMountItem.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/RemoveMountItem.java @@ -24,7 +24,7 @@ public class RemoveMountItem implements MountItem { @Override public void execute(@NonNull MountingManager mountingManager) { - mountingManager.removeViewAt(mParentReactTag, mIndex); + mountingManager.removeViewAt(-1, mParentReactTag, mIndex); } public int getParentReactTag() {