blob: c1f3cd61da1d4460caebd980706c913f7e556d4d [file] [log] [blame]
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001package android.app;
2
3/**
4 * API for performing a set of Fragment operations.
5 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -07006public abstract class FragmentTransaction {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07007 /**
8 * Calls {@link #add(int, Fragment, String)} with a 0 containerViewId.
9 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070010 public abstract FragmentTransaction add(Fragment fragment, String tag);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070011
12 /**
13 * Calls {@link #add(int, Fragment, String)} with a null tag.
14 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070015 public abstract FragmentTransaction add(int containerViewId, Fragment fragment);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070016
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 Hackborn247fe742011-01-08 17:25:57 -080027 * fragment with {@link FragmentManager#findFragmentByTag(String)
28 * FragmentManager.findFragmentByTag(String)}.
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070029 *
30 * @return Returns the same FragmentTransaction instance.
31 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070032 public abstract FragmentTransaction add(int containerViewId, Fragment fragment, String tag);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070033
34 /**
35 * Calls {@link #replace(int, Fragment, String)} with a null tag.
36 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070037 public abstract FragmentTransaction replace(int containerViewId, Fragment fragment);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070038
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 Hackborn247fe742011-01-08 17:25:57 -080050 * fragment with {@link FragmentManager#findFragmentByTag(String)
51 * FragmentManager.findFragmentByTag(String)}.
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070052 *
53 * @return Returns the same FragmentTransaction instance.
54 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070055 public abstract FragmentTransaction replace(int containerViewId, Fragment fragment, String tag);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070056
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 Hackbornab36acb2010-11-05 14:12:11 -070065 public abstract FragmentTransaction remove(Fragment fragment);
Dianne Hackbornf121be72010-05-06 14:10:32 -070066
67 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070068 * 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 Hackbornab36acb2010-11-05 14:12:11 -070076 public abstract FragmentTransaction hide(Fragment fragment);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070077
78 /**
Jim Shumabaa15532010-11-09 20:37:03 -080079 * Shows a previously hidden fragment. This is only relevant for fragments whose
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070080 * 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 Hackbornab36acb2010-11-05 14:12:11 -070087 public abstract FragmentTransaction show(Fragment fragment);
Adam Powell2b6230e2010-09-07 17:55:25 -070088
89 /**
Dianne Hackborn47c41562011-04-15 19:00:20 -070090 * 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 Powell2b6230e2010-09-07 17:55:25 -0700115 * @return <code>true</code> if this transaction contains no operations,
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700116 * <code>false</code> otherwise.
Adam Powell2b6230e2010-09-07 17:55:25 -0700117 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700118 public abstract boolean isEmpty();
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700119
120 /**
121 * Bit mask that is set for all enter transitions.
Dianne Hackbornf121be72010-05-06 14:10:32 -0700122 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700123 public static final int TRANSIT_ENTER_MASK = 0x1000;
Dianne Hackbornf121be72010-05-06 14:10:32 -0700124
125 /**
126 * Bit mask that is set for all exit transitions.
127 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700128 public static final int TRANSIT_EXIT_MASK = 0x2000;
Dianne Hackbornf121be72010-05-06 14:10:32 -0700129
130 /** Not set up for a transition. */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700131 public static final int TRANSIT_UNSET = -1;
Dianne Hackbornf121be72010-05-06 14:10:32 -0700132 /** No animation for transition. */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700133 public static final int TRANSIT_NONE = 0;
Chet Haase9ff82bf2010-10-05 14:30:51 -0700134 /** Fragment is being added onto the stack */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700135 public static final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK;
Chet Haase9ff82bf2010-10-05 14:30:51 -0700136 /** Fragment is being removed from the stack */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700137 public static final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK;
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800138 /** 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 Haase811ed1062010-08-06 10:38:15 -0700141
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700142 /**
143 * Set specific animation resources to run for the fragments that are
Chet Haasebc377842011-03-22 11:35:22 -0700144 * entering and exiting in this transaction. These animations will not be
145 * played when popping the back stack.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700146 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700147 public abstract FragmentTransaction setCustomAnimations(int enter, int exit);
Chet Haasebc377842011-03-22 11:35:22 -0700148
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 Hackbornc6669ca2010-09-16 01:33:24 -0700158 /**
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 Hackbornab36acb2010-11-05 14:12:11 -0700163 public abstract FragmentTransaction setTransition(int transit);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700164
165 /**
166 * Set a custom style resource that will be used for resolving transit
167 * animations.
168 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700169 public abstract FragmentTransaction setTransitionStyle(int styleRes);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700170
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700171 /**
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 Hackbornab36acb2010-11-05 14:12:11 -0700178 public abstract FragmentTransaction addToBackStack(String name);
Dianne Hackborndd913a52010-07-22 12:17:04 -0700179
180 /**
Adam Powell0c24a552010-11-03 16:44:11 -0700181 * 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 Hackbornab36acb2010-11-05 14:12:11 -0700187 public abstract boolean isAddToBackStackAllowed();
Adam Powell0c24a552010-11-03 16:44:11 -0700188
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 Hackbornab36acb2010-11-05 14:12:11 -0700194 public abstract FragmentTransaction disallowAddToBackStack();
Adam Powell0c24a552010-11-03 16:44:11 -0700195
196 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700197 * 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 Hackbornab36acb2010-11-05 14:12:11 -0700202 public abstract FragmentTransaction setBreadCrumbTitle(int res);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700203
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 Hackbornab36acb2010-11-05 14:12:11 -0700209 public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700210
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 Hackbornab36acb2010-11-05 14:12:11 -0700217 public abstract FragmentTransaction setBreadCrumbShortTitle(int res);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700218
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 Hackbornab36acb2010-11-05 14:12:11 -0700224 public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700225
226 /**
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700227 * Schedules a commit of this transaction. The commit does
Dianne Hackborndd913a52010-07-22 12:17:04 -0700228 * 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 Hackbornab36acb2010-11-05 14:12:11 -0700231 * <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 Hackborndd913a52010-07-22 12:17:04 -0700238 * @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 Hackbornab36acb2010-11-05 14:12:11 -0700242 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 Hackborn2dedce62010-04-15 14:45:25 -0700252}