architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

How ViewModel communicate with other ViewModel that in the same Activity?

Open Guang1234567 opened this issue 7 years ago • 2 comments

Helllo dev:

How ViewModel communicate with other ViewModel that in the same Activity?

Could you provide some suggest or sample? Thanks.

Because i have a similar "Master/Detail Fragment" that has "MasterViewModel and DetailViewModel", so i want to reuse them when the device switches between portrait and landscape orientation.

Issue below not my love: https://github.com/googlesamples/android-architecture-components/issues/109

Guang1234567 avatar Aug 03 '18 01:08 Guang1234567

Use a shared ViewModel which provides data for both the MasterFragment and DetailFragment. Initialize the ViewModel in both fragments with activity?.run { ViewModelProvider.of (this, ...)}

elvedinelvis avatar Oct 21 '18 15:10 elvedinelvis

@elvedinelvis I get ViewModel has no zero argument constructor. Could you share the same fragment?

pratz9999 avatar Mar 23 '20 10:03 pratz9999