How To Update Fragement Data When Swiching Tabs
I am using Material Tab that I used 3 tabs. Tab A [ Product List ] Has a Recycle list and data populating from remote Server. For ex, it has Product title, image and favorite ima
Solution 1:
- add
compile 'org.greenrobot:eventbus:3.0.0
' in gradle file In every fragment add in onCreate()
EventBus.getDefault().register(this);
In every fragment add in onDestroyView
EventBus.getDefault().unregister(this);
Create Java class for Event Example:
publicclassEventUpdateList { privateboolean listNeedUpdate= true; publicEventUpdateList(boolean listNeedUpdate) { this.listNeedUpdate= listNeedUpdate; } publicbooleanisListNeedUpdate() { return listNeedUpdate; } }
In all fragments add
@Subscribe(threadMode = Main)publicvoidonListRefresh(EventUpdatelist eventUpdateList) { if (eventUpdateList.isNeedListUpdate) { // refresh your list } }
In your activity add
EventBus.getDefault.post(newEventUpdateList(true));
And all 3 fragments getting information about event to refresh list. You can even post Event with full list to refresh instead of this boolean and get in 3 fragments.
Solution 2:
Since the lib you're using basically uses ViewPager under the hood to manage given Fragment as Tab so this setUserVisibleHint method of Fragment should be the place you can update your tab content every time it loads.
publicvoidsetUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// Do stuffs that you want to do every time this fragment loads
} else {
}
}
Post a Comment for "How To Update Fragement Data When Swiching Tabs"