Chris Lacy Chris Lacy - 1 year ago 406
Android Question

ViewPager.setOffscreenPageLimit(0) doesn't work as expected

The fragments I use in my

instance are quite resource intensive, so I'd only like to load one at a time. When I try the following:


FragmentStatePagerAdapter.getItem(int position)
override function is called 3 times, which is what happens when I call
. I would expect it to only be called once, because I specified 0 offscreen pages.

I believe I'm calling everything correctly, because if I call
FragmentStatePagerAdapter.getItem(int position)
is called 5 times as I would expect.

Does ViewPager require a minimum of 1 offscreen pages, or am I doing something wrong here?

Answer Source

Does ViewPager require a minimum of 1 offscreen pages

Yes. If I am reading the source code correctly, you should be getting a warning about this in LogCat, something like:

Requested offscreen page limit 0 too small; defaulting to 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download