[Android] FragmentPagerAdapter 和 FragmentStatePageAdapter区别

一、官方解释

FragmentPagerAdapter 

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.

FragmentStatePageAdapter

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.

二、通俗解释

1. 适用对象:

FragmentPagerAdapter适用static and less count数目Fragment

FragmentStatePageAdapter适用dynamic and more count数目Fragment

2. Fragment生命周期

FragmentPagerAdapter中的Fragment是Detached,即,仅仅是对应的View被回收,整个Fragment实例仍然存在。

FragmentStatePageAdapter中的Fragment是Removed/Destroyed,整个Fragment实例都被回收干掉。

3. 内存占用

FragmentPagerAdapter占用内存大,因为Fragment实例仍然存在。

FragmentStatePageAdapter不占用内存,但它并不是永远消失了,从名字中的State可以看出,系统会把Fragment的state作为Bundle通过savedInstanceState而保存下来,当我们重新回到该Fragment后,可以从onViewStateRestored中恢复。

参考:http://stackoverflow.com/questions/30235335/difference-between-fragmentpageradapter-with-viewpager-with-offscreenlimit-set-t

发表评论

电子邮件地址不会被公开。 必填项已用*标注