Android FragmentStatePagerAdapter Example


Creating memories - Android FragmentStatePagerAdapter

Android development is full of surprises, you can never predict what’s coming your way. In my latest assignment at Truiton I had to develop an app in which I user can fill segmented data in an interactive way. The best approach I could think of was screen slides. Now to solve this, I used Android FragmentStatePagerAdapter and ViewPager class. And so I thought of writing this Android FragmentStatePagerAdapter Example tutorial. This way each screen can act as a segment and data can be filled in all segments by sliding from one screen to another.

Although there are two ways to implement Android ViewPager class, but in this tutorial I am only going to discuss Android FragmentStatePagerAdapter class with an example. Since the main purpose/advantage of Android FragmentStatePagerAdapter class is to it consume less memory as compared to its counterpart, I used this to solve my problem. Android FragmentStatePagerAdapter class consumes less memory, because it destroys fragments, as soon as they are not visible to user, keeping only saved state of that fragment. And savedInstanceState is all, what I needed in my app to restore the filled out data by user.

But there may exist a situation where you would like to store the entire fragment in memory, if that’s the case have look at my complete series on PagerAdapters used to implement ViewPager class:

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

Now since the Fragments were introduced in android API 11, earlier versions of android are not capable of running fragments and swipes, unless a support library is used in app. Therefore I built this app with Android support library v4, package name android.support.v4. As a result all versions will run following Android FragmentStatePagerAdapter Example.

Android FragmentStatePagerAdapter Example

To get started, first thing to do is, to create a layout for ViewPager and Android FragmentStatePagerAdapter class. Now while creating a layout please keep in mind to use android.support.v4.view.ViewPager as we would like our app to be compatible with older versions of android. Have a look at fragment_pager.xml.

The layout above is the main layout where all the fragments would be inflated and displayed. To display sliding views, now we need to declare a main class where Android FragmentStatePagerAdapter class would be declared. The class defined below is MainActivity.java.

In the class above I defined my own FragmentStatePagerAdapter by the name of MyAdapter class. In this adapter the main method is getItem. The getItem method in FragmentStatePagerAdapter calls each fragment when user swipes though them. In this MyAdapter class we are calling two Fragments, ImageFragment and ArrayListFragment. lets have a look at ImageFragment class first.

Here in the class above I have defined a static method by the name of init. This method takes the position as argument val from inherited/extended Android FragmentStatePagerAdapter class and further its used in onCreate and onCreateView method of this Fragment accordingly. ImageFragment uses fragment_image layout, so lets have a look at fragment_image.xml.

Next lets have a look at the ArrayListFragment.java.

The functioning of this ListFragment is also same as of ImageFragment, it has an init method which takes argument from FragmentStatePagerAdapter, the only difference is that it displays a list. Have a look at its layout fragment_pager_list.xml.

As of now we have our Android FragmentStatePagerAdapter and ViewPager setup and working, so your project should look something like this.

With this I can conclude this tutorial on Android FragmentStatePagerAdapter and ViewPager. In this tutorial I created an app which had the functionality to display 10 sliding fragments, or swipe views. Also I defined two types of fragments, an ImageFragment and ListFragment, which were used according to the required position in FragmentStatePagerAdapter.

As I mentioned earlier Android FragmentStatePagerAdapter destroys fragments as soon as, they are not visible to user. If you are developing an app which has less number of fragments and you want to keep them in memory, have look at my tutorial on Android FragmentPagerAdapter. If this helped you 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 *

9 thoughts on “Android FragmentStatePagerAdapter Example

  • giuliocc

    Nice little intro to viewpagers, but you need to correct a couple of things. What are the defined menu options for this method ?

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    And the @drawable/truition to test the app ?

    Overall nice and quick which is what I am looking for at the moment.

    • Mohit Gupt Post author

      This method onCreateOptionsMenu is just a default method which is automatically added, when a new project is created. You can remove it from your code, it wont do any harm.

  • Varun Jindal

    Hi
    Want to load different fragment into view pager on each slide, I am returning different fragment from getItem() method of adapter.

    Problem:
    On loading the activity, getItem is called more than one time So, getItem is returning fragment for some other position, Can you tell me how to handle this situation.

  • Sujal

    On changing the orientation, MainActivity is recreated, thus creating the fragments again.
    How can I reload the fragments that had been loaded before? Getting views from fragments after orientation change is giving null.

    Thank you

  • federico rufrancos

    EXCELENT EXAMPLE!
    I have a bug in my app that it doesn’t let me slide to others fragment when I pass my finger over the listsview.
    Do you have any idea what’s the problem? Or which is the property in your example that if I remove it, I can repruduce my bug in your example.