Nathan
01/23/2024, 11:48 PMandroidx.compose.material:material:1.5.4
which is from the latest Compose BOM and the latest Jetpack Navigation version (2.7.6)) like this:
val scope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState()
BackHandler(enabled = scaffoldState.drawerState.isOpen) {
coroutineScope.launch { scaffoldState.drawerState.close() }
}
Scaffold(
scaffoldState = scaffoldState,
topBar = { AppTopBar() }
drawerContent = { AppDrawer() },
content = { HomeScreen() }
)
HomeScreen() {
NavHost(...) { ... }
}
The issue is that my BackHandler
only works if I’m on the root of my navigation graph. If I am not, the screens are popped off the backstack until we are at the root.
The reason is NavHost
has the following in the implementation:
BackHandler(currentBackStack.size > 1) {
navController.popBackStack()
}
which means that its BackHandler
is added to the composition last and so it is the one that is enabled. navController.enableOnBackPressed(false)
doesn’t seem to impact behavior at all.
has anyone else ran into this? any suggestions?Nathan
01/24/2024, 10:49 PMval scope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState()
if(scaffoldState.drawerState.isOpen) {
BackHandler(true) {
coroutineScope.launch { scaffoldState.drawerState.close() }
}
}
Scaffold(
scaffoldState = scaffoldState,
topBar = { AppTopBar() }
drawerContent = { AppDrawer() },
content = { HomeScreen() }
)
HomeScreen() {
NavHost(...) { ... }
}