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

Commit f121be73 authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Improvements to fragment API:

- Remove names associated with fragments (we'll use ids).  They may
  come back for other uses.
- Add method to replace a fragment, to help in doing back stacks.
- Add names to back stack states, to allow for jumping back to
  known states.
- Add ability to set animations associated with fragments being
  added or removed.

Change-Id: Ic2799f06947b1511fb1df5965ca32568b860b3c3
parent 763e3b67
Loading
Loading
Loading
Loading
+280 −23
Original line number Diff line number Diff line
@@ -19355,6 +19355,19 @@
<parameter name="ev" type="android.view.MotionEvent">
</parameter>
</method>
<method name="findFragmentById"
 return="android.app.Fragment"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="id" type="int">
</parameter>
</method>
<method name="findViewById"
 return="android.view.View"
 abstract="false"
@@ -20494,6 +20507,8 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="name" type="java.lang.String">
</parameter>
</method>
<method name="registerForContextMenu"
 return="void"
@@ -24841,16 +24856,6 @@
 visibility="public"
>
</constructor>
<constructor name="Fragment"
 type="android.app.Fragment"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="name" type="java.lang.String">
</parameter>
</constructor>
<method name="getActivity"
 return="android.app.Activity"
 abstract="false"
@@ -24862,8 +24867,8 @@
 visibility="public"
>
</method>
<method name="getName"
 return="java.lang.String"
<method name="onAttach"
 return="void"
 abstract="false"
 native="false"
 synchronized="false"
@@ -24872,8 +24877,10 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="activity" type="android.app.Activity">
</parameter>
</method>
<method name="onAttach"
<method name="onConfigurationChanged"
 return="void"
 abstract="false"
 native="false"
@@ -24883,10 +24890,10 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="activity" type="android.app.Activity">
<parameter name="newConfig" type="android.content.res.Configuration">
</parameter>
</method>
<method name="onConfigurationChanged"
<method name="onCreate"
 return="void"
 abstract="false"
 native="false"
@@ -24896,11 +24903,11 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="newConfig" type="android.content.res.Configuration">
<parameter name="savedInstanceState" type="android.os.Bundle">
</parameter>
</method>
<method name="onCreate"
 return="void"
<method name="onCreateAnimation"
 return="android.view.animation.Animation"
 abstract="false"
 native="false"
 synchronized="false"
@@ -24909,7 +24916,9 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="savedInstanceState" type="android.os.Bundle">
<parameter name="transit" type="int">
</parameter>
<parameter name="enter" type="boolean">
</parameter>
</method>
<method name="onCreateView"
@@ -25076,8 +25085,6 @@
>
<parameter name="fragment" type="android.app.Fragment">
</parameter>
<parameter name="containerViewId" type="int">
</parameter>
</method>
<method name="add"
 return="android.app.FragmentTransaction"
@@ -25091,8 +25098,6 @@
>
<parameter name="fragment" type="android.app.Fragment">
</parameter>
<parameter name="name" type="java.lang.String">
</parameter>
<parameter name="containerViewId" type="int">
</parameter>
</method>
@@ -25106,6 +25111,8 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="name" type="java.lang.String">
</parameter>
</method>
<method name="commit"
 return="void"
@@ -25131,6 +25138,256 @@
<parameter name="fragment" type="android.app.Fragment">
</parameter>
</method>
<method name="replace"
 return="android.app.FragmentTransaction"
 abstract="true"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="fragment" type="android.app.Fragment">
</parameter>
<parameter name="containerViewId" type="int">
</parameter>
</method>
<method name="setTransition"
 return="android.app.FragmentTransaction"
 abstract="true"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="transit" type="int">
</parameter>
</method>
<method name="setTransitionStyle"
 return="android.app.FragmentTransaction"
 abstract="true"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="styleRes" type="int">
</parameter>
</method>
<field name="TRANSIT_ACTIVITY_CLOSE"
 type="int"
 transient="false"
 volatile="false"
 value="8199"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_ACTIVITY_OPEN"
 type="int"
 transient="false"
 volatile="false"
 value="4102"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_ENTER"
 type="int"
 transient="false"
 volatile="false"
 value="4097"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_ENTER_MASK"
 type="int"
 transient="false"
 volatile="false"
 value="4096"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_EXIT"
 type="int"
 transient="false"
 volatile="false"
 value="8194"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_EXIT_MASK"
 type="int"
 transient="false"
 volatile="false"
 value="8192"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_HIDE"
 type="int"
 transient="false"
 volatile="false"
 value="8196"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_NONE"
 type="int"
 transient="false"
 volatile="false"
 value="0"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_PREVIEW_DONE"
 type="int"
 transient="false"
 volatile="false"
 value="5"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_SHOW"
 type="int"
 transient="false"
 volatile="false"
 value="4099"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_TASK_CLOSE"
 type="int"
 transient="false"
 volatile="false"
 value="8201"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_TASK_OPEN"
 type="int"
 transient="false"
 volatile="false"
 value="4104"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_TASK_TO_BACK"
 type="int"
 transient="false"
 volatile="false"
 value="8203"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_TASK_TO_FRONT"
 type="int"
 transient="false"
 volatile="false"
 value="4106"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_UNSET"
 type="int"
 transient="false"
 volatile="false"
 value="-1"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_WALLPAPER_CLOSE"
 type="int"
 transient="false"
 volatile="false"
 value="8204"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_WALLPAPER_INTRA_CLOSE"
 type="int"
 transient="false"
 volatile="false"
 value="8207"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_WALLPAPER_INTRA_OPEN"
 type="int"
 transient="false"
 volatile="false"
 value="4110"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="TRANSIT_WALLPAPER_OPEN"
 type="int"
 transient="false"
 volatile="false"
 value="4109"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
</interface>
<class name="Instrumentation"
 extends="java.lang.Object"
+77 −15
Original line number Diff line number Diff line
@@ -665,20 +665,20 @@ public class Activity extends ContextThemeWrapper
            Runnable, BackStackState {
        ArrayList<Fragment> mAdded;
        ArrayList<Fragment> mRemoved;
        int mTransition;
        int mTransitionStyle;
        boolean mAddToBackStack;
        String mName;
        boolean mCommitted;
        
        public FragmentTransaction add(Fragment fragment, int containerViewId) {
            return add(fragment, null, containerViewId);
        public FragmentTransaction add(Fragment fragment) {
            return add(fragment, 0);
        }

        public FragmentTransaction add(Fragment fragment, String name, int containerViewId) {
        public FragmentTransaction add(Fragment fragment, int containerViewId) {
            if (fragment.mActivity != null) {
                throw new IllegalStateException("Fragment already added: " + fragment);
            }
            if (name != null) {
                fragment.mName = name;
            }
            if (mRemoved != null) {
                mRemoved.remove(fragment);
            }
@@ -690,6 +690,21 @@ public class Activity extends ContextThemeWrapper
            return this;
        }

        public FragmentTransaction replace(Fragment fragment, int containerViewId) {
            if (containerViewId == 0) {
                throw new IllegalArgumentException("Must use non-zero containerViewId");
            }
            if (mFragments.mFragments != null) {
                for (int i=0; i<mFragments.mFragments.size(); i++) {
                    Fragment old = mFragments.mFragments.get(i);
                    if (old.mContainerId == containerViewId) {
                        remove(old);
                    }
                }
            }
            return add(fragment, containerViewId);
        }
        
        public FragmentTransaction remove(Fragment fragment) {
            if (fragment.mActivity == null) {
                throw new IllegalStateException("Fragment not added: " + fragment);
@@ -704,8 +719,19 @@ public class Activity extends ContextThemeWrapper
            return this;
        }

        public FragmentTransaction addToBackStack() {
        public FragmentTransaction setTransition(int transition) {
            mTransition = transition;
            return this;
        }
        
        public FragmentTransaction setTransitionStyle(int styleRes) {
            mTransitionStyle = styleRes;
            return this;
        }
        
        public FragmentTransaction addToBackStack(String name) {
            mAddToBackStack = true;
            mName = name;
            return this;
        }

@@ -718,15 +744,21 @@ public class Activity extends ContextThemeWrapper
        public void run() {
            if (mRemoved != null) {
                for (int i=mRemoved.size()-1; i>=0; i--) {
                    mFragments.removeFragment(mRemoved.get(i));
                    mFragments.removeFragment(mRemoved.get(i), mTransition,
                            mTransitionStyle);
                }
            }
            if (mAdded != null) {
                for (int i=mAdded.size()-1; i>=0; i--) {
                    mFragments.addFragment(mAdded.get(i), false);
                    Fragment f = mAdded.get(i);
                    mFragments.addFragment(f, false);
                    if (mAddToBackStack) {
                        f.mBackStackNesting++;
                    }
                }
            }
            mFragments.moveToState(mFragments.mCurState, true);
            mFragments.moveToState(mFragments.mCurState, mTransition,
                    mTransitionStyle, true);
            if (mAddToBackStack) {
                mFragments.addBackStackState(this);
            }
@@ -735,7 +767,13 @@ public class Activity extends ContextThemeWrapper
        public void popFromBackStack() {
            if (mAdded != null) {
                for (int i=mAdded.size()-1; i>=0; i--) {
                    mFragments.removeFragment(mAdded.get(i));
                    Fragment f = mAdded.get(i);
                    if (mAddToBackStack) {
                        f.mBackStackNesting--;
                    }
                    mFragments.removeFragment(f,
                            FragmentManager.reverseTransit(mTransition),
                            mTransitionStyle);
                }
            }
            if (mRemoved != null) {
@@ -743,7 +781,18 @@ public class Activity extends ContextThemeWrapper
                    mFragments.addFragment(mRemoved.get(i), false);
                }
            }
            mFragments.moveToState(mFragments.mCurState, true);
        }
        
        public String getName() {
            return mName;
        }
        
        public int getTransition() {
            return mTransition;
        }
        
        public int getTransitionStyle() {
            return mTransitionStyle;
        }
    }
    
@@ -1743,6 +1792,16 @@ public class Activity extends ContextThemeWrapper
        return getWindow().findViewById(id);
    }

    /**
     * Finds a fragment that was identified by the given id either when inflated
     * from XML or as the container ID when added in a transaction.  This only
     * returns fragments that are currently added to the activity's content.
     * @return The fragment if found or null otherwise.
     */
    public Fragment findFragmentById(int id) {
        return mFragments.findFragmentById(id);
    }
    
    /**
     * Set the activity content from a layout resource.  The resource will be
     * inflated, adding all top-level views to the activity.
@@ -2010,9 +2069,12 @@ public class Activity extends ContextThemeWrapper
    /**
     * Pop the last fragment transition from the local activity's fragment
     * back stack.  If there is nothing to pop, false is returned.
     * @param name If non-null, this is the name of a previous back state
     * to look for; if found, all states up to (but not including) that
     * state will be popped.  If null, only the top state is popped.
     */
    public boolean popBackStack() {
        return mFragments.popBackStackState(mHandler);
    public boolean popBackStack(String name) {
        return mFragments.popBackStackState(mHandler, name);
    }
    
    /**
@@ -2021,7 +2083,7 @@ public class Activity extends ContextThemeWrapper
     * but you can override this to do whatever you want.
     */
    public void onBackPressed() {
        if (!popBackStack()) {
        if (!popBackStack(null)) {
            finish();
        }
    }
+6 −10
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;

/**
 * A Fragment is a piece of an application's user interface or behavior
@@ -35,9 +36,8 @@ public class Fragment implements ComponentCallbacks {
    static final int STARTED = 2;       // Created and started, not resumed.
    static final int RESUMED = 3;       // Created started and resumed.
    
    String mName;
    
    int mState = INITIALIZING;
    int mBackStackNesting;
    Activity mActivity;
    
    boolean mCalled;
@@ -49,14 +49,6 @@ public class Fragment implements ComponentCallbacks {
    public Fragment() {
    }
    
    public Fragment(String name) {
        mName = name;
    }
    
    public String getName() {
        return mName;
    }
    
    public Activity getActivity() {
        return mActivity;
    }
@@ -69,6 +61,10 @@ public class Fragment implements ComponentCallbacks {
        mCalled = true;
    }
    
    public Animation onCreateAnimation(int transit, boolean enter) {
        return null;
    }
    
    public void onCreate(Bundle savedInstanceState) {
        mCalled = true;
    }
+229 −17

File changed.

Preview size limit exceeded, changes collapsed.

+62 −2
Original line number Diff line number Diff line
@@ -4,9 +4,69 @@ package android.app;
 * API for performing a set of Fragment operations.
 */
public interface FragmentTransaction {
    public FragmentTransaction add(Fragment fragment);
    public FragmentTransaction add(Fragment fragment, int containerViewId);
    public FragmentTransaction add(Fragment fragment, String name, int containerViewId);
    public FragmentTransaction replace(Fragment fragment, int containerViewId);
    public FragmentTransaction remove(Fragment fragment);
    public FragmentTransaction addToBackStack();
    
    /**
     * Bit mask that is set for all enter transition.
     */
    public final int TRANSIT_ENTER_MASK = 0x1000;
    
    /**
     * Bit mask that is set for all exit transitions.
     */
    public final int TRANSIT_EXIT_MASK = 0x2000;
    
    /** Not set up for a transition. */
    public final int TRANSIT_UNSET = -1;
    /** No animation for transition. */
    public final int TRANSIT_NONE = 0;
    /** Window has been added to the screen. */
    public final int TRANSIT_ENTER = 1 | TRANSIT_ENTER_MASK;
    /** Window has been removed from the screen. */
    public final int TRANSIT_EXIT = 2 | TRANSIT_EXIT_MASK;
    /** Window has been made visible. */
    public final int TRANSIT_SHOW = 3 | TRANSIT_ENTER_MASK;
    /** Window has been made invisible. */
    public final int TRANSIT_HIDE = 4 | TRANSIT_EXIT_MASK;
    /** The "application starting" preview window is no longer needed, and will
     * animate away to show the real window. */
    public final int TRANSIT_PREVIEW_DONE = 5;
    /** A window in a new activity is being opened on top of an existing one
     * in the same task. */
    public final int TRANSIT_ACTIVITY_OPEN = 6 | TRANSIT_ENTER_MASK;
    /** The window in the top-most activity is being closed to reveal the
     * previous activity in the same task. */
    public final int TRANSIT_ACTIVITY_CLOSE = 7 | TRANSIT_EXIT_MASK;
    /** A window in a new task is being opened on top of an existing one
     * in another activity's task. */
    public final int TRANSIT_TASK_OPEN = 8 | TRANSIT_ENTER_MASK;
    /** A window in the top-most activity is being closed to reveal the
     * previous activity in a different task. */
    public final int TRANSIT_TASK_CLOSE = 9 | TRANSIT_EXIT_MASK;
    /** A window in an existing task is being displayed on top of an existing one
     * in another activity's task. */
    public final int TRANSIT_TASK_TO_FRONT = 10 | TRANSIT_ENTER_MASK;
    /** A window in an existing task is being put below all other tasks. */
    public final int TRANSIT_TASK_TO_BACK = 11 | TRANSIT_EXIT_MASK;
    /** A window in a new activity that doesn't have a wallpaper is being
     * opened on top of one that does, effectively closing the wallpaper. */
    public final int TRANSIT_WALLPAPER_CLOSE = 12 | TRANSIT_EXIT_MASK;
    /** A window in a new activity that does have a wallpaper is being
     * opened on one that didn't, effectively opening the wallpaper. */
    public final int TRANSIT_WALLPAPER_OPEN = 13 | TRANSIT_ENTER_MASK;
    /** A window in a new activity is being opened on top of an existing one,
     * and both are on top of the wallpaper. */
    public final int TRANSIT_WALLPAPER_INTRA_OPEN = 14 | TRANSIT_ENTER_MASK;
    /** The window in the top-most activity is being closed to reveal the
     * previous activity, and both are on top of he wallpaper. */
    public final int TRANSIT_WALLPAPER_INTRA_CLOSE = 15 | TRANSIT_EXIT_MASK;
    
    public FragmentTransaction setTransition(int transit);
    public FragmentTransaction setTransitionStyle(int styleRes);
    
    public FragmentTransaction addToBackStack(String name);
    public void commit();
}