i have one issue common viewmodel and navigation. ...
# multiplatform
m
i have one issue common viewmodel and navigation. this app kotlin multiplatform project android, ios, desktop initialize viewmodel
Copy code
val headlineViewModel = viewModel { HeadlineViewModel() }
main screen to detail screen move
Copy code
navController.navigate(NewsRouteScreen.NewsDetail.route)
back to main screen
Copy code
navController.navigateUp()
problem is back to main screen every time recreated view model so in viewmodel init method inside i api calling so it every time call it any idea how can solve this problem?
i
I'm DroidCon this week, so cannot jump on it now. could you please open GH issue with repro so one of my colleagues can investigate this case?
m
can you tell me where place this issue create
m
ok thanks i will create issue
h
val headlineViewModel = viewModel<HeadlineViewModel>() Try this
m
not work @Hristijan
h
Have you tried the new CMP alpha 1.7.0?
m
Copy code
lifecycleViewmodel = "2.8.3"
navigationCompose = "2.8.0-alpha02"
kotlin = "2.0.0"
compose-plugin = "1.6.11"
i
Navigation 2.8 is incompatible with Compose 1.6. I did a first alpha based on it but then rebased the stuff on 2.7.7. please use 2.7.0-alpha07 (newer than alpha02) with 1.6.11. Sorry for confusing versioning
m
Copy code
compose-plugin = "1.7.0-alpha01"
lifecycleViewmodel = "2.8.3"
navigationCompose = "2.8.0-alpha02"
i change it not work
i
Then it's better to use alpha08 that was released yesterday together with Compose
some bugs were fixed since alpha02
m
which one navigationCompose version change?
i
You can use
Compose 1.7.0-alpha01 + Navigation 2.8.0-alpha08
or
Compose 1.6.11 + Navigation 2.7.0-alpha07
m
ok
sorry both version try it not work
i
Then investigation of repro/gh issue is a way to go
m
ok thanks
Just to confirm: your case is about that Android works, but other platforms don't, right?
m
yes
1
i one try it logcat viewmodel init method i add log after i run that app viewmodel recreate in navigation up
one more try using koin in android viewModelOf and other other platform singleOf it perfect works
i hope you understand my comment @Ivan Matkov
i
I'll take a look at your reproduction project on Monday. Could you please send me a link to your GitHub issue with that repro?
m
where is issue create in github which repro
i
I replied yesterday - https://kotlinlang.slack.com/archives/C3PQML5NU/p1720081728744259?thread_ts=1720079256.601079&amp;cid=C3PQML5NU Please open the issue with attached reproduction if you haven't already
m
ok thanks you for time
https://github.com/JetBrains/compose-multiplatform/issues/5072#issue-2393399615 i created issue i explain my question with code so all are easily understand
i
Aha. A few nested
NavHost
s. It's the problem, tracked in https://github.com/JetBrains/compose-multiplatform/issues/4735
Workaround 1: Use single
NavHostController
and define nested graphs via
NavGraphBuilder.navigation()
function. See documentation
Workaround 2: Move second
rememberNavController()
call out of wiped composition.
m
ok thanks
in future this problem resolve right?
i
yes, it's due to lack of ported serialization of
NavHostController
, but it requires some changes in more low-level adoptings. In my TODO list, I even have some draft locally, but it requires more things to be figured out to make this properly. For now there are workarounds, so nested graphs are supported even now
m
can you more detail i am not understand
i
What exactly?
m
you mean currently not support nested graph
i
Currently it's NOT supported to have multiple
NavHostController
s inside each other. Currently it IS supported to have nested nav graphs. You can define nested graphs via
NavGraphBuilder.navigation()
function. See documentation
m
ok i understand
thanks for time
i
you are welcome
🙌 1
m
currently i have two solution find it
one problem viewmodel destroy in two navhost it move to one navhost and second nav host viewmodel destroy .i solution try koin in android viewModelOf and other other platform singleOf it perfect works second problem two navhost it move to one navhost and second nav host one navhost current destination path destroy o solution try
Copy code
var previousRoute by rememberSaveable() {
    mutableStateOf(navBackStackEntry?.destination?.route)
}
val currentRoute by rememberSaveable(navBackStackEntry) {
    derivedStateOf {
        navBackStackEntry?.destination?.route
    }
}

DisposableEffect(Unit) {
    onDispose {
        previousRoute = currentRoute
    }
}
LaunchedEffect(Unit) {
    if (previousRoute != null) {
        homeNavController.navigate(previousRoute!!) {
            // Pop up to the start destination of the graph to
            // avoid building up a large stack of destinations
            // on the back stack as users select items
            homeNavController.graph.startDestinationRoute?.let { startDestinationRoute ->
                // Pop up to the start destination, clearing the back stack
                popUpTo(startDestinationRoute) {
                    // Save the state of popped destinations
                    saveState = true
                }
            }

            // Configure navigation to avoid multiple instances of the same destination
            launchSingleTop = true

            // Restore state when re-selecting a previously selected item
            restoreState = true
        }
    }
}
i hope you understand my code @Ivan Matkov