blob: 633e85b781184bd78fd39c29f6db090b2b62381a [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
Hemal Patela8e1c752016-08-08 16:19:46 -070020 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer
21 * guide.</p>
Joe Fernandezb54e7a32011-10-03 15:09:50 -070022 * </div>
Dianne Hackborn2dedce62010-04-15 14:45:25 -070023 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070024public abstract class FragmentTransaction {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070025 /**
26 * Calls {@link #add(int, Fragment, String)} with a 0 containerViewId.
27 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070028 public abstract FragmentTransaction add(Fragment fragment, String tag);
Hemal Patela8e1c752016-08-08 16:19:46 -070029
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070030 /**
31 * Calls {@link #add(int, Fragment, String)} with a null tag.
32 */
George Mount27b3ae62015-10-16 12:36:58 -070033 public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment);
Hemal Patela8e1c752016-08-08 16:19:46 -070034
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070035 /**
36 * Add a fragment to the activity state. This fragment may optionally
37 * also have its view (if {@link Fragment#onCreateView Fragment.onCreateView}
Mark Dolinerd0646dc2014-08-27 16:04:02 -070038 * returns non-null) inserted into a container view of the activity.
Hemal Patela8e1c752016-08-08 16:19:46 -070039 *
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070040 * @param containerViewId Optional identifier of the container this fragment is
41 * to be placed in. If 0, it will not be placed in a container.
42 * @param fragment The fragment to be added. This fragment must not already
43 * be added to the activity.
44 * @param tag Optional tag name for the fragment, to later retrieve the
Dianne Hackborn247fe742011-01-08 17:25:57 -080045 * fragment with {@link FragmentManager#findFragmentByTag(String)
46 * FragmentManager.findFragmentByTag(String)}.
Hemal Patela8e1c752016-08-08 16:19:46 -070047 *
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070048 * @return Returns the same FragmentTransaction instance.
49 */
George Mount27b3ae62015-10-16 12:36:58 -070050 public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment,
51 String tag);
Hemal Patela8e1c752016-08-08 16:19:46 -070052
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070053 /**
54 * Calls {@link #replace(int, Fragment, String)} with a null tag.
55 */
George Mount27b3ae62015-10-16 12:36:58 -070056 public abstract FragmentTransaction replace(@IdRes int containerViewId, Fragment fragment);
Hemal Patela8e1c752016-08-08 16:19:46 -070057
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070058 /**
59 * Replace an existing fragment that was added to a container. This is
60 * essentially the same as calling {@link #remove(Fragment)} for all
61 * currently added fragments that were added with the same containerViewId
62 * and then {@link #add(int, Fragment, String)} with the same arguments
63 * given here.
Hemal Patela8e1c752016-08-08 16:19:46 -070064 *
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070065 * @param containerViewId Identifier of the container whose fragment(s) are
66 * to be replaced.
67 * @param fragment The new fragment to place in the container.
68 * @param tag Optional tag name for the fragment, to later retrieve the
Dianne Hackborn247fe742011-01-08 17:25:57 -080069 * fragment with {@link FragmentManager#findFragmentByTag(String)
70 * FragmentManager.findFragmentByTag(String)}.
Hemal Patela8e1c752016-08-08 16:19:46 -070071 *
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070072 * @return Returns the same FragmentTransaction instance.
73 */
George Mount27b3ae62015-10-16 12:36:58 -070074 public abstract FragmentTransaction replace(@IdRes int containerViewId, Fragment fragment,
75 String tag);
Hemal Patela8e1c752016-08-08 16:19:46 -070076
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070077 /**
78 * Remove an existing fragment. If it was added to a container, its view
79 * is also removed from that container.
Hemal Patela8e1c752016-08-08 16:19:46 -070080 *
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070081 * @param fragment The fragment to be removed.
Hemal Patela8e1c752016-08-08 16:19:46 -070082 *
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070083 * @return Returns the same FragmentTransaction instance.
84 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070085 public abstract FragmentTransaction remove(Fragment fragment);
Hemal Patela8e1c752016-08-08 16:19:46 -070086
Dianne Hackbornf121be72010-05-06 14:10:32 -070087 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070088 * Hides an existing fragment. This is only relevant for fragments whose
89 * views have been added to a container, as this will cause the view to
90 * be hidden.
Hemal Patela8e1c752016-08-08 16:19:46 -070091 *
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070092 * @param fragment The fragment to be hidden.
Hemal Patela8e1c752016-08-08 16:19:46 -070093 *
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070094 * @return Returns the same FragmentTransaction instance.
95 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070096 public abstract FragmentTransaction hide(Fragment fragment);
Hemal Patela8e1c752016-08-08 16:19:46 -070097
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070098 /**
Jim Shumabaa15532010-11-09 20:37:03 -080099 * Shows a previously hidden fragment. This is only relevant for fragments whose
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700100 * views have been added to a container, as this will cause the view to
101 * be shown.
Hemal Patela8e1c752016-08-08 16:19:46 -0700102 *
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700103 * @param fragment The fragment to be shown.
Hemal Patela8e1c752016-08-08 16:19:46 -0700104 *
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700105 * @return Returns the same FragmentTransaction instance.
106 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700107 public abstract FragmentTransaction show(Fragment fragment);
Adam Powell2b6230e2010-09-07 17:55:25 -0700108
109 /**
Dianne Hackborn47c41562011-04-15 19:00:20 -0700110 * Detach the given fragment from the UI. This is the same state as
111 * when it is put on the back stack: the fragment is removed from
112 * the UI, however its state is still being actively managed by the
113 * fragment manager. When going into this state its view hierarchy
114 * is destroyed.
115 *
116 * @param fragment The fragment to be detached.
117 *
118 * @return Returns the same FragmentTransaction instance.
119 */
120 public abstract FragmentTransaction detach(Fragment fragment);
121
122 /**
Paul Quei355297c2014-03-14 15:54:15 +0800123 * Re-attach a fragment after it had previously been detached from
Dianne Hackborn47c41562011-04-15 19:00:20 -0700124 * the UI with {@link #detach(Fragment)}. This
125 * causes its view hierarchy to be re-created, attached to the UI,
126 * and displayed.
127 *
128 * @param fragment The fragment to be attached.
129 *
130 * @return Returns the same FragmentTransaction instance.
131 */
132 public abstract FragmentTransaction attach(Fragment fragment);
133
134 /**
Adam Powell2b6230e2010-09-07 17:55:25 -0700135 * @return <code>true</code> if this transaction contains no operations,
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700136 * <code>false</code> otherwise.
Adam Powell2b6230e2010-09-07 17:55:25 -0700137 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700138 public abstract boolean isEmpty();
Hemal Patela8e1c752016-08-08 16:19:46 -0700139
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700140 /**
141 * Bit mask that is set for all enter transitions.
Dianne Hackbornf121be72010-05-06 14:10:32 -0700142 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700143 public static final int TRANSIT_ENTER_MASK = 0x1000;
Hemal Patela8e1c752016-08-08 16:19:46 -0700144
Dianne Hackbornf121be72010-05-06 14:10:32 -0700145 /**
146 * Bit mask that is set for all exit transitions.
147 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700148 public static final int TRANSIT_EXIT_MASK = 0x2000;
Hemal Patela8e1c752016-08-08 16:19:46 -0700149
Dianne Hackbornf121be72010-05-06 14:10:32 -0700150 /** Not set up for a transition. */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700151 public static final int TRANSIT_UNSET = -1;
Dianne Hackbornf121be72010-05-06 14:10:32 -0700152 /** No animation for transition. */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700153 public static final int TRANSIT_NONE = 0;
Chet Haase9ff82bf2010-10-05 14:30:51 -0700154 /** Fragment is being added onto the stack */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700155 public static final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK;
Chet Haase9ff82bf2010-10-05 14:30:51 -0700156 /** Fragment is being removed from the stack */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700157 public static final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK;
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800158 /** Fragment should simply fade in or out; that is, no strong navigation associated
159 * with it except that it is appearing or disappearing for some reason. */
160 public static final int TRANSIT_FRAGMENT_FADE = 3 | TRANSIT_ENTER_MASK;
Chet Haase811ed1062010-08-06 10:38:15 -0700161
George Mount27b3ae62015-10-16 12:36:58 -0700162 /** @hide */
163 @IntDef({TRANSIT_NONE, TRANSIT_FRAGMENT_OPEN, TRANSIT_FRAGMENT_CLOSE, TRANSIT_FRAGMENT_FADE})
164 @Retention(RetentionPolicy.SOURCE)
165 public @interface Transit {}
166
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700167 /**
168 * Set specific animation resources to run for the fragments that are
Chet Haasebc377842011-03-22 11:35:22 -0700169 * entering and exiting in this transaction. These animations will not be
170 * played when popping the back stack.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700171 */
George Mount27b3ae62015-10-16 12:36:58 -0700172 public abstract FragmentTransaction setCustomAnimations(@AnimatorRes int enter,
173 @AnimatorRes int exit);
Chet Haasebc377842011-03-22 11:35:22 -0700174
175 /**
176 * Set specific animation resources to run for the fragments that are
177 * entering and exiting in this transaction. The <code>popEnter</code>
178 * and <code>popExit</code> animations will be played for enter/exit
179 * operations specifically when popping the back stack.
180 */
George Mount27b3ae62015-10-16 12:36:58 -0700181 public abstract FragmentTransaction setCustomAnimations(@AnimatorRes int enter,
182 @AnimatorRes int exit, @AnimatorRes int popEnter, @AnimatorRes int popExit);
Chet Haasebc377842011-03-22 11:35:22 -0700183
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700184 /**
185 * Select a standard transition animation for this transaction. May be
186 * one of {@link #TRANSIT_NONE}, {@link #TRANSIT_FRAGMENT_OPEN},
George Mount27b3ae62015-10-16 12:36:58 -0700187 * {@link #TRANSIT_FRAGMENT_CLOSE}, or {@link #TRANSIT_FRAGMENT_FADE}.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700188 */
George Mount27b3ae62015-10-16 12:36:58 -0700189 public abstract FragmentTransaction setTransition(@Transit int transit);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700190
191 /**
George Mountc03da0e2014-08-22 17:04:02 -0700192 * Used with to map a View from a removed or hidden Fragment to a View from a shown
193 * or added Fragment.
George Mountd4c3c912014-06-09 12:31:34 -0700194 * @param sharedElement A View in a disappearing Fragment to match with a View in an
195 * appearing Fragment.
George Mount0a2ae002014-06-23 14:57:27 +0000196 * @param name The transitionName for a View in an appearing Fragment to match to the shared
George Mountd4c3c912014-06-09 12:31:34 -0700197 * element.
198 */
George Mount448bace2014-08-18 16:27:43 -0700199 public abstract FragmentTransaction addSharedElement(View sharedElement, String name);
200
201 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700202 * Set a custom style resource that will be used for resolving transit
203 * animations.
204 */
George Mount27b3ae62015-10-16 12:36:58 -0700205 public abstract FragmentTransaction setTransitionStyle(@StyleRes int styleRes);
Hemal Patela8e1c752016-08-08 16:19:46 -0700206
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700207 /**
208 * Add this transaction to the back stack. This means that the transaction
209 * will be remembered after it is committed, and will reverse its operation
210 * when later popped off the stack.
211 *
212 * @param name An optional name for this back stack state, or null.
213 */
George Mount27b3ae62015-10-16 12:36:58 -0700214 public abstract FragmentTransaction addToBackStack(@Nullable String name);
Dianne Hackborndd913a52010-07-22 12:17:04 -0700215
216 /**
Adam Powell0c24a552010-11-03 16:44:11 -0700217 * Returns true if this FragmentTransaction is allowed to be added to the back
218 * stack. If this method would return false, {@link #addToBackStack(String)}
219 * will throw {@link IllegalStateException}.
220 *
221 * @return True if {@link #addToBackStack(String)} is permitted on this transaction.
222 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700223 public abstract boolean isAddToBackStackAllowed();
Adam Powell0c24a552010-11-03 16:44:11 -0700224
225 /**
226 * Disallow calls to {@link #addToBackStack(String)}. Any future calls to
227 * addToBackStack will throw {@link IllegalStateException}. If addToBackStack
228 * has already been called, this method will throw IllegalStateException.
229 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700230 public abstract FragmentTransaction disallowAddToBackStack();
Adam Powell0c24a552010-11-03 16:44:11 -0700231
232 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700233 * Set the full title to show as a bread crumb when this transaction
234 * is on the back stack, as used by {@link FragmentBreadCrumbs}.
235 *
236 * @param res A string resource containing the title.
237 */
George Mount27b3ae62015-10-16 12:36:58 -0700238 public abstract FragmentTransaction setBreadCrumbTitle(@StringRes int res);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700239
240 /**
241 * Like {@link #setBreadCrumbTitle(int)} but taking a raw string; this
242 * method is <em>not</em> recommended, as the string can not be changed
243 * later if the locale changes.
244 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700245 public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700246
247 /**
248 * Set the short title to show as a bread crumb when this transaction
249 * is on the back stack, as used by {@link FragmentBreadCrumbs}.
250 *
251 * @param res A string resource containing the title.
252 */
George Mount27b3ae62015-10-16 12:36:58 -0700253 public abstract FragmentTransaction setBreadCrumbShortTitle(@StringRes int res);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700254
255 /**
256 * Like {@link #setBreadCrumbShortTitle(int)} but taking a raw string; this
257 * method is <em>not</em> recommended, as the string can not be changed
258 * later if the locale changes.
259 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700260 public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700261
262 /**
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700263 * Schedules a commit of this transaction. The commit does
Dianne Hackborndd913a52010-07-22 12:17:04 -0700264 * not happen immediately; it will be scheduled as work on the main thread
265 * to be done the next time that thread is ready.
266 *
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700267 * <p class="note">A transaction can only be committed with this method
268 * prior to its containing activity saving its state. If the commit is
269 * attempted after that point, an exception will be thrown. This is
270 * because the state after the commit can be lost if the activity needs to
271 * be restored from its state. See {@link #commitAllowingStateLoss()} for
272 * situations where it may be okay to lose the commit.</p>
Hemal Patela8e1c752016-08-08 16:19:46 -0700273 *
Dianne Hackborndd913a52010-07-22 12:17:04 -0700274 * @return Returns the identifier of this transaction's back stack entry,
275 * if {@link #addToBackStack(String)} had been called. Otherwise, returns
276 * a negative number.
277 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700278 public abstract int commit();
279
280 /**
281 * Like {@link #commit} but allows the commit to be executed after an
282 * activity's state is saved. This is dangerous because the commit can
283 * be lost if the activity needs to later be restored from its state, so
284 * this should only be used for cases where it is okay for the UI state
285 * to change unexpectedly on the user.
286 */
287 public abstract int commitAllowingStateLoss();
Adam Powell8585ed62016-02-04 15:38:20 -0800288
289 /**
290 * Commits this transaction synchronously. Any added fragments will be
291 * initialized and brought completely to the lifecycle state of their host
292 * and any removed fragments will be torn down accordingly before this
293 * call returns. Committing a transaction in this way allows fragments
294 * to be added as dedicated, encapsulated components that monitor the
295 * lifecycle state of their host while providing firmer ordering guarantees
296 * around when those fragments are fully initialized and ready. Fragments
297 * that manage views will have those views created and attached.
298 *
299 * <p>Calling <code>commitNow</code> is preferable to calling
300 * {@link #commit()} followed by {@link FragmentManager#executePendingTransactions()}
301 * as the latter will have the side effect of attempting to commit <em>all</em>
302 * currently pending transactions whether that is the desired behavior
303 * or not.</p>
304 *
305 * <p>Transactions committed in this way may not be added to the
306 * FragmentManager's back stack, as doing so would break other expected
307 * ordering guarantees for other asynchronously committed transactions.
308 * This method will throw {@link IllegalStateException} if the transaction
309 * previously requested to be added to the back stack with
310 * {@link #addToBackStack(String)}.</p>
311 *
312 * <p class="note">A transaction can only be committed with this method
313 * prior to its containing activity saving its state. If the commit is
314 * attempted after that point, an exception will be thrown. This is
315 * because the state after the commit can be lost if the activity needs to
316 * be restored from its state. See {@link #commitAllowingStateLoss()} for
317 * situations where it may be okay to lose the commit.</p>
318 */
319 public abstract void commitNow();
320
321 /**
322 * Like {@link #commitNow} but allows the commit to be executed after an
323 * activity's state is saved. This is dangerous because the commit can
324 * be lost if the activity needs to later be restored from its state, so
325 * this should only be used for cases where it is okay for the UI state
326 * to change unexpectedly on the user.
327 */
328 public abstract void commitNowAllowingStateLoss();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700329}