FragmentStatePagerAdapter.javaAPI DocAndroid 5.1 API8393Thu Mar 12 22:22:56 GMT


public abstract class FragmentStatePagerAdapter extends
Implementation of {@link} that uses a {@link Fragment} to manage each page. This class also handles saving and restoring of fragment's state.

This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to {@link FragmentPagerAdapter} at the cost of potentially more overhead when switching between pages.

When using FragmentPagerAdapter the host ViewPager must have a valid ID set.

Subclasses only need to implement {@link #getItem(int)} and {@link #getCount()} to have a working adapter.

Here is an example implementation of a pager containing fragments of lists: {@sample development/samples/Support4Demos/src/com/example/android/supportv4/app/ complete}

The R.layout.fragment_pager resource of the top-level fragment is: {@sample development/samples/Support4Demos/res/layout/fragment_pager.xml complete}

The R.layout.fragment_pager_list resource containing each individual fragment's layout is: {@sample development/samples/Support4Demos/res/layout/fragment_pager_list.xml complete}

Fields Summary
private static final String
private static final boolean
private final
private ArrayList
private ArrayList
Constructors Summary
public FragmentStatePagerAdapter( fm)

        mFragmentManager = fm;
Methods Summary
public voiddestroyItem(android.view.ViewGroup container, int position, java.lang.Object object)

        Fragment fragment = (Fragment)object;

        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object
                + " v=" + ((Fragment)object).getView());
        while (mSavedState.size() <= position) {
        mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
        mFragments.set(position, null);

public voidfinishUpdate(android.view.ViewGroup container)

        if (mCurTransaction != null) {
            mCurTransaction = null;
public abstract position)
Return the Fragment associated with a specified position.

public java.lang.ObjectinstantiateItem(android.view.ViewGroup container, int position)

        // If we already have this item instantiated, there is nothing
        // to do.  This can happen when we are restoring the entire pager
        // from its saved state, where the fragment manager has already
        // taken care of restoring the fragments we previously had instantiated.
        if (mFragments.size() > position) {
            Fragment f = mFragments.get(position);
            if (f != null) {
                return f;

        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();

        Fragment fragment = getItem(position);
        if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
        if (mSavedState.size() > position) {
            Fragment.SavedState fss = mSavedState.get(position);
            if (fss != null) {
        while (mFragments.size() <= position) {
        FragmentCompat.setMenuVisibility(fragment, false);
        FragmentCompat.setUserVisibleHint(fragment, false);
        mFragments.set(position, fragment);
        mCurTransaction.add(container.getId(), fragment);

        return fragment;
public booleanisViewFromObject(android.view.View view, java.lang.Object object)

        return ((Fragment)object).getView() == view;
public voidrestoreState(android.os.Parcelable state, java.lang.ClassLoader loader)

        if (state != null) {
            Bundle bundle = (Bundle)state;
            Parcelable[] fss = bundle.getParcelableArray("states");
            if (fss != null) {
                for (int i=0; i<fss.length; i++) {
            Iterable<String> keys = bundle.keySet();
            for (String key: keys) {
                if (key.startsWith("f")) {
                    int index = Integer.parseInt(key.substring(1));
                    Fragment f = mFragmentManager.getFragment(bundle, key);
                    if (f != null) {
                        while (mFragments.size() <= index) {
                        FragmentCompat.setMenuVisibility(f, false);
                        mFragments.set(index, f);
                    } else {
                        Log.w(TAG, "Bad fragment at key " + key);
public android.os.ParcelablesaveState()

        Bundle state = null;
        if (mSavedState.size() > 0) {
            state = new Bundle();
            Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
            state.putParcelableArray("states", fss);
        for (int i=0; i<mFragments.size(); i++) {
            Fragment f = mFragments.get(i);
            if (f != null) {
                if (state == null) {
                    state = new Bundle();
                String key = "f" + i;
                mFragmentManager.putFragment(state, key, f);
        return state;
public voidsetPrimaryItem(android.view.ViewGroup container, int position, java.lang.Object object)

        Fragment fragment = (Fragment)object;
        if (fragment != mCurrentPrimaryItem) {
            if (mCurrentPrimaryItem != null) {
                FragmentCompat.setMenuVisibility(mCurrentPrimaryItem, false);
                FragmentCompat.setUserVisibleHint(mCurrentPrimaryItem, false);
            if (fragment != null) {
                FragmentCompat.setMenuVisibility(fragment, true);
                FragmentCompat.setUserVisibleHint(fragment, true);
            mCurrentPrimaryItem = fragment;
public voidstartUpdate(android.view.ViewGroup container)