FileDocCategorySizeDatePackage
FragmentStatePagerAdapter.javaAPI DocAndroid 5.1 API8127Thu Mar 12 22:22:56 GMT 2015android.support.v4.app

FragmentStatePagerAdapter

public abstract class FragmentStatePagerAdapter extends android.support.v4.view.PagerAdapter
Implementation of {@link android.support.v4.view.PagerAdapter} 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/Support13Demos/src/com/example/android/supportv13/app/FragmentStatePagerSupport.java complete}

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

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

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private final FragmentManager
mFragmentManager
private FragmentTransaction
mCurTransaction
private ArrayList
mSavedState
private ArrayList
mFragments
private Fragment
mCurrentPrimaryItem
Constructors Summary
public FragmentStatePagerAdapter(FragmentManager 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.add(null);
        }
        mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
        mFragments.set(position, null);

        mCurTransaction.remove(fragment);
    
public voidfinishUpdate(android.view.ViewGroup container)

        if (mCurTransaction != null) {
            mCurTransaction.commitAllowingStateLoss();
            mCurTransaction = null;
            mFragmentManager.executePendingTransactions();
        }
    
public abstract FragmentgetItem(int 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) {
                fragment.setInitialSavedState(fss);
            }
        }
        while (mFragments.size() <= position) {
            mFragments.add(null);
        }
        fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(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;
            bundle.setClassLoader(loader);
            Parcelable[] fss = bundle.getParcelableArray("states");
            mSavedState.clear();
            mFragments.clear();
            if (fss != null) {
                for (int i=0; i<fss.length; i++) {
                    mSavedState.add((Fragment.SavedState)fss[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) {
                            mFragments.add(null);
                        }
                        f.setMenuVisibility(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()];
            mSavedState.toArray(fss);
            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) {
                mCurrentPrimaryItem.setMenuVisibility(false);
                mCurrentPrimaryItem.setUserVisibleHint(false);
            }
            if (fragment != null) {
                fragment.setMenuVisibility(true);
                fragment.setUserVisibleHint(true);
            }
            mCurrentPrimaryItem = fragment;
        }
    
public voidstartUpdate(android.view.ViewGroup container)