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