android - How to refresh listview in a tab from another activity? -
i have tabbed activity 1 of tab has favorite movie recyclerview list. in tab, movies shown , onclick of 1 movie open movie details activity, can favorite movie. saving favorite movie's id in shared preferences. after goes favorite tab, can fetch movies , display it, every new movie addition, fetching favorite items not idea , maintaining static variables newly favorite movies , refreshing list not looking idea.
i thinking of using interfaces,broadcast receiver or rxjava approach this. interfaces, can't hold favorite tab instance in movie details activity time. rxjava's publish subject good, need maintain static reference send event movie details activity.
i thinking broadcast receivers or rxjava approaches best way here. please me pick right implementation.
you can broadcast intent previous fragment after response in next fragment make sure next fragment/tab loaded can set data in activity if change made in data fragment can incorporate , do not override destroyitem recreate fagment object again , fresh data...problem arise when
you have make changes next fragment previous fragment!
@override protected void onpostexecute(object o) { super.onpostexecute(o); intent intent = new intent("key_to_identify_the_broadcast"); bundle bundle = new bundle(); bundle.putstring("edttext", json.tostring()); intent.putextra("bundle_key_for_intent", bundle); context.sendbroadcast(intent); } and can receive bundle in fragment using broadcastreceiver class
private final broadcastreceiver mhandlemessagereceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { bundle bundle = intent.getextras().getbundle("bundle_key_for_intent"); if(bundle!=null){ string edttext = bundle.getstring("edttext"); } //you can call of methods using bundle use case } }; in oncreateview() of fragment need register broadcast receiver first otherwise broadcast receiver not triggered
intentfilter filter = new intentfilter("key_to_identify_the_broadcast"); getactivity().getapplicationcontext(). registerreceiver(mhandlemessagereceiver, filter); finally can unregister receiver avoid exceptions
@override public void ondestroy() { try { getactivity().getapplicationcontext(). unregisterreceiver(mhandlemessagereceiver); } catch (exception e) { log.e("unregister error", "> " + e.getmessage()); } super.ondestroy(); } you can create separate broadcast receivers in of fragments , use same broadcast broadcast data of fragments. can use different keys different fragments , broadcast using particular key particular fragment.
Comments
Post a Comment