https://kotlinlang.org logo
#compose
Title
# compose
d

Denis

01/10/2021, 7:55 AM
Shouldn't
val vm: MyViewModel = viewModel()
always retrieve the same ViewModel object in different composables but in the same activity?
h

Halil Ozercan

01/10/2021, 8:35 AM
I think there is already an abstraction responsible for this behaviour called ViewModelStoreOwner. Compose also follows this principle with AmbientViewModelStoreOwner. That's why the closest parent which provides ViewModelStoreOwner is responsible for which ViewModel to return. Navigation Compose also takes advantage of this behaviour to make sure that viewmodels are tied to navigation destinations, rather than activity or fragments.
❤️ 1
d

Denis

01/10/2021, 1:21 PM
Navigation Compose also takes advantage of this behaviour to make sure that viewmodels are tied to navigation destinations, rather than activity or fragments.
Ah, that's what tricked me! Thanks a lot, Halil!
2 Views