FragmentTransaction.java revision eedc67283a5a49dce86c625e54596dfdea9465a7
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v4.app; 18 19/** 20 * Static library support version of the framework's {@link android.app.FragmentTransaction}. 21 * Used to write apps that run on platforms prior to Android 3.0. When running 22 * on Android 3.0 or above, this implementation is still used; it does not try 23 * to switch to the framework's implementation. See the framework SDK 24 * documentation for a class overview. 25 */ 26public abstract class FragmentTransaction { 27 /** 28 * Calls {@link #add(int, Fragment, String)} with a 0 containerViewId. 29 */ 30 public abstract FragmentTransaction add(Fragment fragment, String tag); 31 32 /** 33 * Calls {@link #add(int, Fragment, String)} with a null tag. 34 */ 35 public abstract FragmentTransaction add(int containerViewId, Fragment fragment); 36 37 /** 38 * Add a fragment to the activity state. This fragment may optionally 39 * also have its view (if {@link Fragment#onCreateView Fragment.onCreateView} 40 * returns non-null) into a container view of the activity. 41 * 42 * @param containerViewId Optional identifier of the container this fragment is 43 * to be placed in. If 0, it will not be placed in a container. 44 * @param fragment The fragment to be added. This fragment must not already 45 * be added to the activity. 46 * @param tag Optional tag name for the fragment, to later retrieve the 47 * fragment with {@link FragmentManager#findFragmentByTag(String) 48 * FragmentManager.findFragmentByTag(String)}. 49 * 50 * @return Returns the same FragmentTransaction instance. 51 */ 52 public abstract FragmentTransaction add(int containerViewId, Fragment fragment, String tag); 53 54 /** 55 * Calls {@link #replace(int, Fragment, String)} with a null tag. 56 */ 57 public abstract FragmentTransaction replace(int containerViewId, Fragment fragment); 58 59 /** 60 * Replace an existing fragment that was added to a container. This is 61 * essentially the same as calling {@link #remove(Fragment)} for all 62 * currently added fragments that were added with the same containerViewId 63 * and then {@link #add(int, Fragment, String)} with the same arguments 64 * given here. 65 * 66 * @param containerViewId Identifier of the container whose fragment(s) are 67 * to be replaced. 68 * @param fragment The new fragment to place in the container. 69 * @param tag Optional tag name for the fragment, to later retrieve the 70 * fragment with {@link FragmentManager#findFragmentByTag(String) 71 * FragmentManager.findFragmentByTag(String)}. 72 * 73 * @return Returns the same FragmentTransaction instance. 74 */ 75 public abstract FragmentTransaction replace(int containerViewId, Fragment fragment, 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 */ 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 */ 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 */ 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 */ 120 public abstract FragmentTransaction detach(Fragment fragment); 121 122 /** 123 * Re-attach a fragment after it had previously been deatched 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 */ 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 */ 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 /** 163 * Set specific animation resources to run for the fragments that are 164 * entering and exiting in this transaction. 165 */ 166 public abstract FragmentTransaction setCustomAnimations(int enter, int exit); 167 168 /** 169 * Select a standard transition animation for this transaction. May be 170 * one of {@link #TRANSIT_NONE}, {@link #TRANSIT_FRAGMENT_OPEN}, 171 * or {@link #TRANSIT_FRAGMENT_CLOSE} 172 */ 173 public abstract FragmentTransaction setTransition(int transit); 174 175 /** 176 * Set a custom style resource that will be used for resolving transit 177 * animations. 178 */ 179 public abstract FragmentTransaction setTransitionStyle(int styleRes); 180 181 /** 182 * Add this transaction to the back stack. This means that the transaction 183 * will be remembered after it is committed, and will reverse its operation 184 * when later popped off the stack. 185 * 186 * @param name An optional name for this back stack state, or null. 187 */ 188 public abstract FragmentTransaction addToBackStack(String name); 189 190 /** 191 * Returns true if this FragmentTransaction is allowed to be added to the back 192 * stack. If this method would return false, {@link #addToBackStack(String)} 193 * will throw {@link IllegalStateException}. 194 * 195 * @return True if {@link #addToBackStack(String)} is permitted on this transaction. 196 */ 197 public abstract boolean isAddToBackStackAllowed(); 198 199 /** 200 * Disallow calls to {@link #addToBackStack(String)}. Any future calls to 201 * addToBackStack will throw {@link IllegalStateException}. If addToBackStack 202 * has already been called, this method will throw IllegalStateException. 203 */ 204 public abstract FragmentTransaction disallowAddToBackStack(); 205 206 /** 207 * Set the full title to show as a bread crumb when this transaction 208 * is on the back stack, as used by {@link FragmentBreadCrumbs}. 209 * 210 * @param res A string resource containing the title. 211 */ 212 public abstract FragmentTransaction setBreadCrumbTitle(int res); 213 214 /** 215 * Like {@link #setBreadCrumbTitle(int)} but taking a raw string; this 216 * method is <em>not</em> recommended, as the string can not be changed 217 * later if the locale changes. 218 */ 219 public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text); 220 221 /** 222 * Set the short title to show as a bread crumb when this transaction 223 * is on the back stack, as used by {@link FragmentBreadCrumbs}. 224 * 225 * @param res A string resource containing the title. 226 */ 227 public abstract FragmentTransaction setBreadCrumbShortTitle(int res); 228 229 /** 230 * Like {@link #setBreadCrumbShortTitle(int)} but taking a raw string; this 231 * method is <em>not</em> recommended, as the string can not be changed 232 * later if the locale changes. 233 */ 234 public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text); 235 236 /** 237 * Schedules a commit of this transaction. The commit does 238 * not happen immediately; it will be scheduled as work on the main thread 239 * to be done the next time that thread is ready. 240 * 241 * <p class="note">A transaction can only be committed with this method 242 * prior to its containing activity saving its state. If the commit is 243 * attempted after that point, an exception will be thrown. This is 244 * because the state after the commit can be lost if the activity needs to 245 * be restored from its state. See {@link #commitAllowingStateLoss()} for 246 * situations where it may be okay to lose the commit.</p> 247 * 248 * @return Returns the identifier of this transaction's back stack entry, 249 * if {@link #addToBackStack(String)} had been called. Otherwise, returns 250 * a negative number. 251 */ 252 public abstract int commit(); 253 254 /** 255 * Like {@link #commit} but allows the commit to be executed after an 256 * activity's state is saved. This is dangerous because the commit can 257 * be lost if the activity needs to later be restored from its state, so 258 * this should only be used for cases where it is okay for the UI state 259 * to change unexpectedly on the user. 260 */ 261 public abstract int commitAllowingStateLoss(); 262} 263