• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package android.app;
2 
3 import android.annotation.AnimatorRes;
4 import android.annotation.IdRes;
5 import android.annotation.IntDef;
6 import android.annotation.Nullable;
7 import android.annotation.StringRes;
8 import android.annotation.StyleRes;
9 import android.os.Bundle;
10 import android.view.View;
11 
12 import java.lang.annotation.Retention;
13 import java.lang.annotation.RetentionPolicy;
14 
15 /**
16  * API for performing a set of Fragment operations.
17  *
18  * <div class="special reference">
19  * <h3>Developer Guides</h3>
20  * <p>For more information about using fragments, read the
21  * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer
22  * guide.</p>
23  * </div>
24  */
25 public abstract class FragmentTransaction {
26     /**
27      * Calls {@link #add(int, Fragment, String)} with a 0 containerViewId.
28      */
add(Fragment fragment, String tag)29     public abstract FragmentTransaction add(Fragment fragment, String tag);
30 
31     /**
32      * Calls {@link #add(int, Fragment, String)} with a null tag.
33      */
add(@dRes int containerViewId, Fragment fragment)34     public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment);
35 
36     /**
37      * Add a fragment to the activity state.  This fragment may optionally
38      * also have its view (if {@link Fragment#onCreateView Fragment.onCreateView}
39      * returns non-null) inserted into a container view of the activity.
40      *
41      * @param containerViewId Optional identifier of the container this fragment is
42      * to be placed in.  If 0, it will not be placed in a container.
43      * @param fragment The fragment to be added.  This fragment must not already
44      * be added to the activity.
45      * @param tag Optional tag name for the fragment, to later retrieve the
46      * fragment with {@link FragmentManager#findFragmentByTag(String)
47      * FragmentManager.findFragmentByTag(String)}.
48      *
49      * @return Returns the same FragmentTransaction instance.
50      */
add(@dRes int containerViewId, Fragment fragment, String tag)51     public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment,
52             String tag);
53 
54     /**
55      * Calls {@link #replace(int, Fragment, String)} with a null tag.
56      */
replace(@dRes int containerViewId, Fragment fragment)57     public abstract FragmentTransaction replace(@IdRes int containerViewId, Fragment fragment);
58 
59     /**
60      * Replace an existing fragment that was added to a container.  This is
61      * essentially the same as calling {@link #remove(Fragment)} for all
62      * currently added fragments that were added with the same containerViewId
63      * and then {@link #add(int, Fragment, String)} with the same arguments
64      * given here.
65      *
66      * @param containerViewId Identifier of the container whose fragment(s) are
67      * to be replaced.
68      * @param fragment The new fragment to place in the container.
69      * @param tag Optional tag name for the fragment, to later retrieve the
70      * fragment with {@link FragmentManager#findFragmentByTag(String)
71      * FragmentManager.findFragmentByTag(String)}.
72      *
73      * @return Returns the same FragmentTransaction instance.
74      */
replace(@dRes int containerViewId, Fragment fragment, String tag)75     public abstract FragmentTransaction replace(@IdRes int containerViewId, Fragment fragment,
76             String tag);
77 
78     /**
79      * Remove an existing fragment.  If it was added to a container, its view
80      * is also removed from that container.
81      *
82      * @param fragment The fragment to be removed.
83      *
84      * @return Returns the same FragmentTransaction instance.
85      */
remove(Fragment fragment)86     public abstract FragmentTransaction remove(Fragment fragment);
87 
88     /**
89      * Hides an existing fragment.  This is only relevant for fragments whose
90      * views have been added to a container, as this will cause the view to
91      * be hidden.
92      *
93      * @param fragment The fragment to be hidden.
94      *
95      * @return Returns the same FragmentTransaction instance.
96      */
hide(Fragment fragment)97     public abstract FragmentTransaction hide(Fragment fragment);
98 
99     /**
100      * Shows a previously hidden fragment.  This is only relevant for fragments whose
101      * views have been added to a container, as this will cause the view to
102      * be shown.
103      *
104      * @param fragment The fragment to be shown.
105      *
106      * @return Returns the same FragmentTransaction instance.
107      */
show(Fragment fragment)108     public abstract FragmentTransaction show(Fragment fragment);
109 
110     /**
111      * Detach the given fragment from the UI.  This is the same state as
112      * when it is put on the back stack: the fragment is removed from
113      * the UI, however its state is still being actively managed by the
114      * fragment manager.  When going into this state its view hierarchy
115      * is destroyed.
116      *
117      * @param fragment The fragment to be detached.
118      *
119      * @return Returns the same FragmentTransaction instance.
120      */
detach(Fragment fragment)121     public abstract FragmentTransaction detach(Fragment fragment);
122 
123     /**
124      * Re-attach a fragment after it had previously been detached from
125      * the UI with {@link #detach(Fragment)}.  This
126      * causes its view hierarchy to be re-created, attached to the UI,
127      * and displayed.
128      *
129      * @param fragment The fragment to be attached.
130      *
131      * @return Returns the same FragmentTransaction instance.
132      */
attach(Fragment fragment)133     public abstract FragmentTransaction attach(Fragment fragment);
134 
135     /**
136      * Set a currently active fragment in this FragmentManager as the primary navigation fragment.
137      *
138      * <p>The primary navigation fragment's
139      * {@link Fragment#getChildFragmentManager() child FragmentManager} will be called first
140      * to process delegated navigation actions such as {@link FragmentManager#popBackStack()}
141      * if no ID or transaction name is provided to pop to. Navigation operations outside of the
142      * fragment system may choose to delegate those actions to the primary navigation fragment
143      * as returned by {@link FragmentManager#getPrimaryNavigationFragment()}.</p>
144      *
145      * <p>The fragment provided must currently be added to the FragmentManager to be set as
146      * a primary navigation fragment, or previously added as part of this transaction.</p>
147      *
148      * @param fragment the fragment to set as the primary navigation fragment
149      * @return the same FragmentTransaction instance
150      */
setPrimaryNavigationFragment(Fragment fragment)151     public abstract FragmentTransaction setPrimaryNavigationFragment(Fragment fragment);
152 
153     /**
154      * @return <code>true</code> if this transaction contains no operations,
155      * <code>false</code> otherwise.
156      */
isEmpty()157     public abstract boolean isEmpty();
158 
159     /**
160      * Bit mask that is set for all enter transitions.
161      */
162     public static final int TRANSIT_ENTER_MASK = 0x1000;
163 
164     /**
165      * Bit mask that is set for all exit transitions.
166      */
167     public static final int TRANSIT_EXIT_MASK = 0x2000;
168 
169     /** Not set up for a transition. */
170     public static final int TRANSIT_UNSET = -1;
171     /** No animation for transition. */
172     public static final int TRANSIT_NONE = 0;
173     /** Fragment is being added onto the stack */
174     public static final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK;
175     /** Fragment is being removed from the stack */
176     public static final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK;
177     /** Fragment should simply fade in or out; that is, no strong navigation associated
178      * with it except that it is appearing or disappearing for some reason. */
179     public static final int TRANSIT_FRAGMENT_FADE = 3 | TRANSIT_ENTER_MASK;
180 
181     /** @hide */
182     @IntDef({TRANSIT_NONE, TRANSIT_FRAGMENT_OPEN, TRANSIT_FRAGMENT_CLOSE, TRANSIT_FRAGMENT_FADE})
183     @Retention(RetentionPolicy.SOURCE)
184     public @interface Transit {}
185 
186     /**
187      * Set specific animation resources to run for the fragments that are
188      * entering and exiting in this transaction. These animations will not be
189      * played when popping the back stack.
190      */
setCustomAnimations(@nimatorRes int enter, @AnimatorRes int exit)191     public abstract FragmentTransaction setCustomAnimations(@AnimatorRes int enter,
192             @AnimatorRes int exit);
193 
194     /**
195      * Set specific animation resources to run for the fragments that are
196      * entering and exiting in this transaction. The <code>popEnter</code>
197      * and <code>popExit</code> animations will be played for enter/exit
198      * operations specifically when popping the back stack.
199      */
setCustomAnimations(@nimatorRes int enter, @AnimatorRes int exit, @AnimatorRes int popEnter, @AnimatorRes int popExit)200     public abstract FragmentTransaction setCustomAnimations(@AnimatorRes int enter,
201             @AnimatorRes int exit, @AnimatorRes int popEnter, @AnimatorRes int popExit);
202 
203     /**
204      * Select a standard transition animation for this transaction.  May be
205      * one of {@link #TRANSIT_NONE}, {@link #TRANSIT_FRAGMENT_OPEN},
206      * {@link #TRANSIT_FRAGMENT_CLOSE}, or {@link #TRANSIT_FRAGMENT_FADE}.
207      */
setTransition(@ransit int transit)208     public abstract FragmentTransaction setTransition(@Transit int transit);
209 
210     /**
211      * Used with to map a View from a removed or hidden Fragment to a View from a shown
212      * or added Fragment.
213      * @param sharedElement A View in a disappearing Fragment to match with a View in an
214      *                      appearing Fragment.
215      * @param name The transitionName for a View in an appearing Fragment to match to the shared
216      *             element.
217      */
addSharedElement(View sharedElement, String name)218     public abstract FragmentTransaction addSharedElement(View sharedElement, String name);
219 
220     /**
221      * Set a custom style resource that will be used for resolving transit
222      * animations.
223      */
setTransitionStyle(@tyleRes int styleRes)224     public abstract FragmentTransaction setTransitionStyle(@StyleRes int styleRes);
225 
226     /**
227      * Add this transaction to the back stack.  This means that the transaction
228      * will be remembered after it is committed, and will reverse its operation
229      * when later popped off the stack.
230      *
231      * @param name An optional name for this back stack state, or null.
232      */
addToBackStack(@ullable String name)233     public abstract FragmentTransaction addToBackStack(@Nullable String name);
234 
235     /**
236      * Returns true if this FragmentTransaction is allowed to be added to the back
237      * stack. If this method would return false, {@link #addToBackStack(String)}
238      * will throw {@link IllegalStateException}.
239      *
240      * @return True if {@link #addToBackStack(String)} is permitted on this transaction.
241      */
isAddToBackStackAllowed()242     public abstract boolean isAddToBackStackAllowed();
243 
244     /**
245      * Disallow calls to {@link #addToBackStack(String)}. Any future calls to
246      * addToBackStack will throw {@link IllegalStateException}. If addToBackStack
247      * has already been called, this method will throw IllegalStateException.
248      */
disallowAddToBackStack()249     public abstract FragmentTransaction disallowAddToBackStack();
250 
251     /**
252      * Set the full title to show as a bread crumb when this transaction
253      * is on the back stack, as used by {@link FragmentBreadCrumbs}.
254      *
255      * @param res A string resource containing the title.
256      */
setBreadCrumbTitle(@tringRes int res)257     public abstract FragmentTransaction setBreadCrumbTitle(@StringRes int res);
258 
259     /**
260      * Like {@link #setBreadCrumbTitle(int)} but taking a raw string; this
261      * method is <em>not</em> recommended, as the string can not be changed
262      * later if the locale changes.
263      */
setBreadCrumbTitle(CharSequence text)264     public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text);
265 
266     /**
267      * Set the short title to show as a bread crumb when this transaction
268      * is on the back stack, as used by {@link FragmentBreadCrumbs}.
269      *
270      * @param res A string resource containing the title.
271      */
setBreadCrumbShortTitle(@tringRes int res)272     public abstract FragmentTransaction setBreadCrumbShortTitle(@StringRes int res);
273 
274     /**
275      * Like {@link #setBreadCrumbShortTitle(int)} but taking a raw string; this
276      * method is <em>not</em> recommended, as the string can not be changed
277      * later if the locale changes.
278      */
setBreadCrumbShortTitle(CharSequence text)279     public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text);
280 
281     /**
282      * Sets whether or not to allow optimizing operations within and across
283      * transactions. This will remove redundant operations, eliminating
284      * operations that cancel. For example, if two transactions are executed
285      * together, one that adds a fragment A and the next replaces it with fragment B,
286      * the operations will cancel and only fragment B will be added. That means that
287      * fragment A may not go through the creation/destruction lifecycle.
288      * <p>
289      * The side effect of removing redundant operations is that fragments may have state changes
290      * out of the expected order. For example, one transaction adds fragment A,
291      * a second adds fragment B, then a third removes fragment A. Without removing the redundant
292      * operations, fragment B could expect that while it is being created, fragment A will also
293      * exist because fragment A will be removed after fragment B was added.
294      * With removing redundant operations, fragment B cannot expect fragment A to exist when
295      * it has been created because fragment A's add/remove will be optimized out.
296      * <p>
297      * It can also reorder the state changes of Fragments to allow for better Transitions.
298      * Added Fragments may have {@link Fragment#onCreate(Bundle)} called before replaced
299      * Fragments have {@link Fragment#onDestroy()} called.
300      * <p>
301      * The default is {@code false} for applications targeting version
302      * versions prior to O and {@code true} for applications targeting O and
303      * later.
304      *
305      * @param reorderingAllowed {@code true} to enable optimizing out redundant operations
306      *                          or {@code false} to disable optimizing out redundant
307      *                          operations on this transaction.
308      */
setReorderingAllowed(boolean reorderingAllowed)309     public abstract FragmentTransaction setReorderingAllowed(boolean reorderingAllowed);
310 
311     /**
312      * Add a Runnable to this transaction that will be run after this transaction has
313      * been committed. If fragment transactions are {@link #setReorderingAllowed(boolean) optimized}
314      * this may be after other subsequent fragment operations have also taken place, or operations
315      * in this transaction may have been optimized out due to the presence of a subsequent
316      * fragment transaction in the batch.
317      *
318      *
319      * <p>If a transaction is committed using {@link #commitAllowingStateLoss()} this runnable
320      * may be executed when the FragmentManager is in a state where new transactions may not
321      * be committed without allowing state loss.</p>
322      *
323      * <p><code>runOnCommit</code> may not be used with transactions
324      * {@link #addToBackStack(String) added to the back stack} as Runnables cannot be persisted
325      * with back stack state. {@link IllegalStateException} will be thrown if
326      * {@link #addToBackStack(String)} has been previously called for this transaction
327      * or if it is called after a call to <code>runOnCommit</code>.</p>
328      *
329      * @param runnable Runnable to add
330      * @return this FragmentTransaction
331      * @throws IllegalStateException if {@link #addToBackStack(String)} has been called
332      */
runOnCommit(Runnable runnable)333     public abstract FragmentTransaction runOnCommit(Runnable runnable);
334 
335     /**
336      * Schedules a commit of this transaction.  The commit does
337      * not happen immediately; it will be scheduled as work on the main thread
338      * to be done the next time that thread is ready.
339      *
340      * <p class="note">A transaction can only be committed with this method
341      * prior to its containing activity saving its state.  If the commit is
342      * attempted after that point, an exception will be thrown.  This is
343      * because the state after the commit can be lost if the activity needs to
344      * be restored from its state.  See {@link #commitAllowingStateLoss()} for
345      * situations where it may be okay to lose the commit.</p>
346      *
347      * @return Returns the identifier of this transaction's back stack entry,
348      * if {@link #addToBackStack(String)} had been called.  Otherwise, returns
349      * a negative number.
350      */
commit()351     public abstract int commit();
352 
353     /**
354      * Like {@link #commit} but allows the commit to be executed after an
355      * activity's state is saved.  This is dangerous because the commit can
356      * be lost if the activity needs to later be restored from its state, so
357      * this should only be used for cases where it is okay for the UI state
358      * to change unexpectedly on the user.
359      */
commitAllowingStateLoss()360     public abstract int commitAllowingStateLoss();
361 
362     /**
363      * Commits this transaction synchronously. Any added fragments will be
364      * initialized and brought completely to the lifecycle state of their host
365      * and any removed fragments will be torn down accordingly before this
366      * call returns. Committing a transaction in this way allows fragments
367      * to be added as dedicated, encapsulated components that monitor the
368      * lifecycle state of their host while providing firmer ordering guarantees
369      * around when those fragments are fully initialized and ready. Fragments
370      * that manage views will have those views created and attached.
371      *
372      * <p>Calling <code>commitNow</code> is preferable to calling
373      * {@link #commit()} followed by {@link FragmentManager#executePendingTransactions()}
374      * as the latter will have the side effect of attempting to commit <em>all</em>
375      * currently pending transactions whether that is the desired behavior
376      * or not.</p>
377      *
378      * <p>Transactions committed in this way may not be added to the
379      * FragmentManager's back stack, as doing so would break other expected
380      * ordering guarantees for other asynchronously committed transactions.
381      * This method will throw {@link IllegalStateException} if the transaction
382      * previously requested to be added to the back stack with
383      * {@link #addToBackStack(String)}.</p>
384      *
385      * <p class="note">A transaction can only be committed with this method
386      * prior to its containing activity saving its state.  If the commit is
387      * attempted after that point, an exception will be thrown.  This is
388      * because the state after the commit can be lost if the activity needs to
389      * be restored from its state.  See {@link #commitAllowingStateLoss()} for
390      * situations where it may be okay to lose the commit.</p>
391      */
commitNow()392     public abstract void commitNow();
393 
394     /**
395      * Like {@link #commitNow} but allows the commit to be executed after an
396      * activity's state is saved.  This is dangerous because the commit can
397      * be lost if the activity needs to later be restored from its state, so
398      * this should only be used for cases where it is okay for the UI state
399      * to change unexpectedly on the user.
400      */
commitNowAllowingStateLoss()401     public abstract void commitNowAllowingStateLoss();
402 }
403