Meet
07/04/2024, 7:47 AMval headlineViewModel = viewModel { HeadlineViewModel() }
main screen to detail screen move
navController.navigate(NewsRouteScreen.NewsDetail.route)
back to main screen
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?Meet
07/04/2024, 7:52 AMIvan Matkov
07/04/2024, 8:14 AMMeet
07/04/2024, 8:16 AMIvan Matkov
07/04/2024, 8:28 AMMeet
07/04/2024, 8:29 AMHristijan
07/04/2024, 9:23 AMMeet
07/04/2024, 9:24 AMHristijan
07/04/2024, 9:32 AMMeet
07/04/2024, 9:37 AMlifecycleViewmodel = "2.8.3"
navigationCompose = "2.8.0-alpha02"
kotlin = "2.0.0"
compose-plugin = "1.6.11"
Ivan Matkov
07/04/2024, 9:45 AMMeet
07/04/2024, 9:48 AMcompose-plugin = "1.7.0-alpha01"
lifecycleViewmodel = "2.8.3"
navigationCompose = "2.8.0-alpha02"
i change it not workIvan Matkov
07/04/2024, 9:50 AMIvan Matkov
07/04/2024, 9:50 AMMeet
07/04/2024, 9:51 AMIvan Matkov
07/04/2024, 9:53 AMCompose 1.7.0-alpha01 + Navigation 2.8.0-alpha08
or
Compose 1.6.11 + Navigation 2.7.0-alpha07
Meet
07/04/2024, 9:54 AMMeet
07/04/2024, 10:03 AMIvan Matkov
07/04/2024, 10:04 AMMeet
07/04/2024, 10:06 AMIvan Matkov
07/05/2024, 6:46 AMIvan Matkov
07/05/2024, 6:47 AMMeet
07/05/2024, 6:47 AMMeet
07/05/2024, 6:51 AMMeet
07/05/2024, 6:52 AMMeet
07/05/2024, 6:55 AMIvan Matkov
07/05/2024, 6:57 AMMeet
07/05/2024, 6:58 AMIvan Matkov
07/05/2024, 7:00 AMMeet
07/05/2024, 7:01 AMMeet
07/06/2024, 5:29 AMIvan Matkov
07/08/2024, 9:07 AMNavHost
s. It's the problem, tracked in https://github.com/JetBrains/compose-multiplatform/issues/4735
Workaround 1: Use singleand define nested graphs viaNavHostController
function. See documentationNavGraphBuilder.navigation()
Workaround 2: Move secondcall out of wiped composition.rememberNavController()
Meet
07/08/2024, 9:10 AMMeet
07/08/2024, 9:11 AMIvan Matkov
07/08/2024, 9:14 AMNavHostController
, 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 nowMeet
07/08/2024, 9:15 AMIvan Matkov
07/08/2024, 9:16 AMMeet
07/08/2024, 9:17 AMIvan Matkov
07/08/2024, 9:19 AMNavHostController
s inside each other.
Currently it IS supported to have nested nav graphs. You can define nested graphs via NavGraphBuilder.navigation()
function. See documentationMeet
07/08/2024, 9:20 AMMeet
07/08/2024, 9:22 AMIvan Matkov
07/08/2024, 9:22 AMMeet
07/08/2024, 9:44 AMMeet
07/08/2024, 9:48 AMvar 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
}
}
}
Meet
07/08/2024, 9:51 AM