google / accompanist

A collection of extension libraries for Jetpack Compose
https://google.github.io/accompanist
Apache License 2.0
7.34k stars 593 forks source link

[Navigation Material] Incorrect back behavior with nested navigator #1726

Closed WonderCsabo closed 7 months ago

WonderCsabo commented 9 months ago

Description When there is a nested navigation, the topmost bottom sheet does not gets the back event, the nested navigatitor pops instead.

Steps to reproduce


class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterialNavigationApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AppTheme {
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    val bottomSheetNavigator = rememberBottomSheetNavigator()
                    val topNavController = rememberNavController(bottomSheetNavigator)

                    ModalBottomSheetLayout(bottomSheetNavigator) {
                        NavHost(navController = topNavController, startDestination = "root") {
                            composable("root") { Root(topNavController) }
                            bottomSheet("dialog") { Dialog() }
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun Root(topNavController: NavController) {
    Column {

        Button(onClick = { topNavController.navigate("dialog") }) {
            Text(text = "open dialog")
        }

        val nestedNavController = rememberNavController()

        NavHost(navController = nestedNavController, startDestination = "nested1", modifier = Modifier.weight(1f)) {
            composable("nested1") { Nested1(nestedNavController) }
            composable("nested2") { Nested2() }
        }
    }
}

@Composable
fun Nested1(nestedNavController: NavController) {
    Button(onClick = { nestedNavController.navigate("nested2") }) {
        Text(text = "Open nested2 screen")
    }
}

@Composable
fun Nested2() {
    Text(text = "nested2")
}

@Composable
fun Dialog() {
    Text("My Dialog")
}
  1. Open nested2 screen
  2. Open the bottom sheet in the top navigator
  3. Tap the back button

Expected behavior The bottom sheet closes.

Additional context Bottom sheet stays visible, under it we navigate back to nested1 screen.

github-actions[bot] commented 7 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

WonderCsabo commented 7 months ago

This is still an issue.