Closed vitidev closed 3 years ago
Hey - This does not seem related to Hilt, swapping hiltViewModel
with Compose's viewModel
function still causes recomposition, so I don't think Hilt's navigation view model function is related here. The viewModel
you get should still be the same as it is scoped to the nav graph so this should not be an issue. What sort of trouble is it causing? If you are looking for a more general answer of why is there a recomposition, then it might be best to ask in a Compose forum. Maybe in Stackoverflow or Buganizer.
What sort of trouble is it causing?
We can get arguments backStackEntry.arguments?.getString("userId")
but there is no easy way to pass these parameters into the viewmodel constructor. Where, in general, is an example of how pass parameters to hiltViewModel<>()
?
Moreover, the viewmodel may already exist. And the easiest way is to call some method on the viewmodel that ensure that the viewmodel is in valid state before starting the composition of the main content.
For example, "navigate with result" allow get result via navController.previousBackStackEntry?.savedStateHandle?
in compose layer only, not by SavedStateHandle
injected to ViewModel, so we need configure viewmodell
And composable method is good for putting this code in it. because it usually does not depend on any state variables and therefore never recomposes
Of couse, I can use something like
var flag by remember { mutableStateOf(false) }
if (!flag) {
... configure viewmodel state
flag = true
}
But this is a boilerplate code
dependies
code. just 3 routes
navigation to "first" always happens once but when navigating to "second" (with
hiltViewModel<>
) - it call twiceI understand that this is a recomposition, but can it be fixed?