Prevent ViewPager from destroying off-screen views

AndroidAndroid FragmentsAndroid Viewpager

Android Problem Overview


I have a ViewPager hooked up to a FragmentPagerAdapter that's displaying three fragments. The ViewPager appears to destroy a hosted fragment's view when it is more than one swipe away from the current position.

These views are all simple lists and this optimization is completely unnecessary, so I'd like to disable it. It's causing some visual problems because the lists have layout animations applied to them and those animations are being replayed after they've been destroyed and recreated. It also shows the scrollbar intro animation each time (where the scrollbar is briefly visible to indicate that scrolling is possible) which can be distracting, and the user's current scroll position is lost in the process.

It also doesn't load the third fragment until the first swipe happens, which is problematic because each fragment handles its own service calls and I'd prefer to have all three fire off at the same time when the activity loads. Having the third service call delayed is less than ideal.

Is there any way to convince ViewPager to stop this behavior and just keep all my fragments in memory?

Android Solutions


Solution 1 - Android

In revision 4 of the Support Package, a method was added to ViewPager which allows you to specify the number of offscreen pages to use, rather than the default which is 1.

In your case, you want to specify 2, so that when you are on the third page, the first one is not destroyed, and vice-versa.

mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2);

Solution 2 - Android

By default, ViewPager recreates the fragments when you swipe the page. To prevent this, you can try one of two things:

1. In the onCreate() of your fragments, call setRetainInstance(true).

2. If the number of fragments is fixed & relatively small, then in your onCreate() add the following code:

ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(3);

If I remember correctly, the second option is more promising. But I urge you to try both and see which of them work.

Solution 3 - Android

"Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed."

http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)

Solution 4 - Android

The selected answer is good but wasn't really good for me. This is because I had a lot of fragments (20-30) and so the activity with the ViewPager would take a lot of time to load if I had used setOffscreenPageLimit(30). In my case I had to implement the destroyItem() method (in the ViewPager's Adapter class) and remove the call to the super function. Here's the pseudo code

@Override                                                                                    
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                                                                                             
}                                                                                            

Now if you want more context here is the entire ViewPagerAdapter class

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> listFragment =
            new ArrayList<>();
    public ViewPagerAdapter(@NonNull FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return listFragment.get(position);
    }

    @Override
    public int getCount() {
        return listFragment.size();
    }

    /*This is the method I was taking about*/
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    
    }

    public void addFragment(Fragment fragment)
    {
        listFragment.add(fragment);
        notifyDataSetChanged();
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchefgonView Question on Stackoverflow
Solution 1 - AndroidDavid Snabel-CauntView Answer on Stackoverflow
Solution 2 - AndroidPankaj TalaviyaView Answer on Stackoverflow
Solution 3 - AndroidPercy VegaView Answer on Stackoverflow
Solution 4 - AndroidJoelView Answer on Stackoverflow