Repository navigation
Fix addViewAt crash when reparenting a view that is being animated out - #59364
Open
heecheolman wants to merge 1 commit into
Open
heecheolman wants to merge 1 commit into
heecheolman wants to merge 1 commit into
Conversation
Summary: When a view is moved to a new parent (for example because an intermediate view is flattened or unflattened), Fabric emits a Remove from the previous parent followed by an Insert into the new one. If the view is being animated out of its previous parent at that moment (it is in that parent's transitioning views, e.g. via LayoutTransition or startViewTransition), ViewGroup removes it from its children but keeps its parent pointer until the transition ends. addViewAt already detects that the view still has a parent and calls removeView() on it, but removeView() looks the view up in the children array, finds nothing, and silently returns. The following addView() then throws "The specified child already has a parent", which is rethrown as "addViewAt: failed to insert view [...]" and crashes the app. If the view is still attached to the same parent after removeView(), end the view transition on that parent, which clears the parent pointer, so the view can be inserted into its new parent. Changelog: [Android] [Fixed] - Fix `addViewAt: failed to insert view` crash when a view is reparented while being animated out of its previous parent Test Plan: Added SurfaceMountingManagerTest.addViewAt_reparentsViewThatIsTransitioningOutOfPreviousParent, which starts a view transition on the previous parent, removes the child, and inserts it into another parent. Without this change it fails with the same exception seen in production; with it, it passes. ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests 'com.facebook.react.fabric.*' Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
React Native doesn't use Android LayoutTransition / startViewTransition though, and neither should 3rd party view managers directly alter view structures managed by React Native. Do you have a concrete repro for this bug? |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
When a view is moved to a new parent (for example because an intermediate view is flattened or unflattened), Fabric emits a Remove from the previous parent followed by an Insert into the new one. If the view is being animated out of its previous parent at that moment (it is in that parent's transitioning views, e.g. via LayoutTransition or startViewTransition), ViewGroup removes it from its children but keeps its parent pointer until the transition ends.
addViewAt already detects that the view still has a parent and calls removeView() on it, but removeView() looks the view up in the children array, finds nothing, and silently returns. The following addView() then throws "The specified child already has a parent", which is rethrown as "addViewAt: failed to insert view [...]" and crashes the app.
If the view is still attached to the same parent after removeView(), end the view transition on that parent, which clears the parent pointer, so the view can be inserted into its new parent.
Changelog:
[Android] [Fixed] - Fix
addViewAt: failed to insert viewcrash when a view is reparented while being animated out of its previous parentTest Plan:
Added SurfaceMountingManagerTest.addViewAt_reparentsViewThatIsTransitioningOutOfPreviousParent, which starts a view transition on the previous parent, removes the child, and inserts it into another parent. Without this change it fails with the same exception seen in production; with it, it passes.