What is the difference between FragmentPagerAdapter
and FragmentStatePagerAdapter
?
About FragmentPagerAdapter
Google's guide says:
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
.
And about 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.
So I have just 3 fragments. But all of them are separate modules with a large amount of data.
Fragment1
handles some data (which users enter) and passes it via activity into Fragment2
, which is just a simple ListFragment
. Fragment3
is also a ListFragment
.
So my questions are: Which adapter should I use? FragmentPagerAdapter
or FragmentStatePagerAdapter
?
Android FragmentPagerAdapter is type of PagerAdapter which populates the data inside the ViewPager, which in turn allows the flipping of data in form of pages. Usually to display sliding fragments two methods of FragmentPagerAdapter are used, getCount and getItem.
ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .
You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With