Android MVVM DataModel with Context -
i trying learn & implement mvvm databinding in app. facing difficulties in understanding/deciding few things.
i have 1 header view in app need use in multiple activities, created this
view_header.xml headerviewmodel.java (implements java.util.observer) headerdatamodel.java (extends java.util.observable)
where headerviewmodel
bound view_header.xml
. include view_header.xml
in activity_xyz.xml
& set viewmodel of header view xyzactivity
this,
xyzviewbinding.setheadervm(new headerviewmodel(new headerdatamodel(mcontext)));
now have questions based on this:
1) headerdatamodel
needs context work sharedpreference
& broadcastreceiver
, initialize inside xyzactivity
instead of headerviewmodel
- correct approach?
2) had rely on broadcast receiver update header view, registered inside constructor of headerdatamode
. headerdatamodel
observable
, whenever receive broadcast, call notifyobservers()
observer(headerviewmodel) detects & updates view.- correct approach?
3) register receiver in constructor of headerdatamodel, created ondestroy() method inside data model unregister & call ondestroy xyzactivity’s ondestroy().- correct approach?
- as headerdatamodel needs context work sharedpreference & broadcastreceiver, initialize inside xyzactivity instead of headerviewmodel- correct approach? - no, wrong. must initialize in headerdatamodel later on if want update headerdatamodel, can it.
- i had rely on broadcast receiver update header view, registered inside constructor of headerdatamode. headerdatamodel observable, whenever receive broadcast, call notifyobservers() observer(headerviewmodel) detects & updates view.- correct approach? - yes told in first point.
- as register receiver in constructor of headerdatamodel, created ondestroy() method inside data model unregister & call ondestroy xyzactivity’s ondestroy().- correct approach? - yes. make public ondestroy() method in headerdatamodel class.
Comments
Post a Comment