Pablo
01/15/2025, 10:18 PMnavigation
on a compose desktop
app, and I have a main screen, which is the starting one, and it haves some loading data on the init
block of his viewmodel
. The problem is that when I navigate to another screen and then go to the main screen again, the init
block of the main screen is again called, doing the loading again. How can this be avoided?
NavHost(
navController = appStateHolder.navController,
startDestination = ScreenRoute.MainScreen.name,
modifier = modifier
) {
composable(ScreenRoute.MainScreen.name) {
MainScreen(modifier = Modifier.padding(16.dp))
}
composable(ScreenRoute.StopsScreen.name) {
StopsScreen(modifier = Modifier.padding(16.dp))
}
composable(ScreenRoute.LinesScreen.name) {
LinesScreen(modifier = Modifier.padding(16.dp))
}
}
To clarify, I'm navigating back to the main screen by calling navController.navigate(ScreenRoute.MainScreen.name)
and not by pressing back, because back key is not present in compose desktop
.Stylianos Gakis
01/16/2025, 12:47 AMPablo
01/16/2025, 8:24 AMPablo
01/16/2025, 8:25 AMPablo
01/16/2025, 8:25 AMPablo
01/16/2025, 8:25 AMPablo
01/16/2025, 8:28 AMPablo
01/16/2025, 8:28 AMPablo
01/16/2025, 8:29 AMnavigate
call, if a screen is already on the stack, it must pop it instead of creating a new one?Stylianos Gakis
01/16/2025, 8:48 AMnavController.popBackStack<ScreenRoute.MainScreen>(inclusive = false)
And your start destination must always be on your backstack, so this will always work.Stylianos Gakis
01/16/2025, 8:50 AMStylianos Gakis
01/16/2025, 8:52 AMnavController.navigate(SomeScreen()) {
popUpTo<SomeOtherScreen> {
inclusive = true
}
}
Stylianos Gakis
01/16/2025, 8:52 AMPablo
01/16/2025, 9:07 AMnavController.navigate(route) { popUpTo(route) { inclusive = true } }
but it's not working, the init block of the viewmodel is still being called when going back to main screen using that functionPablo
01/16/2025, 9:13 AMPablo
01/16/2025, 9:13 AMStylianos Gakis
01/16/2025, 9:30 AMPablo
01/16/2025, 9:33 AMvm: MainScreenViewModel = koinViewModel()
Pablo
01/16/2025, 9:36 AMPablo
01/16/2025, 9:37 AMif (!navController.popBackStack(route, inclusive = false)) {
navController.navigate(route)
}
Pablo
01/16/2025, 9:37 AMinclusive = false
Pablo
01/16/2025, 9:38 AMinclusive = true
, it stops working, because it removes the destination from the backstack, and doing so, means that when you come back to it, it will be recreated from scratch recalling the init block of the viewmodelPablo
01/16/2025, 9:38 AMnavController.navigate(route) { popUpTo(route) { inclusive = false } }
it doesnt work, doesn't matter if you put true or false on inclsuive, didn't work in either casePablo
01/16/2025, 9:39 AMPablo
01/16/2025, 9:39 AMPablo
01/16/2025, 9:48 AMStylianos Gakis
01/16/2025, 1:44 PMPablo
01/16/2025, 1:58 PMPablo
01/16/2025, 1:58 PMPablo
01/16/2025, 1:59 PMif (!navController.popBackStack(route, inclusive = false)) {
navController.navigate(route)
}
Pablo
01/16/2025, 1:59 PMpopUpTo(route) { inclusive = false }
and with popUpTo(route) { inclusive = true }
, I don't understand itStylianos Gakis
01/16/2025, 2:21 PMStylianos Gakis
01/16/2025, 2:22 PMPablo
01/20/2025, 12:52 PMif (!navController.popBackStack(route, inclusive = false)) {
navController.navigate(route)
}
and also I noticed that it doesn't recall main screen viewmodel init block if I come back from other screen... but if I press "main screen" button when staying in main screen, then, it recalls the init blockPablo
01/20/2025, 12:54 PMnavController.navigate(route) { popUpTo(route) { inclusive = true } }
navController.navigate(route) { popUpTo(route) { inclusive = false} }
if (!navController.popBackStack(route, inclusive = true)) { navController.navigate(route)}
This one doesn't call the init block in that case, but it does when I'm on main and press go to main button and also, increases constantly backstack
if (!navController.popBackStack(route, inclusive = false)) { navController.navigate(route)}
Pablo
01/20/2025, 12:55 PM