hey guys, is there anyone facing this kinda issue ...
# multiplatform
h
hey guys, is there anyone facing this kinda issue using KMP navigation with multi stack bottom bar and ViewModel, i’ve created a min reproducible repo https://github.com/FunkyMuse/kmp-multi-stack-navigation-issue a view model is being re-created all the time except the starting point ViewModel which stays the same all the time, is it an issue in the code or an actual bug
p
I think you are popping up, all the way to the first destination. So the stack is being cleared anytime you switch tabs. "Restore state = true", restores the navigation stack corresponding to that NavDestination if it has any inner history, but doesn't reuse the previous navbackstackentry instance or the previous ViewModel. I think for your use case you have to scope the ViewModel to the parent NabBackstackEntry or the Navbackstackentry that corresponds to the bottom navigation component itself. Although tbh I am not sure if what I described is the right behavior.
h
🤔 i copy pasted this function for navigating to top level destination, maybe it’s not ready for multi stack bottom nav, although it has had worked before perfectly fine on Android
m
i check your code. i have same issue some day before. you can all bottom navigation screen in single navigation like this and some screen you can do different navigation function but still this problem viewmodel recreate so i suggest use koin 4.0.0 i try it all platform it prefect works and koin use in viewmodel inject in common main
Copy code
fun NavGraphBuilder.mainNavGraph(
    rootNavController: NavHostController,
    innerPadding: PaddingValues
) {
    navigation(
        startDestination = MainRouteScreen.Headline.route,
        route = Graph.MainScreenGraph
    ) {
        composable(route = MainRouteScreen.Headline.route) {
            HeadlineScreen(rootNavController = rootNavController, paddingValues = innerPadding)
        }
        composable(route = MainRouteScreen.Search.route) {
            SearchScreen(rootNavController = rootNavController, paddingValues = innerPadding)
        }
        composable(route = MainRouteScreen.Bookmark.route) {
            BookmarkScreen(rootNavController = rootNavController, paddingValues = innerPadding)
        }
    }

}
h
image.png
p
The screenshot matches the behavior as I described above. The first tab NavbackEntry remains the same in the backstack but the rest of the tabs Navbackstackentries are popped and recreated as you switch between them. Perhaps if you remove popUpTo, and just use LaunchSingleTop, may work
m
your code prefect problem multiple navigation graph some issue
viewmodel works
i hope you understand code. and may be in future solve this problem
Copy code
@Composable
fun RootNavGraph(
    rootNavController: NavHostController,
    innerPadding: PaddingValues,
    settingViewModel: SettingViewModel
) {
    NavHost(
        navController = rootNavController,
        startDestination = Graph.MainScreenGraph,
    ) {
        mainNavGraph(rootNavController = rootNavController, innerPadding = innerPadding)
        composable(
            route = NewsRouteScreen.NewsDetail.route,
        ) {
            rootNavController.previousBackStackEntry?.savedStateHandle?.get<String>("article")?.let { article ->
                val currentArticle: Article = Json.decodeFromString(article)
                ArticleDetailScreen(rootNavController, currentArticle)
            }
        }
        composable(
            route = SettingRouteScreen.SettingDetail.route,
        ) {
            SettingScreen(navController = rootNavController, settingViewModel)
        }
    }
}

fun NavGraphBuilder.mainNavGraph(
    rootNavController: NavHostController,
    innerPadding: PaddingValues
) {
    navigation(
        startDestination = MainRouteScreen.Headline.route,
        route = Graph.MainScreenGraph
    ) {
        composable(route = MainRouteScreen.Headline.route) {
            HeadlineScreen(rootNavController = rootNavController, paddingValues = innerPadding)
        }
        composable(route = MainRouteScreen.Search.route) {
            SearchScreen(rootNavController = rootNavController, paddingValues = innerPadding)
        }
        composable(route = MainRouteScreen.Bookmark.route) {
            BookmarkScreen(rootNavController = rootNavController, paddingValues = innerPadding)
        }
    }

}
and you can see the video article detail separate composable so again create viewmodel
h
This code doesn't implement multi stack navigation, you're achieving multi stack only for the mainNavGraph, no?
m
ok ok i understand sorry my information not helpfully
h
No worries, thank you, i might have to open an issue
👍 1
p
Thanks, the issue is that save/restore is not working. It should bring the same Navbackstackentry instance when you do a save/restore cycle.
1
m
i already create issue but close this issue https://github.com/JetBrains/compose-multiplatform/issues/5072
👍 1
h
This isn't a jetbrains issue, have to be reported to Google
m
ok ok
my mistake
i am confuse how can detect jetbrains issue or google issue
and what is difference jetbrains lifecycle library and google lifecycle library
p
They used to be different implementations, not sure anymore, I haven't been up to date. Usually if the issue happens in Android you report it to Android. If Android is ok but the problem only happens in any other platform. Then you report it in Jetbrains "You Track" or Compose Multiplatform GitHub. But after the Android compose repo is hosted by Jetbrains too, I don't even know where to report the issues anymore 🤷‍♂️😕