FragmentManager.java revision e880475b147312ca62bed05bbeb37ec820d693ae
1cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/* 2cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Copyright (C) 2011 The Android Open Source Project 3cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 4cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License"); 5cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * you may not use this file except in compliance with the License. 6cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * You may obtain a copy of the License at 7cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 8cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * http://www.apache.org/licenses/LICENSE-2.0 9cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 10cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Unless required by applicable law or agreed to in writing, software 11cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS, 12cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * See the License for the specific language governing permissions and 14cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * limitations under the License. 15cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 16cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 17cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpackage android.support.v4.app; 18cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 199277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.content.Context; 20cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.res.Configuration; 2136bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powellimport android.content.res.Resources.NotFoundException; 220f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powellimport android.content.res.TypedArray; 235e63ab9505a3a4d11374cbbec418c1aba921409dChris Banesimport android.os.Build; 24cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Bundle; 25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Handler; 26cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Looper; 27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Parcel; 28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Parcelable; 2903526560f132021f8fd7290259762ab362d4d567Doris Liuimport android.support.annotation.CallSuper; 30a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.IdRes; 31a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.StringRes; 32cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.support.v4.util.DebugUtils; 33cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.support.v4.util.LogWriter; 34bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banesimport android.support.v4.view.LayoutInflaterFactory; 355e63ab9505a3a4d11374cbbec418c1aba921409dChris Banesimport android.support.v4.view.ViewCompat; 360f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powellimport android.util.AttributeSet; 37cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.util.Log; 38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.util.SparseArray; 399277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.AccelerateInterpolator; 409277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.AlphaAnimation; 41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.Animation; 429277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.AnimationSet; 43cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.AnimationUtils; 449277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.DecelerateInterpolator; 459277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.Interpolator; 469277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.ScaleAnimation; 47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.Animation.AnimationListener; 48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.Menu; 49cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.MenuInflater; 50cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.MenuItem; 51cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.View; 52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.ViewGroup; 53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 54cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.FileDescriptor; 55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.PrintWriter; 56d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liuimport java.lang.reflect.Field; 57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.util.ArrayList; 58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.util.Arrays; 593a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brownimport java.util.List; 60cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/** 62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Static library support version of the framework's {@link android.app.FragmentManager}. 63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Used to write apps that run on platforms prior to Android 3.0. When running 64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on Android 3.0 or above, this implementation is still used; it does not try 659dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * to switch to the framework's implementation. See the framework {@link FragmentManager} 66cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * documentation for a class overview. 679dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * 689dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <p>Your activity must derive from {@link FragmentActivity} to use this. From such an activity, 699dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * you can acquire the {@link FragmentManager} by calling 709dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * {@link FragmentActivity#getSupportFragmentManager}. 71cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 72cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic abstract class FragmentManager { 73cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 74cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Representation of an entry on the fragment back stack, as created 75cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * with {@link FragmentTransaction#addToBackStack(String) 76cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * FragmentTransaction.addToBackStack()}. Entries can later be 77cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * retrieved with {@link FragmentManager#getBackStackEntryAt(int) 78cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * FragmentManager.getBackStackEntry()}. 79cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * <p>Note that you should never hold on to a BackStackEntry object; 81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the identifier as returned by {@link #getId} is the only thing that 82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * will be persisted across activity instances. 83cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 84cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public interface BackStackEntry { 85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the unique identifier for the entry. This is the only 87cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * representation of the entry that will persist across activity 88cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * instances. 89cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 90cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getId(); 91cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 92cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 932a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn * Get the name that was supplied to 942a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn * {@link FragmentTransaction#addToBackStack(String) 952a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn * FragmentTransaction.addToBackStack(String)} when creating this entry. 962a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn */ 972a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn public String getName(); 982a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn 992a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn /** 100cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the full bread crumb title resource identifier for the entry, 101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * or 0 if it does not have one. 102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 103a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye @StringRes 104cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getBreadCrumbTitleRes(); 105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 107cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the short bread crumb title resource identifier for the entry, 108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * or 0 if it does not have one. 109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 110a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye @StringRes 111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getBreadCrumbShortTitleRes(); 112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the full bread crumb title for the entry, or null if it 115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * does not have one. 116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public CharSequence getBreadCrumbTitle(); 118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the short bread crumb title for the entry, or null if it 121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * does not have one. 122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public CharSequence getBreadCrumbShortTitle(); 124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Interface to watch for changes to the back stack. 128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public interface OnBackStackChangedListener { 130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Called whenever the contents of the back stack change. 132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onBackStackChanged(); 134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Start a series of edit operations on the Fragments associated with 138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * this FragmentManager. 139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 140cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * <p>Note: A fragment transaction can only be created/committed prior 141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to an activity saving its state. If you try to commit a transaction 142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * after {@link FragmentActivity#onSaveInstanceState FragmentActivity.onSaveInstanceState()} 143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * (and prior to a following {@link FragmentActivity#onStart FragmentActivity.onStart} 144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * or {@link FragmentActivity#onResume FragmentActivity.onResume()}, you will get an error. 145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This is because the framework takes care of saving your current fragments 146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * in the state, and if changes are made after the state is saved then they 147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * will be lost.</p> 148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract FragmentTransaction beginTransaction(); 150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** @hide -- remove once prebuilts are in. */ 152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Deprecated 153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentTransaction openTransaction() { 154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return beginTransaction(); 155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * After a {@link FragmentTransaction} is committed with 159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it 160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * is scheduled to be executed asynchronously on the process's main thread. 161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * If you want to immediately executing any such pending operations, you 162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * can call this function (only from the main thread) to do so. Note that 163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * all callbacks and other related behavior will be done from within this 164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * call, so be careful about where this is called from. 165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return Returns true if there were any pending transactions to be 167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * executed. 168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract boolean executePendingTransactions(); 170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Finds a fragment that was identified by the given id either when inflated 173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * from XML or as the container ID when added in a transaction. This first 174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * searches through fragments that are currently added to the manager's 175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * activity; if no such fragment is found, then all fragments currently 176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on the back stack associated with this ID are searched. 177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return The fragment if found or null otherwise. 178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 179a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye public abstract Fragment findFragmentById(@IdRes int id); 180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Finds a fragment that was identified by the given tag either when inflated 183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * from XML or as supplied when added in a transaction. This first 184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * searches through fragments that are currently added to the manager's 185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * activity; if no such fragment is found, then all fragments currently 186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on the back stack are searched. 187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return The fragment if found or null otherwise. 188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract Fragment findFragmentByTag(String tag); 190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Flag for {@link #popBackStack(String, int)} 193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * and {@link #popBackStack(int, int)}: If set, and the name or ID of 194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * a back stack entry has been supplied, then all matching entries will 195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * be consumed until one that doesn't match is found or the bottom of 196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the stack is reached. Otherwise, all entries up to but not including that entry 197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * will be removed. 198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public static final int POP_BACK_STACK_INCLUSIVE = 1<<0; 200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Pop the top state off the back stack. Returns true if there was one 203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to pop, else false. This function is asynchronous -- it enqueues the 204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * request to pop, but the action will not be performed until the application 205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * returns to its event loop. 206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void popBackStack(); 208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Like {@link #popBackStack()}, but performs the operation immediately 211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * inside of the call. This is like calling {@link #executePendingTransactions()} 212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * afterwards. 213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return Returns true if there was something popped, else false. 214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract boolean popBackStackImmediate(); 216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Pop the last fragment transition from the manager's fragment 219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * back stack. If there is nothing to pop, false is returned. 220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This function is asynchronous -- it enqueues the 221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * request to pop, but the action will not be performed until the application 222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * returns to its event loop. 223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param name If non-null, this is the name of a previous back state 225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to look for; if found, all states up to that state will be popped. The 226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether 227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the named state itself is popped. If null, only the top state is popped. 228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. 229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void popBackStack(String name, int flags); 231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Like {@link #popBackStack(String, int)}, but performs the operation immediately 234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * inside of the call. This is like calling {@link #executePendingTransactions()} 235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * afterwards. 236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return Returns true if there was something popped, else false. 237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract boolean popBackStackImmediate(String name, int flags); 239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Pop all back stack states up to the one with the given identifier. 242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This function is asynchronous -- it enqueues the 243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * request to pop, but the action will not be performed until the application 244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * returns to its event loop. 245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param id Identifier of the stated to be popped. If no identifier exists, 247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * false is returned. 248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * The identifier is the number returned by 249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The 250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether 251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the named state itself is popped. 252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. 253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void popBackStack(int id, int flags); 255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Like {@link #popBackStack(int, int)}, but performs the operation immediately 258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * inside of the call. This is like calling {@link #executePendingTransactions()} 259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * afterwards. 260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return Returns true if there was something popped, else false. 261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract boolean popBackStackImmediate(int id, int flags); 263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the number of entries currently in the back stack. 266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract int getBackStackEntryCount(); 268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the BackStackEntry at index <var>index</var> in the back stack; 271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * entries start index 0 being the bottom of the stack. 272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract BackStackEntry getBackStackEntryAt(int index); 274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Add a new listener for changes to the fragment back stack. 277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener); 279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Remove a listener that was previously added with 282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}. 283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener); 285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Put a reference to a fragment in a Bundle. This Bundle can be 288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * persisted as saved state, and when later restoring 289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link #getFragment(Bundle, String)} will return the current 290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * instance of the same fragment. 291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param bundle The bundle in which to put the fragment reference. 293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param key The name of the entry in the bundle. 294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param fragment The Fragment whose reference is to be stored. 295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void putFragment(Bundle bundle, String key, Fragment fragment); 297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Retrieve the current Fragment instance for a reference previously 300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * placed with {@link #putFragment(Bundle, String, Fragment)}. 301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param bundle The bundle from which to retrieve the fragment reference. 303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param key The name of the entry in the bundle. 304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return Returns the current Fragment instance that is associated with 305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the given reference. 306cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract Fragment getFragment(Bundle bundle, String key); 308cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 3103a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown * Get a list of all fragments that have been added to the fragment manager. 3113a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown * 3123a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown * @return The list of all fragments or null if none. 3133a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown * @hide 3143a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown */ 3153a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown public abstract List<Fragment> getFragments(); 3163a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown 3173a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown /** 3185c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Save the current instance state of the given Fragment. This can be 3195c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * used later when creating a new instance of the Fragment and adding 3205c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * it to the fragment manager, to have it create itself to match the 3215c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * current state returned here. Note that there are limits on how 3225c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * this can be used: 3235c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * 3245c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * <ul> 3255c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * <li>The Fragment must currently be attached to the FragmentManager. 3265c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * <li>A new Fragment created using this saved state must be the same class 3275c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * type as the Fragment it was created from. 3285c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * <li>The saved state can not contain dependencies on other fragments -- 3295c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to 3305c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * store a fragment reference because that reference may not be valid when 3315c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * this saved state is later used. Likewise the Fragment's target and 3325c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * result code are not included in this state. 3335c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * </ul> 3345c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * 3355c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * @param f The Fragment whose state is to be saved. 3365c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * @return The generated state. This will be null if there was no 3375c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * interesting state created by the fragment. 3385c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn */ 3395c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f); 3405c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 3415c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn /** 34201df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler * Returns true if the final {@link android.app.Activity#onDestroy() Activity.onDestroy()} 34301df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler * call has been made on the FragmentManager's Activity, so this instance is now dead. 34401df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler */ 34501df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler public abstract boolean isDestroyed(); 34601df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler 34701df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler /** 348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Print the FragmentManager's state into the given stream. 349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param prefix Text to print at the front of each line. 351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param fd The raw file descriptor that the dump is being sent to. 352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param writer A PrintWriter to which the dump is to be set. 353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param args Additional arguments to the dump request. 354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 355cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args); 356cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 357cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Control whether the framework's internal fragment manager debugging 359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * logs are turned on. If enabled, you will see output in logcat as 360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the framework performs fragment operations. 361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public static void enableDebugLogging(boolean enabled) { 363cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.DEBUG = enabled; 364cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn} 366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornfinal class FragmentManagerState implements Parcelable { 368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentState[] mActive; 369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int[] mAdded; 370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackState[] mBackStack; 371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentManagerState() { 373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentManagerState(Parcel in) { 376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive = in.createTypedArray(FragmentState.CREATOR); 377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdded = in.createIntArray(); 378cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack = in.createTypedArray(BackStackState.CREATOR); 379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int describeContents() { 382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return 0; 383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void writeToParcel(Parcel dest, int flags) { 386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn dest.writeTypedArray(mActive, flags); 387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn dest.writeIntArray(mAdded); 388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn dest.writeTypedArray(mBackStack, flags); 389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public static final Parcelable.Creator<FragmentManagerState> CREATOR 392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn = new Parcelable.Creator<FragmentManagerState>() { 393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentManagerState createFromParcel(Parcel in) { 394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return new FragmentManagerState(in); 395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentManagerState[] newArray(int size) { 398cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return new FragmentManagerState[size]; 399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }; 401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn} 402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 403cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/** 404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Container for fragments associated with an activity. 405cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 406bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banesfinal class FragmentManagerImpl extends FragmentManager implements LayoutInflaterFactory { 407cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static boolean DEBUG = false; 408cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static final String TAG = "FragmentManager"; 409cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 410681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn static final boolean HONEYCOMB = android.os.Build.VERSION.SDK_INT >= 11; 411681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 412cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state"; 413cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static final String TARGET_STATE_TAG = "android:target_state"; 414cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static final String VIEW_STATE_TAG = "android:view_state"; 41579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint"; 416cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 41703526560f132021f8fd7290259762ab362d4d567Doris Liu 41803526560f132021f8fd7290259762ab362d4d567Doris Liu static class AnimateOnHWLayerIfNeededListener implements AnimationListener { 419d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu private AnimationListener mOrignalListener = null; 42003526560f132021f8fd7290259762ab362d4d567Doris Liu private boolean mShouldRunOnHWLayer = false; 421d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu private View mView = null; 42203526560f132021f8fd7290259762ab362d4d567Doris Liu public AnimateOnHWLayerIfNeededListener(final View v, Animation anim) { 42303526560f132021f8fd7290259762ab362d4d567Doris Liu if (v == null || anim == null) { 42403526560f132021f8fd7290259762ab362d4d567Doris Liu return; 42503526560f132021f8fd7290259762ab362d4d567Doris Liu } 42603526560f132021f8fd7290259762ab362d4d567Doris Liu mView = v; 42703526560f132021f8fd7290259762ab362d4d567Doris Liu } 42803526560f132021f8fd7290259762ab362d4d567Doris Liu 429d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu public AnimateOnHWLayerIfNeededListener(final View v, Animation anim, 430d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu AnimationListener listener) { 431d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu if (v == null || anim == null) { 432d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu return; 433d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu } 434d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu mOrignalListener = listener; 435d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu mView = v; 436d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu } 437d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu 43803526560f132021f8fd7290259762ab362d4d567Doris Liu @Override 43903526560f132021f8fd7290259762ab362d4d567Doris Liu @CallSuper 44003526560f132021f8fd7290259762ab362d4d567Doris Liu public void onAnimationStart(Animation animation) { 441d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu if (mView != null) { 442d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu mShouldRunOnHWLayer = shouldRunOnHWLayer(mView, animation); 443d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu if (mShouldRunOnHWLayer) { 4447e6e889eb9fdad71b015455ec9353359366f61d3Chris Banes mView.post(new Runnable() { 4457e6e889eb9fdad71b015455ec9353359366f61d3Chris Banes @Override 4467e6e889eb9fdad71b015455ec9353359366f61d3Chris Banes public void run() { 4477e6e889eb9fdad71b015455ec9353359366f61d3Chris Banes ViewCompat.setLayerType(mView, ViewCompat.LAYER_TYPE_HARDWARE, null); 4487e6e889eb9fdad71b015455ec9353359366f61d3Chris Banes } 4497e6e889eb9fdad71b015455ec9353359366f61d3Chris Banes }); 450d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu } 451d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu } 452d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu if (mOrignalListener != null) { 453d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu mOrignalListener.onAnimationStart(animation); 45403526560f132021f8fd7290259762ab362d4d567Doris Liu } 45503526560f132021f8fd7290259762ab362d4d567Doris Liu } 45603526560f132021f8fd7290259762ab362d4d567Doris Liu 45703526560f132021f8fd7290259762ab362d4d567Doris Liu @Override 45803526560f132021f8fd7290259762ab362d4d567Doris Liu @CallSuper 45903526560f132021f8fd7290259762ab362d4d567Doris Liu public void onAnimationEnd(Animation animation) { 460d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu if (mView != null && mShouldRunOnHWLayer) { 46161401e37fd50cd0e3cbb9381d5f8d1295f3d7698Chris Banes mView.post(new Runnable() { 46261401e37fd50cd0e3cbb9381d5f8d1295f3d7698Chris Banes @Override 46361401e37fd50cd0e3cbb9381d5f8d1295f3d7698Chris Banes public void run() { 46461401e37fd50cd0e3cbb9381d5f8d1295f3d7698Chris Banes ViewCompat.setLayerType(mView, ViewCompat.LAYER_TYPE_NONE, null); 46561401e37fd50cd0e3cbb9381d5f8d1295f3d7698Chris Banes } 46661401e37fd50cd0e3cbb9381d5f8d1295f3d7698Chris Banes }); 46703526560f132021f8fd7290259762ab362d4d567Doris Liu } 468d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu if (mOrignalListener != null) { 469d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu mOrignalListener.onAnimationEnd(animation); 470d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu } 47103526560f132021f8fd7290259762ab362d4d567Doris Liu } 47203526560f132021f8fd7290259762ab362d4d567Doris Liu 47303526560f132021f8fd7290259762ab362d4d567Doris Liu @Override 47403526560f132021f8fd7290259762ab362d4d567Doris Liu public void onAnimationRepeat(Animation animation) { 475d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu if (mOrignalListener != null) { 476d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu mOrignalListener.onAnimationRepeat(animation); 477d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu } 47803526560f132021f8fd7290259762ab362d4d567Doris Liu } 47903526560f132021f8fd7290259762ab362d4d567Doris Liu } 48003526560f132021f8fd7290259762ab362d4d567Doris Liu 481cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Runnable> mPendingActions; 482cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Runnable[] mTmpActions; 483cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean mExecutingActions; 484cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 485cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> mActive; 486cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> mAdded; 487cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Integer> mAvailIndices; 488cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<BackStackRecord> mBackStack; 489cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> mCreatedMenus; 490cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 491cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Must be accessed while locked. 492cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<BackStackRecord> mBackStackIndices; 493cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Integer> mAvailBackStackIndices; 494cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 495cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<OnBackStackChangedListener> mBackStackChangeListeners; 496cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 497cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int mCurState = Fragment.INITIALIZING; 4988491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy FragmentHostCallback mHost; 499d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy FragmentController mController; 5000adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn FragmentContainer mContainer; 5010adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn Fragment mParent; 502d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu 503d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu static Field sAnimationListenerField = null; 504cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 505cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean mNeedMenuInvalidate; 506cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean mStateSaved; 507cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean mDestroyed; 508cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn String mNoTransactionsBecause; 50979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell boolean mHavePendingDeferredStart; 510cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 511cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Temporary vars for state save and restore. 512cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Bundle mStateBundle = null; 513cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn SparseArray<Parcelable> mStateArray = null; 514cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 515cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Runnable mExecCommit = new Runnable() { 516cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 517cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void run() { 518cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn execPendingActions(); 519cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 520cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }; 521cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 52203526560f132021f8fd7290259762ab362d4d567Doris Liu static boolean modifiesAlpha(Animation anim) { 52303526560f132021f8fd7290259762ab362d4d567Doris Liu if (anim instanceof AlphaAnimation) { 52403526560f132021f8fd7290259762ab362d4d567Doris Liu return true; 52503526560f132021f8fd7290259762ab362d4d567Doris Liu } else if (anim instanceof AnimationSet) { 52603526560f132021f8fd7290259762ab362d4d567Doris Liu List<Animation> anims = ((AnimationSet) anim).getAnimations(); 52703526560f132021f8fd7290259762ab362d4d567Doris Liu for (int i = 0; i < anims.size(); i++) { 52803526560f132021f8fd7290259762ab362d4d567Doris Liu if (anims.get(i) instanceof AlphaAnimation) { 52903526560f132021f8fd7290259762ab362d4d567Doris Liu return true; 53003526560f132021f8fd7290259762ab362d4d567Doris Liu } 53103526560f132021f8fd7290259762ab362d4d567Doris Liu } 53203526560f132021f8fd7290259762ab362d4d567Doris Liu } 53303526560f132021f8fd7290259762ab362d4d567Doris Liu return false; 53403526560f132021f8fd7290259762ab362d4d567Doris Liu } 53503526560f132021f8fd7290259762ab362d4d567Doris Liu 53603526560f132021f8fd7290259762ab362d4d567Doris Liu static boolean shouldRunOnHWLayer(View v, Animation anim) { 537b8d65fef161c7cd4bb06dc97685cf3fb3d6c3e1aChris Banes return Build.VERSION.SDK_INT >= 19 538410d77a6fbe42b0784cccd046bb8958caa723035Chris Banes && ViewCompat.getLayerType(v) == ViewCompat.LAYER_TYPE_NONE 53903526560f132021f8fd7290259762ab362d4d567Doris Liu && ViewCompat.hasOverlappingRendering(v) 54003526560f132021f8fd7290259762ab362d4d567Doris Liu && modifiesAlpha(anim); 54103526560f132021f8fd7290259762ab362d4d567Doris Liu } 54203526560f132021f8fd7290259762ab362d4d567Doris Liu 54313fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn private void throwException(RuntimeException ex) { 54413fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn Log.e(TAG, ex.getMessage()); 545ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn Log.e(TAG, "Activity state:"); 54613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn LogWriter logw = new LogWriter(TAG); 54713fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn PrintWriter pw = new PrintWriter(logw); 548d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mHost != null) { 549ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn try { 5508491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy mHost.onDump(" ", null, pw, new String[] { }); 551ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } catch (Exception e) { 552ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn Log.e(TAG, "Failed dumping state", e); 553ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } 554ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } else { 555ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn try { 55613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn dump(" ", null, pw, new String[] { }); 557ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } catch (Exception e) { 55813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn Log.e(TAG, "Failed dumping state", e); 559ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } 560ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } 56113fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throw ex; 562ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } 563ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn 564cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 565cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentTransaction beginTransaction() { 566cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return new BackStackRecord(this); 567cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 568cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 569cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 570cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean executePendingTransactions() { 571cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return execPendingActions(); 572cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 573cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 574cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 575cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void popBackStack() { 576cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn enqueueAction(new Runnable() { 577cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override public void run() { 578d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy popBackStackState(mHost.getHandler(), null, -1, 0); 579cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 580cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }, false); 581cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 582cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 583cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 584cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean popBackStackImmediate() { 585cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn checkStateLoss(); 586cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn executePendingTransactions(); 587d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return popBackStackState(mHost.getHandler(), null, -1, 0); 588cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 589cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 590cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 591cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void popBackStack(final String name, final int flags) { 592cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn enqueueAction(new Runnable() { 593cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override public void run() { 594d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy popBackStackState(mHost.getHandler(), name, -1, flags); 595cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 596cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }, false); 597cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 598cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 599cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 600cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean popBackStackImmediate(String name, int flags) { 601cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn checkStateLoss(); 602cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn executePendingTransactions(); 603d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return popBackStackState(mHost.getHandler(), name, -1, flags); 604cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 605cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 606cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 607cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void popBackStack(final int id, final int flags) { 608cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (id < 0) { 609cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalArgumentException("Bad id: " + id); 610cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 611cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn enqueueAction(new Runnable() { 612cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override public void run() { 613d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy popBackStackState(mHost.getHandler(), null, id, flags); 614cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 615cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }, false); 616cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 617cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 618cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 619cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean popBackStackImmediate(int id, int flags) { 620cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn checkStateLoss(); 621cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn executePendingTransactions(); 622cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (id < 0) { 623cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalArgumentException("Bad id: " + id); 624cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 625d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return popBackStackState(mHost.getHandler(), null, id, flags); 626cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 627cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 628cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 629cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getBackStackEntryCount() { 630cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mBackStack != null ? mBackStack.size() : 0; 631cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 632cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 633cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 634cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public BackStackEntry getBackStackEntryAt(int index) { 635cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mBackStack.get(index); 636cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 637cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 638cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 639cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void addOnBackStackChangedListener(OnBackStackChangedListener listener) { 640cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackChangeListeners == null) { 641cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>(); 642cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 643cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackChangeListeners.add(listener); 644cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 645cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 646cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 647cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) { 648cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackChangeListeners != null) { 649cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackChangeListeners.remove(listener); 650cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 651cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 652cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 653cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 654cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void putFragment(Bundle bundle, String key, Fragment fragment) { 655cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mIndex < 0) { 65613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalStateException("Fragment " + fragment 65713fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + " is not currently in the FragmentManager")); 658cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 659cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn bundle.putInt(key, fragment.mIndex); 660cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 661cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 662cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 663cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public Fragment getFragment(Bundle bundle, String key) { 664cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int index = bundle.getInt(key, -1); 665cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (index == -1) { 666cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 667cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 668cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (index >= mActive.size()) { 6692b336307cf98ca5142db6736812178293d47c500Cyril Mottier throwException(new IllegalStateException("Fragment no longer exists for key " 67013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + key + ": index " + index)); 671cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 672cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(index); 673cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f == null) { 6742b336307cf98ca5142db6736812178293d47c500Cyril Mottier throwException(new IllegalStateException("Fragment no longer exists for key " 67513fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + key + ": index " + index)); 676cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 677cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 678cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 679cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 680cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 6813a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown public List<Fragment> getFragments() { 6823a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown return mActive; 6833a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown } 6843a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown 6853a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown @Override 6865c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) { 6875c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (fragment.mIndex < 0) { 68813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException( new IllegalStateException("Fragment " + fragment 68913fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + " is not currently in the FragmentManager")); 6905c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 6915c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (fragment.mState > Fragment.INITIALIZING) { 6925c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn Bundle result = saveFragmentBasicState(fragment); 6935c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn return result != null ? new Fragment.SavedState(result) : null; 6945c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 6955c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn return null; 6965c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 6975c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 6985c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn @Override 69901df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler public boolean isDestroyed() { 70001df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler return mDestroyed; 70101df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler } 70201df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler 70301df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler @Override 704cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public String toString() { 705cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn StringBuilder sb = new StringBuilder(128); 706cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn sb.append("FragmentManager{"); 707cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn sb.append(Integer.toHexString(System.identityHashCode(this))); 708cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn sb.append(" in "); 7090adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (mParent != null) { 7100adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn DebugUtils.buildShortClassTag(mParent, sb); 7110adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } else { 712d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy DebugUtils.buildShortClassTag(mHost, sb); 7130adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 714cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn sb.append("}}"); 715cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return sb.toString(); 716cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 717cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 718cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 719cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 720cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn String innerPrefix = prefix + " "; 721cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 722cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int N; 723cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive != null) { 724cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mActive.size(); 725cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 726cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print("Active Fragments in "); 727cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(Integer.toHexString(System.identityHashCode(this))); 728cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(":"); 729cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 730cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 731cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 732cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(f); 733cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null) { 734cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.dump(innerPrefix, fd, writer, args); 735cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 736cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 737cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 738cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 739cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 740cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAdded != null) { 741cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mAdded.size(); 742cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 743cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("Added Fragments:"); 744cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 745cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 746cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 747cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(f.toString()); 748cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 749cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 750cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 751cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 752cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mCreatedMenus != null) { 753cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mCreatedMenus.size(); 754cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 755cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("Fragments Created Menus:"); 756cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 757cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mCreatedMenus.get(i); 758cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 759cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(f.toString()); 760cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 761cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 762cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 763cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 764cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStack != null) { 765cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mBackStack.size(); 766cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 767cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("Back Stack:"); 768cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 769cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackRecord bs = mBackStack.get(i); 770cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 771cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(bs.toString()); 772cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn bs.dump(innerPrefix, fd, writer, args); 773cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 774cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 775cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 776cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 777cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 778cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackIndices != null) { 779cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mBackStackIndices.size(); 780cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 781cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("Back Stack Indices:"); 782cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 783cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackRecord bs = mBackStackIndices.get(i); 784cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 785cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(bs); 786cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 787cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 788cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 789cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 790cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) { 791cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print("mAvailBackStackIndices: "); 792cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(Arrays.toString(mAvailBackStackIndices.toArray())); 793cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 794cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 795cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 796cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mPendingActions != null) { 797cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mPendingActions.size(); 798cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 799cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("Pending Actions:"); 800cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 801cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Runnable r = mPendingActions.get(i); 802cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 803cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(r); 804cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 805cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 806cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 807cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 808cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("FragmentManager misc state:"); 809d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy writer.print(prefix); writer.print(" mHost="); writer.println(mHost); 8100adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn writer.print(prefix); writer.print(" mContainer="); writer.println(mContainer); 8110adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (mParent != null) { 8120adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn writer.print(prefix); writer.print(" mParent="); writer.println(mParent); 8130adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 814cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState); 815cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(" mStateSaved="); writer.print(mStateSaved); 816cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(" mDestroyed="); writer.println(mDestroyed); 817cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mNeedMenuInvalidate) { 818cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" mNeedMenuInvalidate="); 819cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(mNeedMenuInvalidate); 820cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 821cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mNoTransactionsBecause != null) { 822cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" mNoTransactionsBecause="); 823cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(mNoTransactionsBecause); 824cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 825cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailIndices != null && mAvailIndices.size() > 0) { 826cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" mAvailIndices: "); 827cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(Arrays.toString(mAvailIndices.toArray())); 828cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 829cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 830cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 8319277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f); 8329277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static final Interpolator DECELERATE_CUBIC = new DecelerateInterpolator(1.5f); 8339277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static final Interpolator ACCELERATE_QUINT = new AccelerateInterpolator(2.5f); 8349277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static final Interpolator ACCELERATE_CUBIC = new AccelerateInterpolator(1.5f); 8359277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 8369277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static final int ANIM_DUR = 220; 8379277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 8389277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static Animation makeOpenCloseAnimation(Context context, float startScale, 8399277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn float endScale, float startAlpha, float endAlpha) { 8409277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn AnimationSet set = new AnimationSet(false); 8419277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn ScaleAnimation scale = new ScaleAnimation(startScale, endScale, startScale, endScale, 8429277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f); 8439277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn scale.setInterpolator(DECELERATE_QUINT); 8449277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn scale.setDuration(ANIM_DUR); 8459277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn set.addAnimation(scale); 8469277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn AlphaAnimation alpha = new AlphaAnimation(startAlpha, endAlpha); 8479277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn alpha.setInterpolator(DECELERATE_CUBIC); 8489277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn alpha.setDuration(ANIM_DUR); 8499277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn set.addAnimation(alpha); 8509277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn return set; 8519277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn } 8529277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 8539277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static Animation makeFadeAnimation(Context context, float start, float end) { 8549277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn AlphaAnimation anim = new AlphaAnimation(start, end); 8559277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn anim.setInterpolator(DECELERATE_CUBIC); 8569277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn anim.setDuration(ANIM_DUR); 8579277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn return anim; 8589277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn } 85903526560f132021f8fd7290259762ab362d4d567Doris Liu 8609277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn Animation loadAnimation(Fragment fragment, int transit, boolean enter, 861cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int transitionStyle) { 8629277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn Animation animObj = fragment.onCreateAnimation(transit, enter, 863cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mNextAnim); 864cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (animObj != null) { 865cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return animObj; 866cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 867cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 868cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mNextAnim != 0) { 869d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy Animation anim = AnimationUtils.loadAnimation(mHost.getContext(), fragment.mNextAnim); 870cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (anim != null) { 871cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return anim; 872cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 873cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 874cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 875cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (transit == 0) { 876cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 877cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 878cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 879cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int styleIndex = transitToStyleIndex(transit, enter); 880cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (styleIndex < 0) { 881cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 882cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 88303526560f132021f8fd7290259762ab362d4d567Doris Liu 8849277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn switch (styleIndex) { 8859277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_OPEN_ENTER: 886d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeOpenCloseAnimation(mHost.getContext(), 1.125f, 1.0f, 0, 1); 8879277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_OPEN_EXIT: 888d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeOpenCloseAnimation(mHost.getContext(), 1.0f, .975f, 1, 0); 8899277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_CLOSE_ENTER: 890d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeOpenCloseAnimation(mHost.getContext(), .975f, 1.0f, 0, 1); 8919277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_CLOSE_EXIT: 892d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeOpenCloseAnimation(mHost.getContext(), 1.0f, 1.075f, 1, 0); 8939277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_FADE_ENTER: 894d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeFadeAnimation(mHost.getContext(), 0, 1); 8959277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_FADE_EXIT: 896d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeFadeAnimation(mHost.getContext(), 1, 0); 8979277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn } 8989277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 8998491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy if (transitionStyle == 0 && mHost.onHasWindowAnimations()) { 9008491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy transitionStyle = mHost.onGetWindowAnimations(); 901cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 902cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (transitionStyle == 0) { 903cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 904cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 905cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 906cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle, 907cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // com.android.internal.R.styleable.FragmentAnimation); 908cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //int anim = attrs.getResourceId(styleIndex, 0); 909cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //attrs.recycle(); 910cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 911cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //if (anim == 0) { 912cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // return null; 913cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //} 914cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 915cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //return AnimatorInflater.loadAnimator(mActivity, anim); 916cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 917cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 918cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 919abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell public void performPendingDeferredStart(Fragment f) { 920abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell if (f.mDeferStart) { 92179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (mExecutingActions) { 92279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell // Wait until we're done executing our pending transactions 92379398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell mHavePendingDeferredStart = true; 92479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell return; 92579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 926abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell f.mDeferStart = false; 9275506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(f, mCurState, 0, 0, false); 928abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 929abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 930abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell 93103526560f132021f8fd7290259762ab362d4d567Doris Liu /** 93203526560f132021f8fd7290259762ab362d4d567Doris Liu * Sets the to be animated view on hardware layer during the animation. Note 93303526560f132021f8fd7290259762ab362d4d567Doris Liu * that calling this will replace any existing animation listener on the animation 93403526560f132021f8fd7290259762ab362d4d567Doris Liu * with a new one, as animations do not support more than one listeners. Therefore, 93503526560f132021f8fd7290259762ab362d4d567Doris Liu * animations that already have listeners should do the layer change operations 93603526560f132021f8fd7290259762ab362d4d567Doris Liu * in their existing listeners, rather than calling this function. 93703526560f132021f8fd7290259762ab362d4d567Doris Liu */ 93803526560f132021f8fd7290259762ab362d4d567Doris Liu private void setHWLayerAnimListenerIfAlpha(final View v, Animation anim) { 93903526560f132021f8fd7290259762ab362d4d567Doris Liu if (v == null || anim == null) { 94003526560f132021f8fd7290259762ab362d4d567Doris Liu return; 94103526560f132021f8fd7290259762ab362d4d567Doris Liu } 94203526560f132021f8fd7290259762ab362d4d567Doris Liu if (shouldRunOnHWLayer(v, anim)) { 943d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu AnimationListener originalListener = null; 944d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu try { 945d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu if (sAnimationListenerField == null) { 946d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu sAnimationListenerField = Animation.class.getDeclaredField("mListener"); 947d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu sAnimationListenerField.setAccessible(true); 948d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu } 949d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu originalListener = (AnimationListener) sAnimationListenerField.get(anim); 950d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu } catch (NoSuchFieldException e) { 951d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu Log.e(TAG, "No field with the name mListener is found in Animation class", e); 952d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu } catch (IllegalAccessException e) { 953d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu Log.e(TAG, "Cannot access Animation's mListener field", e); 954d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu } 955d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu // If there's already a listener set on the animation, we need wrap the new listener 956d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu // around the existing listener, so that they will both get animation listener 957d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu // callbacks. 958d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu anim.setAnimationListener(new AnimateOnHWLayerIfNeededListener(v, anim, 959d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu originalListener)); 96003526560f132021f8fd7290259762ab362d4d567Doris Liu } 96103526560f132021f8fd7290259762ab362d4d567Doris Liu } 96203526560f132021f8fd7290259762ab362d4d567Doris Liu 9635506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn void moveToState(Fragment f, int newState, int transit, int transitionStyle, 9645506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn boolean keepActive) { 965cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Fragments that are not currently added will sit in the onCreate() state. 96674c671b3b67000bf16b4865a8d361344310dccbeDianne Hackborn if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) { 967cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn newState = Fragment.CREATED; 968cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 9692c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn if (f.mRemoving && newState > f.mState) { 9702c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn // While removing a fragment, we can't change it to a higher state. 9712c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn newState = f.mState; 9722c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn } 9736cafd27a3c89dfed10d7e226dc6168307513d4a6Adam Powell // Defer start if requested; don't allow it to move to STARTED or higher 9746cafd27a3c89dfed10d7e226dc6168307513d4a6Adam Powell // if it's not already started. 9756cafd27a3c89dfed10d7e226dc6168307513d4a6Adam Powell if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) { 976abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell newState = Fragment.STOPPED; 977abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 978cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mState < newState) { 9799277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn // For fragments that are created from a layout, when restoring from 9809277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn // state we don't want to allow them to be created until they are 9819277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn // being reloaded from the layout. 9829277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn if (f.mFromLayout && !f.mInLayout) { 9839277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn return; 9849277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn } 985cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mAnimatingAway != null) { 986cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // The fragment is currently being animated... but! Now we 987cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // want to move our state back up. Give up on waiting for the 988cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // animation, move to whatever the final state should be once 989cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // the animation is done, and then we can proceed from there. 990cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mAnimatingAway = null; 9915506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(f, f.mStateAfterAnimating, 0, 0, true); 992cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 993cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn switch (f.mState) { 994cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.INITIALIZING: 995cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "moveto CREATED: " + f); 996cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mSavedFragmentState != null) { 997d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy f.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader()); 998cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray( 999cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.VIEW_STATE_TAG); 1000cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mTarget = getFragment(f.mSavedFragmentState, 1001cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.TARGET_STATE_TAG); 1002cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mTarget != null) { 1003cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mTargetRequestCode = f.mSavedFragmentState.getInt( 1004cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0); 1005cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 100679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell f.mUserVisibleHint = f.mSavedFragmentState.getBoolean( 100779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true); 100879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (!f.mUserVisibleHint) { 100979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell f.mDeferStart = true; 101079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (newState > Fragment.STOPPED) { 101179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell newState = Fragment.STOPPED; 101279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 101379398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 1014cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1015d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy f.mHost = mHost; 10160adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mParentFragment = mParent; 10170adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mFragmentManager = mParent != null 1018d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy ? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl(); 1019cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mCalled = false; 1020b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy f.onAttach(mHost.getContext()); 1021cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!f.mCalled) { 1022cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new SuperNotCalledException("Fragment " + f 1023cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + " did not call through to super.onAttach()"); 1024cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 10250adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f.mParentFragment == null) { 1026b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy mHost.onAttachFragment(f); 10270adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 10280adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn 1029cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!f.mRetaining) { 10300adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performCreate(f.mSavedFragmentState); 1031cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1032cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mRetaining = false; 1033cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mFromLayout) { 1034cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // For fragments that are part of the content view 1035cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // layout, we need to instantiate the view immediately 1036cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // and the inflater will take care of adding it. 10370adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mView = f.performCreateView(f.getLayoutInflater( 10380adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mSavedFragmentState), null, f.mSavedFragmentState); 1039cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mView != null) { 1040cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView = f.mView; 10415e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes if (Build.VERSION.SDK_INT >= 11) { 10425e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes ViewCompat.setSaveFromParentEnabled(f.mView, false); 10435e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes } else { 10445e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes f.mView = NoSaveStateFrameLayout.wrap(f.mView); 10455e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes } 1046715a71e5f73cbc33f307cbd3625db8f889a05c1aDianne Hackborn if (f.mHidden) f.mView.setVisibility(View.GONE); 1047e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn f.onViewCreated(f.mView, f.mSavedFragmentState); 1048cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1049cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView = null; 1050cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1051cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1052cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.CREATED: 1053cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState > Fragment.CREATED) { 1054e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f); 1055cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!f.mFromLayout) { 1056cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ViewGroup container = null; 1057cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mContainerId != 0) { 105836bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell if (f.mContainerId == View.NO_ID) { 105936bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell throwException(new IllegalArgumentException( 106036bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell "Cannot create fragment " 106136bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell + f 106236bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell + " for a container view with no id")); 106336bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell } 106436bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell container = (ViewGroup) mContainer.onFindViewById(f.mContainerId); 1065cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (container == null && !f.mRestored) { 106636bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell String resName; 106736bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell try { 106836bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell resName = f.getResources().getResourceName(f.mContainerId); 106936bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell } catch (NotFoundException e) { 107036bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell resName = "unknown"; 107136bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell } 107213fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalArgumentException( 107313fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn "No view found for id 0x" 107413fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + Integer.toHexString(f.mContainerId) + " (" 107536bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell + resName 107613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + ") for fragment " + f)); 1077cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1078cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1079cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mContainer = container; 10800adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mView = f.performCreateView(f.getLayoutInflater( 10810adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mSavedFragmentState), container, f.mSavedFragmentState); 1082cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mView != null) { 1083cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView = f.mView; 10845e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes if (Build.VERSION.SDK_INT >= 11) { 10855e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes ViewCompat.setSaveFromParentEnabled(f.mView, false); 10865e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes } else { 10875e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes f.mView = NoSaveStateFrameLayout.wrap(f.mView); 10885e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes } 1089cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (container != null) { 10909277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn Animation anim = loadAnimation(f, transit, true, 1091cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn transitionStyle); 1092cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (anim != null) { 109303526560f132021f8fd7290259762ab362d4d567Doris Liu setHWLayerAnimListenerIfAlpha(f.mView, anim); 1094cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mView.startAnimation(anim); 1095cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1096cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn container.addView(f.mView); 1097cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1098715a71e5f73cbc33f307cbd3625db8f889a05c1aDianne Hackborn if (f.mHidden) f.mView.setVisibility(View.GONE); 1099e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn f.onViewCreated(f.mView, f.mSavedFragmentState); 1100cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView = null; 1102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 11040adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn 11050adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performActivityCreated(f.mSavedFragmentState); 1106e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (f.mView != null) { 11070adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.restoreViewState(f.mSavedFragmentState); 1108e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mSavedFragmentState = null; 1110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.ACTIVITY_CREATED: 1112e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn case Fragment.STOPPED: 1113e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (newState > Fragment.STOPPED) { 1114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "moveto STARTED: " + f); 11159c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn f.performStart(); 1116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.STARTED: 1118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState > Fragment.STARTED) { 1119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f); 11200adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performResume(); 11214e6647fe2551985f33407acd712a4942b090207aDianne Hackborn f.mSavedFragmentState = null; 11224e6647fe2551985f33407acd712a4942b090207aDianne Hackborn f.mSavedViewState = null; 1123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else if (f.mState > newState) { 1126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn switch (f.mState) { 1127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.RESUMED: 1128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState < Fragment.RESUMED) { 1129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f); 11300adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performPause(); 1131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.STARTED: 1133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState < Fragment.STARTED) { 1134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f); 1135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.performStop(); 1136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1137e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn case Fragment.STOPPED: 1138218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn if (newState < Fragment.STOPPED) { 1139218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn if (DEBUG) Log.v(TAG, "movefrom STOPPED: " + f); 1140218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn f.performReallyStop(); 1141218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn } 1142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.ACTIVITY_CREATED: 1143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState < Fragment.ACTIVITY_CREATED) { 1144e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f); 1145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mView != null) { 1146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Need to save the current view state if not 1147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // done already. 11488491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy if (mHost.onShouldSaveFragmentState(f) && f.mSavedViewState == null) { 1149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn saveFragmentViewState(f); 1150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 11529c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn f.performDestroyView(); 1153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mView != null && f.mContainer != null) { 1154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Animation anim = null; 1155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mCurState > Fragment.INITIALIZING && !mDestroyed) { 11569277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn anim = loadAnimation(f, transit, false, 1157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn transitionStyle); 1158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (anim != null) { 1160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final Fragment fragment = f; 11619d5a2665b76a1e87e1442d00ed699150ffbfb658Aurimas Liutikas final ViewGroup container = f.mContainer; 1162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mAnimatingAway = f.mView; 1163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mStateAfterAnimating = newState; 116403526560f132021f8fd7290259762ab362d4d567Doris Liu final View viewToAnimate = f.mView; 116503526560f132021f8fd7290259762ab362d4d567Doris Liu anim.setAnimationListener(new AnimateOnHWLayerIfNeededListener( 116603526560f132021f8fd7290259762ab362d4d567Doris Liu viewToAnimate, anim) { 1167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 1168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onAnimationEnd(Animation animation) { 116903526560f132021f8fd7290259762ab362d4d567Doris Liu super.onAnimationEnd(animation); 1170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mAnimatingAway != null) { 11719d5a2665b76a1e87e1442d00ed699150ffbfb658Aurimas Liutikas container.removeView(fragment.mAnimatingAway); 1172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mAnimatingAway = null; 1173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(fragment, fragment.mStateAfterAnimating, 11745506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn 0, 0, false); 1175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }); 11789277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn f.mView.startAnimation(anim); 11799d5a2665b76a1e87e1442d00ed699150ffbfb658Aurimas Liutikas } else { 11809d5a2665b76a1e87e1442d00ed699150ffbfb658Aurimas Liutikas f.mContainer.removeView(f.mView); 1181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mContainer = null; 1184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mView = null; 1185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView = null; 1186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.CREATED: 1188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState < Fragment.CREATED) { 1189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mDestroyed) { 1190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mAnimatingAway != null) { 1191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // The fragment's containing activity is 1192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // being destroyed, but this fragment is 1193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // currently animating away. Stop the 1194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // animation right now -- it is not needed, 1195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // and we can't wait any more on destroying 1196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // the fragment. 1197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn View v = f.mAnimatingAway; 1198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mAnimatingAway = null; 1199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn v.clearAnimation(); 1200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mAnimatingAway != null) { 1203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // We are waiting for the fragment's view to finish 1204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // animating away. Just make a note of the state 1205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // the fragment now should move to once the animation 1206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // is done. 1207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mStateAfterAnimating = newState; 12082c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn newState = Fragment.CREATED; 1209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f); 1211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!f.mRetaining) { 12120adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performDestroy(); 121320735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell } else { 121420735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell f.mState = Fragment.INITIALIZING; 1215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mCalled = false; 1218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.onDetach(); 1219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!f.mCalled) { 1220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new SuperNotCalledException("Fragment " + f 1221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + " did not call through to super.onDetach()"); 1222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 12235506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn if (!keepActive) { 12245506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn if (!f.mRetaining) { 12255506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn makeInactive(f); 12265506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn } else { 1227d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy f.mHost = null; 12286252d78085a07c9d6bb4645a4e8086bf23b0a49aTim Kilbourn f.mParentFragment = null; 12295506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn f.mFragmentManager = null; 12306252d78085a07c9d6bb4645a4e8086bf23b0a49aTim Kilbourn f.mChildFragmentManager = null; 12315506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn } 12322c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn } 1233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 123720735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell 123820735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell if (f.mState != newState) { 123920735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell Log.w(TAG, "moveToState: Fragment state for " + f + " not updated inline; " 124020735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell + "expected state " + newState + " found " + f.mState); 124120735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell f.mState = newState; 124220735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell } 1243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void moveToState(Fragment f) { 12465506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(f, mCurState, 0, 0, false); 1247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void moveToState(int newState, boolean always) { 1250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(newState, 0, 0, always); 1251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void moveToState(int newState, int transit, int transitStyle, boolean always) { 1254d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mHost == null && newState != Fragment.INITIALIZING) { 1255d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy throw new IllegalStateException("No host"); 1256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 12570adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn 1258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!always && mCurState == newState) { 1259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 1260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 12610adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn 1262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mCurState = newState; 1263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive != null) { 1264abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell boolean loadersRunning = false; 1265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mActive.size(); i++) { 1266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 1267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null) { 12685506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(f, newState, transit, transitStyle, false); 1269abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell if (f.mLoaderManager != null) { 1270abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell loadersRunning |= f.mLoaderManager.hasRunningLoaders(); 1271abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 1272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1275abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell if (!loadersRunning) { 1276abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell startPendingDeferredFragments(); 1277abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 1278abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell 1279d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mNeedMenuInvalidate && mHost != null && mCurState == Fragment.RESUMED) { 12808491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy mHost.onSupportInvalidateOptionsMenu(); 1281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mNeedMenuInvalidate = false; 1282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1285abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell 1286abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell void startPendingDeferredFragments() { 12871199ae7067cdf8cf3eb30c057a61ae71a0aea1e5Adam Powell if (mActive == null) return; 12881199ae7067cdf8cf3eb30c057a61ae71a0aea1e5Adam Powell 1289abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell for (int i=0; i<mActive.size(); i++) { 1290abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell Fragment f = mActive.get(i); 1291abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell if (f != null) { 1292abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell performPendingDeferredStart(f); 1293abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 1294abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 1295abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 1296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void makeActive(Fragment f) { 1298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mIndex >= 0) { 1299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 1300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailIndices == null || mAvailIndices.size() <= 0) { 1303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive == null) { 1304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive = new ArrayList<Fragment>(); 1305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 13060adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.setIndex(mActive.size(), mParent); 1307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive.add(f); 1308cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 13100adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent); 1311cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive.set(f.mIndex, f); 1312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1313be2c79d9a5439922030d2a3846c81c61f0e16912Dianne Hackborn if (DEBUG) Log.v(TAG, "Allocated fragment index " + f); 1314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void makeInactive(Fragment f) { 1317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mIndex < 0) { 1318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 1319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1321be2c79d9a5439922030d2a3846c81c61f0e16912Dianne Hackborn if (DEBUG) Log.v(TAG, "Freeing fragment index " + f); 1322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive.set(f.mIndex, null); 1323cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailIndices == null) { 1324cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailIndices = new ArrayList<Integer>(); 1325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailIndices.add(f.mIndex); 1327d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.inactivateFragment(f.mWho); 13289c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn f.initState(); 1329cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1330cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1331cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void addFragment(Fragment fragment, boolean moveToStateNow) { 1332cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAdded == null) { 1333cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdded = new ArrayList<Fragment>(); 1334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1335cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "add: " + fragment); 1336e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn makeActive(fragment); 1337e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (!fragment.mDetached) { 13383a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (mAdded.contains(fragment)) { 13393a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn throw new IllegalStateException("Fragment already added: " + fragment); 13403a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn } 1341e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mAdded.add(fragment); 1342e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mAdded = true; 1343e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mRemoving = false; 13442a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mHasMenu && fragment.mMenuVisible) { 1345e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mNeedMenuInvalidate = true; 1346e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1347e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (moveToStateNow) { 1348e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn moveToState(fragment); 1349e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void removeFragment(Fragment fragment, int transition, int transitionStyle) { 1354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting); 1355e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn final boolean inactive = !fragment.isInBackStack(); 1356e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (!fragment.mDetached || inactive) { 1357464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 1358464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn mAdded.remove(fragment); 1359464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn } 13602a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mHasMenu && fragment.mMenuVisible) { 1361e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mNeedMenuInvalidate = true; 1362e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1363e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mAdded = false; 1364e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mRemoving = true; 1365e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED, 13665506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn transition, transitionStyle, false); 1367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void hideFragment(Fragment fragment, int transition, int transitionStyle) { 1371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "hide: " + fragment); 1372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!fragment.mHidden) { 1373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mHidden = true; 1374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mView != null) { 1375495efcad8c8865e3e6f14e4ea3d79cfb1d2410f9Alexander Mironov Animation anim = loadAnimation(fragment, transition, false, 1376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn transitionStyle); 1377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (anim != null) { 137803526560f132021f8fd7290259762ab362d4d567Doris Liu setHWLayerAnimListenerIfAlpha(fragment.mView, anim); 1379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mView.startAnimation(anim); 1380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mView.setVisibility(View.GONE); 1382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 13832a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) { 1384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mNeedMenuInvalidate = true; 1385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.onHiddenChanged(true); 1387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void showFragment(Fragment fragment, int transition, int transitionStyle) { 1391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "show: " + fragment); 1392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mHidden) { 1393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mHidden = false; 1394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mView != null) { 13959277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn Animation anim = loadAnimation(fragment, transition, true, 1396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn transitionStyle); 1397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (anim != null) { 139803526560f132021f8fd7290259762ab362d4d567Doris Liu setHWLayerAnimListenerIfAlpha(fragment.mView, anim); 1399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mView.startAnimation(anim); 1400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mView.setVisibility(View.VISIBLE); 1402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 14032a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) { 1404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mNeedMenuInvalidate = true; 1405cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1406cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.onHiddenChanged(false); 1407cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1408cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1409cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1410e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn public void detachFragment(Fragment fragment, int transition, int transitionStyle) { 1411e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (DEBUG) Log.v(TAG, "detach: " + fragment); 1412e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (!fragment.mDetached) { 1413e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mDetached = true; 1414e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (fragment.mAdded) { 1415e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn // We are not already in back stack, so need to remove the fragment. 1416464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 14173a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) Log.v(TAG, "remove from detach: " + fragment); 1418464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn mAdded.remove(fragment); 1419464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn } 14202a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mHasMenu && fragment.mMenuVisible) { 1421e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mNeedMenuInvalidate = true; 1422e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1423e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mAdded = false; 14245506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false); 1425e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1426e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1427e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1428e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn 1429e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn public void attachFragment(Fragment fragment, int transition, int transitionStyle) { 1430e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (DEBUG) Log.v(TAG, "attach: " + fragment); 1431e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (fragment.mDetached) { 1432e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mDetached = false; 1433e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (!fragment.mAdded) { 1434464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded == null) { 1435464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn mAdded = new ArrayList<Fragment>(); 1436464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn } 14373a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (mAdded.contains(fragment)) { 14383a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn throw new IllegalStateException("Fragment already added: " + fragment); 14393a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn } 14403a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) Log.v(TAG, "add from attach: " + fragment); 1441e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mAdded.add(fragment); 1442e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mAdded = true; 14432a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mHasMenu && fragment.mMenuVisible) { 1444e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mNeedMenuInvalidate = true; 1445e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 14465506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(fragment, mCurState, transition, transitionStyle, false); 1447e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1448e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1449e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1450e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn 1451cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public Fragment findFragmentById(int id) { 1452464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 1453cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // First look through added fragments. 1454cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mAdded.size()-1; i>=0; i--) { 1455cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 1456cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null && f.mFragmentId == id) { 1457cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 1458cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1459cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1460464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn } 1461464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mActive != null) { 1462cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Now for any known fragment. 1463cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mActive.size()-1; i>=0; i--) { 1464cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 1465cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null && f.mFragmentId == id) { 1466cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 1467cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1468cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1469cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1470cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 1471cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1472cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1473cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public Fragment findFragmentByTag(String tag) { 1474464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null && tag != null) { 1475cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // First look through added fragments. 1476cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mAdded.size()-1; i>=0; i--) { 1477cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 1478cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null && tag.equals(f.mTag)) { 1479cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 1480cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1481cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1482464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn } 1483464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mActive != null && tag != null) { 1484cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Now for any known fragment. 1485cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mActive.size()-1; i>=0; i--) { 1486cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 1487cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null && tag.equals(f.mTag)) { 1488cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 1489cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1490cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1491cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1492cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 1493cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1494cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1495cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public Fragment findFragmentByWho(String who) { 1496cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive != null && who != null) { 1497cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mActive.size()-1; i>=0; i--) { 1498cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 14990adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null && (f=f.findFragmentByWho(who)) != null) { 1500cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 1501cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1502cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1503cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1504cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 1505cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1506cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1507cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn private void checkStateLoss() { 1508cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mStateSaved) { 1509cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException( 1510cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn "Can not perform this action after onSaveInstanceState"); 1511cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1512cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mNoTransactionsBecause != null) { 1513cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException( 1514cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn "Can not perform this action inside of " + mNoTransactionsBecause); 1515cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1516cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1517cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1518ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette /** 1519ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette * Adds an action to the queue of pending actions. 1520ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette * 1521ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette * @param action the action to add 1522ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette * @param allowStateLoss whether to allow loss of state information 1523ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette * @throws IllegalStateException if the activity has been destroyed 1524ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette */ 1525cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void enqueueAction(Runnable action, boolean allowStateLoss) { 1526cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!allowStateLoss) { 1527cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn checkStateLoss(); 1528cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1529cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 1530d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mDestroyed || mHost == null) { 1531cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException("Activity has been destroyed"); 1532cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1533cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mPendingActions == null) { 1534cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mPendingActions = new ArrayList<Runnable>(); 1535cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1536cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mPendingActions.add(action); 1537cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mPendingActions.size() == 1) { 1538d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.getHandler().removeCallbacks(mExecCommit); 1539d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.getHandler().post(mExecCommit); 1540cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1541cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1542cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1543cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1544cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int allocBackStackIndex(BackStackRecord bse) { 1545cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 1546cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) { 1547cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackIndices == null) { 1548cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices = new ArrayList<BackStackRecord>(); 1549cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1550cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int index = mBackStackIndices.size(); 1551cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); 1552cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.add(bse); 1553cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return index; 1554cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1555cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1556cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1); 1557cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); 1558cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.set(index, bse); 1559cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return index; 1560cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1561cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1562cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1563cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1564cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setBackStackIndex(int index, BackStackRecord bse) { 1565cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 1566cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackIndices == null) { 1567cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices = new ArrayList<BackStackRecord>(); 1568cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1569cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int N = mBackStackIndices.size(); 1570cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (index < N) { 1571cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); 1572cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.set(index, bse); 1573cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1574cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn while (N < index) { 1575cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.add(null); 1576cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailBackStackIndices == null) { 1577cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailBackStackIndices = new ArrayList<Integer>(); 1578cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1579cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Adding available back stack index " + N); 1580cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailBackStackIndices.add(N); 1581cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N++; 1582cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1583cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); 1584cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.add(bse); 1585cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1586cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1587cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1588cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1589cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void freeBackStackIndex(int index) { 1590cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 1591cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.set(index, null); 1592cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailBackStackIndices == null) { 1593cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailBackStackIndices = new ArrayList<Integer>(); 1594cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1595cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Freeing back stack index " + index); 1596cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailBackStackIndices.add(index); 1597cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1598cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1599cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1600e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell public void execSingleAction(Runnable action, boolean allowStateLoss) { 1601e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell if (mExecutingActions) { 1602e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell throw new IllegalStateException("FragmentManager is already executing transactions"); 1603e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell } 1604e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell 1605e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell if (Looper.myLooper() != mHost.getHandler().getLooper()) { 1606e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell throw new IllegalStateException("Must be called from main thread of fragment host"); 1607e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell } 1608e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell 1609e880475b147312ca62bed05bbeb37ec820d693aeAdam Powell if (!allowStateLoss) { 1610e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell checkStateLoss(); 1611e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell } 1612e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell 1613e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell mExecutingActions = true; 1614e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell action.run(); 1615e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell mExecutingActions = false; 1616e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell 1617e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell doPendingDeferredStart(); 1618e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell } 1619e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell 1620cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 1621cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Only call from main thread! 1622cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 1623cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean execPendingActions() { 1624cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mExecutingActions) { 1625e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell throw new IllegalStateException("FragmentManager is already executing transactions"); 1626cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1627cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1628d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (Looper.myLooper() != mHost.getHandler().getLooper()) { 1629e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell throw new IllegalStateException("Must be called from main thread of fragment host"); 1630cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1631cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1632cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean didSomething = false; 1633cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1634cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn while (true) { 1635cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int numActions; 1636cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1637cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 1638cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mPendingActions == null || mPendingActions.size() == 0) { 163979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell break; 1640cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1641cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1642cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn numActions = mPendingActions.size(); 1643cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mTmpActions == null || mTmpActions.length < numActions) { 1644cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mTmpActions = new Runnable[numActions]; 1645cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1646cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mPendingActions.toArray(mTmpActions); 1647cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mPendingActions.clear(); 1648d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.getHandler().removeCallbacks(mExecCommit); 1649cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1650cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1651cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mExecutingActions = true; 1652cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<numActions; i++) { 1653cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mTmpActions[i].run(); 16549f491519261135d9de50b60c2182755ccb665bf5Jeff Sharkey mTmpActions[i] = null; 1655cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1656cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mExecutingActions = false; 1657cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn didSomething = true; 1658cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 165979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell 1660e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell doPendingDeferredStart(); 1661e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell 1662e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell return didSomething; 1663e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell } 1664e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell 1665e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell void doPendingDeferredStart() { 166679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (mHavePendingDeferredStart) { 166779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell boolean loadersRunning = false; 1668e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell for (int i = 0; i < mActive.size(); i++) { 166979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell Fragment f = mActive.get(i); 167079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (f != null && f.mLoaderManager != null) { 167179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell loadersRunning |= f.mLoaderManager.hasRunningLoaders(); 167279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 167379398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 167479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (!loadersRunning) { 167579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell mHavePendingDeferredStart = false; 167679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell startPendingDeferredFragments(); 167779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 167879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 1679cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 168079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell 1681cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void reportBackStackChanged() { 1682cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackChangeListeners != null) { 1683cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mBackStackChangeListeners.size(); i++) { 1684cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackChangeListeners.get(i).onBackStackChanged(); 1685cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1686cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1687cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1688cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1689cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void addBackStackState(BackStackRecord state) { 1690cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStack == null) { 1691cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack = new ArrayList<BackStackRecord>(); 1692cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1693cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack.add(state); 1694cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn reportBackStackChanged(); 1695cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1696cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1697d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @SuppressWarnings("unused") 1698cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean popBackStackState(Handler handler, String name, int id, int flags) { 1699cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStack == null) { 1700cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 1701cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1702cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) { 1703cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int last = mBackStack.size()-1; 1704cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (last < 0) { 1705cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 1706cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1707cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final BackStackRecord bss = mBackStack.remove(last); 17089f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>(); 17099f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>(); 17109f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount bss.calculateBackFragments(firstOutFragments, lastInFragments); 17119f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount bss.popFromBackStack(true, null, firstOutFragments, lastInFragments); 1712cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn reportBackStackChanged(); 1713cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1714cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int index = -1; 1715cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (name != null || id >= 0) { 1716cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // If a name or ID is specified, look for that place in 1717cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // the stack. 1718cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn index = mBackStack.size()-1; 1719cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn while (index >= 0) { 1720cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackRecord bss = mBackStack.get(index); 1721cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (name != null && name.equals(bss.getName())) { 1722cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 1723cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1724cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (id >= 0 && id == bss.mIndex) { 1725cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 1726cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1727cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn index--; 1728cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1729cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (index < 0) { 1730cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 1731cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1732cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) { 1733cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn index--; 1734cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Consume all following entries that match. 1735cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn while (index >= 0) { 1736cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackRecord bss = mBackStack.get(index); 1737cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if ((name != null && name.equals(bss.getName())) 1738cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn || (id >= 0 && id == bss.mIndex)) { 1739cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn index--; 1740cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn continue; 1741cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1742cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 1743cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1744cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1745cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1746cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (index == mBackStack.size()-1) { 1747cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 1748cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1749cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final ArrayList<BackStackRecord> states 1750cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn = new ArrayList<BackStackRecord>(); 1751cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mBackStack.size()-1; i>index; i--) { 1752cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn states.add(mBackStack.remove(i)); 1753cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1754cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final int LAST = states.size()-1; 17559f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>(); 17569f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>(); 17579f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount for (int i=0; i<=LAST; i++) { 17589f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount states.get(i).calculateBackFragments(firstOutFragments, lastInFragments); 17599f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount } 17609f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount BackStackRecord.TransitionState state = null; 1761cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<=LAST; i++) { 1762cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i)); 17639f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount state = states.get(i).popFromBackStack(i == LAST, state, 17649f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount firstOutFragments, lastInFragments); 1765cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1766cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn reportBackStackChanged(); 1767cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1768cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return true; 1769cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1770cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1771c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell FragmentManagerNonConfig retainNonConfig() { 1772cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> fragments = null; 1773c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell ArrayList<FragmentManagerNonConfig> childFragments = null; 1774cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive != null) { 1775cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mActive.size(); i++) { 1776cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 1777c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell if (f != null) { 1778c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell if (f.mRetainInstance) { 1779c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell if (fragments == null) { 1780c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell fragments = new ArrayList<Fragment>(); 1781c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell } 1782c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell fragments.add(f); 1783c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell f.mRetaining = true; 1784c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1; 1785c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f); 1786c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell } 1787c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell boolean addedChild = false; 1788c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell if (f.mChildFragmentManager != null) { 1789c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell FragmentManagerNonConfig child = f.mChildFragmentManager.retainNonConfig(); 1790c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell if (child != null) { 1791c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell if (childFragments == null) { 1792c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell childFragments = new ArrayList<FragmentManagerNonConfig>(); 1793c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell for (int j = 0; j < i; j++) { 1794c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell childFragments.add(null); 1795c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell } 1796c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell } 1797c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell childFragments.add(child); 1798c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell addedChild = true; 1799c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell } 1800c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell } 1801c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell if (childFragments != null && !addedChild) { 1802c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell childFragments.add(null); 1803cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1804cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1805cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1806cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1807c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell if (fragments == null && childFragments == null) { 1808c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell return null; 1809c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell } 1810c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell return new FragmentManagerNonConfig(fragments, childFragments); 1811cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1812cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1813cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void saveFragmentViewState(Fragment f) { 1814cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mInnerView == null) { 1815cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 1816cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1817cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mStateArray == null) { 1818cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateArray = new SparseArray<Parcelable>(); 1819ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn } else { 1820ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn mStateArray.clear(); 1821cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1822cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView.saveHierarchyState(mStateArray); 1823cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mStateArray.size() > 0) { 1824cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mSavedViewState = mStateArray; 1825cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateArray = null; 1826cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1827cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1828cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 18295c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn Bundle saveFragmentBasicState(Fragment f) { 18305c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn Bundle result = null; 18315c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 18325c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (mStateBundle == null) { 18335c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mStateBundle = new Bundle(); 18345c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 18350adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performSaveInstanceState(mStateBundle); 18365c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (!mStateBundle.isEmpty()) { 18375c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn result = mStateBundle; 18385c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mStateBundle = null; 18395c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 18405c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 18415c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (f.mView != null) { 18425c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn saveFragmentViewState(f); 18435c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 18445c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (f.mSavedViewState != null) { 18455c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (result == null) { 18465c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn result = new Bundle(); 18475c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 18485c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn result.putSparseParcelableArray( 18495c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); 18505c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 185179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (!f.mUserVisibleHint) { 1852f4c0cf637ba73374a206cec26c09d4dfa4c1a364Jake Wharton if (result == null) { 1853f4c0cf637ba73374a206cec26c09d4dfa4c1a364Jake Wharton result = new Bundle(); 1854f4c0cf637ba73374a206cec26c09d4dfa4c1a364Jake Wharton } 185579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell // Only add this if it's not the default value 185679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint); 185779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 18585c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 18595c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn return result; 18605c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 18615c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 1862cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Parcelable saveAllState() { 1863cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Make sure all pending operations have now been executed to get 1864cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // our state update-to-date. 1865cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn execPendingActions(); 1866cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1867681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn if (HONEYCOMB) { 1868681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // As of Honeycomb, we save state after pausing. Prior to that 1869681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // it is before pausing. With fragments this is an issue, since 1870681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // there are many things you may do after pausing but before 1871681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // stopping that change the fragment state. For those older 1872681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // devices, we will not at this point say that we have saved 1873681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // the state, so we will allow them to continue doing fragment 1874681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // transactions. This retains the same semantics as Honeycomb, 1875681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // though you do have the risk of losing the very most recent state 1876681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // if the process is killed... we'll live with that. 1877681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mStateSaved = true; 1878681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 1879cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1880cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive == null || mActive.size() <= 0) { 1881cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 1882cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1883cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1884cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // First collect all active fragments. 1885cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int N = mActive.size(); 1886cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentState[] active = new FragmentState[N]; 1887cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean haveFragments = false; 1888cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 1889cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 1890cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null) { 18911b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn if (f.mIndex < 0) { 189213fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalStateException( 189313fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn "Failure saving state: active " + f 189413fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + " has cleared index: " + f.mIndex)); 18951b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn } 18961b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn 1897cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn haveFragments = true; 1898cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1899cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentState fs = new FragmentState(f); 1900cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn active[i] = fs; 1901cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1902cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) { 19035c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn fs.mSavedFragmentState = saveFragmentBasicState(f); 1904cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1905cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mTarget != null) { 1906cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mTarget.mIndex < 0) { 190713fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalStateException( 190813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn "Failure saving state: " + f 190913fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + " has target not in fragment manager: " + f.mTarget)); 1910cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1911cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fs.mSavedFragmentState == null) { 1912cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fs.mSavedFragmentState = new Bundle(); 1913cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1914cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn putFragment(fs.mSavedFragmentState, 1915cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget); 1916cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mTargetRequestCode != 0) { 1917cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fs.mSavedFragmentState.putInt( 1918cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 1919cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mTargetRequestCode); 1920cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1921cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1922cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1923cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1924cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fs.mSavedFragmentState = f.mSavedFragmentState; 1925cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1926cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1927cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Saved state of " + f + ": " 1928cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + fs.mSavedFragmentState); 1929cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1930cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1931cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1932cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!haveFragments) { 1933cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "saveAllState: no fragments!"); 1934cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 1935cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1936cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1937cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int[] added = null; 1938cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackState[] backStack = null; 1939cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1940cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Build list of currently added fragments. 1941cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAdded != null) { 1942cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mAdded.size(); 1943cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 1944cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn added = new int[N]; 1945cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 1946cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn added[i] = mAdded.get(i).mIndex; 19471b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn if (added[i] < 0) { 194813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalStateException( 194913fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn "Failure saving state: active " + mAdded.get(i) 195013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + " has cleared index: " + added[i])); 19511b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn } 1952cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i 1953cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + ": " + mAdded.get(i)); 1954cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1955cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1956cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1957cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1958cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Now save back stack. 1959cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStack != null) { 1960cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mBackStack.size(); 1961cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 1962cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn backStack = new BackStackState[N]; 1963cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 1964d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy backStack[i] = new BackStackState(mBackStack.get(i)); 1965cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i 1966cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + ": " + mBackStack.get(i)); 1967cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1968cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1969cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1970cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1971cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerState fms = new FragmentManagerState(); 1972cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fms.mActive = active; 1973cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fms.mAdded = added; 1974cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fms.mBackStack = backStack; 1975cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return fms; 1976cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1977cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1978c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) { 1979cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // If there is no saved state at all, then there can not be 1980cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // any nonConfig fragments either, so that is that. 1981cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (state == null) return; 1982cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerState fms = (FragmentManagerState)state; 1983cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fms.mActive == null) return; 1984c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell 1985c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell List<FragmentManagerNonConfig> childNonConfigs = null; 1986c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell 1987cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // First re-attach any non-config instances we are retaining back 1988cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // to their saved state, so we don't try to instantiate them again. 1989cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (nonConfig != null) { 1990c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell List<Fragment> nonConfigFragments = nonConfig.getFragments(); 1991c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell childNonConfigs = nonConfig.getChildNonConfigs(); 1992c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0; 1993c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell for (int i = 0; i < count; i++) { 1994c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell Fragment f = nonConfigFragments.get(i); 1995cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f); 1996cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentState fs = fms.mActive[f.mIndex]; 1997cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fs.mInstance = f; 1998cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mSavedViewState = null; 1999cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mBackStackNesting = 0; 2000cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInLayout = false; 2001cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mAdded = false; 20022c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn f.mTarget = null; 2003cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fs.mSavedFragmentState != null) { 2004d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy fs.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader()); 2005cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray( 2006cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.VIEW_STATE_TAG); 20078e4a59b54e9225b77151805dd6b8867dcd8e60a4Craig Mautner f.mSavedFragmentState = fs.mSavedFragmentState; 2008cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2009cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2010cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2011cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2012cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Build the full list of active fragments, instantiating them from 2013cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // their saved state. 2014c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell mActive = new ArrayList<>(fms.mActive.length); 2015cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailIndices != null) { 2016cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailIndices.clear(); 2017cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2018cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<fms.mActive.length; i++) { 2019cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentState fs = fms.mActive[i]; 2020cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fs != null) { 2021c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell FragmentManagerNonConfig childNonConfig = null; 2022c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell if (childNonConfigs != null && i < childNonConfigs.size()) { 2023c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell childNonConfig = childNonConfigs.get(i); 2024c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell } 2025c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell Fragment f = fs.instantiate(mHost, mParent, childNonConfig); 20263a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f); 2027cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive.add(f); 2028cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Now that the fragment is instantiated (or came from being 2029cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // retained above), clear mInstance in case we end up re-restoring 2030cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // from this FragmentState again. 2031cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fs.mInstance = null; 2032cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 2033cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive.add(null); 2034cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailIndices == null) { 2035cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailIndices = new ArrayList<Integer>(); 2036cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 20373a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i); 2038cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailIndices.add(i); 2039cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2040cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2041cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2042cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Update the target of all retained fragments. 2043cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (nonConfig != null) { 2044c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell List<Fragment> nonConfigFragments = nonConfig.getFragments(); 2045c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0; 2046c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell for (int i = 0; i < count; i++) { 2047c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell Fragment f = nonConfigFragments.get(i); 20482c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn if (f.mTargetIndex >= 0) { 20492c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn if (f.mTargetIndex < mActive.size()) { 20502c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn f.mTarget = mActive.get(f.mTargetIndex); 2051cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 2052cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Log.w(TAG, "Re-attaching retained fragment " + f 20532c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn + " target no longer exists: " + f.mTargetIndex); 2054cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mTarget = null; 2055cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2056cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2057cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2058cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2059cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2060cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Build the list of currently added fragments. 2061cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fms.mAdded != null) { 2062cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdded = new ArrayList<Fragment>(fms.mAdded.length); 2063cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<fms.mAdded.length; i++) { 2064cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(fms.mAdded[i]); 2065cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f == null) { 206613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalStateException( 206713fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn "No instantiated fragment for index #" + fms.mAdded[i])); 2068cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2069cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mAdded = true; 20703a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f); 20713a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (mAdded.contains(f)) { 20723a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn throw new IllegalStateException("Already added!"); 20733a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn } 2074cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdded.add(f); 2075cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2076cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 2077cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdded = null; 2078cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2079cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2080cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Build the back stack. 2081cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fms.mBackStack != null) { 2082cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length); 2083cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<fms.mBackStack.length; i++) { 2084cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackRecord bse = fms.mBackStack[i].instantiate(this); 20853a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) { 20863a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn Log.v(TAG, "restoreAllState: back stack #" + i 2087cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + " (index " + bse.mIndex + "): " + bse); 20883a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn LogWriter logw = new LogWriter(TAG); 20893a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn PrintWriter pw = new PrintWriter(logw); 20903a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn bse.dump(" ", pw, false); 20913a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn } 2092cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack.add(bse); 2093cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (bse.mIndex >= 0) { 2094cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setBackStackIndex(bse.mIndex, bse); 2095cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2096cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2097cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 2098cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack = null; 2099cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2100cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2101d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 21028491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy public void attachController(FragmentHostCallback host, 21030adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn FragmentContainer container, Fragment parent) { 2104d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mHost != null) throw new IllegalStateException("Already attached"); 2105d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost = host; 21060adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn mContainer = container; 21070adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn mParent = parent; 2108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void noteStateNotSaved() { 2111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateSaved = false; 2112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchCreate() { 2115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateSaved = false; 2116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.CREATED, false); 2117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchActivityCreated() { 2120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateSaved = false; 2121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.ACTIVITY_CREATED, false); 2122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchStart() { 2125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateSaved = false; 2126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.STARTED, false); 2127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchResume() { 2130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateSaved = false; 2131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.RESUMED, false); 2132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchPause() { 2135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.STARTED, false); 2136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchStop() { 2139681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // See saveAllState() for the explanation of this. We do this for 2140681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // all platform versions, to keep our behavior more consistent between 2141681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // them. 2142681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mStateSaved = true; 2143681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 2144e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn moveToState(Fragment.STOPPED, false); 2145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2147218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn public void dispatchReallyStop() { 2148218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn moveToState(Fragment.ACTIVITY_CREATED, false); 2149681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 2150681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 21510adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn public void dispatchDestroyView() { 21520adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn moveToState(Fragment.CREATED, false); 21530adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 21540adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn 2155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchDestroy() { 2156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mDestroyed = true; 2157e8b402b00c0cbdac050c349a5fc89c34580f3185Dianne Hackborn execPendingActions(); 2158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.INITIALIZING, false); 2159d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost = null; 21600adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn mContainer = null; 21610adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn mParent = null; 2162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchConfigurationChanged(Configuration newConfig) { 2165464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 2168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null) { 21690adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performConfigurationChanged(newConfig); 2170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchLowMemory() { 2176464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 2179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null) { 21800adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performLowMemory(); 2181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) { 2187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean show = false; 2188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> newMenus = null; 2189464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 21920adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null) { 21930adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f.performCreateOptionsMenu(menu, inflater)) { 21940adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn show = true; 21950adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (newMenus == null) { 21960adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn newMenus = new ArrayList<Fragment>(); 21970adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 21980adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn newMenus.add(f); 2199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2203c1e87ac17c772ee3c5fd3b3f08321226e2c7ffa4Chris Banes 2204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mCreatedMenus != null) { 2205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mCreatedMenus.size(); i++) { 2206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mCreatedMenus.get(i); 2207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newMenus == null || !newMenus.contains(f)) { 2208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.onDestroyOptionsMenu(); 2209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2212c1e87ac17c772ee3c5fd3b3f08321226e2c7ffa4Chris Banes 2213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mCreatedMenus = newMenus; 2214c1e87ac17c772ee3c5fd3b3f08321226e2c7ffa4Chris Banes 2215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return show; 2216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean dispatchPrepareOptionsMenu(Menu menu) { 2219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean show = false; 2220464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 22230adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null) { 22240adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f.performPrepareOptionsMenu(menu)) { 22250adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn show = true; 22260adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 2227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return show; 2231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean dispatchOptionsItemSelected(MenuItem item) { 2234464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 22370adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null) { 22380adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f.performOptionsItemSelected(item)) { 2239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return true; 2240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 2245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean dispatchContextItemSelected(MenuItem item) { 2248464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 22510adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null) { 22520adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f.performContextItemSelected(item)) { 2253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return true; 2254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 2259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchOptionsMenuClosed(Menu menu) { 2262464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 22650adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null) { 22660adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performOptionsMenuClosed(menu); 2267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2271461b48b4588ac21b97aa40553f04222c2c0344e7Chris Banes 2272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public static int reverseTransit(int transit) { 2273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int rev = 0; 2274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn switch (transit) { 2275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: 2276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE; 2277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: 2279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN; 2280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_FADE: 2282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE; 2283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return rev; 2286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 22899277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_OPEN_ENTER = 1; 22909277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_OPEN_EXIT = 2; 22919277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_CLOSE_ENTER = 3; 22929277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_CLOSE_EXIT = 4; 22939277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_FADE_ENTER = 5; 22949277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_FADE_EXIT = 6; 22959277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 2296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public static int transitToStyleIndex(int transit, boolean enter) { 2297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int animAttr = -1; 2298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn switch (transit) { 2299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: 23009277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn animAttr = enter ? ANIM_STYLE_OPEN_ENTER : ANIM_STYLE_OPEN_EXIT; 2301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: 23039277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn animAttr = enter ? ANIM_STYLE_CLOSE_ENTER : ANIM_STYLE_CLOSE_EXIT; 2304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_FADE: 23069277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn animAttr = enter ? ANIM_STYLE_FADE_ENTER : ANIM_STYLE_FADE_EXIT; 2307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2308cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return animAttr; 2310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 23110f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 23120f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell @Override 2313bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banes public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 23140f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (!"fragment".equals(name)) { 23150f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell return null; 23160f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 23170f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 23180f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell String fname = attrs.getAttributeValue(null, "class"); 23190f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell TypedArray a = context.obtainStyledAttributes(attrs, FragmentTag.Fragment); 23200f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fname == null) { 23210f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fname = a.getString(FragmentTag.Fragment_name); 23220f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 23230f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID); 23240f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell String tag = a.getString(FragmentTag.Fragment_tag); 23250f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell a.recycle(); 23260f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 2327d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (!Fragment.isSupportFragmentClass(mHost.getContext(), fname)) { 23280f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // Invalid support lib fragment; let the device's framework handle it. 23290f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // This will allow android.app.Fragments to do the right thing. 23300f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell return null; 23310f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 23320f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 23330f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell int containerId = parent != null ? parent.getId() : 0; 23340f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (containerId == View.NO_ID && id == View.NO_ID && tag == null) { 23350f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell throw new IllegalArgumentException(attrs.getPositionDescription() 23360f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname); 23370f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 23380f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 23390f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // If we restored from a previous state, we may already have 23400f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // instantiated this fragment from the state and should use 23410f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // that instance instead of making a new one. 23420f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null; 23430f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fragment == null && tag != null) { 23440f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment = findFragmentByTag(tag); 23450f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 23460f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fragment == null && containerId != View.NO_ID) { 23470f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment = findFragmentById(containerId); 23480f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 23490f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 23500f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x" 23510f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + Integer.toHexString(id) + " fname=" + fname 23520f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + " existing=" + fragment); 23530f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fragment == null) { 23540f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment = Fragment.instantiate(context, fname); 23550f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mFromLayout = true; 23560f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mFragmentId = id != 0 ? id : containerId; 23570f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mContainerId = containerId; 23580f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mTag = tag; 23590f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mInLayout = true; 23600f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mFragmentManager = this; 23611b84066e4233b4b0c8a32fffc30f95b8cd20ced4Chris Banes fragment.mHost = mHost; 2362b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState); 23630f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell addFragment(fragment, true); 23640f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 23650f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } else if (fragment.mInLayout) { 23660f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // A fragment already exists and it is not one we restored from 23670f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // previous state. 23680f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell throw new IllegalArgumentException(attrs.getPositionDescription() 23690f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + ": Duplicate id 0x" + Integer.toHexString(id) 23700f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId) 23710f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + " with another fragment for " + fname); 23720f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } else { 23730f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // This fragment was retained from a previous instance; get it 23740f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // going now. 23750f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mInLayout = true; 2376e4148d65bbd62585c68c5782c2081bab6b303568Todd Kennedy fragment.mHost = mHost; 23770f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // If this fragment is newly instantiated (either right now, or 23780f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // from last saved state), then give it the attributes to 23790f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // initialize itself. 23800f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (!fragment.mRetaining) { 2381b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState); 23820f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 23830f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 23840f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 23850f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // If we haven't finished entering the CREATED state ourselves yet, 23860f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // push the inflated child fragment along. 23870f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (mCurState < Fragment.CREATED && fragment.mFromLayout) { 23880f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell moveToState(fragment, Fragment.CREATED, 0, 0, false); 23890f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } else { 23900f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell moveToState(fragment); 23910f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 23920f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 23930f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fragment.mView == null) { 23940f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell throw new IllegalStateException("Fragment " + fname 23950f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + " did not create a view."); 23960f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 23970f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (id != 0) { 23980f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mView.setId(id); 23990f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 24000f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fragment.mView.getTag() == null) { 24010f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mView.setTag(tag); 24020f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 24030f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell return fragment.mView; 24040f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 24050f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 2406bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banes LayoutInflaterFactory getLayoutInflaterFactory() { 24070f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell return this; 24080f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 24090f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 24100f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell static class FragmentTag { 24110f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell public static final int[] Fragment = { 24120f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 0x01010003, 0x010100d0, 0x010100d1 24130f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell }; 24140f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell public static final int Fragment_id = 1; 24150f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell public static final int Fragment_name = 0; 24160f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell public static final int Fragment_tag = 2; 24170f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 2418cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn} 2419