Skip to content Skip to sidebar Skip to footer

Android | Stop Tablayout Reload/refresh My Fragments

I've a similar tab layout, with 4 tabs. When I go from tab 0 to tab 2 and then I come back to tab 0, Fragment0 is reloaded.. Same problem when I go from a tab to another 'away' ta

Solution 1:

As you can see in the Documentation setOffscreenPageLimit

Inside your function setupViewPager(ViewPager viewPager) add this:

viewPager.setOffscreenPageLimit(limitNumberOfPages); //before setAdapter
viewPager.setAdapter(adapter);

And the limitNumberOfPages is an int that contains the amount of pages that can be scrolled without reloading the fragments. If you have 4 tabs, you should use:

int limitNumberOfPages = 3;

The default number for this property is 1.

Solution 2:

I solved it by myself Simple, use:

viewPager.setOffscreenPageLimit(numberOfPages);

From reference: Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.

In my case, I need 3 pages that should be retained.

Solution 3:

ViewPager holds Fragments left and/or right of the selected Tab. This is needed for fast animating tab change.

You can keep references of your Fragments in Your Activity, but this references can be null.

Post a Comment for "Android | Stop Tablayout Reload/refresh My Fragments"