What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

后端 未结 7 2044
傲寒
傲寒 2020-11-22 12:31

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

About FragmentPagerAdapter Google\'s guide

相关标签:
7条回答
  • 2020-11-22 13:02

    Here is a log lifecycle of each fragment in ViewPager which have 4 fragment and offscreenPageLimit = 1 (default value)

    FragmentStatePagerAdapter

    Go to Fragment1 (launch activity)

    Fragment1: onCreateView
    Fragment1: onStart
    Fragment2: onCreateView
    Fragment2: onStart
    

    Go to Fragment2

    Fragment3: onCreateView
    Fragment3: onStart
    

    Go to Fragment3

    Fragment1: onStop
    Fragment1: onDestroyView
    Fragment1: onDestroy
    Fragment1: onDetach
    Fragment4: onCreateView
    Fragment4: onStart
    

    Go to Fragment4

    Fragment2: onStop
    Fragment2: onDestroyView
    Fragment2: onDestroy
    

    FragmentPagerAdapter

    Go to Fragment1 (launch activity)

    Fragment1: onCreateView
    Fragment1: onStart
    Fragment2: onCreateView
    Fragment2: onStart
    

    Go to Fragment2

    Fragment3: onCreateView
    Fragment3: onStart
    

    Go to Fragment3

    Fragment1: onStop
    Fragment1: onDestroyView
    Fragment4: onCreateView
    Fragment4: onStart
    

    Go to Fragment4

    Fragment2: onStop
    Fragment2: onDestroyView
    

    Conclusion: FragmentStatePagerAdapter call onDestroy when the Fragment is overcome offscreenPageLimit while FragmentPagerAdapter not.

    Note: I think we should use FragmentStatePagerAdapter for a ViewPager which have a lot of page because it will good for performance.

    Example of offscreenPageLimit:

    If we go to Fragment3, it will detroy Fragment1 (or Fragment5 if have) because offscreenPageLimit = 1. If we set offscreenPageLimit > 1 it will not destroy.
    If in this example, we set offscreenPageLimit=4, there is no different between using FragmentStatePagerAdapter or FragmentPagerAdapter because Fragment never call onDestroyView and onDestroy when we change tab

    Github demo here

    0 讨论(0)
  • 2020-11-22 13:03

    FragmentPagerAdapter stores the previous data which is fetched from the adapter while FragmentStatePagerAdapter takes the new value from the adapter everytime it is executed.

    0 讨论(0)
  • 2020-11-22 13:06

    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.

    0 讨论(0)
  • 2020-11-22 13:11

    FragmentStatePagerAdapter:

    • with FragmentStatePagerAdapter,your unneeded fragment is destroyed.A transaction is committed to completely remove the fragment from your activity's FragmentManager.

    • The state in FragmentStatePagerAdapter comes from the fact that it will save out your fragment's Bundle from savedInstanceState 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 calls detach(Fragment) on the transaction instead of remove(Fragment).

    • This destroy's the fragment's view but leaves the fragment's instance alive in the FragmentManager.so the fragments created in the FragmentPagerAdapter are never destroyed.

    0 讨论(0)
  • 2020-11-22 13:11

    FragmentPagerAdapter: the fragment of each page the user visits will be stored in memory, although the view will be destroyed. So when the page is visible again, the view will be recreated but the fragment instance is not recreated. This can result in a significant amount of memory being used. FragmentPagerAdapter should be used when we need to store the whole fragment in memory. FragmentPagerAdapter calls detach(Fragment) on the transaction instead of remove(Fragment).

    FragmentStatePagerAdapter: the fragment instance is destroyed when it is not visible to the User, except the saved state of the fragment. This results in using only a small amount of Memory and can be useful for handling larger data sets. Should be used when we have to use dynamic fragments, like fragments with widgets, as their data could be stored in the savedInstanceState.Also it won’t affect the performance even if there are large number of fragments.

    0 讨论(0)
  • 2020-11-22 13:14

    Something that is not explicitly said in the documentation or in the answers on this page (even though implied by @Naruto), is that FragmentPagerAdapter will not update the Fragments if the data in the Fragment changes because it keeps the Fragment in memory.

    So even if you have a limited number of Fragments to display, if you want to be able to refresh your fragments (say for example you re-run the query to update the listView in the Fragment), you need to use FragmentStatePagerAdapter.

    My whole point here is that the number of Fragments and whether or not they are similar is not always the key aspect to consider. Whether or not your fragments are dynamic is also key.

    0 讨论(0)
提交回复
热议问题