I have an activity with a viewpager that has 3 tabs and 3 fragments, inside each tab is a fragment. I know that the fragment manager stores the initial instances of the fragments so that if I swipe away from a tab then return to that tab, it is loaded in memory and no updates are applied to it.
I need the opposite to happen, that is: Every time I swipe to a tab, a new instance of the fragment in that tab needs to load so that data will be updated.
I have tried:
- detach attach fragment in onTabSelected (nothing happens)
- remove add fragment in onTabSelected (nothing happens)
- setOffscreenPageLimit(0) (Nothing happens)
- refresh function inside each fragment to be called onTabSelected (gives nullPointerException)
I have been struggling with this problem for a week with no progress. I appreciate any help, be it a hint, a comment, a suggestion.
Let me know what code should I post, Thanks.
So when you click your Button , find the Fragment you want to refresh by its tag, and then call your refresh method.
OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.
Please try this cod:
@Override
public void setMenuVisibility(boolean visible) {
super.setMenuVisibility(visible);
if (isVisible()) {
//refresh your page
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With