Example usage for android.app FragmentState FragmentState

List of usage examples for android.app FragmentState FragmentState

Introduction

In this page you can find the example usage for android.app FragmentState FragmentState.

Prototype

FragmentState(Parcel in) 

Source Link

Usage

From source file:android.app.FragmentManager.java

Parcelable saveAllState() {
    // Make sure all pending operations have now been executed to get
    // our state update-to-date.
    execPendingActions();/* w  w w  . java 2 s.  c o  m*/

    mStateSaved = true;

    if (mActive == null || mActive.size() <= 0) {
        return null;
    }

    // First collect all active fragments.
    int N = mActive.size();
    FragmentState[] active = new FragmentState[N];
    boolean haveFragments = false;
    for (int i = 0; i < N; i++) {
        Fragment f = mActive.get(i);
        if (f != null) {
            if (f.mIndex < 0) {
                throwException(new IllegalStateException(
                        "Failure saving state: active " + f + " has cleared index: " + f.mIndex));
            }

            haveFragments = true;

            FragmentState fs = new FragmentState(f);
            active[i] = fs;

            if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
                fs.mSavedFragmentState = saveFragmentBasicState(f);

                if (f.mTarget != null) {
                    if (f.mTarget.mIndex < 0) {
                        throwException(new IllegalStateException("Failure saving state: " + f
                                + " has target not in fragment manager: " + f.mTarget));
                    }
                    if (fs.mSavedFragmentState == null) {
                        fs.mSavedFragmentState = new Bundle();
                    }
                    putFragment(fs.mSavedFragmentState, FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
                    if (f.mTargetRequestCode != 0) {
                        fs.mSavedFragmentState.putInt(FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
                                f.mTargetRequestCode);
                    }
                }

            } else {
                fs.mSavedFragmentState = f.mSavedFragmentState;
            }

            if (DEBUG)
                Log.v(TAG, "Saved state of " + f + ": " + fs.mSavedFragmentState);
        }
    }

    if (!haveFragments) {
        if (DEBUG)
            Log.v(TAG, "saveAllState: no fragments!");
        return null;
    }

    int[] added = null;
    BackStackState[] backStack = null;

    // Build list of currently added fragments.
    if (mAdded != null) {
        N = mAdded.size();
        if (N > 0) {
            added = new int[N];
            for (int i = 0; i < N; i++) {
                added[i] = mAdded.get(i).mIndex;
                if (added[i] < 0) {
                    throwException(new IllegalStateException("Failure saving state: active " + mAdded.get(i)
                            + " has cleared index: " + added[i]));
                }
                if (DEBUG)
                    Log.v(TAG, "saveAllState: adding fragment #" + i + ": " + mAdded.get(i));
            }
        }
    }

    // Now save back stack.
    if (mBackStack != null) {
        N = mBackStack.size();
        if (N > 0) {
            backStack = new BackStackState[N];
            for (int i = 0; i < N; i++) {
                backStack[i] = new BackStackState(this, mBackStack.get(i));
                if (DEBUG)
                    Log.v(TAG, "saveAllState: adding back stack #" + i + ": " + mBackStack.get(i));
            }
        }
    }

    FragmentManagerState fms = new FragmentManagerState();
    fms.mActive = active;
    fms.mAdded = added;
    fms.mBackStack = backStack;
    return fms;
}