Skip to content

NullPointerException in BottomSheetNavigator.sheetContent due to retainedEntry race condition #773

@jackforsky

Description

@jackforsky

Crash:

NullPointerException
    at BottomSheetNavigator$sheetContent$1.invoke$lambda$5$lambda$4(BottomSheetNavigator.kt:166)
    at ComposeBackHandler.onBackCompleted(BackHandler.kt:168)
    at NavigationEventHandler.doOnBackCompleted(NavigationEventHandler.kt:242)
    at NavigationEventProcessor.dispatchOnCompleted(NavigationEventProcessor.kt:487)
    at NavigationEventDispatcher.dispatchOnCompleted(NavigationEventDispatcher.kt:383)
    at DirectNavigationEventInput.backCompleted(DirectNavigationEventInput.kt:55)
    at ComponentActivity.onBackPressed(ComponentActivity.kt:598)
    ...

Description:
BottomSheetNavigator.kt:166 (from androidx.compose.material:material-navigation:1.10.4) crashes with a NullPointerException when a back gesture completes while the bottom sheet is mid-transition. The retainedEntry becomes null between the if (retainedEntry != null) guard (line 163) and the BackHandler callback invocation (line 166).

Crash location

  // BottomSheetNavigator.kt (androidx.compose.material:material-navigation:1.10.4)
  if (retainedEntry != null) {                                          // line 163
      LaunchedEffect(retainedEntry) { sheetState.show() }              // line 164
      BackHandler { state.popWithTransition(popUpTo = retainedEntry!!, saveState = false) } // line 166 — NPE here
  }

The retainedEntry is a produceState value that can become null asynchronously (when backStack emits an empty list during hide animation). The if check passes, Compose registers the BackHandler, but by
the time the predictive back gesture's onBackCompleted fires the callback, retainedEntry has already been set to null.

Suggested fix
Replace the force-unwrap with a null-safe call:

  if (retainedEntry != null) {
      LaunchedEffect(retainedEntry) { sheetState.show() }
      BackHandler {
          retainedEntry?.let { entry ->
              state.popWithTransition(popUpTo = entry, saveState = false)
          }
      }
  }

Environment

  • io.github.raamcosta.compose-destinations:bottom-sheet:2.3.0
  • androidx.compose.material:material-navigation:1.10.4
  • androidx.navigation:navigation-compose:2.9.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions