Android FragmentPagerAdapter Example

Go By Streetcar Part III

How cool is it to have an app which has swipe views, it makes app interactive and fun to use, right? So being a mobile app consultant at Truiton I was working on an app where segmented forms were to be filled by user. I thought why not use swipe tabs and sliding views. I searched and found this can be implemented by Android ViewPager class, which is used in conjunction with Android FragmentPagerAdapter class or Android FragmentStatePagerAdapter class. Since any sort of software development requires precision and accuracy in work my purpose was solved by the later one. Still I decided to write a tutorial about Android FragmentPagerAdapter Example.

There are two ways to implement Android ViewPager class, but this tutorial only focuses on Android FragmentPagerAdapter with an example. Now since both adapters have different ways of working, you might want to read about the other one too, have a look at my series on PagerAdapters :

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

In my previous tutorial I mentioned a case, if we wish to keep the fragment in the memory. Here steps in Android FragmentPagerAdapter, the main advantage of using Android FragmentPagerAdapter is that, it keeps the fragment in memory. This definitely increases the memory usage, therefore it is advised to use Android FragmentPagerAdapter only when there are less number of fragments. If you app has non changing fragments like, fragments with images or static text it might work even better. But using Android FragmentPagerAdapter for large number of fragments would result choppy and laggy UX.

Android FragmentPagerAdapter Example

Lets start with the main activity, this activity is where the Android FragmentPagerAdapter class is declared. Have a look at

In the above class one can see that MainActivity extends FragmentActivity and in this class, both MyAdapter and ViewPager class are instantiated. If you look closely you would find that MyAdapter class extends Android FragmentPagerAdapter class. Lets have a look at the extended Android FragmentPagerAdapter class, here exists a method by the name of getItem. This getItem method is the one which calls all the fragments which are to be displayed according to the position. is displayed by layout fragment_pager.xml.

Above is the main layout where all fragments would be displayed. Have a look at tag, since this tag is used here, this app would run on android devices lower than API version 11 too. Next lets have a look at

In the above class a static method by the name of init is declared. This init method is called in the extended Android FragmentPagerAdapter class with position of the fragment as argument. This argument is later on used in onCreate and onCreateView method of this ImageFragment. The layout file for this class is fragment_image.xml.

Now have a look at the second type of fragment used in Android FragmentPagerAdapter Example,

In the above class, the only difference is, that ListFragment is used, rest is same as in ImageFragment. It has a static init method which is called from extended Android FragmentPagerAdapter. It uses layout fragment_pager_list.xml :

After defining all the classes and layouts your app should look like screenshots below

In this tutorial I defined a class which extended FragmentActivity, and instantiated extended version of Android FragmentPagerAdapter and ViewPager. This whole Android FragmentPagerAdapter example is made with support library, hence this would run on all android devices. Again to sum up, I would say that use Android FragmentPagerAdapter only when you have low amount of fragments that you want to keep in memory. Otherwise it will create a memory overhead and affect your app’s performance. This would work even better with non changing fragments. If you still have concerns on which PagerAdapter to use, have a look at Android FragmentPagerAdapter vs FragmentStatePagerAdapter. Hope this helped you out, if it did please like and share this with your friends on Google+ and Facebook. Also like our Facebook page for updates.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

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

19 thoughts on “Android FragmentPagerAdapter Example

  • anuj

    Hi Mohit Sir! Thanks a lot for this tut…

    In above example suppose if we want to open 1st fragment from List-Fragment and also want to show which item was clicked… So what can we do in this case?
    Can you please help me? I am new to android developement.

    • Mohit Gupt Post author

      Hi Anuj,
      You can do this by adding a static method in MainActivity, which would be called from the ListFragment’s onListItemClick method:

      static void callFragment(int num){

  • Matt Sellner

    Hi Mohit,

    Thanks for the tutorial!

    How would I go about changing the list data from the main activity? I can pass the initial array through the init (return TruitonListFragment.init(position,som_array);) but when the dataset changes in the MainActivity how do I notify the listfragment to update?



  • sandeep

    Nice tutorial.. great post
    Mohit, I have a tabhost which hosts two activities. How can i recreate the activity which is under tabhost. Please give me solution for this. Sorry for asking this here.Thanks in advance..

  • srinivas

    Hi Mohit,

    This is very helpfull for me.

    I am facing a problem with FragmentStatePagerAdapter, in my App i have 4 Fragments,If i left my App idle for some 20 minutes and above, Currently showing Fragment state is losing.i.e UI is lost, showing only empty screen..Can you please help me to solve this.


  • Naveen

    Nice tutorial.. great post,

    I want check a flag true or false on the basis of that I want make Pager swipe enable or disable.

    Please help me how to do that.

  • Ameya

    I’ve created a tablayout with Viewpager.The Tablayout has three 3 fragments ‘Photo’,’Video’,’Docs’. Each fragment has a gridview. In ‘Photo’ fragment images are loaded to the gridview as thumbnails, In ‘Video’ fragment videos are loaded to the gridview as thumbnails and so on. When an image is loaded to the Gridview in ‘Photo’ fragment and after that when the application is killed and opened again, the Gridview has lost its state that is it doesn’t have the uploaded images. I know this is happening because the onCreateView is creating the entire UI again. But how to save the State of the Fragment before it gets killed ?

    Can you please make an example demonstrating how to save the state of the fragments which have gridview !

    Thank you in advance.

  • Pedro

    Hey, I am implementing your solution for FragmentPagerAdapter (kind of), but with some modification.

    The main modification is that images are loaded into each “slide” by clicking the corresponding ImageView, up to 6 images

    My problem: everything works fine, images are loaded by clicking on the ImageView and displayed correctly, however, let’s say I click and load image #0, them move forward to image #3, now if I go back to image #0 it is gone….any clue or suggestion about it??.


  • Hina Arora

    How can we maintain the previous fragment state?
    The problem I am facing is on back pressed from any fragment , it will lead to exit an app.