android - Fragment which implements Observer pattern -
i have maybe little bit stupid question, didn't find answers on this. have fragment , entity object extends observable. want fragment notified when changes made in entity, implemented observer there.
now have doubts observers have added , deleted. mean correct callbacks in fragment.
now i'm using onviewcreated
add observer, ondestroyview
delete observer, think there may better places it.
here sample code:
@override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); wheelmanager.getinstance().addobserver(this); preferencemanager.getinstance().addobserver(this); } @override public void ondestroyview() { super.ondestroyview(); wheelmanager.getinstance().deleteobserver(this); preferencemanager.getinstance().deleteobserver(this); }
i thankful answers.
upd: i've looked through official developer guide, find nothing question.
as you've said you're updating views, oncreateview , ondestroyview suitable places code.
there exception , that's when you're updating adapterviews , recyclerviews -- can instantiate adapter in onattach , start observing there (and stop in ondetach). can link adapter view in oncreateview , never worry npe's etc.
Comments
Post a Comment