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