Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between FragmentPagerAdapter with ViewPager with OffScreenLimit set to 1 and FragmentStatePagerAdapter?

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?

like image 444
dowjones123 Avatar asked May 14 '15 10:05

dowjones123


1 Answers

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)

like image 195
simekadam Avatar answered Oct 11 '22 08:10

simekadam