12944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber/*
22944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * Copyright (C) 2011 The Android Open Source Project
32944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber *
42944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
52944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * you may not use this file except in compliance with the License.
62944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * You may obtain a copy of the License at
72944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber *
82944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
92944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber *
102944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * Unless required by applicable law or agreed to in writing, software
112944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
122944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * See the License for the specific language governing permissions and
142944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * limitations under the License.
152944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber */
162944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber
172944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberpackage android.support.v4.app;
182944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber
192944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberimport android.support.annotation.AnimRes;
202944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberimport android.support.annotation.IdRes;
212944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberimport android.support.annotation.IntDef;
222944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberimport android.support.annotation.Nullable;
232944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberimport android.support.annotation.StringRes;
242944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberimport android.support.annotation.StyleRes;
252944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberimport android.support.v4.util.Pair;
262944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberimport android.view.View;
272944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber
282944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberimport java.lang.annotation.Retention;
292944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberimport java.lang.annotation.RetentionPolicy;
302944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber
31ee4e1b1a63758941460ae79a064249d3a5189443Lajos Molnar/**
322944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * Static library support version of the framework's {@link android.app.FragmentTransaction}.
332944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * Used to write apps that run on platforms prior to Android 3.0.  When running
342944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * on Android 3.0 or above, this implementation is still used; it does not try
352944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * to switch to the framework's implementation.  See the framework SDK
362944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber * documentation for a class overview.
37b2487f03f12dcafdb801fc0007c8df8412397f44Marco Nelissen */
382944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huberpublic abstract class FragmentTransaction {
392944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber    /**
402944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * Calls {@link #add(int, Fragment, String)} with a 0 containerViewId.
412944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     */
422944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber    public abstract FragmentTransaction add(Fragment fragment, String tag);
43a48a51c056198a778755cbed52884dc30b74ac0aMarco Nelissen
442944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber    /**
452944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * Calls {@link #add(int, Fragment, String)} with a null tag.
462944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     */
472944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber    public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment);
482944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber
492944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber    /**
502944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * Add a fragment to the activity state.  This fragment may optionally
512944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * also have its view (if {@link Fragment#onCreateView Fragment.onCreateView}
522944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * returns non-null) into a container view of the activity.
532944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     *
542944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * @param containerViewId Optional identifier of the container this fragment is
552944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * to be placed in.  If 0, it will not be placed in a container.
562944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * @param fragment The fragment to be added.  This fragment must not already
572944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * be added to the activity.
582944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * @param tag Optional tag name for the fragment, to later retrieve the
592944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * fragment with {@link FragmentManager#findFragmentByTag(String)
602944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * FragmentManager.findFragmentByTag(String)}.
612944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     *
622944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * @return Returns the same FragmentTransaction instance.
632944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     */
642944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber    public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment,
652944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber            @Nullable String tag);
662944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber
672944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber    /**
682944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * Calls {@link #replace(int, Fragment, String)} with a null tag.
692944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     */
702944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber    public abstract FragmentTransaction replace(@IdRes int containerViewId, Fragment fragment);
712944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber
722944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber    /**
732944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * Replace an existing fragment that was added to a container.  This is
742944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * essentially the same as calling {@link #remove(Fragment)} for all
752944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * currently added fragments that were added with the same containerViewId
762944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * and then {@link #add(int, Fragment, String)} with the same arguments
772944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * given here.
782944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     *
792944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * @param containerViewId Identifier of the container whose fragment(s) are
802944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * to be replaced.
812944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber     * @param fragment The new fragment to place in the container.
82     * @param tag Optional tag name for the fragment, to later retrieve the
83     * fragment with {@link FragmentManager#findFragmentByTag(String)
84     * FragmentManager.findFragmentByTag(String)}.
85     *
86     * @return Returns the same FragmentTransaction instance.
87     */
88    public abstract FragmentTransaction replace(@IdRes int containerViewId, Fragment fragment,
89            @Nullable String tag);
90
91    /**
92     * Remove an existing fragment.  If it was added to a container, its view
93     * is also removed from that container.
94     *
95     * @param fragment The fragment to be removed.
96     *
97     * @return Returns the same FragmentTransaction instance.
98     */
99    public abstract FragmentTransaction remove(Fragment fragment);
100
101    /**
102     * Hides an existing fragment.  This is only relevant for fragments whose
103     * views have been added to a container, as this will cause the view to
104     * be hidden.
105     *
106     * @param fragment The fragment to be hidden.
107     *
108     * @return Returns the same FragmentTransaction instance.
109     */
110    public abstract FragmentTransaction hide(Fragment fragment);
111
112    /**
113     * Shows a previously hidden fragment.  This is only relevant for fragments whose
114     * views have been added to a container, as this will cause the view to
115     * be shown.
116     *
117     * @param fragment The fragment to be shown.
118     *
119     * @return Returns the same FragmentTransaction instance.
120     */
121    public abstract FragmentTransaction show(Fragment fragment);
122
123    /**
124     * Detach the given fragment from the UI.  This is the same state as
125     * when it is put on the back stack: the fragment is removed from
126     * the UI, however its state is still being actively managed by the
127     * fragment manager.  When going into this state its view hierarchy
128     * is destroyed.
129     *
130     * @param fragment The fragment to be detached.
131     *
132     * @return Returns the same FragmentTransaction instance.
133     */
134    public abstract FragmentTransaction detach(Fragment fragment);
135
136    /**
137     * Re-attach a fragment after it had previously been deatched from
138     * the UI with {@link #detach(Fragment)}.  This
139     * causes its view hierarchy to be re-created, attached to the UI,
140     * and displayed.
141     *
142     * @param fragment The fragment to be attached.
143     *
144     * @return Returns the same FragmentTransaction instance.
145     */
146    public abstract FragmentTransaction attach(Fragment fragment);
147
148    /**
149     * @return <code>true</code> if this transaction contains no operations,
150     * <code>false</code> otherwise.
151     */
152    public abstract boolean isEmpty();
153
154    /**
155     * Bit mask that is set for all enter transitions.
156     */
157    public static final int TRANSIT_ENTER_MASK = 0x1000;
158
159    /**
160     * Bit mask that is set for all exit transitions.
161     */
162    public static final int TRANSIT_EXIT_MASK = 0x2000;
163
164    /** @hide */
165    @IntDef({TRANSIT_NONE, TRANSIT_FRAGMENT_OPEN, TRANSIT_FRAGMENT_CLOSE, TRANSIT_FRAGMENT_FADE})
166    @Retention(RetentionPolicy.SOURCE)
167    private @interface Transit {}
168
169    /** Not set up for a transition. */
170    public static final int TRANSIT_UNSET = -1;
171    /** No animation for transition. */
172    public static final int TRANSIT_NONE = 0;
173    /** Fragment is being added onto the stack */
174    public static final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK;
175    /** Fragment is being removed from the stack */
176    public static final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK;
177    /** Fragment should simply fade in or out; that is, no strong navigation associated
178     * with it except that it is appearing or disappearing for some reason. */
179    public static final int TRANSIT_FRAGMENT_FADE = 3 | TRANSIT_ENTER_MASK;
180
181    /**
182     * Set specific animation resources to run for the fragments that are
183     * entering and exiting in this transaction. These animations will not be
184     * played when popping the back stack.
185     */
186    public abstract FragmentTransaction setCustomAnimations(@AnimRes int enter,
187            @AnimRes int exit);
188
189    /**
190     * Set specific animation resources to run for the fragments that are
191     * entering and exiting in this transaction. The <code>popEnter</code>
192     * and <code>popExit</code> animations will be played for enter/exit
193     * operations specifically when popping the back stack.
194     */
195    public abstract FragmentTransaction setCustomAnimations(@AnimRes int enter,
196            @AnimRes int exit, @AnimRes int popEnter, @AnimRes int popExit);
197
198    /**
199     * Used with custom Transitions to map a View from a removed or hidden
200     * Fragment to a View from a shown or added Fragment.
201     * <var>sharedElement</var> must have a unique transitionName in the View hierarchy.
202     *
203     * @param sharedElement A View in a disappearing Fragment to match with a View in an
204     *                      appearing Fragment.
205     * @param name The transitionName for a View in an appearing Fragment to match to the shared
206     *             element.
207     * @see Fragment#setSharedElementReturnTransition(Object)
208     * @see Fragment#setSharedElementEnterTransition(Object)
209     */
210    public abstract FragmentTransaction addSharedElement(View sharedElement, String name);
211
212    /**
213     * Select a standard transition animation for this transaction.  May be
214     * one of {@link #TRANSIT_NONE}, {@link #TRANSIT_FRAGMENT_OPEN},
215     * {@link #TRANSIT_FRAGMENT_CLOSE}, or {@link #TRANSIT_FRAGMENT_FADE}.
216     */
217    public abstract FragmentTransaction setTransition(@Transit int transit);
218
219    /**
220     * Set a custom style resource that will be used for resolving transit
221     * animations.
222     */
223    public abstract FragmentTransaction setTransitionStyle(@StyleRes int styleRes);
224
225    /**
226     * Add this transaction to the back stack.  This means that the transaction
227     * will be remembered after it is committed, and will reverse its operation
228     * when later popped off the stack.
229     *
230     * @param name An optional name for this back stack state, or null.
231     */
232    public abstract FragmentTransaction addToBackStack(@Nullable String name);
233
234    /**
235     * Returns true if this FragmentTransaction is allowed to be added to the back
236     * stack. If this method would return false, {@link #addToBackStack(String)}
237     * will throw {@link IllegalStateException}.
238     *
239     * @return True if {@link #addToBackStack(String)} is permitted on this transaction.
240     */
241    public abstract boolean isAddToBackStackAllowed();
242
243    /**
244     * Disallow calls to {@link #addToBackStack(String)}. Any future calls to
245     * addToBackStack will throw {@link IllegalStateException}. If addToBackStack
246     * has already been called, this method will throw IllegalStateException.
247     */
248    public abstract FragmentTransaction disallowAddToBackStack();
249
250    /**
251     * Set the full title to show as a bread crumb when this transaction
252     * is on the back stack.
253     *
254     * @param res A string resource containing the title.
255     */
256    public abstract FragmentTransaction setBreadCrumbTitle(@StringRes int res);
257
258    /**
259     * Like {@link #setBreadCrumbTitle(int)} but taking a raw string; this
260     * method is <em>not</em> recommended, as the string can not be changed
261     * later if the locale changes.
262     */
263    public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text);
264
265    /**
266     * Set the short title to show as a bread crumb when this transaction
267     * is on the back stack.
268     *
269     * @param res A string resource containing the title.
270     */
271    public abstract FragmentTransaction setBreadCrumbShortTitle(@StringRes int res);
272
273    /**
274     * Like {@link #setBreadCrumbShortTitle(int)} but taking a raw string; this
275     * method is <em>not</em> recommended, as the string can not be changed
276     * later if the locale changes.
277     */
278    public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text);
279
280    /**
281     * Schedules a commit of this transaction.  The commit does
282     * not happen immediately; it will be scheduled as work on the main thread
283     * to be done the next time that thread is ready.
284     *
285     * <p class="note">A transaction can only be committed with this method
286     * prior to its containing activity saving its state.  If the commit is
287     * attempted after that point, an exception will be thrown.  This is
288     * because the state after the commit can be lost if the activity needs to
289     * be restored from its state.  See {@link #commitAllowingStateLoss()} for
290     * situations where it may be okay to lose the commit.</p>
291     *
292     * @return Returns the identifier of this transaction's back stack entry,
293     * if {@link #addToBackStack(String)} had been called.  Otherwise, returns
294     * a negative number.
295     */
296    public abstract int commit();
297
298    /**
299     * Like {@link #commit} but allows the commit to be executed after an
300     * activity's state is saved.  This is dangerous because the commit can
301     * be lost if the activity needs to later be restored from its state, so
302     * this should only be used for cases where it is okay for the UI state
303     * to change unexpectedly on the user.
304     */
305    public abstract int commitAllowingStateLoss();
306
307    /**
308     * Commits this transaction synchronously. Any added fragments will be
309     * initialized and brought completely to the lifecycle state of their host
310     * and any removed fragments will be torn down accordingly before this
311     * call returns. Committing a transaction in this way allows fragments
312     * to be added as dedicated, encapsulated components that monitor the
313     * lifecycle state of their host while providing firmer ordering guarantees
314     * around when those fragments are fully initialized and ready. Fragments
315     * that manage views will have those views created and attached.
316     *
317     * <p>Calling <code>commitNow</code> is preferable to calling
318     * {@link #commit()} followed by {@link FragmentManager#executePendingTransactions()}
319     * as the latter will have the side effect of attempting to commit <em>all</em>
320     * currently pending transactions whether that is the desired behavior
321     * or not.</p>
322     *
323     * <p>Transactions committed in this way may not be added to the
324     * FragmentManager's back stack, as doing so would break other expected
325     * ordering guarantees for other asynchronously committed transactions.
326     * This method will throw {@link IllegalStateException} if the transaction
327     * previously requested to be added to the back stack with
328     * {@link #addToBackStack(String)}.</p>
329     *
330     * <p class="note">A transaction can only be committed with this method
331     * prior to its containing activity saving its state.  If the commit is
332     * attempted after that point, an exception will be thrown.  This is
333     * because the state after the commit can be lost if the activity needs to
334     * be restored from its state.  See {@link #commitAllowingStateLoss()} for
335     * situations where it may be okay to lose the commit.</p>
336     */
337    public abstract void commitNow();
338
339    /**
340     * Like {@link #commitNow} but allows the commit to be executed after an
341     * activity's state is saved.  This is dangerous because the commit can
342     * be lost if the activity needs to later be restored from its state, so
343     * this should only be used for cases where it is okay for the UI state
344     * to change unexpectedly on the user.
345     */
346    public abstract void commitNowAllowingStateLoss();
347}
348