What would be the correct way of passing a default...
# compose
z
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
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.
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
does savedStateHandle work for your usecase?
z
that solved my problem, thanks!
I’m probably over thinking it but adding a simple “didLoadInitialGenre” Boolean in my ViewModel also solves the issue