Android FragmentPagerAdapter vs FragmentStatePagerAdapter - Truiton
Skip to content

Android FragmentPagerAdapter vs FragmentStatePagerAdapter

Android FragmentPagerAdapter vs FragmentStatePagerAdapter

To develop an app in Android with sliding views, a variation of PagerAdapter class along with ViewPager class is used. Recently I wrote two tutorials about PagerAdapter classes, describing Android FragmentPagerAdapter and FragmentStatePagerAdapter, as both of them have benefits of their own. This Android FragmentPagerAdapter vs FragmentStatePagerAdapter is my concluding article on PagerAdapter series. In this article I will compare both PagerAdapters and explain the perfect situation for their use. If you wish to read my complete series on PagerAdapters and ViewPagers have a look at links below:

  1. Android FragmentStatePagerAdapter
  2. Android FragmentPagerAdapter
  3. Android FragmentPagerAdapter vs FragmentStatePagerAdapter

To compare Android FragmentPagerAdapter vs FragmentStatePagerAdapter, first lets understand what Android FragmentPagerAdapter does. FragmentPagerAdapter is used along with ViewPager class, to display sliding fragments. ViewPager class is a sort of layout manager which provides the functionality of flipping data in form of pages, left and right. In simple words ViewPager class provides the functionality to flip pages in an app. Moving forward with this basic info, to understand the Android FragmentPagerAdapter vs FragmentStatePagerAdapter lets try to understand the technical aspect of Android FragmentPagerAdapter.

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. The first one returns the number of fragments to be displayed, and the second one is used to display the actual fragment. To understand the code have a look at my tutorial about Android FragmentPagerAdapter. The purpose for using this type of fragment adapter is to keep the whole fragment in memory. Its view hierarchy may be destroyed but the fragment may still remain in memory. Hence it is advised to use this Android FragmentPagerAdapter only when there are low number of static fragments, since all fragments would be kept in the memory and this would increase the memory heap. Which could result a slow running app.

Further to understand Android FragmentPagerAdapter vs FragmentStatePagerAdapter, lets try to understand the FragmentStatePagerAdapter. This adapter also falls under the PagerAdapter class and is used with ViewPager to display sliding views. It also has same methods getCount and getItem. Which are used to get the fragment count and get the fragment reference respectively, same as in FragmentPagerAdapter. To further understand the code part of it please read my tutorial on Android FragmentStatePagerAdapter. The main functionality for which FragmentStatePagerAdapter is used, to accommodate a large number of fragments in ViewPager. As this adapter destroys the fragment when it is not visible to the user and only savedInstanceState of the fragment is kept for further use. This way a low amount of memory is used and a better performance is delivered in case of dynamic fragments.

Android FragmentPagerAdapter vs FragmentStatePagerAdapter

Now that I have explained what FragmentStatePagerAdapter and FragmentPagerAdapter does, lets finally discuss Android FragmentPagerAdapter vs FragmentStatePagerAdapter. As I explained FragmentPagerAdapter stores the whole fragment in memory, and could increase a memory overhead if a large amount of fragments are used in ViewPager. In contrary its sibling, FragmentStatePagerAdapter only stores the savedInstanceState of fragments, and destroys all the fragments when they lose focus. Therefore FragmentStatePagerAdapter should be used when we have to use dynamic fragments, like fragments with widgets, as their data could be stored in the savedInstanceState. Also it wont affect the performance even if there are large number of fragments. In contrary its sibling FragmentPagerAdapter should be used when we need to store the whole fragment in memory. When I say the whole fragment is kept in memory it means, its instances wont be destroyed and would create a memory overhead. Therefore it is advised to use FragmentPagerAdapter only when there are low number of fragments for ViewPager. It would be even better if the fragments are static, since they would not be having large amount of objects whose instances would be stored. Hope this clears out the difference between Android FragmentPagerAdapter and FragmentStatePagerAdapter. If you like this article please share it with your friends on Google+ and Facebook, also like our page on Facebook for latest updates.

7 thoughts on “Android FragmentPagerAdapter vs FragmentStatePagerAdapter”

  1. Hi Mohit,
    You blog has been a great help for beginners like me. Thanks for this blog and keep this great work of yours up and running. I am stuck with a problem and hoping you can help me to resolve this:

    I have created a SherlockFragmentActivity that has three fragments for ViewPager. But now I want to use this Activity in NavigationDrawer but I am confused how to do this. Google+ app has this kind of implementation but I am wondering about how to achieve this.

    NavigationDrawer will have following UI elements:
    1. FragmentActivity(contains three fragment as ViewPager)
    2. Second Fragment
    3. Third Fragment

    Is this kind of layout possible with Navigation Drawer If yes, how should I do it. If not, what should I do to achieve this kind of navigation in my app.

    1. Hi Niranjan,

      Thanks for the praise. But I am afraid cant help you on this one. I’ve not worked on NavigationDrawer yet. Will update you if I write a tutorial about it.

  2. Hi Mohit,

    Your Blog was really helpfull. I am using view pager where i show three pages details friends and albums. Earlier when the fragment activity was using Fragmentpager adapter the view pager was not loaded more than once. It was showing empty contents. But after reading you blog i changed it to FragmentstatePagerAdapter. Its working fine. but when i come to same activity from some other activity using back button . nothing is displayed. again three pages are displayed but not contents.

    can u suggest something.

    Thanks

  3. Hey Mohit,

    First of all, thank you very much for this simple but really good comparison.
    I’ve just one question that i think it was not covered. Even if a had few fragments (eg.: 3), if all data of its content came from a webservice call, will be FragmentStatePagerAdapter still recommended ?

    Thank in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *