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.
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.
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.
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.
FragmentPagerAdapter
stores the whole fragment in memory, and could increase a memory overhead if a large amount of fragments are used inViewPager
.- In contrary its sibling,
FragmentStatePagerAdapter
only stores the savedInstanceState of fragments, and destroys all the fragments when they lose focus. - Therefore
FragmentStatePagerAdapter
should be used when we have to use dynamic fragments, like fragments with widgets, as their data could be stored in thesavedInstanceState
.Also it wont affect the performance even if there are large number of fragments. - In contrary its sibling
FragmentPagerAdapter
should be used when we need to store the whole fragment in memory. - When I say the whole fragment is kept in memory it means, its instances wont be destroyed and would create a memory overhead. Therefore it is advised to use
FragmentPagerAdapter
only when there are low number of fragments forViewPager
. - It would be even better if the fragments are static, since they would not be having large amount of objects whose instances would be stored.
To be more detail,
FragmentStatePagerAdapter:
- with
FragmentStatePagerAdapter
,your unneeded fragment is destroyed.A transaction is committed to completely remove the fragment from your activity'sFragmentManager
. - The state in
FragmentStatePagerAdapter
comes from the fact that it will save out your fragment'sBundle
fromsavedInstanceState
when it is destroyed.When the user navigates back,the new fragment will be restored using the fragment's state.
FragmentPagerAdapter:
- By comparision
FragmentPagerAdapter
does nothing of the kind.When the fragment is no longer needed.FragmentPagerAdapter
callsdetach(Fragment)
on the transaction instead ofremove(Fragment)
. - This destroy's the fragment's view but leaves the fragment's instance alive in the
FragmentManager
.so the fragments created in theFragmentPagerAdapter
are never destroyed.
No comments:
Post a Comment