I'm facing an issue with compose navigation in KMP...
# compose
r
I'm facing an issue with compose navigation in KMP when I have multiple NavHosts for each bottom-nav-bar item, whenever I switch to another bottomNav item and back to the start destination, the first navigation in that start destination doesn't work, second one works. e.g. startDestination = Home 1. while in Home -> doing any inner navigation works 2. go to Search tab 3. go back to Home 4. navigate to any screen inside Home -> first click doesn't work, second one doesn't I tried using a different startDestination, it works again for Home, but the new start will have the same issue
code snippet:
Copy code
@Composable
fun MainScreen() = AppTheme {
  val navController = rememberNavController()

  Scaffold(
    bottomBar = {
      BottomNavigationBar(navController = navController, onItemClick = { route ->

        navController.navigate(route) {
          popUpTo(navController.graph.findStartDestination().id) {
            saveState = true
          }
          launchSingleTop = true
          restoreState = true
        }

      })
    }
  ) {
    NavHost(
      navController = navController,
      startDestination = MainScreenRoute.HomeNavHost,
    ) {
      composable<MainScreenRoute.HomeNavHost> {

        val navController = rememberNavController()
        NavHost(
          navController = navController,
          startDestination = HomeRoute.Home,
        ) {
          composable<HomeRoute.Home> {
            HomeScreen()
          }
        }
      }

      composable<MainScreenRoute.MoreNavHost> {
        // another nav host similar to Home
      }

    }
  }
}
s
Don't have multiple NavHosts
r
thats a fix 😄
but what should I do instead?
s
One NavHost, split things up in navgraphs if that's what you wanna do. The docs cover this, multiple NavHosts is more or less never the right approach
r
oh I can do that? 🤔 interesting, so I can have multiple graphs, a graph for each bottom bar item? and switching between them will keep the stack in each of them?
s
Exactly. Checkout NowInAndroid for an example where they do that. Also the docs definitely cover this somewhere, not sure where right now I'm afk
✅ 1
i
https://youtrack.jetbrains.com/issue/CMP-6689 Fixed, will be published with next alpha
r
Thanks for the suggestion @Stylianos Gakis and thanks for that @Ivan Matkov!
Hey @Ivan Matkov this not the same issue 🤔
I tried
navigation { }
and it works, but I still have one issue - Home Tab - Home - Post - Profile Tab - Profile Tab - Post now if I'm in Home Tab Home screen -> then open a post then click on Profile Tab then click on "back" I go back to Home Tab but the state is not restored, how to restore the state when I'm back to first tab?