Hi all, how to avoir nested NavHost when one child...
# compose
b
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
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
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(...)
}
b
thank you for your support, it helps