circuit: Root `BackHandler` possible bug

Having an issue in the following scenario. The root screen sometimes has a BackHandler like so:

@Composable
fun RootUi(
  state: TestState,
  modifier: Modifier,
) {
  RootContent()
  BackHandler {
    // don't let the user out of the app just yet
  }
}

At some point ScreenA is pushed to the backstack which has the following logic in its presenter:

navigator.apply {
   pop() // or resetRoot(ScreenRoot)
   goTo(ScreenB)
}

Effectively I want ScreenA to pop and to push ScreenB. Tapping android back button in ScreenB calls BackHandler on RootUi from my testing even thought it’s not visible on the screen. Removing BackHandler navigation logic works as expected and navigates back to ScreenRoot.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 15 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Couple things to note:

I put a println inside the onRootPop and… it seems like it’s not being called at all? It appears like Circuit isn’t handling anything, whether I’m on the root screen or a second screen, so the default system handler kicks in? No clue.

Another note: The navigation-specific docs also seem to be outdated, still using a BackHandler before calling rememberCircuitNavigator to handle root pop (that didn’t work either, Circuit doesn’t take precedence nor handle anything so that BackHandler gets executed). The tutorial uses the updated version though.

I guess I’ll just create BackHandlers inside each screen to manually pop the current screen using the navigator for now.

This depends on which rememberCircuitNavigator is being used:

  • The common one doesn’t include any back handling so a BackHandler is still needed to manipulate the navigator.
  • The android one sets up a BackHandler and handles the onRootPop disaptching, also prevents any infinite looping.

Managed to write a test case that fails : 0e7467e

Was revisiting this after 0.26.0 and it should be fixed for AndroidPredictiveBackNavigationDecoration as the back handler was moved up a level. CupertinoGestureNavigationDecoration hasn’t been updated yet and will still fail.

CupertinoGestureNavigationDecoration doesn’t have any back handling in it, so a BackHandler is still needed along side rememberCircuitNavigator to manipulate the backstack.

      val navigator =
            rememberCircuitNavigator(
              backStack = backStack,
              onRootPop = {},
            )
          BackHandler { navigator.pop() }

So the test fails correctly here.

Retested again by doing swipe back from the left side or pressing the back button and can confirm that the BackHandler issue seems to be fixed when using AndroidPredictiveBackNavigationDecoration.

Same as #1927?