In activity I have ViewPager.
I don't know how many fragments will be added to viewPager.
The question is:
What I need to use in this case: FragmentPagerAdapter or FragmentStatePagerAdapter?
Surely go with FragmentStatePagerAdapter.
FragmentPagerAdapter loads all fragments at once and will consume more memory. If you have a lot of fragments, loading all of them at once even may lead to out of memory error.
Even you have known number of fragments, FragmentStatePagerAdapter is recommended in most of the cases.
Related
I've been searching for it and all I found was the difference between them. And that's not the point.
If you use FragmentStatePagerAdapter in a ViewPager, you'll end up doing the same as you'd do with FragmentPagerAdapter, but consuming much less memory. If that's true, why should I use the FragmentPagerAdapter?
What is the advantages of using FragmentPagerAdapter?
What is the advantages of using FragmentPagerAdapter?
Speed, particularly when you have an intermediate number of pages: enough to easily hold in memory but beyond the handful that ViewPager wants to hold onto itself. With FragmentStatePagerAdapter, as the user navigates the pager, the adapter destroys some fragments and creates new ones. That takes time, both in terms of the direct Java code and in terms of the impact upon garbage collection. If you do not need that in some circumstance, why pay the price?
FragmentStatePagerAdapter:
If your page contains more fragments better to use FragmentStatePagerAdapter because it will save only state
of the fragment.
FragmentPagerAdapter:
Where as FragmentPagerAdapter will keep each fragment in memory as a result it will consume more moemory.
For example if you have around 3 fragments[in Viewpager] which contains less images/bitmaps better to go with FragmentPagerAdapter
For optimisation better to define mViewPager.setOffscreenPageLimit(2);
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state
if your structure depend on nested fragment and if you need to use childFragmentManager on inner fragment you might have a stack problem with FragmentStatePagerAdapter. when i got this problem i've changed FragmentStatePagerAdapter with FragmentPagerAdapter and it worked well.
What is the difference between FragmentPagerAdapter with ViewPager with OffScreenLimit set to 1 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.
I think if I set the offscreenlimit to 1, it would destroy all fragments outside of the two next of the current fragment, and it behaves similar to a FragmentStatePagerAdapter. Is this correct?
The difference is exactly as it's written in docs. But it can be a little confusing.
FragmentPagerAdapter holds its fragments in the FragmentManager in detached state while they are not visible (while they are over the offscreen limit bounds) and FragmentStatePagerAdapter removes them from the FragmentManager.
The offscreenPageLimit is something little different. All fragments within it stays attached to the UI. Once they go over, they are either removed, or detached.
You can see it in sources of FragmentPagerAdapter (line 121) and FragmentStatePagerAdapter (line 144)
I am using a FragmentStatePagerAdapter as using a ViewPager with a FragmentPagerAdapter (which has many pages) was causing memory issues.
This works well, however I'd like to be able to restore pages (ListFragments) to their original position when the Fragment is recreated. This seems straightforward but I'm not having much luck achieving it.
Does anyone have any tips?
Thanks
How many pager fragments does Android save using FragmentPagerAdapter? It seems to me that it saves eight fragments on the left, because they don't load correctly, so it seems that they aren't rebuilding.
Can I change how many fragments to keep?
FragmentPagerAdapter doesn't actually do any removal of Fragments, it simply detaches the Fragment (which means it's state is kept, but it's Views are destroyed).
If you want the Fragments to be fully removed, then use FragmentStatePagerAdapter instead.
I read the documentation for android pageadapter and fragmentpageadapter and I didn't see any difference. I mean one is a fragment and one isn't but.. is that all? I don't have really much experience with fragments so maybe thats why I don't notice any difference.
So whats the difference if I use a FragmentPagerAdapter or a PagerAdapter??
The difference is that you can use Fragments inside a FragmentPageAdapter. If you want to have fragments that are going to be used in other parts of your code this is your Adapter.
Otherwise if you only want to have code that isn't going to be reused, you can use PagerAdapter.
Implementation of PagerAdapter that uses a Fragment to manage each page. But I highly recommend to use FragmentStatePagerAdapter class also handles saving and restoring of fragment's state.
FragmentStatePagerAdapter 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.
When using FragmentPagerAdapter the host ViewPager must have a valid ID set.
Subclasses only need to implement getItem(int) and getCount() to have a working adapter.
Here is an example implementation of a FragmentStatePagerAdapter pager containing fragments of lists