https://kotlinlang.org logo
#compose
Title
# compose
b

brabo-hi

01/21/2022, 5:33 PM
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
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

brabo-hi

01/22/2022, 4:10 AM
thank you for your support, it helps
13 Views