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 */ 19 public abstract FragmentTransaction add(Fragment fragment, String tag); 20 21 /** 22 * Calls {@link #add(int, Fragment, String)} with a null tag. 23 */ 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) inserted 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 172 public abstract FragmentTransaction setTransition(int transit); 173 174 /** 175 * Used with to map a View from a removed or hidden Fragment to a View from a shown 176 * or added Fragment. 177 * @param sharedElement A View in a disappearing Fragment to match with a View in an 178 * appearing Fragment. 179 * @param name The transitionName for a View in an appearing Fragment to match to the shared 180 * element. 181 */ 182 public abstract FragmentTransaction addSharedElement(View sharedElement, String name); 183 184 /** 185 * Set a custom style resource that will be used for resolving transit 186 * animations. 187 */ 188 public abstract FragmentTransaction setTransitionStyle(int styleRes); 189 190 /** 191 * Add this transaction to the back stack. This means that the transaction 192 * will be remembered after it is committed, and will reverse its operation 193 * when later popped off the stack. 194 * 195 * @param name An optional name for this back stack state, or null. 196 */ 197 public abstract FragmentTransaction addToBackStack(String name); 198 199 /** 200 * Returns true if this FragmentTransaction is allowed to be added to the back 201 * stack. If this method would return false, {@link #addToBackStack(String)} 202 * will throw {@link IllegalStateException}. 203 * 204 * @return True if {@link #addToBackStack(String)} is permitted on this transaction. 205 */ 206 public abstract boolean isAddToBackStackAllowed(); 207 208 /** 209 * Disallow calls to {@link #addToBackStack(String)}. Any future calls to 210 * addToBackStack will throw {@link IllegalStateException}. If addToBackStack 211 * has already been called, this method will throw IllegalStateException. 212 */ 213 public abstract FragmentTransaction disallowAddToBackStack(); 214 215 /** 216 * Set the full title to show as a bread crumb when this transaction 217 * is on the back stack, as used by {@link FragmentBreadCrumbs}. 218 * 219 * @param res A string resource containing the title. 220 */ 221 public abstract FragmentTransaction setBreadCrumbTitle(int res); 222 223 /** 224 * Like {@link #setBreadCrumbTitle(int)} but taking a raw string; this 225 * method is <em>not</em> recommended, as the string can not be changed 226 * later if the locale changes. 227 */ 228 public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text); 229 230 /** 231 * Set the short title to show as a bread crumb when this transaction 232 * is on the back stack, as used by {@link FragmentBreadCrumbs}. 233 * 234 * @param res A string resource containing the title. 235 */ 236 public abstract FragmentTransaction setBreadCrumbShortTitle(int res); 237 238 /** 239 * Like {@link #setBreadCrumbShortTitle(int)} but taking a raw string; this 240 * method is <em>not</em> recommended, as the string can not be changed 241 * later if the locale changes. 242 */ 243 public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text); 244 245 /** 246 * Schedules a commit of this transaction. The commit does 247 * not happen immediately; it will be scheduled as work on the main thread 248 * to be done the next time that thread is ready. 249 * 250 * <p class="note">A transaction can only be committed with this method 251 * prior to its containing activity saving its state. If the commit is 252 * attempted after that point, an exception will be thrown. This is 253 * because the state after the commit can be lost if the activity needs to 254 * be restored from its state. See {@link #commitAllowingStateLoss()} for 255 * situations where it may be okay to lose the commit.</p> 256 * 257 * @return Returns the identifier of this transaction's back stack entry, 258 * if {@link #addToBackStack(String)} had been called. Otherwise, returns 259 * a negative number. 260 */ 261 public abstract int commit(); 262 263 /** 264 * Like {@link #commit} but allows the commit to be executed after an 265 * activity's state is saved. This is dangerous because the commit can 266 * be lost if the activity needs to later be restored from its state, so 267 * this should only be used for cases where it is okay for the UI state 268 * to change unexpectedly on the user. 269 */ 270 public abstract int commitAllowingStateLoss(); 271 } 272