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.view.View; 10 11 import java.lang.annotation.Retention; 12 import java.lang.annotation.RetentionPolicy; 13 14 /** 15 * API for performing a set of Fragment operations. 16 * 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/components/fragments.html">Fragments</a> developer 21 * guide.</p> 22 * </div> 23 */ 24 public abstract class FragmentTransaction { 25 /** 26 * Calls {@link #add(int, Fragment, String)} with a 0 containerViewId. 27 */ add(Fragment fragment, String tag)28 public abstract FragmentTransaction add(Fragment fragment, String tag); 29 30 /** 31 * Calls {@link #add(int, Fragment, String)} with a null tag. 32 */ add(@dRes int containerViewId, Fragment fragment)33 public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment); 34 35 /** 36 * Add a fragment to the activity state. This fragment may optionally 37 * also have its view (if {@link Fragment#onCreateView Fragment.onCreateView} 38 * returns non-null) inserted into a container view of the activity. 39 * 40 * @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 45 * fragment with {@link FragmentManager#findFragmentByTag(String) 46 * FragmentManager.findFragmentByTag(String)}. 47 * 48 * @return Returns the same FragmentTransaction instance. 49 */ add(@dRes int containerViewId, Fragment fragment, String tag)50 public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment, 51 String tag); 52 53 /** 54 * Calls {@link #replace(int, Fragment, String)} with a null tag. 55 */ replace(@dRes int containerViewId, Fragment fragment)56 public abstract FragmentTransaction replace(@IdRes int containerViewId, Fragment fragment); 57 58 /** 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. 64 * 65 * @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 69 * fragment with {@link FragmentManager#findFragmentByTag(String) 70 * FragmentManager.findFragmentByTag(String)}. 71 * 72 * @return Returns the same FragmentTransaction instance. 73 */ replace(@dRes int containerViewId, Fragment fragment, String tag)74 public abstract FragmentTransaction replace(@IdRes int containerViewId, Fragment fragment, 75 String tag); 76 77 /** 78 * Remove an existing fragment. If it was added to a container, its view 79 * is also removed from that container. 80 * 81 * @param fragment The fragment to be removed. 82 * 83 * @return Returns the same FragmentTransaction instance. 84 */ remove(Fragment fragment)85 public abstract FragmentTransaction remove(Fragment fragment); 86 87 /** 88 * 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. 91 * 92 * @param fragment The fragment to be hidden. 93 * 94 * @return Returns the same FragmentTransaction instance. 95 */ hide(Fragment fragment)96 public abstract FragmentTransaction hide(Fragment fragment); 97 98 /** 99 * Shows a previously hidden fragment. This is only relevant for fragments whose 100 * views have been added to a container, as this will cause the view to 101 * be shown. 102 * 103 * @param fragment The fragment to be shown. 104 * 105 * @return Returns the same FragmentTransaction instance. 106 */ show(Fragment fragment)107 public abstract FragmentTransaction show(Fragment fragment); 108 109 /** 110 * 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 */ detach(Fragment fragment)120 public abstract FragmentTransaction detach(Fragment fragment); 121 122 /** 123 * Re-attach a fragment after it had previously been detached from 124 * 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 */ attach(Fragment fragment)132 public abstract FragmentTransaction attach(Fragment fragment); 133 134 /** 135 * @return <code>true</code> if this transaction contains no operations, 136 * <code>false</code> otherwise. 137 */ isEmpty()138 public abstract boolean isEmpty(); 139 140 /** 141 * Bit mask that is set for all enter transitions. 142 */ 143 public static final int TRANSIT_ENTER_MASK = 0x1000; 144 145 /** 146 * Bit mask that is set for all exit transitions. 147 */ 148 public static final int TRANSIT_EXIT_MASK = 0x2000; 149 150 /** Not set up for a transition. */ 151 public static final int TRANSIT_UNSET = -1; 152 /** No animation for transition. */ 153 public static final int TRANSIT_NONE = 0; 154 /** Fragment is being added onto the stack */ 155 public static final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK; 156 /** Fragment is being removed from the stack */ 157 public static final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK; 158 /** 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; 161 162 /** @hide */ 163 @IntDef({TRANSIT_NONE, TRANSIT_FRAGMENT_OPEN, TRANSIT_FRAGMENT_CLOSE, TRANSIT_FRAGMENT_FADE}) 164 @Retention(RetentionPolicy.SOURCE) 165 public @interface Transit {} 166 167 /** 168 * Set specific animation resources to run for the fragments that are 169 * entering and exiting in this transaction. These animations will not be 170 * played when popping the back stack. 171 */ setCustomAnimations(@nimatorRes int enter, @AnimatorRes int exit)172 public abstract FragmentTransaction setCustomAnimations(@AnimatorRes int enter, 173 @AnimatorRes int exit); 174 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 */ setCustomAnimations(@nimatorRes int enter, @AnimatorRes int exit, @AnimatorRes int popEnter, @AnimatorRes int popExit)181 public abstract FragmentTransaction setCustomAnimations(@AnimatorRes int enter, 182 @AnimatorRes int exit, @AnimatorRes int popEnter, @AnimatorRes int popExit); 183 184 /** 185 * Select a standard transition animation for this transaction. May be 186 * one of {@link #TRANSIT_NONE}, {@link #TRANSIT_FRAGMENT_OPEN}, 187 * {@link #TRANSIT_FRAGMENT_CLOSE}, or {@link #TRANSIT_FRAGMENT_FADE}. 188 */ setTransition(@ransit int transit)189 public abstract FragmentTransaction setTransition(@Transit int transit); 190 191 /** 192 * Used with to map a View from a removed or hidden Fragment to a View from a shown 193 * or added Fragment. 194 * @param sharedElement A View in a disappearing Fragment to match with a View in an 195 * appearing Fragment. 196 * @param name The transitionName for a View in an appearing Fragment to match to the shared 197 * element. 198 */ addSharedElement(View sharedElement, String name)199 public abstract FragmentTransaction addSharedElement(View sharedElement, String name); 200 201 /** 202 * Set a custom style resource that will be used for resolving transit 203 * animations. 204 */ setTransitionStyle(@tyleRes int styleRes)205 public abstract FragmentTransaction setTransitionStyle(@StyleRes int styleRes); 206 207 /** 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 */ addToBackStack(@ullable String name)214 public abstract FragmentTransaction addToBackStack(@Nullable String name); 215 216 /** 217 * 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 */ isAddToBackStackAllowed()223 public abstract boolean isAddToBackStackAllowed(); 224 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 */ disallowAddToBackStack()230 public abstract FragmentTransaction disallowAddToBackStack(); 231 232 /** 233 * 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 */ setBreadCrumbTitle(@tringRes int res)238 public abstract FragmentTransaction setBreadCrumbTitle(@StringRes int res); 239 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 */ setBreadCrumbTitle(CharSequence text)245 public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text); 246 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 */ setBreadCrumbShortTitle(@tringRes int res)253 public abstract FragmentTransaction setBreadCrumbShortTitle(@StringRes int res); 254 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 */ setBreadCrumbShortTitle(CharSequence text)260 public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text); 261 262 /** 263 * Schedules a commit of this transaction. The commit does 264 * 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 * 267 * <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> 273 * 274 * @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 */ commit()278 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 */ commitAllowingStateLoss()287 public abstract int commitAllowingStateLoss(); 288 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 */ commitNow()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 */ commitNowAllowingStateLoss()328 public abstract void commitNowAllowingStateLoss(); 329 } 330