Marko Novakovic
08/03/2021, 5:37 PM2.4.0-alpha05
and am navigating from 1st to 2nd screen like this:
navController.navigate("$route/${id.value}")
and routes are defined like so:
@JvmInline
value class Routes(val route: String) {
companion object {
val Home = Routes("home")
val ItemDetails = Routes("item_details/{id}")
val ItemGallery = Routes("item_gallery")
}
}
I access that id
parameter inside ViewModel
savedStateHandle.get<String>(ARG_ITEM_ID) // ARG_ITEM_ID = "id", same as route arg
when I go from 1st screen, list of items, to 2nd, details, it works fine BUT when I go from details to gallery and press system back button ARG_ITEM_ID
is null
. it works fine with navigation version 2.4.0-alpha04
Ian Lake
08/03/2021, 5:51 PMinit
(which would the appropriate place to read the ID from the savedStateHandle
) shouldn't be done again when you return to that previous destinationMarko Novakovic
08/03/2021, 5:55 PMinit
but it crashes. that’s weird. I go from details to gallery with navigate(route_string)
. when I go back details ViewModel
is recreatedMarko Novakovic
08/03/2021, 5:56 PMinit
even when going backMarko Novakovic
08/03/2021, 5:57 PMViewModel
is annotated with @HiltViewModel
and created with hiltViewModel()
functionMarko Novakovic
08/03/2021, 5:58 PMconst val hiltNavigation = "1.0.0-alpha02"
Ian Lake
08/03/2021, 6:02 PMIan Lake
08/03/2021, 6:03 PMMarko Novakovic
08/03/2021, 6:04 PMMarko Novakovic
08/03/2021, 6:35 PMIan Lake
08/03/2021, 6:37 PMBackHandler
or overriding onBackPressed()
in your activity)?Marko Novakovic
08/03/2021, 6:39 PMViewModel
from the details screen to the gallery
composable(Routes.Gallery.route) {
val ItemDetails = navController.previousBackStackEntry!!
val viewModel = hiltViewModel<ItemDetailsViewModel>(itemDetails)
Gallery(viewModel = viewModel)
}
can this be a problem?Marko Novakovic
08/03/2021, 6:40 PMMarko Novakovic
08/03/2021, 6:44 PMMarko Novakovic
08/03/2021, 6:45 PMMarko Novakovic
08/03/2021, 6:46 PMandroidx.hilt:hilt-navigation-compose:1.0.0-alpha03
androidx.navigation:navigation-compose:2.4.0-alpha05
com.google.dagger:hilt-android:2.37
com.google.dagger:hilt-android-compiler:2.37
Ian Lake
08/03/2021, 6:46 PMpreviousBackStackEntry
call in a remember
?Ian Lake
08/03/2021, 6:47 PMpreviousBackStackEntry
starts pointing to your Home
destinationIan Lake
08/03/2021, 6:48 PMIan Lake
08/03/2021, 6:50 PMMarko Novakovic
08/03/2021, 7:21 PMMarko Novakovic
08/03/2021, 7:22 PMMarko Novakovic
08/03/2021, 7:24 PMMarko Novakovic
08/03/2021, 7:25 PMpreviousBackStackEntry
in remember
works. it’s not crashing now. thank you very much!Marko Novakovic
08/03/2021, 7:26 PM2.4.0-alpha05
because it introduced fade
as default animation which wasn’t the case in 2.4.0-alpha04
?Ian Lake
08/03/2021, 8:32 PMIan Lake
08/03/2021, 8:33 PMcurrentBackStackEntry
is now ItemDetail and previousBackStackEntry
becomes HomeIan Lake
08/03/2021, 8:34 PMIan Lake
08/03/2021, 8:35 PMpreviousBackStackEntry
entry over recompositions so that it always points to ItemDetailIan Lake
08/03/2021, 8:36 PMcurrentBackStackEntry
would become your new destination and previousBackStackEntry
would become Gallery)Marko Novakovic
08/03/2021, 10:20 PM