Android Architecture Patterns Part 3: Model-View-ViewModel
Florina Muntenescu

Hi, thanks for such an awesome article. I have started understanding & implementing MVVM in my apps.

but need help regarding following,

I have one header view in my app which I need to use in more then 1 activity, so I basically created this

view_header.xml (implements java.util.Observer) (extends java.util.Observable)

where HeaderViewModel is bound with view_header.xml.
Now I include the view_header.xml in activity_xyz.xml & set ViewModel of Header View from that XyzActivity like this,

xyzViewBinding.setHeaderVm(new HeaderViewModel(new HeaderDataModel(mContext)));

Now I have questions based on this:
1) As HeaderDataModel needs context to work with shared preference & broadcast receiver, I initialize it inside the XyzActivity instead of HeaderViewModel- Is this correct approach?

2) I had to rely on broadcast receiver to update my header view, so I registered it inside the constructor of HeaderDataMode. Now as my HeaderDataModel is Obserable, whenever I receive broadcast, I call notifyObservers() so that the observer(HeaderViewModel) detects it & updates it view.- Is this correct approach?

3) as I register receiver in constructor of HeaderDataModel, I created onDestroy() method inside data model where I unregister it & I call this onDestroy from XyzActivity’s onDestroy().- Is this correct approach?