What is the difference between FragmentPagerAdapter
with ViewPager with OffScreenLimit set to 1 and FragmentStatePagerAdapter
?
About FragmentPagerAdapter
Google's guide says:
This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider FragmentStatePagerAdapter.
And about FragmentStatePagerAdapter
:
This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.
I think if I set the offscreenlimit to 1, it would destroy all fragments outside of the two next of the current fragment, and it behaves similar to a FragmentStatePagerAdapter. Is this correct?
The difference is exactly as it's written in docs. But it can be a little confusing.
FragmentPagerAdapter
holds its fragments in the FragmentManager
in detached state while they are not visible (while they are over the offscreen limit bounds) and FragmentStatePagerAdapter
removes them from the FragmentManager
.
The offscreenPageLimit
is something little different. All fragments within it stays attached to the UI. Once they go over, they are either removed, or detached.
You can see it in sources of FragmentPagerAdapter (line 121) and FragmentStatePagerAdapter (line 144)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With