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
Zun
06/12/2021, 4:31 PM
Current code
Copy 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.
Zun
06/12/2021, 4:32 PM
My issue is, I can not simply do
Copy code
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!
Zun
06/12/2021, 7:27 PM
I’m probably over thinking it but adding a simple “didLoadInitialGenre” Boolean in my ViewModel also solves the issue