23

I have a ViewPager with a FragmentPagerAdapter, and my app has previously used just two fragments (different types) without issues.

I just added a third fragment, though, and now my adapter/viewpager seems to be destroying my fragments when I get far away from them. For example, if I'm on page 1, page 3 is destroyed and recreated when I get close to it. If I'm on page 3, the same happens to page 1.

This is causing lots of issues in my app. The fragments aren't very RAM-heavy at all, so how can I stop this from happening?

5
  • Is it destroying the Fragment or destroying the View?
    – DeeV
    Commented Nov 7, 2012 at 15:55
  • I'm not sure. How would I check? Commented Nov 7, 2012 at 15:56
  • 1
    You just said "it's causing a lot of issues." If the Fragment is being destroyed, then it's state is being wiped from memory and reseting to defaults when reloaded.
    – DeeV
    Commented Nov 7, 2012 at 16:12
  • If you use FragmentPagerAdapter your Fragments once created will never be destroyed while swiping. Only onPause and onStop method will be called. Probably you are using FragmentStatePagerAdapter
    – AppiDevo
    Commented Mar 21, 2017 at 17:31
  • And of course onDestroyView will be called too, after onStop.
    – AppiDevo
    Commented Mar 21, 2017 at 17:40

2 Answers 2

53

I believe you are looking for ViewPager.setOffscreenPageLimit().

In your case, the following should keep your fragments in memory and not destroy them.

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

However, I suspect that you are not correctly storing your view state when being destroyed. Your fragments should correctly handle being destroyed/recreated. Your fragments would for example also be destroyed and recreated if an orientation change happens. It could also happen if the user leaves your application and the system later needs memory and destroys your Activity. It should be able to reopen and be in the same state as before. If this is indeed the problem for you, consider saving state in onSaveInstanceState(). The saved state will be presented to you in onCreate where you can initialize the state of the fragment to be the same as the destroyed one.

2
  • Perfect! This is going to be my temporary solution until I make my fragments more network-independent. Commented Nov 7, 2012 at 16:20
  • 1
    onSaveInstanceState() is not called most of the times. E.g., you're on page 1, move to page 5: the first fragment goes through onDestroyView(), for example, but not onSaveInstanceState(). In my case at least.
    – natario
    Commented Sep 24, 2015 at 18:33
5

just override this method in FragmentpagerAdapter

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
       // TODO Auto-generated method stub
       super.destroyItem(ViewGroup container, int position, Object object);
}

remove super.destroyItem(ViewGroup container, int position, Object object);

from your code

2
  • This will work as long as you never want your Fragment destroyed (even if the FragmentPagerAdapter goes out of scope). This could be problematic, but in some situations it's perfect. For those problem situations, you could always call destroyItem(..) on your own.
    – SMBiggs
    Commented Oct 9, 2016 at 3:03
  • Thank you so much for this solution :) Commented May 12, 2017 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.