https://kotlinlang.org logo
Title
z

Zun

06/12/2021, 4:29 PM
What would be the correct way of passing a default value to a ViewModel inside a composable, one that can be accessed inside its
init
function? Context in thread
Current code
val exploreViewModel: ExploreViewModel = hiltViewModel(it)
My ExploreViewModel needs to load shows by a given genre inside the init. The user can then (on the same screen) select a different genre.
My issue is, I can not simply do
val exploreViewModel: ExploreViewModel = hiltViewModel(it)
exploreViewModel.load("Comedy")
Because, if the user navigates as follows: Home -> ExploreScreen -> ShowScreen And from ShowScreen back to ExploreScreen with a back press, then the "Comedy" tag will be loaded again due to how composable works
b

brandonmcansh

06/12/2021, 4:34 PM
does savedStateHandle work for your usecase?
z

Zun

06/12/2021, 5:20 PM
that solved my problem, thanks!
I’m probably over thinking it but adding a simple “didLoadInitialGenre” Boolean in my ViewModel also solves the issue