willyrs
10/06/2022, 12:24 PM@Composable
fun A() { }
@Composable
fun B() {}
I start in A, then I navController.navigate() to B. Then, I go back by pressing the back button and A is recreated from scratch, as the LifecycleEventObservers passes the on_create, on_start and on_resume, like this:
A: ON_CREATE
A: ON_START
A: ON_RESUME
navigating to B
B: ON_CREATE
B: ON_START
B: ON_RESUME
back button
A: ON_CREATE
A: ON_START
A: ON_RESUME
Is there something wrong in my code, or is this the expected behaviour? I don't want A to be recreated every time I go back to itIan Lake
10/06/2022, 1:34 PMIan Lake
10/06/2022, 1:37 PMIan Lake
10/06/2022, 1:38 PMIan Lake
10/06/2022, 1:40 PMwillyrs
10/06/2022, 1:41 PMwillyrs
10/06/2022, 1:50 PMvar initialized by rememberSaveable {
mutableStateOf(false)
}
if(!initialized) {
initialized = true
doSomethingOnce()
}
Rafael Costa
10/06/2022, 2:29 PMLaunchedEffect(backStackEntry) {
}
Rafael Costa
10/06/2022, 2:32 PMNavBackStackEntry
is what gets passed to you in the composable { }
calls you do in the NavHost.Rafael Costa
10/06/2022, 2:33 PMNavBackStackEntry
. Which seems like it's what you want.willyrs
10/06/2022, 2:33 PMRafael Costa
10/06/2022, 2:35 PMinit {}
) of the ViewModel.willyrs
10/06/2022, 2:36 PMwillyrs
10/06/2022, 2:37 PMRafael Costa
10/06/2022, 2:49 PMRafael Costa
10/06/2022, 2:50 PMwillyrs
10/06/2022, 2:52 PM