android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.45k stars 8.28k forks source link

Can activity or fragment have multiple viewmodel #205

Closed prabinshrestha closed 7 years ago

prabinshrestha commented 7 years ago

i have this dashboard activity which shows advertismentList and other Property List

I have made AdvertisementModel which consult with AdvertismentRepository for data, in similar way I have made PropertyViewModel which consult with PropertyRepository for data

Now,In DashBoardActivity

*First Approach i set both view model(property and advertisement) with ViewModelProviders --> is this right ??

*Second Approach should i have to make separate DashBoardActivityViewModel and then it consist of these two dependent component viewmodel in its fields,property and advertisement.

mynote commented 7 years ago

Sure.

You can have as many ViewModels as you want aslong as you bind them to a livecycle (activity, fragment).

prabinshrestha commented 7 years ago

@mynote

Thank you so much :)

bernardonigbinde commented 5 years ago

You can, but I'd like to know, should you?