Colton Idle
08/15/2021, 1:16 AMFlorian
08/15/2021, 1:24 AMFlorian
08/15/2021, 1:25 AMColton Idle
08/15/2021, 1:30 AMScott Kruse
08/15/2021, 1:30 AMColton Idle
08/15/2021, 2:17 AMColton Idle
08/15/2021, 2:18 AMColton Idle
08/15/2021, 2:26 AMcomposable("detail/{id}") { MyDetailScreen() }
Composable
@Composable
fun MyDetailScreen(
vm: MyDetailViewModel = hiltViewModel(),
) {
ViewModel
@HiltViewModel
class MyDetailViewModel @Inject constructor(
savedStateHandle: SavedStateHandle
) : ViewModel() {
savedStateHandle.get<String>("id").orEmpty()
Oleksandr Balan
08/15/2021, 7:26 AMFlorian
08/15/2021, 9:28 AMFlorian
08/15/2021, 9:28 AMColton Idle
08/15/2021, 5:24 PMColton Idle
08/15/2021, 5:39 PMDylan
08/15/2021, 6:43 PMSavedStateHandle
too. Turns out after digging a lot into it, Ian Lake does mention that in a video from the Google I/O and also gives an example:
One of the Jetpack Compose samples is also using SavedStateHandle
.
https://github.com/android/compose-samples/blob/22eea87c898c8cc76e0c1042274e0962f0[…]java/androidx/compose/samples/crane/details/DetailsViewModel.kt
I really had a hard time figuring that out.
I should mention that I'm now trying to save some data to the SavedStateHandle
, but it looks like the SavedStateHandle
is not restored once my ViewModel is used again even though I'm using hiltViewModel(backStackEntry)
in my nav graph. So I still haven't figured that part yet and I have to say documentation would help a lot.Florian
08/15/2021, 7:06 PMDylan
08/15/2021, 7:15 PMFlorian
08/15/2021, 7:17 PMFlorian
08/15/2021, 7:17 PM