Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh/Reload/Re instantiate a Fragment in an android tab system

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:

  1. detach attach fragment in onTabSelected (nothing happens)
  2. remove add fragment in onTabSelected (nothing happens)
  3. setOffscreenPageLimit(0) (Nothing happens)
  4. 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.

like image 832
Pacemaker Avatar asked May 27 '14 13:05

Pacemaker


People also ask

How do I refresh a fragment from another fragment?

So when you click your Button , find the Fragment you want to refresh by its tag, and then call your refresh method.

How do I refresh a ViewPager fragment?

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.


1 Answers

Please try this cod:

@Override
public void setMenuVisibility(boolean visible) {
    super.setMenuVisibility(visible);

    if (isVisible()) {
        //refresh your page
    }
}
like image 52
ch65 Avatar answered Oct 07 '22 06:10

ch65