Skip to content

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

Merged
illuminati1911 merged 3 commits into
googlemaps:mainfrom
christian-apollo:fix/navview-drop-wrong-fragment-manager
Aug 13, 2026
Merged

fix: remove NavView fragment via the FragmentManager it is attached to#625
illuminati1911 merged 3 commits into
googlemaps:mainfrom
christian-apollo:fix/navview-drop-wrong-fragment-manager

Conversation

@christian-apollo

Copy link
Copy Markdown
Contributor

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.
@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.

.beginTransaction()
.remove((Fragment) fragment)
.commitNowAllowingStateLoss();
// Remove via the FragmentManager the fragment is actually attached to.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to explain the earlier functionality in the comments. Also considering that the code is pretty straight forward I think this whole comment can be removed.

@christian-apollo christian-apollo Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed in 45c8186.

@illuminati1911

Copy link
Copy Markdown
Contributor

Hey @christian-apollo. It seems you have signed the CLA, but there was an error. Could you please amend your commit and remove the Co-Author: <no****y​@anthropic.com> from contributors.
See all the details here:
https://github.com/googlemaps/react-native-navigation-sdk/pull/625/checks?check_run_id=86393044697

Also please see my comment above.

Requested in review: the code is straightforward and the comment
mostly explained the previous behavior.
@christian-apollo
christian-apollo force-pushed the fix/navview-drop-wrong-fragment-manager branch from d77c873 to 45c8186 Compare August 11, 2026 15:39

@jokerttu jokerttu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@illuminati1911
illuminati1911 merged commit 8c264b5 into googlemaps:main Aug 13, 2026
17 checks passed
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

4 participants