FragmentTransaction.java revision bc377841db05bd5197ffadb58ba52c54b2a85f16
1package android.app;
2
3/**
4 * API for performing a set of Fragment operations.
5 */
6public abstract class FragmentTransaction {
7    /**
8     * Calls {@link #add(int, Fragment, String)} with a 0 containerViewId.
9     */
10    public abstract FragmentTransaction add(Fragment fragment, String tag);
11
12    /**
13     * Calls {@link #add(int, Fragment, String)} with a null tag.
14     */
15    public abstract FragmentTransaction add(int containerViewId, Fragment fragment);
16
17    /**
18     * Add a fragment to the activity state.  This fragment may optionally
19     * also have its view (if {@link Fragment#onCreateView Fragment.onCreateView}
20     * returns non-null) into a container view of the activity.
21     *
22     * @param containerViewId Optional identifier of the container this fragment is
23     * to be placed in.  If 0, it will not be placed in a container.
24     * @param fragment The fragment to be added.  This fragment must not already
25     * be added to the activity.
26     * @param tag Optional tag name for the fragment, to later retrieve the
27     * fragment with {@link FragmentManager#findFragmentByTag(String)
28     * FragmentManager.findFragmentByTag(String)}.
29     *
30     * @return Returns the same FragmentTransaction instance.
31     */
32    public abstract FragmentTransaction add(int containerViewId, Fragment fragment, String tag);
33
34    /**
35     * Calls {@link #replace(int, Fragment, String)} with a null tag.
36     */
37    public abstract FragmentTransaction replace(int containerViewId, Fragment fragment);
38
39    /**
40     * Replace an existing fragment that was added to a container.  This is
41     * essentially the same as calling {@link #remove(Fragment)} for all
42     * currently added fragments that were added with the same containerViewId
43     * and then {@link #add(int, Fragment, String)} with the same arguments
44     * given here.
45     *
46     * @param containerViewId Identifier of the container whose fragment(s) are
47     * to be replaced.
48     * @param fragment The new fragment to place in the container.
49     * @param tag Optional tag name for the fragment, to later retrieve the
50     * fragment with {@link FragmentManager#findFragmentByTag(String)
51     * FragmentManager.findFragmentByTag(String)}.
52     *
53     * @return Returns the same FragmentTransaction instance.
54     */
55    public abstract FragmentTransaction replace(int containerViewId, Fragment fragment, String tag);
56
57    /**
58     * Remove an existing fragment.  If it was added to a container, its view
59     * is also removed from that container.
60     *
61     * @param fragment The fragment to be removed.
62     *
63     * @return Returns the same FragmentTransaction instance.
64     */
65    public abstract FragmentTransaction remove(Fragment fragment);
66
67    /**
68     * Hides an existing fragment.  This is only relevant for fragments whose
69     * views have been added to a container, as this will cause the view to
70     * be hidden.
71     *
72     * @param fragment The fragment to be hidden.
73     *
74     * @return Returns the same FragmentTransaction instance.
75     */
76    public abstract FragmentTransaction hide(Fragment fragment);
77
78    /**
79     * Shows a previously hidden fragment.  This is only relevant for fragments whose
80     * views have been added to a container, as this will cause the view to
81     * be shown.
82     *
83     * @param fragment The fragment to be shown.
84     *
85     * @return Returns the same FragmentTransaction instance.
86     */
87    public abstract FragmentTransaction show(Fragment fragment);
88
89    /**
90     * @return <code>true</code> if this transaction contains no operations,
91     * <code>false</code> otherwise.
92     */
93    public abstract boolean isEmpty();
94
95    /**
96     * Bit mask that is set for all enter transitions.
97     */
98    public static final int TRANSIT_ENTER_MASK = 0x1000;
99
100    /**
101     * Bit mask that is set for all exit transitions.
102     */
103    public static final int TRANSIT_EXIT_MASK = 0x2000;
104
105    /** Not set up for a transition. */
106    public static final int TRANSIT_UNSET = -1;
107    /** No animation for transition. */
108    public static final int TRANSIT_NONE = 0;
109    /** Fragment is being added onto the stack */
110    public static final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK;
111    /** Fragment is being removed from the stack */
112    public static final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK;
113    /** Fragment should simply fade in or out; that is, no strong navigation associated
114     * with it except that it is appearing or disappearing for some reason. */
115    public static final int TRANSIT_FRAGMENT_FADE = 3 | TRANSIT_ENTER_MASK;
116
117    /**
118     * Set specific animation resources to run for the fragments that are
119     * entering and exiting in this transaction. These animations will not be
120     * played when popping the back stack.
121     */
122    public abstract FragmentTransaction setCustomAnimations(int enter, int exit);
123
124    /**
125     * Set specific animation resources to run for the fragments that are
126     * entering and exiting in this transaction. The <code>popEnter</code>
127     * and <code>popExit</code> animations will be played for enter/exit
128     * operations specifically when popping the back stack.
129     */
130    public abstract FragmentTransaction setCustomAnimations(int enter, int exit,
131            int popEnter, int popExit);
132
133    /**
134     * Select a standard transition animation for this transaction.  May be
135     * one of {@link #TRANSIT_NONE}, {@link #TRANSIT_FRAGMENT_OPEN},
136     * or {@link #TRANSIT_FRAGMENT_CLOSE}
137     */
138    public abstract FragmentTransaction setTransition(int transit);
139
140    /**
141     * Set a custom style resource that will be used for resolving transit
142     * animations.
143     */
144    public abstract FragmentTransaction setTransitionStyle(int styleRes);
145
146    /**
147     * Add this transaction to the back stack.  This means that the transaction
148     * will be remembered after it is committed, and will reverse its operation
149     * when later popped off the stack.
150     *
151     * @param name An optional name for this back stack state, or null.
152     */
153    public abstract FragmentTransaction addToBackStack(String name);
154
155    /**
156     * Returns true if this FragmentTransaction is allowed to be added to the back
157     * stack. If this method would return false, {@link #addToBackStack(String)}
158     * will throw {@link IllegalStateException}.
159     *
160     * @return True if {@link #addToBackStack(String)} is permitted on this transaction.
161     */
162    public abstract boolean isAddToBackStackAllowed();
163
164    /**
165     * Disallow calls to {@link #addToBackStack(String)}. Any future calls to
166     * addToBackStack will throw {@link IllegalStateException}. If addToBackStack
167     * has already been called, this method will throw IllegalStateException.
168     */
169    public abstract FragmentTransaction disallowAddToBackStack();
170
171    /**
172     * Set the full title to show as a bread crumb when this transaction
173     * is on the back stack, as used by {@link FragmentBreadCrumbs}.
174     *
175     * @param res A string resource containing the title.
176     */
177    public abstract FragmentTransaction setBreadCrumbTitle(int res);
178
179    /**
180     * Like {@link #setBreadCrumbTitle(int)} but taking a raw string; this
181     * method is <em>not</em> recommended, as the string can not be changed
182     * later if the locale changes.
183     */
184    public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text);
185
186    /**
187     * Set the short title to show as a bread crumb when this transaction
188     * is on the back stack, as used by {@link FragmentBreadCrumbs}.
189     *
190     * @param res A string resource containing the title.
191     */
192    public abstract FragmentTransaction setBreadCrumbShortTitle(int res);
193
194    /**
195     * Like {@link #setBreadCrumbShortTitle(int)} but taking a raw string; this
196     * method is <em>not</em> recommended, as the string can not be changed
197     * later if the locale changes.
198     */
199    public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text);
200
201    /**
202     * Schedules a commit of this transaction.  The commit does
203     * not happen immediately; it will be scheduled as work on the main thread
204     * to be done the next time that thread is ready.
205     *
206     * <p class="note">A transaction can only be committed with this method
207     * prior to its containing activity saving its state.  If the commit is
208     * attempted after that point, an exception will be thrown.  This is
209     * because the state after the commit can be lost if the activity needs to
210     * be restored from its state.  See {@link #commitAllowingStateLoss()} for
211     * situations where it may be okay to lose the commit.</p>
212     *
213     * @return Returns the identifier of this transaction's back stack entry,
214     * if {@link #addToBackStack(String)} had been called.  Otherwise, returns
215     * a negative number.
216     */
217    public abstract int commit();
218
219    /**
220     * Like {@link #commit} but allows the commit to be executed after an
221     * activity's state is saved.  This is dangerous because the commit can
222     * be lost if the activity needs to later be restored from its state, so
223     * this should only be used for cases where it is okay for the UI state
224     * to change unexpectedly on the user.
225     */
226    public abstract int commitAllowingStateLoss();
227}
228