FragmentPagerAdapter和FragmentStatePagerAdapter的区别是什么?
关于FragmentPagerAdapter谷歌的指南说:
此版本的寻呼机最适合在有少量
通常需要分页更多的静态片段,例如一组
选项卡。用户访问的每个页面的片段将被保留
内存,尽管它的视图层次结构可能在不可见时被破坏。
这可能导致使用大量的内存,因为片段
实例可以保留任意数量的状态。对于较大的电视机
,考虑FragmentStatePagerAdapter。
关于FragmentStatePagerAdapter:
这个版本的寻呼机在有大量数据时更有用
页面,工作起来更像一个列表视图。当页面不可见时
对用户来说,他们的整个碎片可能被销毁,只保留了
该片段的保存状态。这使得寻呼机可以保留很多信息
与。相比,与每个访问页面关联的内存更少
FragmentPagerAdapter以潜在的更多开销为代价
在页面之间切换。
所以我只有3个片段。但它们都是包含大量数据的独立模块。
Fragment1处理一些数据(用户输入的),并通过活动将其传递给Fragment2,这只是一个简单的ListFragment。Fragment3也是一个ListFragment。
所以我的问题是:我应该使用哪个适配器?FragmentPagerAdapter还是FragmentStatePagerAdapter?
医生说:
有两种类型的标准pageradapter管理每个片段的生命周期:FragmentPagerAdapter和FragmentStatePagerAdapter。它们都能很好地处理片段,但它们更适合不同的场景:
The FragmentPagerAdapter stores the fragments in memory as long as the user can navigate between them. When a fragment is not visible, the PagerAdapter will detach it, but not destroy it, so the fragment instance remains alive in the FragmentManager. It will release it from memory only when the Activity shuts down. This can make the transition between pages fast and smooth, but it could cause memory issues in your app if you need many fragments.
The FragmentStatePagerAdapter makes sure to destroy all the fragments the user does not see and only keep their saved states in the FragmentManager, hence the name. When the user navigates back to a fragment, it will restore it using the saved state. This PagerAdapter requires much less memory, but the process of switching between pages can be slower.
Like the docs say, think about it this way. If you were to do an application like a book reader, you will not want to load all the fragments into memory at once. You would like to load and destroy Fragments as the user reads. In this case you will use FragmentStatePagerAdapter. If you are just displaying 3 "tabs" that do not contain a lot of heavy data (like Bitmaps), then FragmentPagerAdapter might suit you well. Also, keep in mind that ViewPager by default will load 3 fragments into memory. The first Adapter you mention might destroy View hierarchy and re load it when needed, the second Adapter only saves the state of the Fragment and completely destroys it, if the user then comes back to that page, the state is retrieved.
医生说:
有两种类型的标准pageradapter管理每个片段的生命周期:FragmentPagerAdapter和FragmentStatePagerAdapter。它们都能很好地处理片段,但它们更适合不同的场景:
The FragmentPagerAdapter stores the fragments in memory as long as the user can navigate between them. When a fragment is not visible, the PagerAdapter will detach it, but not destroy it, so the fragment instance remains alive in the FragmentManager. It will release it from memory only when the Activity shuts down. This can make the transition between pages fast and smooth, but it could cause memory issues in your app if you need many fragments.
The FragmentStatePagerAdapter makes sure to destroy all the fragments the user does not see and only keep their saved states in the FragmentManager, hence the name. When the user navigates back to a fragment, it will restore it using the saved state. This PagerAdapter requires much less memory, but the process of switching between pages can be slower.