Skip to content

fix: remove NavView fragment via the FragmentManager it is attached to#625

Open
christian-apollo wants to merge 1 commit into
googlemaps:mainfrom
christian-apollo:fix/navview-drop-wrong-fragment-manager
Open

fix: remove NavView fragment via the FragmentManager it is attached to#625
christian-apollo wants to merge 1 commit into
googlemaps:mainfrom
christian-apollo:fix/navview-drop-wrong-fragment-manager

Conversation

@christian-apollo

Copy link
Copy Markdown

Fixes #624

Problem

NavViewManager.onDropViewInstance removes the map/navigation fragment through reactContext.getCurrentActivity().getSupportFragmentManager(). The fragment was attached to whichever Activity was current when the fragment transaction was committed. If getCurrentActivity() points at a different Activity at drop time — e.g. after Activity recreation while the original host is still alive — that FragmentManager never hosted the fragment, and androidx throws a fatal:

java.lang.IllegalStateException: Cannot remove Fragment attached to a different FragmentManager.
    at androidx.fragment.app.BackStackRecord.remove(BackStackRecord.java:204)
    at com.google.android.react.navsdk.NavViewManager.onDropViewInstance(NavViewManager.java:317)
    at com.facebook.react.fabric.mounting.SurfaceMountingManager.onViewStateDeleted(SurfaceMountingManager.java:1163)

BackStackRecord.remove throws exactly when fragment.mFragmentManager != null && fragment.mFragmentManager != mManager, i.e. when the transaction's FragmentManager is not the one the fragment is attached to.

Fix

Open the remove transaction on the fragment's own getParentFragmentManager(), which is by definition the FragmentManager the fragment is attached to, so the mismatch can no longer occur. isAdded() is checked first, so getParentFragmentManager() cannot throw for a detached fragment; a remaining IllegalStateException (FragmentManager already destroyed mid-teardown) is caught, since in that case the fragment is torn down with its host Activity anyway.

This also removes the early return when getCurrentActivity() is null — the current Activity is no longer needed for removal, and returning early leaked the fragmentMap entry.

When the current Activity is the fragment's host (the common case), getParentFragmentManager() and activity.getSupportFragmentManager() are the same instance, so behavior is unchanged.

Testing

🤖 Generated with Claude Code

NavViewManager.onDropViewInstance removed the map/navigation fragment
through reactContext.getCurrentActivity()'s support FragmentManager.
When the view is dropped while getCurrentActivity() points at a
different Activity than the one hosting the fragment (e.g. after
Activity recreation), that FragmentManager never hosted the fragment
and androidx throws:

  IllegalStateException: Cannot remove Fragment attached to a different
  FragmentManager

Use the fragment's own getParentFragmentManager() for the remove
transaction instead, and guard against the FragmentManager being
already destroyed mid-teardown. This also no longer skips cleanup
(leaking the fragmentMap entry) when getCurrentActivity() is null.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@google-cla

google-cla Bot commented Jul 10, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android crash on NavView unmount: "Cannot remove Fragment attached to a different FragmentManager" in NavViewManager.onDropViewInstance

2 participants