Difference between ActivityViewModels and lazy ViewModelProvider?
I've seen 2 ways to initialize a viewmodel:
private val someViewModel: SomeViewModel by activityViewModels()
private val someOtherViewModel: SomeOtherViewModel by lazy {
ViewModelProvider(this).get(SomeOtherViewModel::class.java)
}
I know lazy
initializes the ViewModel only when it's needed and that activityViewModels
is useful for sharing data between fragments, but other than that what's the difference?
Android documentation says the activityViewModels
is scoped to the activity, but does that mean if the same viewmodel is used in multiple fragments within the same activity using activityViewModels
that there's only one instance created that's shared between all the fragments?