3

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?

Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51

3 Answers3

6

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?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Good answer! I just didn't understand what is the impact on garbage collection if I use `FragmentStatePagerAdapter `, since it doesn't keep the entire fragment in memory. `FragmentPagerAdapter ` would have more impact on GC, would it? – Lennon Spirlandelli Aug 01 '17 at 18:16
  • 1
    @LennonPetrick: `FragmentPagerAdapter` has more impact on *memory*. `FragmentStatePagerAdapter` has more impact on *GC*. Suppose that we have 10 pages. The user, being bored, swipes from the first page through all pages to the last, then swipes back to the beginning. `FragmentPagerAdapter` will create and hold onto 10 fragments, and so increase memory pressure. `FragmentStatePagerAdapter`, though, will create and destroy ~14 fragments along the way, forcing extra work for inflating layouts and such, and dumping a lot of garbage onto the GC's plate. – CommonsWare Aug 01 '17 at 18:19
  • Got it! As `FragmentStatePagerAdapter ` keeps destroying and creating all, every time it destroys a fragment, all the garbage of it will be dumped directly to GC. Great explanation!! Thank you very much!! – Lennon Spirlandelli Aug 01 '17 at 18:33
1

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

Jeelan
  • 173
  • 1
  • 11
  • You didn't answer my question. Note that I said I know the difference between them. However, my question was if `FragmentPagerAdapter ` consumes more memory, why should I use it? Since `FragmentPagerAdapter ` does the same as `FragmentStatePagerAdapter ` – Lennon Spirlandelli Aug 01 '17 at 18:09
1

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.

ali ozkara
  • 5,425
  • 2
  • 27
  • 24
  • and I'm also facing this problem with `FragmentStatePagerAdapter` because I'm using `getChildFragmentManager()` and the problem is my view is not clicking. When I scroll it then click. – M DEV May 28 '22 at 11:45