does anybody ever pass viewmodels into other viewm...
# android-architecture
r
does anybody ever pass viewmodels into other viewmodels, or is it better to just inject multiple viewmodels into the common view
g
Inject VM into the view? what do you mean? use multiple VM for some complex view is fine for me, but you don’t inject VM to Views
r
inject was poor choice of word, thanks
g
We use this approach with injecting VM to another VM sometimes, but only if those has some related features and one VM want to call method or subscibe on another But if we can avoid it we just use multiple VM on a single view, you have much more decoupled code in this case
1
but in our case we don’t use VM from arch components, but I don’t see how it may be different or any pitfals
s
We have some shared ViewModels that we pass around and some screens have multiple, it seems to work fine
g
in case of arch-comp, sharing activity's VM with different fragments is fine (https://developer.android.com/topic/libraries/architecture/viewmodel#sharing). Not so sure about sharing fragments VMs, you might end up leaking your VM.
1
s
Yeah you'd have to scope to an activity