https://kotlinlang.org logo
r

Rafal

03/31/2021, 9:28 AM
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

Javier

03/31/2021, 9:29 AM
I think you are looking for a shared ViewModel instead of injecting one VM in another one
r

Rafal

03/31/2021, 9:46 AM
oh, thanks! I’ll check that out 📖
🙂 1
w

Will Shelor

03/31/2021, 5:05 PM
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

Ariel Bogdziewicz

04/04/2021, 2:45 PM
Common code for view model is use case
m

marlonlom

04/10/2021, 2:49 PM
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 :)
3 Views