1 package android.app; 2 3 import android.util.Pair; 4 import android.view.View; 5 6 /** 7 * API for performing a set of Fragment operations. 8 * 9 * <div class="special reference"> 10 * <h3>Developer Guides</h3> 11 * <p>For more information about using fragments, read the 12 * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p> 13 * </div> 14 */ 15 public abstract class FragmentTransaction { 16 /** 17 * Calls {@link #add(int, Fragment, String)} with a 0 containerViewId. 18 */ add(Fragment fragment, String tag)19 public abstract FragmentTransaction add(Fragment fragment, String tag); 20 21 /** 22 * Calls {@link #add(int, Fragment, String)} with a null tag. 23 */ add(int containerViewId, Fragment fragment)24 public abstract FragmentTransaction add(int containerViewId, Fragment fragment); 25 26 /** 27 * Add a fragment to the activity state. This fragment may optionally 28 * also have its view (if {@link Fragment#onCreateView Fragment.onCreateView} 29 * returns non-null) into a container view of the activity. 30 * 31 * @param containerViewId Optional identifier of the container this fragment is 32 * to be placed in. If 0, it will not be placed in a container. 33 * @param fragment The fragment to be added. This fragment must not already 34 * be added to the activity. 35 * @param tag Optional tag name for the fragment, to later retrieve the 36 * fragment with {@link FragmentManager#findFragmentByTag(String) 37 * FragmentManager.findFragmentByTag(String)}. 38 * 39 * @return Returns the same FragmentTransaction instance. 40 */ add(int containerViewId, Fragment fragment, String tag)41 public abstract FragmentTransaction add(int containerViewId, Fragment fragment, String tag); 42 43 /** 44 * Calls {@link #replace(int, Fragment, String)} with a null tag. 45 */ replace(int containerViewId, Fragment fragment)46 public abstract FragmentTransaction replace(int containerViewId, Fragment fragment); 47 48 /** 49 * Replace an existing fragment that was added to a container. This is 50 * essentially the same as calling {@link #remove(Fragment)} for all 51 * currently added fragments that were added with the same containerViewId 52 * and then {@link #add(int, Fragment, String)} with the same arguments 53 * given here. 54 * 55 * @param containerViewId Identifier of the container whose fragment(s) are 56 * to be replaced. 57 * @param fragment The new fragment to place in the container. 58 * @param tag Optional tag name for the fragment, to later retrieve the 59 * fragment with {@link FragmentManager#findFragmentByTag(String) 60 * FragmentManager.findFragmentByTag(String)}. 61 * 62 * @return Returns the same FragmentTransaction instance. 63 */ replace(int containerViewId, Fragment fragment, String tag)64 public abstract FragmentTransaction replace(int containerViewId, Fragment fragment, String tag); 65 66 /** 67 * Remove an existing fragment. If it was added to a container, its view 68 * is also removed from that container. 69 * 70 * @param fragment The fragment to be removed. 71 * 72 * @return Returns the same FragmentTransaction instance. 73 */ remove(Fragment fragment)74 public abstract FragmentTransaction remove(Fragment fragment); 75 76 /** 77 * Hides an existing fragment. This is only relevant for fragments whose 78 * views have been added to a container, as this will cause the view to 79 * be hidden. 80 * 81 * @param fragment The fragment to be hidden. 82 * 83 * @return Returns the same FragmentTransaction instance. 84 */ hide(Fragment fragment)85 public abstract FragmentTransaction hide(Fragment fragment); 86 87 /** 88 * Shows a previously hidden 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 shown. 91 * 92 * @param fragment The fragment to be shown. 93 * 94 * @return Returns the same FragmentTransaction instance. 95 */ show(Fragment fragment)96 public abstract FragmentTransaction show(Fragment fragment); 97 98 /** 99 * Detach the given fragment from the UI. This is the same state as 100 * when it is put on the back stack: the fragment is removed from 101 * the UI, however its state is still being actively managed by the 102 * fragment manager. When going into this state its view hierarchy 103 * is destroyed. 104 * 105 * @param fragment The fragment to be detached. 106 * 107 * @return Returns the same FragmentTransaction instance. 108 */ detach(Fragment fragment)109 public abstract FragmentTransaction detach(Fragment fragment); 110 111 /** 112 * Re-attach a fragment after it had previously been detached from 113 * the UI with {@link #detach(Fragment)}. This 114 * causes its view hierarchy to be re-created, attached to the UI, 115 * and displayed. 116 * 117 * @param fragment The fragment to be attached. 118 * 119 * @return Returns the same FragmentTransaction instance. 120 */ attach(Fragment fragment)121 public abstract FragmentTransaction attach(Fragment fragment); 122 123 /** 124 * @return <code>true</code> if this transaction contains no operations, 125 * <code>false</code> otherwise. 126 */ isEmpty()127 public abstract boolean isEmpty(); 128 129 /** 130 * Bit mask that is set for all enter transitions. 131 */ 132 public static final int TRANSIT_ENTER_MASK = 0x1000; 133 134 /** 135 * Bit mask that is set for all exit transitions. 136 */ 137 public static final int TRANSIT_EXIT_MASK = 0x2000; 138 139 /** Not set up for a transition. */ 140 public static final int TRANSIT_UNSET = -1; 141 /** No animation for transition. */ 142 public static final int TRANSIT_NONE = 0; 143 /** Fragment is being added onto the stack */ 144 public static final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK; 145 /** Fragment is being removed from the stack */ 146 public static final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK; 147 /** Fragment should simply fade in or out; that is, no strong navigation associated 148 * with it except that it is appearing or disappearing for some reason. */ 149 public static final int TRANSIT_FRAGMENT_FADE = 3 | TRANSIT_ENTER_MASK; 150 151 /** 152 * Set specific animation resources to run for the fragments that are 153 * entering and exiting in this transaction. These animations will not be 154 * played when popping the back stack. 155 */ setCustomAnimations(int enter, int exit)156 public abstract FragmentTransaction setCustomAnimations(int enter, int exit); 157 158 /** 159 * Set specific animation resources to run for the fragments that are 160 * entering and exiting in this transaction. The <code>popEnter</code> 161 * and <code>popExit</code> animations will be played for enter/exit 162 * operations specifically when popping the back stack. 163 */ setCustomAnimations(int enter, int exit, int popEnter, int popExit)164 public abstract FragmentTransaction setCustomAnimations(int enter, int exit, 165 int popEnter, int popExit); 166 167 /** 168 * Select a standard transition animation for this transaction. May be 169 * one of {@link #TRANSIT_NONE}, {@link #TRANSIT_FRAGMENT_OPEN}, 170 * or {@link #TRANSIT_FRAGMENT_CLOSE} 171 */ setTransition(int transit)172 public abstract FragmentTransaction setTransition(int transit); 173 174 /** 175 * TODO: remove from API 176 * @hide 177 */ setCustomTransition(int sceneRootId, int transitionId)178 public FragmentTransaction setCustomTransition(int sceneRootId, int transitionId) { 179 return this; 180 } 181 182 /** 183 * Used with to map a View from a removed or hidden Fragment to a View from a shown 184 * or added Fragment. 185 * @param sharedElement A View in a disappearing Fragment to match with a View in an 186 * appearing Fragment. 187 * @param name The transitionName for a View in an appearing Fragment to match to the shared 188 * element. 189 */ addSharedElement(View sharedElement, String name)190 public abstract FragmentTransaction addSharedElement(View sharedElement, String name); 191 192 /** 193 * TODO: remove from API 194 * @hide 195 */ setSharedElement(View sharedElement, String name)196 public abstract FragmentTransaction setSharedElement(View sharedElement, String name); 197 198 /** 199 * TODO: remove from API 200 * @hide 201 */ setSharedElements(Pair<View, String>.... sharedElements)202 public abstract FragmentTransaction setSharedElements(Pair<View, String>... sharedElements); 203 204 /** 205 * Set a custom style resource that will be used for resolving transit 206 * animations. 207 */ setTransitionStyle(int styleRes)208 public abstract FragmentTransaction setTransitionStyle(int styleRes); 209 210 /** 211 * Add this transaction to the back stack. This means that the transaction 212 * will be remembered after it is committed, and will reverse its operation 213 * when later popped off the stack. 214 * 215 * @param name An optional name for this back stack state, or null. 216 */ addToBackStack(String name)217 public abstract FragmentTransaction addToBackStack(String name); 218 219 /** 220 * Returns true if this FragmentTransaction is allowed to be added to the back 221 * stack. If this method would return false, {@link #addToBackStack(String)} 222 * will throw {@link IllegalStateException}. 223 * 224 * @return True if {@link #addToBackStack(String)} is permitted on this transaction. 225 */ isAddToBackStackAllowed()226 public abstract boolean isAddToBackStackAllowed(); 227 228 /** 229 * Disallow calls to {@link #addToBackStack(String)}. Any future calls to 230 * addToBackStack will throw {@link IllegalStateException}. If addToBackStack 231 * has already been called, this method will throw IllegalStateException. 232 */ disallowAddToBackStack()233 public abstract FragmentTransaction disallowAddToBackStack(); 234 235 /** 236 * Set the full title to show as a bread crumb when this transaction 237 * is on the back stack, as used by {@link FragmentBreadCrumbs}. 238 * 239 * @param res A string resource containing the title. 240 */ setBreadCrumbTitle(int res)241 public abstract FragmentTransaction setBreadCrumbTitle(int res); 242 243 /** 244 * Like {@link #setBreadCrumbTitle(int)} but taking a raw string; this 245 * method is <em>not</em> recommended, as the string can not be changed 246 * later if the locale changes. 247 */ setBreadCrumbTitle(CharSequence text)248 public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text); 249 250 /** 251 * Set the short title to show as a bread crumb when this transaction 252 * is on the back stack, as used by {@link FragmentBreadCrumbs}. 253 * 254 * @param res A string resource containing the title. 255 */ setBreadCrumbShortTitle(int res)256 public abstract FragmentTransaction setBreadCrumbShortTitle(int res); 257 258 /** 259 * Like {@link #setBreadCrumbShortTitle(int)} but taking a raw string; this 260 * method is <em>not</em> recommended, as the string can not be changed 261 * later if the locale changes. 262 */ setBreadCrumbShortTitle(CharSequence text)263 public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text); 264 265 /** 266 * Schedules a commit of this transaction. The commit does 267 * not happen immediately; it will be scheduled as work on the main thread 268 * to be done the next time that thread is ready. 269 * 270 * <p class="note">A transaction can only be committed with this method 271 * prior to its containing activity saving its state. If the commit is 272 * attempted after that point, an exception will be thrown. This is 273 * because the state after the commit can be lost if the activity needs to 274 * be restored from its state. See {@link #commitAllowingStateLoss()} for 275 * situations where it may be okay to lose the commit.</p> 276 * 277 * @return Returns the identifier of this transaction's back stack entry, 278 * if {@link #addToBackStack(String)} had been called. Otherwise, returns 279 * a negative number. 280 */ commit()281 public abstract int commit(); 282 283 /** 284 * Like {@link #commit} but allows the commit to be executed after an 285 * activity's state is saved. This is dangerous because the commit can 286 * be lost if the activity needs to later be restored from its state, so 287 * this should only be used for cases where it is okay for the UI state 288 * to change unexpectedly on the user. 289 */ commitAllowingStateLoss()290 public abstract int commitAllowingStateLoss(); 291 } 292