From 0d7a92b551bb8bd91aeba73316cbaf7315362094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20B=C5=82oniarz?= Date: Thu, 14 Mar 2024 07:27:29 -0700 Subject: [PATCH] Add conditional return in `handleRemoveView` (#43389) Summary: I was recently working on an [issue](https://github.com/software-mansion/react-native-reanimated/issues/5715) in Reanimated where z-index of some views was broken after a Layout Animation was used. The issue was that in some cases we were calling the `removeView` function on a already removed view. On plain Android this wouldn't be an issue, since the `removeView` function ignores such calls. Unfortunately, the `ReactViewGroup.java` implementation maintains a counter of views with user defined z-index. This counter is decremented whenever a call to `removeView` is made, even if the view is not a child of this `ViewGroup`. This PR adds an additional check in the `handleRemoveView` function to unify the `removeView` behavior between Android and react-native. ## Changelog: [ANDROID] [CHANGED] - Changed the `handleRemoveView` function in `ReactViewGroup.java` to ignore calls for `Views` that are not children of this `ViewGroup` Pull Request resolved: https://github.com/facebook/react-native/pull/43389 Test Plan: I tested if the `rn-tester` app behaves correctly after those changes. Reviewed By: NickGerleman Differential Revision: D54874780 Pulled By: javache fbshipit-source-id: f1a34947419ef6106ee73b196ae99b7f8c2f7a77 --- .../java/com/facebook/react/views/view/ReactViewGroup.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java index bc5d22b34cc..1af57b99847 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java @@ -519,6 +519,9 @@ public class ReactViewGroup extends ViewGroup UiThreadUtil.assertOnUiThread(); if (!customDrawOrderDisabled()) { + if (indexOfChild(view) == -1) { + return; + } getDrawingOrderHelper().handleRemoveView(view); setChildrenDrawingOrderEnabled(getDrawingOrderHelper().shouldEnableCustomDrawingOrder()); } else {