Is it ok to inject a `ViewModel` into another `Vie...
# android-architecture
r
Is it ok to inject a
ViewModel
into another
ViewModel
? I have one
ViewModel
scoped to an Activity and the second one scoped to a Fragment that could use one
flow
from the Activity one
j
I think you are looking for a shared ViewModel instead of injecting one VM in another one
r
oh, thanks! I’ll check that out 📖
🙂 1
w
If that doesn’t work, my personal thought (not knowing that much about your situation or scope) would probably be to have the fragment subscribe to the flow, and call its viewmodel when the root VM updated- if they truly are distinct conceptually.
a
Common code for view model is use case
m
Mmm @Rafal i think you could use the repository/use cases that are common to use inside the viewmodel, the purpose of the viewmodel is to have one per screen :)