What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter? What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter? android android

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?


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.


FragmentStatePagerAdapter:

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

  • The state in FragmentStatePagerAdapter comes from the fact that itwill save out your fragment's Bundle from savedInstanceState whenit is destroyed.When the user navigates back,the new fragment will berestored using the fragment's state.

FragmentPagerAdapter:

  • By comparision FragmentPagerAdapter does nothing of the kind.Whenthe fragment is no longer needed.FragmentPagerAdapter callsdetach(Fragment) on the transaction instead of remove(Fragment).

  • This destroy's the fragment's view but leaves the fragment's instancealive in the FragmentManager.so the fragments created in theFragmentPagerAdapter are never destroyed.


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: onCreateViewFragment1: onStartFragment2: onCreateViewFragment2: onStart

Go to Fragment2

Fragment3: onCreateViewFragment3: onStart

Go to Fragment3

Fragment1: onStopFragment1: onDestroyViewFragment1: onDestroyFragment1: onDetachFragment4: onCreateViewFragment4: onStart

Go to Fragment4

Fragment2: onStopFragment2: onDestroyViewFragment2: onDestroy

FragmentPagerAdapter

Go to Fragment1 (launch activity)

Fragment1: onCreateViewFragment1: onStartFragment2: onCreateViewFragment2: onStart

Go to Fragment2

Fragment3: onCreateViewFragment3: onStart

Go to Fragment3

Fragment1: onStopFragment1: onDestroyViewFragment4: onCreateViewFragment4: onStart

Go to Fragment4

Fragment2: onStopFragment2: 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