Skip to content

Fix addViewAt crash when reparenting a view that is being animated out - #59364

Open
heecheolman wants to merge 1 commit into
react:mainfrom
heecheolman:fix/addviewat-transitioning-child
Open

heecheolman wants to merge 1 commit into
react:mainfrom
heecheolman:fix/addviewat-transitioning-child

Conversation

@heecheolman

@heecheolman heecheolman commented Oct 22, 2026 •

Copy link
Copy Markdown

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.*'

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>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 22, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 22, 2026
@javache

javache commented Oct 23, 2026

Copy link
Copy Markdown
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants