blob: e435580d55fef9626ff9195af921550cbb834d3c [file] [log] [blame]
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001package android.app;
2
George Mount27b3ae62015-10-16 12:36:58 -07003import android.annotation.AnimatorRes;
4import android.annotation.IdRes;
5import android.annotation.IntDef;
6import android.annotation.Nullable;
7import android.annotation.StringRes;
8import android.annotation.StyleRes;
George Mountd4c3c912014-06-09 12:31:34 -07009import android.view.View;
10
George Mount27b3ae62015-10-16 12:36:58 -070011import java.lang.annotation.Retention;
12import java.lang.annotation.RetentionPolicy;
13
Dianne Hackborn2dedce62010-04-15 14:45:25 -070014/**
15 * API for performing a set of Fragment operations.
Joe Fernandezb54e7a32011-10-03 15:09:50 -070016 *
17 * <div class="special reference">
18 * <h3>Developer Guides</h3>
19 * <p>For more information about using fragments, read the
20 * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p>
21 * </div>
Dianne Hackborn2dedce62010-04-15 14:45:25 -070022 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070023public abstract class FragmentTransaction {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070024 /**
25 * Calls {@link #add(int, Fragment, String)} with a 0 containerViewId.
26 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070027 public abstract FragmentTransaction add(Fragment fragment, String tag);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070028
29 /**
30 * Calls {@link #add(int, Fragment, String)} with a null tag.
31 */
George Mount27b3ae62015-10-16 12:36:58 -070032 public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070033
34 /**
35 * Add a fragment to the activity state. This fragment may optionally
36 * also have its view (if {@link Fragment#onCreateView Fragment.onCreateView}
Mark Dolinerd0646dc2014-08-27 16:04:02 -070037 * returns non-null) inserted into a container view of the activity.
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070038 *
39 * @param containerViewId Optional identifier of the container this fragment is
40 * to be placed in. If 0, it will not be placed in a container.
41 * @param fragment The fragment to be added. This fragment must not already
42 * be added to the activity.
43 * @param tag Optional tag name for the fragment, to later retrieve the
Dianne Hackborn247fe742011-01-08 17:25:57 -080044 * fragment with {@link FragmentManager#findFragmentByTag(String)
45 * FragmentManager.findFragmentByTag(String)}.
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070046 *
47 * @return Returns the same FragmentTransaction instance.
48 */
George Mount27b3ae62015-10-16 12:36:58 -070049 public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment,
50 String tag);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070051
52 /**
53 * Calls {@link #replace(int, Fragment, String)} with a null tag.
54 */
George Mount27b3ae62015-10-16 12:36:58 -070055 public abstract FragmentTransaction replace(@IdRes int containerViewId, Fragment fragment);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070056
57 /**
58 * Replace an existing fragment that was added to a container. This is
59 * essentially the same as calling {@link #remove(Fragment)} for all
60 * currently added fragments that were added with the same containerViewId
61 * and then {@link #add(int, Fragment, String)} with the same arguments
62 * given here.
63 *
64 * @param containerViewId Identifier of the container whose fragment(s) are
65 * to be replaced.
66 * @param fragment The new fragment to place in the container.
67 * @param tag Optional tag name for the fragment, to later retrieve the
Dianne Hackborn247fe742011-01-08 17:25:57 -080068 * fragment with {@link FragmentManager#findFragmentByTag(String)
69 * FragmentManager.findFragmentByTag(String)}.
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070070 *
71 * @return Returns the same FragmentTransaction instance.
72 */
George Mount27b3ae62015-10-16 12:36:58 -070073 public abstract FragmentTransaction replace(@IdRes int containerViewId, Fragment fragment,
74 String tag);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070075
76 /**
77 * Remove an existing fragment. If it was added to a container, its view
78 * is also removed from that container.
79 *
80 * @param fragment The fragment to be removed.
81 *
82 * @return Returns the same FragmentTransaction instance.
83 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070084 public abstract FragmentTransaction remove(Fragment fragment);
Dianne Hackbornf121be72010-05-06 14:10:32 -070085
86 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070087 * Hides an existing fragment. This is only relevant for fragments whose
88 * views have been added to a container, as this will cause the view to
89 * be hidden.
90 *
91 * @param fragment The fragment to be hidden.
92 *
93 * @return Returns the same FragmentTransaction instance.
94 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070095 public abstract FragmentTransaction hide(Fragment fragment);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070096
97 /**
Jim Shumabaa15532010-11-09 20:37:03 -080098 * Shows a previously hidden fragment. This is only relevant for fragments whose
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070099 * views have been added to a container, as this will cause the view to
100 * be shown.
101 *
102 * @param fragment The fragment to be shown.
103 *
104 * @return Returns the same FragmentTransaction instance.
105 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700106 public abstract FragmentTransaction show(Fragment fragment);
Adam Powell2b6230e2010-09-07 17:55:25 -0700107
108 /**
Dianne Hackborn47c41562011-04-15 19:00:20 -0700109 * Detach the given fragment from the UI. This is the same state as
110 * when it is put on the back stack: the fragment is removed from
111 * the UI, however its state is still being actively managed by the
112 * fragment manager. When going into this state its view hierarchy
113 * is destroyed.
114 *
115 * @param fragment The fragment to be detached.
116 *
117 * @return Returns the same FragmentTransaction instance.
118 */
119 public abstract FragmentTransaction detach(Fragment fragment);
120
121 /**
Paul Quei355297c2014-03-14 15:54:15 +0800122 * Re-attach a fragment after it had previously been detached from
Dianne Hackborn47c41562011-04-15 19:00:20 -0700123 * the UI with {@link #detach(Fragment)}. This
124 * causes its view hierarchy to be re-created, attached to the UI,
125 * and displayed.
126 *
127 * @param fragment The fragment to be attached.
128 *
129 * @return Returns the same FragmentTransaction instance.
130 */
131 public abstract FragmentTransaction attach(Fragment fragment);
132
133 /**
Adam Powell2b6230e2010-09-07 17:55:25 -0700134 * @return <code>true</code> if this transaction contains no operations,
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700135 * <code>false</code> otherwise.
Adam Powell2b6230e2010-09-07 17:55:25 -0700136 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700137 public abstract boolean isEmpty();
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700138
139 /**
140 * Bit mask that is set for all enter transitions.
Dianne Hackbornf121be72010-05-06 14:10:32 -0700141 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700142 public static final int TRANSIT_ENTER_MASK = 0x1000;
Dianne Hackbornf121be72010-05-06 14:10:32 -0700143
144 /**
145 * Bit mask that is set for all exit transitions.
146 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700147 public static final int TRANSIT_EXIT_MASK = 0x2000;
Dianne Hackbornf121be72010-05-06 14:10:32 -0700148
149 /** Not set up for a transition. */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700150 public static final int TRANSIT_UNSET = -1;
Dianne Hackbornf121be72010-05-06 14:10:32 -0700151 /** No animation for transition. */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700152 public static final int TRANSIT_NONE = 0;
Chet Haase9ff82bf2010-10-05 14:30:51 -0700153 /** Fragment is being added onto the stack */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700154 public static final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK;
Chet Haase9ff82bf2010-10-05 14:30:51 -0700155 /** Fragment is being removed from the stack */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700156 public static final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK;
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800157 /** Fragment should simply fade in or out; that is, no strong navigation associated
158 * with it except that it is appearing or disappearing for some reason. */
159 public static final int TRANSIT_FRAGMENT_FADE = 3 | TRANSIT_ENTER_MASK;
Chet Haase811ed1062010-08-06 10:38:15 -0700160
George Mount27b3ae62015-10-16 12:36:58 -0700161 /** @hide */
162 @IntDef({TRANSIT_NONE, TRANSIT_FRAGMENT_OPEN, TRANSIT_FRAGMENT_CLOSE, TRANSIT_FRAGMENT_FADE})
163 @Retention(RetentionPolicy.SOURCE)
164 public @interface Transit {}
165
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700166 /**
167 * Set specific animation resources to run for the fragments that are
Chet Haasebc377842011-03-22 11:35:22 -0700168 * entering and exiting in this transaction. These animations will not be
169 * played when popping the back stack.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700170 */
George Mount27b3ae62015-10-16 12:36:58 -0700171 public abstract FragmentTransaction setCustomAnimations(@AnimatorRes int enter,
172 @AnimatorRes int exit);
Chet Haasebc377842011-03-22 11:35:22 -0700173
174 /**
175 * Set specific animation resources to run for the fragments that are
176 * entering and exiting in this transaction. The <code>popEnter</code>
177 * and <code>popExit</code> animations will be played for enter/exit
178 * operations specifically when popping the back stack.
179 */
George Mount27b3ae62015-10-16 12:36:58 -0700180 public abstract FragmentTransaction setCustomAnimations(@AnimatorRes int enter,
181 @AnimatorRes int exit, @AnimatorRes int popEnter, @AnimatorRes int popExit);
Chet Haasebc377842011-03-22 11:35:22 -0700182
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700183 /**
184 * Select a standard transition animation for this transaction. May be
185 * one of {@link #TRANSIT_NONE}, {@link #TRANSIT_FRAGMENT_OPEN},
George Mount27b3ae62015-10-16 12:36:58 -0700186 * {@link #TRANSIT_FRAGMENT_CLOSE}, or {@link #TRANSIT_FRAGMENT_FADE}.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700187 */
George Mount27b3ae62015-10-16 12:36:58 -0700188 public abstract FragmentTransaction setTransition(@Transit int transit);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700189
190 /**
George Mountc03da0e2014-08-22 17:04:02 -0700191 * Used with to map a View from a removed or hidden Fragment to a View from a shown
192 * or added Fragment.
George Mountd4c3c912014-06-09 12:31:34 -0700193 * @param sharedElement A View in a disappearing Fragment to match with a View in an
194 * appearing Fragment.
George Mount0a2ae002014-06-23 14:57:27 +0000195 * @param name The transitionName for a View in an appearing Fragment to match to the shared
George Mountd4c3c912014-06-09 12:31:34 -0700196 * element.
197 */
George Mount448bace2014-08-18 16:27:43 -0700198 public abstract FragmentTransaction addSharedElement(View sharedElement, String name);
199
200 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700201 * Set a custom style resource that will be used for resolving transit
202 * animations.
203 */
George Mount27b3ae62015-10-16 12:36:58 -0700204 public abstract FragmentTransaction setTransitionStyle(@StyleRes int styleRes);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700205
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700206 /**
207 * Add this transaction to the back stack. This means that the transaction
208 * will be remembered after it is committed, and will reverse its operation
209 * when later popped off the stack.
210 *
211 * @param name An optional name for this back stack state, or null.
212 */
George Mount27b3ae62015-10-16 12:36:58 -0700213 public abstract FragmentTransaction addToBackStack(@Nullable String name);
Dianne Hackborndd913a52010-07-22 12:17:04 -0700214
215 /**
Adam Powell0c24a552010-11-03 16:44:11 -0700216 * Returns true if this FragmentTransaction is allowed to be added to the back
217 * stack. If this method would return false, {@link #addToBackStack(String)}
218 * will throw {@link IllegalStateException}.
219 *
220 * @return True if {@link #addToBackStack(String)} is permitted on this transaction.
221 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700222 public abstract boolean isAddToBackStackAllowed();
Adam Powell0c24a552010-11-03 16:44:11 -0700223
224 /**
225 * Disallow calls to {@link #addToBackStack(String)}. Any future calls to
226 * addToBackStack will throw {@link IllegalStateException}. If addToBackStack
227 * has already been called, this method will throw IllegalStateException.
228 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700229 public abstract FragmentTransaction disallowAddToBackStack();
Adam Powell0c24a552010-11-03 16:44:11 -0700230
231 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700232 * Set the full title to show as a bread crumb when this transaction
233 * is on the back stack, as used by {@link FragmentBreadCrumbs}.
234 *
235 * @param res A string resource containing the title.
236 */
George Mount27b3ae62015-10-16 12:36:58 -0700237 public abstract FragmentTransaction setBreadCrumbTitle(@StringRes int res);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700238
239 /**
240 * Like {@link #setBreadCrumbTitle(int)} but taking a raw string; this
241 * method is <em>not</em> recommended, as the string can not be changed
242 * later if the locale changes.
243 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700244 public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700245
246 /**
247 * Set the short title to show as a bread crumb when this transaction
248 * is on the back stack, as used by {@link FragmentBreadCrumbs}.
249 *
250 * @param res A string resource containing the title.
251 */
George Mount27b3ae62015-10-16 12:36:58 -0700252 public abstract FragmentTransaction setBreadCrumbShortTitle(@StringRes int res);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700253
254 /**
255 * Like {@link #setBreadCrumbShortTitle(int)} but taking a raw string; this
256 * method is <em>not</em> recommended, as the string can not be changed
257 * later if the locale changes.
258 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700259 public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700260
261 /**
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700262 * Schedules a commit of this transaction. The commit does
Dianne Hackborndd913a52010-07-22 12:17:04 -0700263 * not happen immediately; it will be scheduled as work on the main thread
264 * to be done the next time that thread is ready.
265 *
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700266 * <p class="note">A transaction can only be committed with this method
267 * prior to its containing activity saving its state. If the commit is
268 * attempted after that point, an exception will be thrown. This is
269 * because the state after the commit can be lost if the activity needs to
270 * be restored from its state. See {@link #commitAllowingStateLoss()} for
271 * situations where it may be okay to lose the commit.</p>
272 *
Dianne Hackborndd913a52010-07-22 12:17:04 -0700273 * @return Returns the identifier of this transaction's back stack entry,
274 * if {@link #addToBackStack(String)} had been called. Otherwise, returns
275 * a negative number.
276 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700277 public abstract int commit();
278
279 /**
280 * Like {@link #commit} but allows the commit to be executed after an
281 * activity's state is saved. This is dangerous because the commit can
282 * be lost if the activity needs to later be restored from its state, so
283 * this should only be used for cases where it is okay for the UI state
284 * to change unexpectedly on the user.
285 */
286 public abstract int commitAllowingStateLoss();
Adam Powellf6b30662016-02-04 15:38:20 -0800287
288 /**
289 * Commits this transaction synchronously. Any added fragments will be
290 * initialized and brought completely to the lifecycle state of their host
291 * and any removed fragments will be torn down accordingly before this
292 * call returns. Committing a transaction in this way allows fragments
293 * to be added as dedicated, encapsulated components that monitor the
294 * lifecycle state of their host while providing firmer ordering guarantees
295 * around when those fragments are fully initialized and ready. Fragments
296 * that manage views will have those views created and attached.
297 *
298 * <p>Calling <code>commitNow</code> is preferable to calling
299 * {@link #commit()} followed by {@link FragmentManager#executePendingTransactions()}
300 * as the latter will have the side effect of attempting to commit <em>all</em>
301 * currently pending transactions whether that is the desired behavior
302 * or not.</p>
303 *
304 * <p>Transactions committed in this way may not be added to the
305 * FragmentManager's back stack, as doing so would break other expected
306 * ordering guarantees for other asynchronously committed transactions.
307 * This method will throw {@link IllegalStateException} if the transaction
308 * previously requested to be added to the back stack with
309 * {@link #addToBackStack(String)}.</p>
310 *
311 * <p class="note">A transaction can only be committed with this method
312 * prior to its containing activity saving its state. If the commit is
313 * attempted after that point, an exception will be thrown. This is
314 * because the state after the commit can be lost if the activity needs to
315 * be restored from its state. See {@link #commitAllowingStateLoss()} for
316 * situations where it may be okay to lose the commit.</p>
317 */
318 public abstract void commitNow();
319
320 /**
321 * Like {@link #commitNow} but allows the commit to be executed after an
322 * activity's state is saved. This is dangerous because the commit can
323 * be lost if the activity needs to later be restored from its state, so
324 * this should only be used for cases where it is okay for the UI state
325 * to change unexpectedly on the user.
326 */
327 public abstract void commitNowAllowingStateLoss();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700328}