1

I have 3 fragments in ViewPager. When I slide from fragment 1 to 3, the fragment 3 is refreshing data (re-execute all methods) but when I slide from 1 to 2 or 2 to 3 the next fragment doesn't refresh data.

Can you help me to understand why? How can I refreshing every time?

My activity extends base class FragmentActivity and fragment extends base class Fragment.

Marko
  • 20,385
  • 13
  • 48
  • 64
simvar77
  • 85
  • 5

2 Answers2

1

ViewPager by default uses setOffscreenPageLimit(1), which means that it 'caches' one view to the left and another one to the right, so if you are on 2nd fragment and go to either 1st or 3rd, it will not reload. If you would go back to 1st and than 2nd, the 3rd would be reloaded.

setOffscreenPageLimit(0) produces a warning (minimum has to be 1).

A solution would be to load use the setUserVisibleHint in your Fragments to detect when they become visible, and handle loading/refreshing data there.

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        // load data here
    }else{
        // fragment is no longer visible
    }
}

This answer is taken from this SO answer.

Community
  • 1
  • 1
Marko
  • 20,385
  • 13
  • 48
  • 64
-1

Firstly in fragment structure android loads left and right side fragment previously.For example in the second fragment 1 and 2 already has been installed. There is setOffscreenPageLimit function for this.

Your second question: it is possible but in a tricky way. I generally use notify adapter for this. After every swipe you can use notifyDataSetChanged(); or reload your data to adapters.

Anil KARA
  • 21
  • 4