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