android - Broadcast receivers and Fragments -
i have scenario implemented have navigation drawer nested fragments added stack tag. every fragment has next implemented:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { if (rootview == null) { rootview = inflater.inflate(r.layout.fragment_feed, container, false); // doing initialisaion here } else { ((viewgroup) rootview.getparent()).removeview(rootview); } homecoming rootview;
this best way think maintain lists within these fragments , dynamic headers , footers (thinking implementing new approach here already).
furthermore have these broadcasts when user loges out of app refresh these lists, or when location changes:
@override public void onattach(activity activity) { super.onattach(activity); localbroadcastmanager.getinstance(getactivity()).registerreceiver( locationbroadcastreciever, new intentfilter(updatelocationintentservice.location_changed)); localbroadcastmanager.getinstance(getactivity()).registerreceiver( significantbroadcastreciever, new intentfilter(userdefaultsmanager.significant_changed)); // farther code here }
the on detach called when fragment detached:
@override public void ondetach() { super.ondetach(); localbroadcastmanager.getinstance(getactivity()).unregisterreceiver( locationbroadcastreciever); localbroadcastmanager.getinstance(getactivity()).unregisterreceiver( significantbroadcastreciever); // cancel pending request fetching info };
this way right working have check when request finished whether of views have been destroyed because ondestroyview called when replace fragment ondetach not called since fragment on stack , not detached yet.
the current implementation view nullpointer checks allowing me update 4 fragments simultaneously when user logged out (or logged in) or location changed , there no items on list.
i sure there improve way, cannot seem same results @ ever else try.
my case update textfield in 1 of fragments hosted mainactivity.the simplest solution found this:--
in mainactivity class retrieved running instance of mainactivtiy.this mainactivity
private static mainactivity mainactivityrunninginstance; public static mainactivity getinstace(){ homecoming mainactivityrunninginstance; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mainactivityrunninginstance =this; ---------- }
now in broadcast reciever's onrecieve method, instance , phone call update method
@override public void onreceive(context context, intent intent) { if (intent.getaction().matches(intents.provider_changed_action)) { string textvalue="the text field value"; // null check for, if activity not running or in paused state, in case update required onlyif main activity active or paused if(mainactivity.getinstace()!=null) mainactivity.getinstace().updateui(textvalue); }
now part need run update in uithread,the updateui method of mainactivity phone call fragments update method.
public void updateui(final string str) { mainactivity.this.runonuithread(new runnable() { public void run() { //use findfragmentbyid fragments defined in xml ((simplefragment)getsupportfragmentmanager().findfragmentbytag(fragmenttag)).updateui(str); } }); }
and final step updating fragment's text field
public void updateui(string str){ tv_content.settext(str); }
and bingo, done. referred post solve issue. hope help others.
android android-fragments broadcastreceiver
No comments:
Post a Comment