1

I am using Accompanist Bottom Sheet Destinations in my project. The setup is exactly the same as shown in docs.

@Composable
fun MyApp() {
    val bottomSheetNavigator = rememberBottomSheetNavigator()
    val navController = rememberNavController(bottomSheetNavigator)
    ModalBottomSheetLayout(bottomSheetNavigator) {
        NavHost(navController, Destinations.Home) {
           composable(route = "home") {
               HomeScreen(
                   openBottomSheet = { navController.navigate("sheet")  }
               )
           }
           bottomSheet(route = "sheet") {
               MyBottonSheet(
                   navigateToSomeRoute = { navController.navigate("some_route") }
               )
           }
           composable(route = "some_route") {
               SomeComposable(
                   onBackPress = { navController.navigateUp() }
               )
           }
        }
    }
}

Here I have a button in MyBottomSheet which opens the SomeComposable screen. But when I navigateUp from there, I reach HomeScreen i.e. the bottom sheet was popped off the backstack when I navigated away from it. How can I keep my bottom sheet in the backstack so that when I press Back in SomeComposable I go back to the previous state with MyBottonSheet open (on top of HomeScreen)?

Arpit Shukla
  • 9,612
  • 1
  • 14
  • 40

1 Answers1

4

This is not possible. As per the Navigation and the back stack documentation:

Dialog destinations implement the FloatingWindow interface, indicating that they overlay other destinations on the back stack. As such, one or more FloatingWindow destinations can be present only on the top of the navigation back stack. Navigating to a destination that does not implement FloatingWindow automatically pops all FloatingWindow destinations off of the top of the stack. This ensures that the current destination is always fully visible above other destinations on the back stack.

Bottom sheet destinations are also FloatingWindow destinations that float above the other destinations. As such, it is expected that they are automatically popped off the back stack when you navigate to anything other than another dialog or bottomSheet destination.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thanks for the answer. If this is the case then I think I will have to navigate to the bottom sheet again when I reach Home. – Arpit Shukla Jun 23 '22 at 04:45
  • 1
    Or you shouldn't be using a bottom sheet for something that needs to go on the back stack - bottom sheets and other dialog destinations should be for temporary overlays (which is exactly what they look like from the user's perspective). – ianhanniballake Jun 23 '22 at 04:49