Android Fragment 103

Using ViewPager, FragmentStatePagerAdapter vs FragmentPagerAdapter

ViewPager using Fragments

Let's start by building a ViewPager using Fragments and see what can be best practises to implement it

We have seen something like this. A full-screen content card which can be scrolled left or right to get to the other page.

This is build using ViewPager which most often used in conjunction with Fragment. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These are FragmentPagerAdapter and FragmentStatePagerAdapter; each of these classes has a sample code showing how to build a full user interface with them. And we will also discuss then in great details later on in this article.

So first let’s create a new project and open the default XML file for the Main Activity. Add the ViewPager component in the XML file as shown below

Let’s look into the model class which will hold the data that will be shown in the Adapter for viewpager.

This is an enum class which holds all the pages of the view pager. It holds three pages with their respective layouts. Similarly, I have created the layout of each page, which you will be able to check out in the CodeRepo.

As we need an adapter to display all the pages we shall implement the pager adapter and discuss all the method we have implemented.

CustomPagerAdapter(Context context) : The constructor needs a Context reference. The context is saved as a member variable of the class since it’s used later to access the individual page layouts from the enum class.

instantiateItem : In this case, we use the enum and inflate the particular enum value’s associated layout. Then, we add the newly inflated layout to the ViewGroup(collection of Views) maintained by the PagerAdapter, and then we return that layout. The object being returned by this method is also used later on, as the second parameter in the isViewFromObject method.

destroyItem : This method removes a particular view from the ViewGroup maintained by the PagerAdapter.

getCount : It simply returns the number of views that will be maintained by the ViewPager. For this example, the count is the number of enum values in the model object.

isViewFromObject : This method checks whether a particular object belongs to a given position, which is made simple. As noted earlier, the second parameter is of type Object and is the same as the return value from the instantiateItem method.

getPageTitle : At a given position, we need to supply the PagerAdapter with a title. This usually manifests itself in the ActionBar as the Activity’s title, or sometimes tabs will hook into this method for labelling each tab. In this case, we’ve kept it for labelling only.

Lastly, add out adapter in the main activity. and that’s pretty much everything we need to do.

Time to see the demo….:)

In the above implementation, we used PagerAdapter, but we can use FragmentStatePagerAdapter or FragmentPagerAdapter.

FragmentStatePagerAdapter FragmentPagerAdapter stores the whole fragment in memory and can cause an increase of memory overhead if a large number of fragments are used in ViewPager.

FragmentPagerAdapter — Only stores the savedInstanceState of fragments and destroys all the fragments when they lose focus. Let’s look at some commonly faced issues.

Update ViewPager not working:

Remember ViewPager fragments are managed by FragmentManager, either from fragment or activity, and FragmentManager holds instances of all ViewPager fragments.

So when people say ViewPager is not refreshed, it’s nothing but old instances of fragments are still being held by FragmentManager. You need to find out why FragmentManger is holding an instance of fragments.

Access current Fragment from ViewPager:

This is also a very common issue we come across. If you come across this, either create an array list of fragments inside the adapter or try to access the fragment using some tags. I prefer another option, however. FragmentStateAdapter and FragmentPagerAdapter both provide the method setPrimaryItem.

I am leaving a GitHub link to this simple ViewPager project so that everyone can understand better. FragmentStatePager and FragmentPagerAdater are also being implemented in this.

We keep adding stories to this series. Next, we will discuss FragmentManager: getSupportFragmentManager / getChildFragmentManager,DialogFragment, and BottomSheetFragment.