Skip to content

Commit afe3420

Browse files
fix: remove NavView fragment via the FragmentManager it is attached to
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>
1 parent 0b1819c commit afe3420

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

android/src/main/java/com/google/android/react/navsdk/NavViewManager.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,18 +304,24 @@ public void onDropViewInstance(@NonNull FrameLayout view) {
304304
controllerSink.clear();
305305
}
306306

307-
FragmentActivity activity = (FragmentActivity) reactContext.getCurrentActivity();
308-
if (activity == null) return;
309-
310307
WeakReference<IMapViewFragment> weakReference = fragmentMap.remove(viewId);
311308
if (weakReference != null) {
312309
IMapViewFragment fragment = weakReference.get();
313310
if (fragment != null && fragment.isAdded()) {
314-
activity
315-
.getSupportFragmentManager()
316-
.beginTransaction()
317-
.remove((Fragment) fragment)
318-
.commitNowAllowingStateLoss();
311+
// Remove via the FragmentManager the fragment is actually attached to.
312+
// reactContext.getCurrentActivity() can be a different Activity than the one hosting the
313+
// fragment (e.g. after Activity recreation), in which case removing through its
314+
// FragmentManager throws "Cannot remove Fragment attached to a different FragmentManager".
315+
try {
316+
((Fragment) fragment)
317+
.getParentFragmentManager()
318+
.beginTransaction()
319+
.remove((Fragment) fragment)
320+
.commitNowAllowingStateLoss();
321+
} catch (IllegalStateException e) {
322+
// FragmentManager already destroyed mid-teardown; the fragment is torn down with its
323+
// host Activity anyway.
324+
}
319325
}
320326
}
321327
}

0 commit comments

Comments
 (0)