FragmentTransaction.java revision cba2e2c881e8e16ea5025b564c94320174d65f01
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     * @return <code>true</code> if this transaction contains no operations,
111     * <code>false</code> otherwise.
112     */
113    public abstract boolean isEmpty();
114
115    /**
116     * Bit mask that is set for all enter transitions.
117     */
118    public static final int TRANSIT_ENTER_MASK = 0x1000;
119
120    /**
121     * Bit mask that is set for all exit transitions.
122     */
123    public static final int TRANSIT_EXIT_MASK = 0x2000;
124
125    /** Not set up for a transition. */
126    public static final int TRANSIT_UNSET = -1;
127    /** No animation for transition. */
128    public static final int TRANSIT_NONE = 0;
129    /** Fragment is being added onto the stack */
130    public static final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK;
131    /** Fragment is being removed from the stack */
132    public static final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK;
133    /** Fragment should simply fade in or out; that is, no strong navigation associated
134     * with it except that it is appearing or disappearing for some reason. */
135    public static final int TRANSIT_FRAGMENT_FADE = 3 | TRANSIT_ENTER_MASK;
136
137    /**
138     * Set specific animation resources to run for the fragments that are
139     * entering and exiting in this transaction.
140     */
141    public abstract FragmentTransaction setCustomAnimations(int enter, int exit);
142
143    /**
144     * Select a standard transition animation for this transaction.  May be
145     * one of {@link #TRANSIT_NONE}, {@link #TRANSIT_FRAGMENT_OPEN},
146     * or {@link #TRANSIT_FRAGMENT_CLOSE}
147     */
148    public abstract FragmentTransaction setTransition(int transit);
149
150    /**
151     * Set a custom style resource that will be used for resolving transit
152     * animations.
153     */
154    public abstract FragmentTransaction setTransitionStyle(int styleRes);
155
156    /**
157     * Add this transaction to the back stack.  This means that the transaction
158     * will be remembered after it is committed, and will reverse its operation
159     * when later popped off the stack.
160     *
161     * @param name An optional name for this back stack state, or null.
162     */
163    public abstract FragmentTransaction addToBackStack(String name);
164
165    /**
166     * Returns true if this FragmentTransaction is allowed to be added to the back
167     * stack. If this method would return false, {@link #addToBackStack(String)}
168     * will throw {@link IllegalStateException}.
169     *
170     * @return True if {@link #addToBackStack(String)} is permitted on this transaction.
171     */
172    public abstract boolean isAddToBackStackAllowed();
173
174    /**
175     * Disallow calls to {@link #addToBackStack(String)}. Any future calls to
176     * addToBackStack will throw {@link IllegalStateException}. If addToBackStack
177     * has already been called, this method will throw IllegalStateException.
178     */
179    public abstract FragmentTransaction disallowAddToBackStack();
180
181    /**
182     * Set the full title to show as a bread crumb when this transaction
183     * is on the back stack, as used by {@link FragmentBreadCrumbs}.
184     *
185     * @param res A string resource containing the title.
186     */
187    public abstract FragmentTransaction setBreadCrumbTitle(int res);
188
189    /**
190     * Like {@link #setBreadCrumbTitle(int)} but taking a raw string; this
191     * method is <em>not</em> recommended, as the string can not be changed
192     * later if the locale changes.
193     */
194    public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text);
195
196    /**
197     * Set the short 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     */
202    public abstract FragmentTransaction setBreadCrumbShortTitle(int res);
203
204    /**
205     * Like {@link #setBreadCrumbShortTitle(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     */
209    public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text);
210
211    /**
212     * Schedules a commit of this transaction.  The commit does
213     * not happen immediately; it will be scheduled as work on the main thread
214     * to be done the next time that thread is ready.
215     *
216     * <p class="note">A transaction can only be committed with this method
217     * prior to its containing activity saving its state.  If the commit is
218     * attempted after that point, an exception will be thrown.  This is
219     * because the state after the commit can be lost if the activity needs to
220     * be restored from its state.  See {@link #commitAllowingStateLoss()} for
221     * situations where it may be okay to lose the commit.</p>
222     *
223     * @return Returns the identifier of this transaction's back stack entry,
224     * if {@link #addToBackStack(String)} had been called.  Otherwise, returns
225     * a negative number.
226     */
227    public abstract int commit();
228
229    /**
230     * Like {@link #commit} but allows the commit to be executed after an
231     * activity's state is saved.  This is dangerous because the commit can
232     * be lost if the activity needs to later be restored from its state, so
233     * this should only be used for cases where it is okay for the UI state
234     * to change unexpectedly on the user.
235     */
236    public abstract int commitAllowingStateLoss();
237}
238