Is there any way to override the back stack button to go back to a screen with its index in jetpack compose?

Viewed 45

Is there any way to override onBackPress to go back to a screen with its index in jetpack compose? For example, go back to the second previous screen.

2 Answers

You can with backhandler function;

BackHandler(
   enabled = //condition if you want.
) {
   // Navigate where ever you want.
   //Example;
   navController.navigate(
      route = navController.backQueue[i].destination.route.toString()
   )
}

You can use Custom BackHandler in this case.

BackHandler(
   enabled = true,
   onBack  = {navController.navigate(route)}
)
Related