From e0f1101e7e2acc5a09715737043f727dcc0db9a1 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Tue, 9 Feb 2021 22:40:17 -0800 Subject: [PATCH] Add invariant to SurfaceMountingManager: Views must not have parents when they are inserted Summary: We have no evidence of this happening on Android, but we are hitting a similar invariant on iOS. Adding this to Android for debugging purposes. For now it's a SoftException to catch in debug and capture information; if we don't hit this prod at all, we'll elevate to a hard crash. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D26281585 fbshipit-source-id: 8ea9cf3ac555b13bf311f24c81bbbbc2845521d5 --- .../fabric/mounting/SurfaceMountingManager.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 d5d937f8eda..36d69b2dfce 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 @@ -260,6 +260,23 @@ public class SurfaceMountingManager { logViewHierarchy(parentView, false); } + ViewParent viewParent = view.getParent(); + if (viewParent != null) { + int actualParentId = + viewParent instanceof ViewGroup ? ((ViewGroup) viewParent).getId() : View.NO_ID; + ReactSoftException.logSoftException( + TAG, + new IllegalStateException( + "addViewAt: cannot insert view [" + + tag + + "] into parent [" + + parentTag + + "]: View already has a parent: [" + + actualParentId + + "] " + + viewParent.getClass().getSimpleName())); + } + try { getViewGroupManager(parentViewState).addView(parentView, view, index); } catch (IllegalStateException e) {