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