Hi all, how to avoir nested NavHost when one child composable has a bottomNavBar
Copy code
NavHost(navController = navController, startDestination = "") {
composable("viewA"){}
composable("viewB") {}
Scaffold(){
NavHost(myOwnNavController, startDestination = "Child1") { // How to avoid this
composable("Child1") {}
}
}
}
In this example i have to use two
NavController
i
Ian Lake
01/21/2022, 10:07 PM
This isn't the right way to approach the problem at all: you should be using the
currentBackStackEntryAsState()
to hide and show your bottom bar based on what destination you are on
Ian Lake
01/21/2022, 10:21 PM
Copy code
val navController = rememberNavController()
Scaffold(
bottomBar = {
// Use the NavController as the source of truth
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
// Use whatever logic you want to decide when to show the bottom bar
val showBottomBar = currentDestination?.route == "bottomBarScreen"
// Instead of instantly popping the bottom bar in and out of existence, animate it
AnimatedVisibility(visible = showBottomBar) {
BottomNavigation {
//
}
}
}
) {
NavHost(...)
}