To develop an app in Andorid 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:
- Android FragmentStatePagerAdapter
- Android FragmentPagerAdapter
- 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.