Lennon Spirlandelli
Lennon Spirlandelli

Reputation: 3193

Why should I use FragmentPagerAdapter instead of FragmentStatePagerAdapter?

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?

Upvotes: 3

Views: 2721

Answers (3)

ali ozkara
ali ozkara

Reputation: 5646

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.

Upvotes: 1

Jeelan
Jeelan

Reputation: 173

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

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007554

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?

Upvotes: 6

Related Questions