https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
r

rezenebe

12/18/2018, 3:43 PM
does anybody ever pass viewmodels into other viewmodels, or is it better to just inject multiple viewmodels into the common view
g

gildor

12/18/2018, 3:46 PM
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

rezenebe

12/18/2018, 3:47 PM
inject was poor choice of word, thanks
g

gildor

12/18/2018, 3:48 PM
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

sam

12/18/2018, 5:35 PM
We have some shared ViewModels that we pass around and some screens have multiple, it seems to work fine
g

ghedeon

12/19/2018, 9:13 PM
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

sam

12/22/2018, 5:00 AM
Yeah you'd have to scope to an activity
4 Views