Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 47c41565 authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Back-port new fragment detach APIs from support lib.

This allow a much cleaner implementation of things like the
fragment pager class.

Change-Id: I433be3d6c24c9a059d908d442bcfddbf1e8e57e5
parent b0dfade9
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -2923,8 +2923,10 @@ package android.app {
    method public abstract android.app.FragmentTransaction add(int, android.app.Fragment);
    method public abstract android.app.FragmentTransaction add(int, android.app.Fragment, java.lang.String);
    method public abstract android.app.FragmentTransaction addToBackStack(java.lang.String);
    method public abstract android.app.FragmentTransaction attach(android.app.Fragment);
    method public abstract int commit();
    method public abstract int commitAllowingStateLoss();
    method public abstract android.app.FragmentTransaction detach(android.app.Fragment);
    method public abstract android.app.FragmentTransaction disallowAddToBackStack();
    method public abstract android.app.FragmentTransaction hide(android.app.Fragment);
    method public abstract boolean isAddToBackStackAllowed();
+48 −0
Original line number Diff line number Diff line
@@ -173,6 +173,8 @@ final class BackStackRecord extends FragmentTransaction implements
    static final int OP_REMOVE = 3;
    static final int OP_HIDE = 4;
    static final int OP_SHOW = 5;
    static final int OP_DETACH = 6;
    static final int OP_ATTACH = 7;

    static final class Op {
        Op next;
@@ -416,6 +418,32 @@ final class BackStackRecord extends FragmentTransaction implements
        return this;
    }

    public FragmentTransaction detach(Fragment fragment) {
        //if (fragment.mImmediateActivity == null) {
        //    throw new IllegalStateException("Fragment not added: " + fragment);
        //}

        Op op = new Op();
        op.cmd = OP_DETACH;
        op.fragment = fragment;
        addOp(op);

        return this;
    }

    public FragmentTransaction attach(Fragment fragment) {
        //if (fragment.mImmediateActivity == null) {
        //    throw new IllegalStateException("Fragment not added: " + fragment);
        //}

        Op op = new Op();
        op.cmd = OP_ATTACH;
        op.fragment = fragment;
        addOp(op);

        return this;
    }

    public FragmentTransaction setCustomAnimations(int enter, int exit) {
        return setCustomAnimations(enter, exit, 0, 0);
    }
@@ -589,6 +617,16 @@ final class BackStackRecord extends FragmentTransaction implements
                    f.mNextAnim = op.enterAnim;
                    mManager.showFragment(f, mTransition, mTransitionStyle);
                } break;
                case OP_DETACH: {
                    Fragment f = op.fragment;
                    f.mNextAnim = op.exitAnim;
                    mManager.detachFragment(f, mTransition, mTransitionStyle);
                } break;
                case OP_ATTACH: {
                    Fragment f = op.fragment;
                    f.mNextAnim = op.enterAnim;
                    mManager.attachFragment(f, mTransition, mTransitionStyle);
                } break;
                default: {
                    throw new IllegalArgumentException("Unknown cmd: " + op.cmd);
                }
@@ -655,6 +693,16 @@ final class BackStackRecord extends FragmentTransaction implements
                    mManager.hideFragment(f,
                            FragmentManagerImpl.reverseTransit(mTransition), mTransitionStyle);
                } break;
                case OP_DETACH: {
                    Fragment f = op.fragment;
                    mManager.attachFragment(f,
                            FragmentManagerImpl.reverseTransit(mTransition), mTransitionStyle);
                } break;
                case OP_ATTACH: {
                    Fragment f = op.fragment;
                    mManager.detachFragment(f,
                            FragmentManagerImpl.reverseTransit(mTransition), mTransitionStyle);
                } break;
                default: {
                    throw new IllegalArgumentException("Unknown cmd: " + op.cmd);
                }
+16 −3
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ final class FragmentState implements Parcelable {
    final int mContainerId;
    final String mTag;
    final boolean mRetainInstance;
    final boolean mDetached;
    final Bundle mArguments;
    
    Bundle mSavedFragmentState;
@@ -66,6 +67,7 @@ final class FragmentState implements Parcelable {
        mContainerId = frag.mContainerId;
        mTag = frag.mTag;
        mRetainInstance = frag.mRetainInstance;
        mDetached = frag.mDetached;
        mArguments = frag.mArguments;
    }
    
@@ -77,6 +79,7 @@ final class FragmentState implements Parcelable {
        mContainerId = in.readInt();
        mTag = in.readString();
        mRetainInstance = in.readInt() != 0;
        mDetached = in.readInt() != 0;
        mArguments = in.readBundle();
        mSavedFragmentState = in.readBundle();
    }
@@ -103,6 +106,7 @@ final class FragmentState implements Parcelable {
        mInstance.mContainerId = mContainerId;
        mInstance.mTag = mTag;
        mInstance.mRetainInstance = mRetainInstance;
        mInstance.mDetached = mDetached;
        mInstance.mFragmentManager = activity.mFragments;
        
        return mInstance;
@@ -120,6 +124,7 @@ final class FragmentState implements Parcelable {
        dest.writeInt(mContainerId);
        dest.writeString(mTag);
        dest.writeInt(mRetainInstance ? 1 : 0);
        dest.writeInt(mDetached ? 1 : 0);
        dest.writeBundle(mArguments);
        dest.writeBundle(mSavedFragmentState);
    }
@@ -404,6 +409,9 @@ public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener
    // from the user.
    boolean mHidden;
    
    // Set to true when the app has requested that this fragment be detached.
    boolean mDetached;

    // If set this fragment would like its instance retained across
    // configuration changes.
    boolean mRetainInstance;
@@ -511,23 +519,27 @@ public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener
        }
    }
    
    void restoreViewState() {
    final void restoreViewState() {
        if (mSavedViewState != null) {
            mView.restoreHierarchyState(mSavedViewState);
            mSavedViewState = null;
        }
    }
    
    void setIndex(int index) {
    final void setIndex(int index) {
        mIndex = index;
        mWho = "android:fragment:" + mIndex;
   }
    
    void clearIndex() {
    final void clearIndex() {
        mIndex = -1;
        mWho = null;
    }
    
    final boolean isInBackStack() {
        return mBackStackNesting > 0;
    }

    /**
     * Subclasses can not override equals().
     */
@@ -1280,6 +1292,7 @@ public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener
                writer.print(" mFromLayout="); writer.print(mFromLayout);
                writer.print(" mInLayout="); writer.println(mInLayout);
        writer.print(prefix); writer.print("mHidden="); writer.print(mHidden);
                writer.print(" mDetached="); writer.print(mDetached);
                writer.print(" mRetainInstance="); writer.print(mRetainInstance);
                writer.print(" mRetaining="); writer.print(mRetaining);
                writer.print(" mHasMenu="); writer.println(mHasMenu);
+57 −20
Original line number Diff line number Diff line
@@ -971,9 +971,10 @@ final class FragmentManagerImpl extends FragmentManager {
        if (mAdded == null) {
            mAdded = new ArrayList<Fragment>();
        }
        mAdded.add(fragment);
        makeActive(fragment);
        if (DEBUG) Log.v(TAG, "add: " + fragment);
        makeActive(fragment);
        if (!fragment.mDetached) {
            mAdded.add(fragment);
            fragment.mAdded = true;
            fragment.mRemoving = false;
            if (fragment.mHasMenu) {
@@ -983,11 +984,13 @@ final class FragmentManagerImpl extends FragmentManager {
                moveToState(fragment);
            }
        }
    }
    
    public void removeFragment(Fragment fragment, int transition, int transitionStyle) {
        if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
        final boolean inactive = !fragment.isInBackStack();
        if (!fragment.mDetached || inactive) {
            mAdded.remove(fragment);
        final boolean inactive = fragment.mBackStackNesting <= 0;
            if (fragment.mHasMenu) {
                mNeedMenuInvalidate = true;
            }
@@ -999,6 +1002,7 @@ final class FragmentManagerImpl extends FragmentManager {
                makeInactive(fragment);
            }
        }
    }
    
    public void hideFragment(Fragment fragment, int transition, int transitionStyle) {
        if (DEBUG) Log.v(TAG, "hide: " + fragment);
@@ -1052,6 +1056,39 @@ final class FragmentManagerImpl extends FragmentManager {
        }
    }
    
    public void detachFragment(Fragment fragment, int transition, int transitionStyle) {
        if (DEBUG) Log.v(TAG, "detach: " + fragment);
        if (!fragment.mDetached) {
            fragment.mDetached = true;
            if (fragment.mAdded) {
                // We are not already in back stack, so need to remove the fragment.
                mAdded.remove(fragment);
                if (fragment.mHasMenu) {
                    mNeedMenuInvalidate = true;
                }
                fragment.mAdded = false;
                fragment.mRemoving = true;
                moveToState(fragment, Fragment.CREATED, transition, transitionStyle);
            }
        }
    }

    public void attachFragment(Fragment fragment, int transition, int transitionStyle) {
        if (DEBUG) Log.v(TAG, "attach: " + fragment);
        if (fragment.mDetached) {
            fragment.mDetached = false;
            if (!fragment.mAdded) {
                mAdded.add(fragment);
                fragment.mAdded = true;
                fragment.mRemoving = false;
                if (fragment.mHasMenu) {
                    mNeedMenuInvalidate = true;
                }
                moveToState(fragment, mCurState, transition, transitionStyle);
            }
        }
    }

    public Fragment findFragmentById(int id) {
        if (mActive != null) {
            // First look through added fragments.
+25 −0
Original line number Diff line number Diff line
@@ -86,6 +86,31 @@ public abstract class FragmentTransaction {
     */
    public abstract FragmentTransaction show(Fragment fragment);

    /**
     * Detach the given fragment from the UI.  This is the same state as
     * when it is put on the back stack: the fragment is removed from
     * the UI, however its state is still being actively managed by the
     * fragment manager.  When going into this state its view hierarchy
     * is destroyed.
     *
     * @param fragment The fragment to be detached.
     *
     * @return Returns the same FragmentTransaction instance.
     */
    public abstract FragmentTransaction detach(Fragment fragment);

    /**
     * Re-attach a fragment after it had previously been deatched from
     * the UI with {@link #detach(Fragment)}.  This
     * causes its view hierarchy to be re-created, attached to the UI,
     * and displayed.
     *
     * @param fragment The fragment to be attached.
     *
     * @return Returns the same FragmentTransaction instance.
     */
    public abstract FragmentTransaction attach(Fragment fragment);

    /**
     * @return <code>true</code> if this transaction contains no operations,
     * <code>false</code> otherwise.