Skip to content

Conversation

@jpancotti
Copy link

When the FragmentManager has stale fragments from a previous tabs session (e.g., after login → tabs → logout → login → tabs flow), the app would crash with "[RNScreens] There can be only a single focused tab". This fix gracefully cleans up stale fragments instead of crashing.

Description

When using NativeTabs with navigation flows that mount/unmount the tab container (e.g., auth flows), the native Android FragmentManager may retain fragments from a previous session. The previous assertion would crash the app; this fix removes stale fragments gracefully.

Fixes apps using expo-router NativeTabs with conditional rendering based on auth state.

Changes

  • Replaced crash assertion with defensive cleanup in TabsHost.kt
  • When multiple fragments exist, removes extras via commitNowAllowingStateLoss()
  • Logs cleanup for debugging via RNSLog.d

Test code and steps to reproduce

function RootLayout() {
  const { isLoggedIn } = useAuth();
  
  if (!isLoggedIn) {
    return <LoginScreen />;
  }
  
  return (
    <Tabs>
      <Tabs.Screen name="home" />
      <Tabs.Screen name="profile" />
    </Tabs>
  );
}
  1. Login → see tabs
  2. Logout → see login screen
  3. Login again
  4. Before: Crash with "There can be only a single focused tab"
  5. After: Tabs work correctly, stale fragments cleaned up

Checklist

Previously, if the fragment manager had multiple fragments (e.g., after
navigating away from tabs during logout and back during re-login), the
app would crash with the assertion:
'[RNScreens] There can be only a single focused tab'

This can happen because the native FragmentManager may retain stale tab
fragments from a previous session that were not properly cleaned up.

Instead of asserting and crashing, this patch:
1. Detects when multiple fragments exist
2. Removes the stale fragments gracefully
3. Logs the cleanup for debugging purposes
4. Continues with normal tab transition

This makes NativeTabs resilient to navigation state inconsistencies that
can occur during app lifecycle events like login/logout flows.
@kkafar kkafar self-requested a review December 18, 2025 10:35
@kkafar kkafar self-assigned this Dec 18, 2025
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.

2 participants