diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java index a7389a8b48f..2e8cab11087 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java @@ -173,7 +173,7 @@ public class FabricUIManager private final CopyOnWriteArrayList mListeners = new CopyOnWriteArrayList<>(); private boolean mMountNotificationScheduled = false; - private final List mMountedSurfaceIds = new ArrayList<>(); + private List mSurfaceIdsWithPendingMountNotification = new ArrayList<>(); @ThreadConfined(UI) @NonNull @@ -1250,12 +1250,13 @@ public class FabricUIManager // Collect surface IDs for all the mount items for (MountItem mountItem : mountItems) { - if (mountItem != null && !mMountedSurfaceIds.contains(mountItem.getSurfaceId())) { - mMountedSurfaceIds.add(mountItem.getSurfaceId()); + if (mountItem != null + && !mSurfaceIdsWithPendingMountNotification.contains(mountItem.getSurfaceId())) { + mSurfaceIdsWithPendingMountNotification.add(mountItem.getSurfaceId()); } } - if (!mMountNotificationScheduled && !mMountedSurfaceIds.isEmpty()) { + if (!mMountNotificationScheduled && !mSurfaceIdsWithPendingMountNotification.isEmpty()) { mMountNotificationScheduled = true; // Notify mount when the effects are visible and prevent mount hooks to @@ -1267,17 +1268,19 @@ public class FabricUIManager public void run() { mMountNotificationScheduled = false; + // Create a copy in case mount hooks trigger more mutations + final List surfaceIdsToReportMount = + mSurfaceIdsWithPendingMountNotification; + mSurfaceIdsWithPendingMountNotification = new ArrayList<>(); + final @Nullable FabricUIManagerBinding binding = mBinding; if (binding == null || mDestroyed) { - mMountedSurfaceIds.clear(); return; } - for (int surfaceId : mMountedSurfaceIds) { + for (int surfaceId : surfaceIdsToReportMount) { binding.reportMount(surfaceId); } - - mMountedSurfaceIds.clear(); } }); }