FragmentManager.java revision d608cf6e08769bf320c1b595cbbd9a7664160449
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; 210f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powellimport android.content.res.TypedArray; 225e63ab9505a3a4d11374cbbec418c1aba921409dChris Banesimport android.os.Build; 23cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Bundle; 24cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Handler; 25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Looper; 26cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Parcel; 27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Parcelable; 28a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.IdRes; 29a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.StringRes; 30cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.support.v4.util.DebugUtils; 31cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.support.v4.util.LogWriter; 32bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banesimport android.support.v4.view.LayoutInflaterFactory; 335e63ab9505a3a4d11374cbbec418c1aba921409dChris Banesimport android.support.v4.view.ViewCompat; 340f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powellimport android.util.AttributeSet; 35cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.util.Log; 36cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.util.SparseArray; 379277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.AccelerateInterpolator; 389277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.AlphaAnimation; 39cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.Animation; 409277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.AnimationSet; 41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.AnimationUtils; 429277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.DecelerateInterpolator; 439277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.Interpolator; 449277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.ScaleAnimation; 45cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.Animation.AnimationListener; 46cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.Menu; 47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.MenuInflater; 48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.MenuItem; 49cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.View; 50cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.ViewGroup; 51cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.FileDescriptor; 53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.PrintWriter; 54cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.util.ArrayList; 55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.util.Arrays; 563a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brownimport java.util.List; 57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/** 59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Static library support version of the framework's {@link android.app.FragmentManager}. 60cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Used to write apps that run on platforms prior to Android 3.0. When running 61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on Android 3.0 or above, this implementation is still used; it does not try 629dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * to switch to the framework's implementation. See the framework {@link FragmentManager} 63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * documentation for a class overview. 649dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * 659dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <p>Your activity must derive from {@link FragmentActivity} to use this. From such an activity, 669dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * you can acquire the {@link FragmentManager} by calling 679dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * {@link FragmentActivity#getSupportFragmentManager}. 68cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 69cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic abstract class FragmentManager { 70cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 71cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Representation of an entry on the fragment back stack, as created 72cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * with {@link FragmentTransaction#addToBackStack(String) 73cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * FragmentTransaction.addToBackStack()}. Entries can later be 74cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * retrieved with {@link FragmentManager#getBackStackEntryAt(int) 75cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * FragmentManager.getBackStackEntry()}. 76cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 77cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * <p>Note that you should never hold on to a BackStackEntry object; 78cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the identifier as returned by {@link #getId} is the only thing that 79cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * will be persisted across activity instances. 80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public interface BackStackEntry { 82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 83cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the unique identifier for the entry. This is the only 84cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * representation of the entry that will persist across activity 85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * instances. 86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 87cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getId(); 88cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 89cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 902a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn * Get the name that was supplied to 912a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn * {@link FragmentTransaction#addToBackStack(String) 922a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn * FragmentTransaction.addToBackStack(String)} when creating this entry. 932a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn */ 942a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn public String getName(); 952a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn 962a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn /** 97cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the full bread crumb title resource identifier for the entry, 98cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * or 0 if it does not have one. 99cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 100a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye @StringRes 101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getBreadCrumbTitleRes(); 102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 104cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the short bread crumb title resource identifier for the entry, 105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * or 0 if it does not have one. 106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 107a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye @StringRes 108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getBreadCrumbShortTitleRes(); 109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the full bread crumb title for the entry, or null if it 112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * does not have one. 113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public CharSequence getBreadCrumbTitle(); 115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the short bread crumb title for the entry, or null if it 118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * does not have one. 119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public CharSequence getBreadCrumbShortTitle(); 121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Interface to watch for changes to the back stack. 125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public interface OnBackStackChangedListener { 127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Called whenever the contents of the back stack change. 129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onBackStackChanged(); 131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Start a series of edit operations on the Fragments associated with 135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * this FragmentManager. 136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * <p>Note: A fragment transaction can only be created/committed prior 138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to an activity saving its state. If you try to commit a transaction 139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * after {@link FragmentActivity#onSaveInstanceState FragmentActivity.onSaveInstanceState()} 140cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * (and prior to a following {@link FragmentActivity#onStart FragmentActivity.onStart} 141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * or {@link FragmentActivity#onResume FragmentActivity.onResume()}, you will get an error. 142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This is because the framework takes care of saving your current fragments 143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * in the state, and if changes are made after the state is saved then they 144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * will be lost.</p> 145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract FragmentTransaction beginTransaction(); 147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** @hide -- remove once prebuilts are in. */ 149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Deprecated 150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentTransaction openTransaction() { 151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return beginTransaction(); 152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * After a {@link FragmentTransaction} is committed with 156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it 157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * is scheduled to be executed asynchronously on the process's main thread. 158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * If you want to immediately executing any such pending operations, you 159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * can call this function (only from the main thread) to do so. Note that 160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * all callbacks and other related behavior will be done from within this 161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * call, so be careful about where this is called from. 162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return Returns true if there were any pending transactions to be 164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * executed. 165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract boolean executePendingTransactions(); 167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Finds a fragment that was identified by the given id either when inflated 170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * from XML or as the container ID when added in a transaction. This first 171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * searches through fragments that are currently added to the manager's 172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * activity; if no such fragment is found, then all fragments currently 173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on the back stack associated with this ID are searched. 174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return The fragment if found or null otherwise. 175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 176a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye public abstract Fragment findFragmentById(@IdRes int id); 177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Finds a fragment that was identified by the given tag either when inflated 180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * from XML or as supplied when added in a transaction. This first 181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * searches through fragments that are currently added to the manager's 182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * activity; if no such fragment is found, then all fragments currently 183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on the back stack are searched. 184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return The fragment if found or null otherwise. 185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract Fragment findFragmentByTag(String tag); 187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Flag for {@link #popBackStack(String, int)} 190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * and {@link #popBackStack(int, int)}: If set, and the name or ID of 191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * a back stack entry has been supplied, then all matching entries will 192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * be consumed until one that doesn't match is found or the bottom of 193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the stack is reached. Otherwise, all entries up to but not including that entry 194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * will be removed. 195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public static final int POP_BACK_STACK_INCLUSIVE = 1<<0; 197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Pop the top state off the back stack. Returns true if there was one 200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to pop, else false. This function is asynchronous -- it enqueues the 201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * request to pop, but the action will not be performed until the application 202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * returns to its event loop. 203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void popBackStack(); 205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Like {@link #popBackStack()}, but performs the operation immediately 208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * inside of the call. This is like calling {@link #executePendingTransactions()} 209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * afterwards. 210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return Returns true if there was something popped, else false. 211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract boolean popBackStackImmediate(); 213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Pop the last fragment transition from the manager's fragment 216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * back stack. If there is nothing to pop, false is returned. 217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This function is asynchronous -- it enqueues the 218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * request to pop, but the action will not be performed until the application 219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * returns to its event loop. 220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param name If non-null, this is the name of a previous back state 222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to look for; if found, all states up to that state will be popped. The 223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether 224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the named state itself is popped. If null, only the top state is popped. 225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. 226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void popBackStack(String name, int flags); 228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Like {@link #popBackStack(String, int)}, but performs the operation immediately 231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * inside of the call. This is like calling {@link #executePendingTransactions()} 232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * afterwards. 233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return Returns true if there was something popped, else false. 234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract boolean popBackStackImmediate(String name, int flags); 236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Pop all back stack states up to the one with the given identifier. 239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This function is asynchronous -- it enqueues the 240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * request to pop, but the action will not be performed until the application 241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * returns to its event loop. 242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param id Identifier of the stated to be popped. If no identifier exists, 244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * false is returned. 245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * The identifier is the number returned by 246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The 247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether 248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the named state itself is popped. 249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. 250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void popBackStack(int id, int flags); 252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Like {@link #popBackStack(int, int)}, but performs the operation immediately 255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * inside of the call. This is like calling {@link #executePendingTransactions()} 256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * afterwards. 257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return Returns true if there was something popped, else false. 258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract boolean popBackStackImmediate(int id, int flags); 260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the number of entries currently in the back stack. 263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract int getBackStackEntryCount(); 265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the BackStackEntry at index <var>index</var> in the back stack; 268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * entries start index 0 being the bottom of the stack. 269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract BackStackEntry getBackStackEntryAt(int index); 271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Add a new listener for changes to the fragment back stack. 274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener); 276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Remove a listener that was previously added with 279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}. 280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener); 282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Put a reference to a fragment in a Bundle. This Bundle can be 285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * persisted as saved state, and when later restoring 286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link #getFragment(Bundle, String)} will return the current 287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * instance of the same fragment. 288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param bundle The bundle in which to put the fragment reference. 290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param key The name of the entry in the bundle. 291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param fragment The Fragment whose reference is to be stored. 292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void putFragment(Bundle bundle, String key, Fragment fragment); 294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Retrieve the current Fragment instance for a reference previously 297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * placed with {@link #putFragment(Bundle, String, Fragment)}. 298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param bundle The bundle from which to retrieve the fragment reference. 300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param key The name of the entry in the bundle. 301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return Returns the current Fragment instance that is associated with 302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the given reference. 303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract Fragment getFragment(Bundle bundle, String key); 305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 306cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 3073a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown * Get a list of all fragments that have been added to the fragment manager. 3083a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown * 3093a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown * @return The list of all fragments or null if none. 3103a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown * @hide 3113a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown */ 3123a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown public abstract List<Fragment> getFragments(); 3133a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown 3143a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown /** 3155c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Save the current instance state of the given Fragment. This can be 3165c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * used later when creating a new instance of the Fragment and adding 3175c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * it to the fragment manager, to have it create itself to match the 3185c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * current state returned here. Note that there are limits on how 3195c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * this can be used: 3205c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * 3215c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * <ul> 3225c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * <li>The Fragment must currently be attached to the FragmentManager. 3235c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * <li>A new Fragment created using this saved state must be the same class 3245c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * type as the Fragment it was created from. 3255c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * <li>The saved state can not contain dependencies on other fragments -- 3265c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to 3275c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * store a fragment reference because that reference may not be valid when 3285c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * this saved state is later used. Likewise the Fragment's target and 3295c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * result code are not included in this state. 3305c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * </ul> 3315c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * 3325c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * @param f The Fragment whose state is to be saved. 3335c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * @return The generated state. This will be null if there was no 3345c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * interesting state created by the fragment. 3355c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn */ 3365c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f); 3375c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 3385c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn /** 33901df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler * Returns true if the final {@link android.app.Activity#onDestroy() Activity.onDestroy()} 34001df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler * call has been made on the FragmentManager's Activity, so this instance is now dead. 34101df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler */ 34201df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler public abstract boolean isDestroyed(); 34301df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler 34401df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler /** 345cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Print the FragmentManager's state into the given stream. 346cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 347cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param prefix Text to print at the front of each line. 348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param fd The raw file descriptor that the dump is being sent to. 349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param writer A PrintWriter to which the dump is to be set. 350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param args Additional arguments to the dump request. 351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args); 353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 355cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Control whether the framework's internal fragment manager debugging 356cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * logs are turned on. If enabled, you will see output in logcat as 357cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the framework performs fragment operations. 358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public static void enableDebugLogging(boolean enabled) { 360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.DEBUG = enabled; 361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn} 363cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 364cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornfinal class FragmentManagerState implements Parcelable { 365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentState[] mActive; 366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int[] mAdded; 367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackState[] mBackStack; 368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentManagerState() { 370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentManagerState(Parcel in) { 373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive = in.createTypedArray(FragmentState.CREATOR); 374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdded = in.createIntArray(); 375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack = in.createTypedArray(BackStackState.CREATOR); 376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 378cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int describeContents() { 379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return 0; 380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void writeToParcel(Parcel dest, int flags) { 383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn dest.writeTypedArray(mActive, flags); 384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn dest.writeIntArray(mAdded); 385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn dest.writeTypedArray(mBackStack, flags); 386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public static final Parcelable.Creator<FragmentManagerState> CREATOR 389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn = new Parcelable.Creator<FragmentManagerState>() { 390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentManagerState createFromParcel(Parcel in) { 391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return new FragmentManagerState(in); 392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentManagerState[] newArray(int size) { 395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return new FragmentManagerState[size]; 396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }; 398cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn} 399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/** 401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Container for fragments associated with an activity. 402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 403bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banesfinal class FragmentManagerImpl extends FragmentManager implements LayoutInflaterFactory { 404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static boolean DEBUG = false; 405cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static final String TAG = "FragmentManager"; 406cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 407681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn static final boolean HONEYCOMB = android.os.Build.VERSION.SDK_INT >= 11; 408681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 409cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state"; 410cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static final String TARGET_STATE_TAG = "android:target_state"; 411cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn static final String VIEW_STATE_TAG = "android:view_state"; 41279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint"; 413cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 414cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Runnable> mPendingActions; 415cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Runnable[] mTmpActions; 416cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean mExecutingActions; 417cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 418cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> mActive; 419cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> mAdded; 420cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Integer> mAvailIndices; 421cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<BackStackRecord> mBackStack; 422cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> mCreatedMenus; 423cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 424cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Must be accessed while locked. 425cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<BackStackRecord> mBackStackIndices; 426cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Integer> mAvailBackStackIndices; 427cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 428cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<OnBackStackChangedListener> mBackStackChangeListeners; 429cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 430cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int mCurState = Fragment.INITIALIZING; 431d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy FragmentHostCallbacks mHost; 432d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy FragmentController mController; 4330adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn FragmentContainer mContainer; 4340adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn Fragment mParent; 435cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 436cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean mNeedMenuInvalidate; 437cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean mStateSaved; 438cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean mDestroyed; 439cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn String mNoTransactionsBecause; 44079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell boolean mHavePendingDeferredStart; 441cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 442cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Temporary vars for state save and restore. 443cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Bundle mStateBundle = null; 444cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn SparseArray<Parcelable> mStateArray = null; 445cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 446cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Runnable mExecCommit = new Runnable() { 447cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 448cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void run() { 449cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn execPendingActions(); 450cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 451cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }; 452cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 45313fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn private void throwException(RuntimeException ex) { 45413fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn Log.e(TAG, ex.getMessage()); 455ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn Log.e(TAG, "Activity state:"); 45613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn LogWriter logw = new LogWriter(TAG); 45713fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn PrintWriter pw = new PrintWriter(logw); 458d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mHost != null) { 459ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn try { 460d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.dump(" ", null, pw, new String[] { }); 461ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } catch (Exception e) { 462ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn Log.e(TAG, "Failed dumping state", e); 463ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } 464ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } else { 465ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn try { 46613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn dump(" ", null, pw, new String[] { }); 467ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } catch (Exception e) { 46813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn Log.e(TAG, "Failed dumping state", e); 469ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } 470ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } 47113fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throw ex; 472ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn } 473ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn 474cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 475cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public FragmentTransaction beginTransaction() { 476cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return new BackStackRecord(this); 477cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 478cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 479cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 480cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean executePendingTransactions() { 481cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return execPendingActions(); 482cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 483cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 484cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 485cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void popBackStack() { 486cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn enqueueAction(new Runnable() { 487cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override public void run() { 488d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy popBackStackState(mHost.getHandler(), null, -1, 0); 489cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 490cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }, false); 491cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 492cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 493cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 494cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean popBackStackImmediate() { 495cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn checkStateLoss(); 496cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn executePendingTransactions(); 497d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return popBackStackState(mHost.getHandler(), null, -1, 0); 498cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 499cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 500cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 501cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void popBackStack(final String name, final int flags) { 502cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn enqueueAction(new Runnable() { 503cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override public void run() { 504d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy popBackStackState(mHost.getHandler(), name, -1, flags); 505cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 506cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }, false); 507cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 508cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 509cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 510cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean popBackStackImmediate(String name, int flags) { 511cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn checkStateLoss(); 512cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn executePendingTransactions(); 513d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return popBackStackState(mHost.getHandler(), name, -1, flags); 514cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 515cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 516cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 517cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void popBackStack(final int id, final int flags) { 518cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (id < 0) { 519cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalArgumentException("Bad id: " + id); 520cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 521cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn enqueueAction(new Runnable() { 522cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override public void run() { 523d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy popBackStackState(mHost.getHandler(), null, id, flags); 524cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 525cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }, false); 526cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 527cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 528cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 529cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean popBackStackImmediate(int id, int flags) { 530cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn checkStateLoss(); 531cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn executePendingTransactions(); 532cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (id < 0) { 533cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalArgumentException("Bad id: " + id); 534cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 535d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return popBackStackState(mHost.getHandler(), null, id, flags); 536cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 537cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 538cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 539cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getBackStackEntryCount() { 540cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mBackStack != null ? mBackStack.size() : 0; 541cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 542cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 543cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 544cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public BackStackEntry getBackStackEntryAt(int index) { 545cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mBackStack.get(index); 546cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 547cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 548cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 549cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void addOnBackStackChangedListener(OnBackStackChangedListener listener) { 550cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackChangeListeners == null) { 551cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>(); 552cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 553cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackChangeListeners.add(listener); 554cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 555cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 556cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 557cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) { 558cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackChangeListeners != null) { 559cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackChangeListeners.remove(listener); 560cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 561cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 562cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 563cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 564cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void putFragment(Bundle bundle, String key, Fragment fragment) { 565cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mIndex < 0) { 56613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalStateException("Fragment " + fragment 56713fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + " is not currently in the FragmentManager")); 568cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 569cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn bundle.putInt(key, fragment.mIndex); 570cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 571cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 572cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 573cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public Fragment getFragment(Bundle bundle, String key) { 574cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int index = bundle.getInt(key, -1); 575cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (index == -1) { 576cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 577cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 578cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (index >= mActive.size()) { 5792b336307cf98ca5142db6736812178293d47c500Cyril Mottier throwException(new IllegalStateException("Fragment no longer exists for key " 58013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + key + ": index " + index)); 581cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 582cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(index); 583cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f == null) { 5842b336307cf98ca5142db6736812178293d47c500Cyril Mottier throwException(new IllegalStateException("Fragment no longer exists for key " 58513fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + key + ": index " + index)); 586cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 587cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 588cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 589cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 590cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 5913a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown public List<Fragment> getFragments() { 5923a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown return mActive; 5933a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown } 5943a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown 5953a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown @Override 5965c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) { 5975c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (fragment.mIndex < 0) { 59813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException( new IllegalStateException("Fragment " + fragment 59913fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + " is not currently in the FragmentManager")); 6005c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 6015c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (fragment.mState > Fragment.INITIALIZING) { 6025c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn Bundle result = saveFragmentBasicState(fragment); 6035c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn return result != null ? new Fragment.SavedState(result) : null; 6045c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 6055c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn return null; 6065c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 6075c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 6085c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn @Override 60901df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler public boolean isDestroyed() { 61001df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler return mDestroyed; 61101df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler } 61201df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler 61301df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler @Override 614cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public String toString() { 615cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn StringBuilder sb = new StringBuilder(128); 616cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn sb.append("FragmentManager{"); 617cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn sb.append(Integer.toHexString(System.identityHashCode(this))); 618cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn sb.append(" in "); 6190adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (mParent != null) { 6200adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn DebugUtils.buildShortClassTag(mParent, sb); 6210adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } else { 622d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy DebugUtils.buildShortClassTag(mHost, sb); 6230adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 624cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn sb.append("}}"); 625cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return sb.toString(); 626cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 627cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 628cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 629cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 630cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn String innerPrefix = prefix + " "; 631cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 632cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int N; 633cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive != null) { 634cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mActive.size(); 635cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 636cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print("Active Fragments in "); 637cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(Integer.toHexString(System.identityHashCode(this))); 638cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(":"); 639cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 640cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 641cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 642cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(f); 643cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null) { 644cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.dump(innerPrefix, fd, writer, args); 645cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 646cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 647cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 648cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 649cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 650cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAdded != null) { 651cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mAdded.size(); 652cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 653cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("Added Fragments:"); 654cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 655cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 656cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 657cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(f.toString()); 658cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 659cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 660cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 661cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 662cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mCreatedMenus != null) { 663cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mCreatedMenus.size(); 664cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 665cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("Fragments Created Menus:"); 666cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 667cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mCreatedMenus.get(i); 668cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 669cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(f.toString()); 670cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 671cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 672cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 673cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 674cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStack != null) { 675cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mBackStack.size(); 676cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 677cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("Back Stack:"); 678cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 679cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackRecord bs = mBackStack.get(i); 680cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 681cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(bs.toString()); 682cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn bs.dump(innerPrefix, fd, writer, args); 683cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 684cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 685cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 686cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 687cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 688cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackIndices != null) { 689cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mBackStackIndices.size(); 690cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 691cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("Back Stack Indices:"); 692cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 693cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackRecord bs = mBackStackIndices.get(i); 694cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 695cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(bs); 696cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 697cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 698cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 699cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 700cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) { 701cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print("mAvailBackStackIndices: "); 702cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(Arrays.toString(mAvailBackStackIndices.toArray())); 703cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 704cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 705cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 706cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mPendingActions != null) { 707cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mPendingActions.size(); 708cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 709cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("Pending Actions:"); 710cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 711cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Runnable r = mPendingActions.get(i); 712cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" #"); writer.print(i); 713cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(": "); writer.println(r); 714cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 715cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 716cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 717cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 718cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.println("FragmentManager misc state:"); 719d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy writer.print(prefix); writer.print(" mHost="); writer.println(mHost); 7200adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn writer.print(prefix); writer.print(" mContainer="); writer.println(mContainer); 7210adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (mParent != null) { 7220adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn writer.print(prefix); writer.print(" mParent="); writer.println(mParent); 7230adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 724cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState); 725cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(" mStateSaved="); writer.print(mStateSaved); 726cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(" mDestroyed="); writer.println(mDestroyed); 727cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mNeedMenuInvalidate) { 728cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" mNeedMenuInvalidate="); 729cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(mNeedMenuInvalidate); 730cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 731cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mNoTransactionsBecause != null) { 732cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" mNoTransactionsBecause="); 733cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(mNoTransactionsBecause); 734cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 735cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailIndices != null && mAvailIndices.size() > 0) { 736cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.print(prefix); writer.print(" mAvailIndices: "); 737cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn writer.println(Arrays.toString(mAvailIndices.toArray())); 738cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 739cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 740cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 7419277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f); 7429277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static final Interpolator DECELERATE_CUBIC = new DecelerateInterpolator(1.5f); 7439277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static final Interpolator ACCELERATE_QUINT = new AccelerateInterpolator(2.5f); 7449277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static final Interpolator ACCELERATE_CUBIC = new AccelerateInterpolator(1.5f); 7459277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 7469277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static final int ANIM_DUR = 220; 7479277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 7489277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static Animation makeOpenCloseAnimation(Context context, float startScale, 7499277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn float endScale, float startAlpha, float endAlpha) { 7509277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn AnimationSet set = new AnimationSet(false); 7519277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn ScaleAnimation scale = new ScaleAnimation(startScale, endScale, startScale, endScale, 7529277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f); 7539277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn scale.setInterpolator(DECELERATE_QUINT); 7549277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn scale.setDuration(ANIM_DUR); 7559277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn set.addAnimation(scale); 7569277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn AlphaAnimation alpha = new AlphaAnimation(startAlpha, endAlpha); 7579277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn alpha.setInterpolator(DECELERATE_CUBIC); 7589277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn alpha.setDuration(ANIM_DUR); 7599277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn set.addAnimation(alpha); 7609277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn return set; 7619277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn } 7629277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 7639277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn static Animation makeFadeAnimation(Context context, float start, float end) { 7649277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn AlphaAnimation anim = new AlphaAnimation(start, end); 7659277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn anim.setInterpolator(DECELERATE_CUBIC); 7669277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn anim.setDuration(ANIM_DUR); 7679277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn return anim; 7689277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn } 7699277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 7709277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn Animation loadAnimation(Fragment fragment, int transit, boolean enter, 771cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int transitionStyle) { 7729277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn Animation animObj = fragment.onCreateAnimation(transit, enter, 773cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mNextAnim); 774cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (animObj != null) { 775cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return animObj; 776cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 777cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 778cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mNextAnim != 0) { 779d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy Animation anim = AnimationUtils.loadAnimation(mHost.getContext(), fragment.mNextAnim); 780cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (anim != null) { 781cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return anim; 782cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 783cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 784cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 785cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (transit == 0) { 786cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 787cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 788cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 789cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int styleIndex = transitToStyleIndex(transit, enter); 790cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (styleIndex < 0) { 791cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 792cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 793cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 7949277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn switch (styleIndex) { 7959277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_OPEN_ENTER: 796d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeOpenCloseAnimation(mHost.getContext(), 1.125f, 1.0f, 0, 1); 7979277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_OPEN_EXIT: 798d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeOpenCloseAnimation(mHost.getContext(), 1.0f, .975f, 1, 0); 7999277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_CLOSE_ENTER: 800d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeOpenCloseAnimation(mHost.getContext(), .975f, 1.0f, 0, 1); 8019277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_CLOSE_EXIT: 802d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeOpenCloseAnimation(mHost.getContext(), 1.0f, 1.075f, 1, 0); 8039277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_FADE_ENTER: 804d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeFadeAnimation(mHost.getContext(), 0, 1); 8059277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn case ANIM_STYLE_FADE_EXIT: 806d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy return makeFadeAnimation(mHost.getContext(), 1, 0); 8079277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn } 8089277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 809d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (transitionStyle == 0 && mHost.hasWindowAnimations()) { 810d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy transitionStyle = mHost.getWindowAnimations(); 811cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 812cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (transitionStyle == 0) { 813cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 814cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 815cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 816cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle, 817cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // com.android.internal.R.styleable.FragmentAnimation); 818cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //int anim = attrs.getResourceId(styleIndex, 0); 819cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //attrs.recycle(); 820cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 821cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //if (anim == 0) { 822cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // return null; 823cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //} 824cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 825cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn //return AnimatorInflater.loadAnimator(mActivity, anim); 826cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 827cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 828cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 829abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell public void performPendingDeferredStart(Fragment f) { 830abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell if (f.mDeferStart) { 83179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (mExecutingActions) { 83279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell // Wait until we're done executing our pending transactions 83379398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell mHavePendingDeferredStart = true; 83479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell return; 83579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 836abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell f.mDeferStart = false; 8375506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(f, mCurState, 0, 0, false); 838abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 839abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 840abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell 8415506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn void moveToState(Fragment f, int newState, int transit, int transitionStyle, 8425506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn boolean keepActive) { 843cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Fragments that are not currently added will sit in the onCreate() state. 84474c671b3b67000bf16b4865a8d361344310dccbeDianne Hackborn if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) { 845cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn newState = Fragment.CREATED; 846cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 8472c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn if (f.mRemoving && newState > f.mState) { 8482c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn // While removing a fragment, we can't change it to a higher state. 8492c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn newState = f.mState; 8502c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn } 8516cafd27a3c89dfed10d7e226dc6168307513d4a6Adam Powell // Defer start if requested; don't allow it to move to STARTED or higher 8526cafd27a3c89dfed10d7e226dc6168307513d4a6Adam Powell // if it's not already started. 8536cafd27a3c89dfed10d7e226dc6168307513d4a6Adam Powell if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) { 854abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell newState = Fragment.STOPPED; 855abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 856cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mState < newState) { 8579277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn // For fragments that are created from a layout, when restoring from 8589277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn // state we don't want to allow them to be created until they are 8599277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn // being reloaded from the layout. 8609277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn if (f.mFromLayout && !f.mInLayout) { 8619277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn return; 8629277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn } 863cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mAnimatingAway != null) { 864cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // The fragment is currently being animated... but! Now we 865cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // want to move our state back up. Give up on waiting for the 866cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // animation, move to whatever the final state should be once 867cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // the animation is done, and then we can proceed from there. 868cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mAnimatingAway = null; 8695506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(f, f.mStateAfterAnimating, 0, 0, true); 870cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 871cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn switch (f.mState) { 872cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.INITIALIZING: 873cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "moveto CREATED: " + f); 874cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mSavedFragmentState != null) { 875d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy f.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader()); 876cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray( 877cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.VIEW_STATE_TAG); 878cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mTarget = getFragment(f.mSavedFragmentState, 879cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.TARGET_STATE_TAG); 880cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mTarget != null) { 881cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mTargetRequestCode = f.mSavedFragmentState.getInt( 882cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0); 883cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 88479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell f.mUserVisibleHint = f.mSavedFragmentState.getBoolean( 88579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true); 88679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (!f.mUserVisibleHint) { 88779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell f.mDeferStart = true; 88879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (newState > Fragment.STOPPED) { 88979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell newState = Fragment.STOPPED; 89079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 89179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 892cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 893d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy f.mHost = mHost; 8940adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mParentFragment = mParent; 8950adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mFragmentManager = mParent != null 896d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy ? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl(); 897cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mCalled = false; 898d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.onFragmentAttach(f); 899cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!f.mCalled) { 900cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new SuperNotCalledException("Fragment " + f 901cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + " did not call through to super.onAttach()"); 902cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 9030adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f.mParentFragment == null) { 904d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.onFragmentAttach(f); 9050adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 9060adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn 907cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!f.mRetaining) { 9080adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performCreate(f.mSavedFragmentState); 909cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 910cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mRetaining = false; 911cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mFromLayout) { 912cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // For fragments that are part of the content view 913cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // layout, we need to instantiate the view immediately 914cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // and the inflater will take care of adding it. 9150adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mView = f.performCreateView(f.getLayoutInflater( 9160adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mSavedFragmentState), null, f.mSavedFragmentState); 917cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mView != null) { 918cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView = f.mView; 9195e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes if (Build.VERSION.SDK_INT >= 11) { 9205e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes ViewCompat.setSaveFromParentEnabled(f.mView, false); 9215e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes } else { 9225e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes f.mView = NoSaveStateFrameLayout.wrap(f.mView); 9235e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes } 924715a71e5f73cbc33f307cbd3625db8f889a05c1aDianne Hackborn if (f.mHidden) f.mView.setVisibility(View.GONE); 925e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn f.onViewCreated(f.mView, f.mSavedFragmentState); 926cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 927cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView = null; 928cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 929cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 930cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.CREATED: 931cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState > Fragment.CREATED) { 932e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f); 933cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!f.mFromLayout) { 934cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ViewGroup container = null; 935cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mContainerId != 0) { 9360adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn container = (ViewGroup)mContainer.findViewById(f.mContainerId); 937cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (container == null && !f.mRestored) { 93813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalArgumentException( 93913fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn "No view found for id 0x" 94013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + Integer.toHexString(f.mContainerId) + " (" 94113fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + f.getResources().getResourceName(f.mContainerId) 94213fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + ") for fragment " + f)); 943cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 944cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 945cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mContainer = container; 9460adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mView = f.performCreateView(f.getLayoutInflater( 9470adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.mSavedFragmentState), container, f.mSavedFragmentState); 948cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mView != null) { 949cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView = f.mView; 9505e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes if (Build.VERSION.SDK_INT >= 11) { 9515e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes ViewCompat.setSaveFromParentEnabled(f.mView, false); 9525e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes } else { 9535e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes f.mView = NoSaveStateFrameLayout.wrap(f.mView); 9545e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes } 955cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (container != null) { 9569277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn Animation anim = loadAnimation(f, transit, true, 957cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn transitionStyle); 958cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (anim != null) { 959cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mView.startAnimation(anim); 960cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 961cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn container.addView(f.mView); 962cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 963715a71e5f73cbc33f307cbd3625db8f889a05c1aDianne Hackborn if (f.mHidden) f.mView.setVisibility(View.GONE); 964e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn f.onViewCreated(f.mView, f.mSavedFragmentState); 965cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 966cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView = null; 967cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 968cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 9690adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn 9700adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performActivityCreated(f.mSavedFragmentState); 971e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (f.mView != null) { 9720adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.restoreViewState(f.mSavedFragmentState); 973e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 974cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mSavedFragmentState = null; 975cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 976cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.ACTIVITY_CREATED: 977e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn case Fragment.STOPPED: 978e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (newState > Fragment.STOPPED) { 979cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "moveto STARTED: " + f); 9809c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn f.performStart(); 981cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 982cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.STARTED: 983cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState > Fragment.STARTED) { 984cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f); 985cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mResumed = true; 9860adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performResume(); 9874e6647fe2551985f33407acd712a4942b090207aDianne Hackborn f.mSavedFragmentState = null; 9884e6647fe2551985f33407acd712a4942b090207aDianne Hackborn f.mSavedViewState = null; 989cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 990cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 991cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else if (f.mState > newState) { 992cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn switch (f.mState) { 993cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.RESUMED: 994cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState < Fragment.RESUMED) { 995cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f); 9960adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performPause(); 997cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mResumed = false; 998cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 999cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.STARTED: 1000cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState < Fragment.STARTED) { 1001cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f); 1002cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.performStop(); 1003cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1004e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn case Fragment.STOPPED: 1005218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn if (newState < Fragment.STOPPED) { 1006218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn if (DEBUG) Log.v(TAG, "movefrom STOPPED: " + f); 1007218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn f.performReallyStop(); 1008218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn } 1009cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.ACTIVITY_CREATED: 1010cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState < Fragment.ACTIVITY_CREATED) { 1011e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f); 1012cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mView != null) { 1013cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Need to save the current view state if not 1014cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // done already. 1015d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mHost.shouldSaveFragmentState(f) && f.mSavedViewState == null) { 1016cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn saveFragmentViewState(f); 1017cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1018cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 10199c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn f.performDestroyView(); 1020cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mView != null && f.mContainer != null) { 1021cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Animation anim = null; 1022cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mCurState > Fragment.INITIALIZING && !mDestroyed) { 10239277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn anim = loadAnimation(f, transit, false, 1024cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn transitionStyle); 1025cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1026cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (anim != null) { 1027cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final Fragment fragment = f; 1028cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mAnimatingAway = f.mView; 1029cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mStateAfterAnimating = newState; 1030cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn anim.setAnimationListener(new AnimationListener() { 1031cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 1032cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onAnimationEnd(Animation animation) { 1033cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mAnimatingAway != null) { 1034cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mAnimatingAway = null; 1035cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(fragment, fragment.mStateAfterAnimating, 10365506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn 0, 0, false); 1037cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1038cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1039cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 1040cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onAnimationRepeat(Animation animation) { 1041cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1042cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 1043cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void onAnimationStart(Animation animation) { 1044cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1045cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn }); 10469277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn f.mView.startAnimation(anim); 1047cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1048cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mContainer.removeView(f.mView); 1049cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1050cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mContainer = null; 1051cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mView = null; 1052cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView = null; 1053cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1054cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case Fragment.CREATED: 1055cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newState < Fragment.CREATED) { 1056cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mDestroyed) { 1057cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mAnimatingAway != null) { 1058cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // The fragment's containing activity is 1059cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // being destroyed, but this fragment is 1060cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // currently animating away. Stop the 1061cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // animation right now -- it is not needed, 1062cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // and we can't wait any more on destroying 1063cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // the fragment. 1064cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn View v = f.mAnimatingAway; 1065cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mAnimatingAway = null; 1066cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn v.clearAnimation(); 1067cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1068cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1069cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mAnimatingAway != null) { 1070cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // We are waiting for the fragment's view to finish 1071cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // animating away. Just make a note of the state 1072cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // the fragment now should move to once the animation 1073cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // is done. 1074cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mStateAfterAnimating = newState; 10752c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn newState = Fragment.CREATED; 1076cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1077cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f); 1078cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!f.mRetaining) { 10790adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performDestroy(); 1080cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1081cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1082cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mCalled = false; 1083cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.onDetach(); 1084cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!f.mCalled) { 1085cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new SuperNotCalledException("Fragment " + f 1086cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + " did not call through to super.onDetach()"); 1087cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 10885506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn if (!keepActive) { 10895506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn if (!f.mRetaining) { 10905506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn makeInactive(f); 10915506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn } else { 1092d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy f.mHost = null; 10936252d78085a07c9d6bb4645a4e8086bf23b0a49aTim Kilbourn f.mParentFragment = null; 10945506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn f.mFragmentManager = null; 10956252d78085a07c9d6bb4645a4e8086bf23b0a49aTim Kilbourn f.mChildFragmentManager = null; 10965506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn } 10972c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn } 1098cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1099cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1100cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mState = newState; 1104cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void moveToState(Fragment f) { 11075506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(f, mCurState, 0, 0, false); 1108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void moveToState(int newState, boolean always) { 1111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(newState, 0, 0, always); 1112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void moveToState(int newState, int transit, int transitStyle, boolean always) { 1115d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mHost == null && newState != Fragment.INITIALIZING) { 1116d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy throw new IllegalStateException("No host"); 1117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 11180adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn 1119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!always && mCurState == newState) { 1120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 1121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 11220adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn 1123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mCurState = newState; 1124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive != null) { 1125abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell boolean loadersRunning = false; 1126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mActive.size(); i++) { 1127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 1128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null) { 11295506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(f, newState, transit, transitStyle, false); 1130abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell if (f.mLoaderManager != null) { 1131abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell loadersRunning |= f.mLoaderManager.hasRunningLoaders(); 1132abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 1133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1136abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell if (!loadersRunning) { 1137abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell startPendingDeferredFragments(); 1138abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 1139abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell 1140d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mNeedMenuInvalidate && mHost != null && mCurState == Fragment.RESUMED) { 1141d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.supportInvalidateOptionsMenu(); 1142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mNeedMenuInvalidate = false; 1143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1146abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell 1147abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell void startPendingDeferredFragments() { 11481199ae7067cdf8cf3eb30c057a61ae71a0aea1e5Adam Powell if (mActive == null) return; 11491199ae7067cdf8cf3eb30c057a61ae71a0aea1e5Adam Powell 1150abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell for (int i=0; i<mActive.size(); i++) { 1151abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell Fragment f = mActive.get(i); 1152abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell if (f != null) { 1153abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell performPendingDeferredStart(f); 1154abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 1155abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 1156abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell } 1157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void makeActive(Fragment f) { 1159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mIndex >= 0) { 1160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 1161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailIndices == null || mAvailIndices.size() <= 0) { 1164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive == null) { 1165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive = new ArrayList<Fragment>(); 1166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 11670adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.setIndex(mActive.size(), mParent); 1168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive.add(f); 1169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 11710adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent); 1172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive.set(f.mIndex, f); 1173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1174be2c79d9a5439922030d2a3846c81c61f0e16912Dianne Hackborn if (DEBUG) Log.v(TAG, "Allocated fragment index " + f); 1175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void makeInactive(Fragment f) { 1178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mIndex < 0) { 1179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 1180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1182be2c79d9a5439922030d2a3846c81c61f0e16912Dianne Hackborn if (DEBUG) Log.v(TAG, "Freeing fragment index " + f); 1183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive.set(f.mIndex, null); 1184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailIndices == null) { 1185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailIndices = new ArrayList<Integer>(); 1186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailIndices.add(f.mIndex); 1188d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.inactivateFragment(f.mWho); 11899c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn f.initState(); 1190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void addFragment(Fragment fragment, boolean moveToStateNow) { 1193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAdded == null) { 1194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdded = new ArrayList<Fragment>(); 1195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "add: " + fragment); 1197e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn makeActive(fragment); 1198e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (!fragment.mDetached) { 11993a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (mAdded.contains(fragment)) { 12003a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn throw new IllegalStateException("Fragment already added: " + fragment); 12013a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn } 1202e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mAdded.add(fragment); 1203e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mAdded = true; 1204e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mRemoving = false; 12052a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mHasMenu && fragment.mMenuVisible) { 1206e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mNeedMenuInvalidate = true; 1207e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1208e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (moveToStateNow) { 1209e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn moveToState(fragment); 1210e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void removeFragment(Fragment fragment, int transition, int transitionStyle) { 1215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting); 1216e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn final boolean inactive = !fragment.isInBackStack(); 1217e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (!fragment.mDetached || inactive) { 1218464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 1219464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn mAdded.remove(fragment); 1220464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn } 12212a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mHasMenu && fragment.mMenuVisible) { 1222e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mNeedMenuInvalidate = true; 1223e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1224e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mAdded = false; 1225e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mRemoving = true; 1226e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED, 12275506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn transition, transitionStyle, false); 1228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void hideFragment(Fragment fragment, int transition, int transitionStyle) { 1232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "hide: " + fragment); 1233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!fragment.mHidden) { 1234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mHidden = true; 1235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mView != null) { 1236495efcad8c8865e3e6f14e4ea3d79cfb1d2410f9Alexander Mironov Animation anim = loadAnimation(fragment, transition, false, 1237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn transitionStyle); 1238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (anim != null) { 1239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mView.startAnimation(anim); 1240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mView.setVisibility(View.GONE); 1242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 12432a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) { 1244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mNeedMenuInvalidate = true; 1245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.onHiddenChanged(true); 1247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void showFragment(Fragment fragment, int transition, int transitionStyle) { 1251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "show: " + fragment); 1252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mHidden) { 1253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mHidden = false; 1254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragment.mView != null) { 12559277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn Animation anim = loadAnimation(fragment, transition, true, 1256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn transitionStyle); 1257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (anim != null) { 1258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mView.startAnimation(anim); 1259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.mView.setVisibility(View.VISIBLE); 1261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 12622a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) { 1263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mNeedMenuInvalidate = true; 1264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragment.onHiddenChanged(false); 1266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1269e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn public void detachFragment(Fragment fragment, int transition, int transitionStyle) { 1270e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (DEBUG) Log.v(TAG, "detach: " + fragment); 1271e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (!fragment.mDetached) { 1272e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mDetached = true; 1273e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (fragment.mAdded) { 1274e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn // We are not already in back stack, so need to remove the fragment. 1275464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 12763a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) Log.v(TAG, "remove from detach: " + fragment); 1277464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn mAdded.remove(fragment); 1278464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn } 12792a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mHasMenu && fragment.mMenuVisible) { 1280e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mNeedMenuInvalidate = true; 1281e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1282e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mAdded = false; 12835506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false); 1284e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1285e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1286e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1287e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn 1288e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn public void attachFragment(Fragment fragment, int transition, int transitionStyle) { 1289e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (DEBUG) Log.v(TAG, "attach: " + fragment); 1290e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (fragment.mDetached) { 1291e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mDetached = false; 1292e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn if (!fragment.mAdded) { 1293464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded == null) { 1294464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn mAdded = new ArrayList<Fragment>(); 1295464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn } 12963a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (mAdded.contains(fragment)) { 12973a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn throw new IllegalStateException("Fragment already added: " + fragment); 12983a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn } 12993a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) Log.v(TAG, "add from attach: " + fragment); 1300e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mAdded.add(fragment); 1301e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn fragment.mAdded = true; 13022a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment.mHasMenu && fragment.mMenuVisible) { 1303e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn mNeedMenuInvalidate = true; 1304e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 13055506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn moveToState(fragment, mCurState, transition, transitionStyle, false); 1306e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1307e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1308e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn } 1309e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn 1310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public Fragment findFragmentById(int id) { 1311464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 1312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // First look through added fragments. 1313cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mAdded.size()-1; i>=0; i--) { 1314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 1315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null && f.mFragmentId == id) { 1316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 1317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1319464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn } 1320464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mActive != null) { 1321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Now for any known fragment. 1322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mActive.size()-1; i>=0; i--) { 1323cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 1324cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null && f.mFragmentId == id) { 1325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 1326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1327cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1328cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1329cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 1330cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1331cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1332cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public Fragment findFragmentByTag(String tag) { 1333464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null && tag != null) { 1334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // First look through added fragments. 1335cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mAdded.size()-1; i>=0; i--) { 1336cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 1337cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null && tag.equals(f.mTag)) { 1338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 1339cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1340cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1341464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn } 1342464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mActive != null && tag != null) { 1343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Now for any known fragment. 1344cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mActive.size()-1; i>=0; i--) { 1345cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 1346cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null && tag.equals(f.mTag)) { 1347cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 1348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 1352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public Fragment findFragmentByWho(String who) { 1355cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive != null && who != null) { 1356cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mActive.size()-1; i>=0; i--) { 1357cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 13580adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null && (f=f.findFragmentByWho(who)) != null) { 1359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return f; 1360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1363cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 1364cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn private void checkStateLoss() { 1367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mStateSaved) { 1368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException( 1369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn "Can not perform this action after onSaveInstanceState"); 1370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mNoTransactionsBecause != null) { 1372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException( 1373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn "Can not perform this action inside of " + mNoTransactionsBecause); 1374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1377ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette /** 1378ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette * Adds an action to the queue of pending actions. 1379ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette * 1380ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette * @param action the action to add 1381ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette * @param allowStateLoss whether to allow loss of state information 1382ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette * @throws IllegalStateException if the activity has been destroyed 1383ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette */ 1384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void enqueueAction(Runnable action, boolean allowStateLoss) { 1385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!allowStateLoss) { 1386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn checkStateLoss(); 1387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 1389d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mDestroyed || mHost == null) { 1390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException("Activity has been destroyed"); 1391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mPendingActions == null) { 1393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mPendingActions = new ArrayList<Runnable>(); 1394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mPendingActions.add(action); 1396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mPendingActions.size() == 1) { 1397d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.getHandler().removeCallbacks(mExecCommit); 1398d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.getHandler().post(mExecCommit); 1399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1403cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int allocBackStackIndex(BackStackRecord bse) { 1404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 1405cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) { 1406cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackIndices == null) { 1407cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices = new ArrayList<BackStackRecord>(); 1408cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1409cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int index = mBackStackIndices.size(); 1410cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); 1411cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.add(bse); 1412cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return index; 1413cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1414cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1415cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1); 1416cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); 1417cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.set(index, bse); 1418cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return index; 1419cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1420cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1421cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1422cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1423cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setBackStackIndex(int index, BackStackRecord bse) { 1424cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 1425cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackIndices == null) { 1426cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices = new ArrayList<BackStackRecord>(); 1427cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1428cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int N = mBackStackIndices.size(); 1429cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (index < N) { 1430cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); 1431cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.set(index, bse); 1432cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1433cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn while (N < index) { 1434cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.add(null); 1435cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailBackStackIndices == null) { 1436cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailBackStackIndices = new ArrayList<Integer>(); 1437cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1438cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Adding available back stack index " + N); 1439cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailBackStackIndices.add(N); 1440cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N++; 1441cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1442cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); 1443cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.add(bse); 1444cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1445cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1446cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1447cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1448cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void freeBackStackIndex(int index) { 1449cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 1450cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackIndices.set(index, null); 1451cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailBackStackIndices == null) { 1452cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailBackStackIndices = new ArrayList<Integer>(); 1453cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1454cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Freeing back stack index " + index); 1455cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailBackStackIndices.add(index); 1456cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1457cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1458cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1459cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 1460cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Only call from main thread! 1461cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 1462cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean execPendingActions() { 1463cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mExecutingActions) { 1464cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException("Recursive entry to executePendingTransactions"); 1465cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1466cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1467d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (Looper.myLooper() != mHost.getHandler().getLooper()) { 1468cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn throw new IllegalStateException("Must be called from main thread of process"); 1469cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1470cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1471cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean didSomething = false; 1472cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1473cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn while (true) { 1474cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int numActions; 1475cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1476cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn synchronized (this) { 1477cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mPendingActions == null || mPendingActions.size() == 0) { 147879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell break; 1479cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1480cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1481cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn numActions = mPendingActions.size(); 1482cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mTmpActions == null || mTmpActions.length < numActions) { 1483cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mTmpActions = new Runnable[numActions]; 1484cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1485cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mPendingActions.toArray(mTmpActions); 1486cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mPendingActions.clear(); 1487d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.getHandler().removeCallbacks(mExecCommit); 1488cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1489cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1490cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mExecutingActions = true; 1491cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<numActions; i++) { 1492cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mTmpActions[i].run(); 14939f491519261135d9de50b60c2182755ccb665bf5Jeff Sharkey mTmpActions[i] = null; 1494cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1495cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mExecutingActions = false; 1496cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn didSomething = true; 1497cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 149879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell 149979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (mHavePendingDeferredStart) { 150079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell boolean loadersRunning = false; 150179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell for (int i=0; i<mActive.size(); i++) { 150279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell Fragment f = mActive.get(i); 150379398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (f != null && f.mLoaderManager != null) { 150479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell loadersRunning |= f.mLoaderManager.hasRunningLoaders(); 150579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 150679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 150779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (!loadersRunning) { 150879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell mHavePendingDeferredStart = false; 150979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell startPendingDeferredFragments(); 151079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 151179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 151279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell return didSomething; 1513cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 151479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell 1515cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void reportBackStackChanged() { 1516cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStackChangeListeners != null) { 1517cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mBackStackChangeListeners.size(); i++) { 1518cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStackChangeListeners.get(i).onBackStackChanged(); 1519cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1520cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1521cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1522cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1523cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void addBackStackState(BackStackRecord state) { 1524cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStack == null) { 1525cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack = new ArrayList<BackStackRecord>(); 1526cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1527cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack.add(state); 1528cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn reportBackStackChanged(); 1529cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1530cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1531d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy @SuppressWarnings("unused") 1532cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean popBackStackState(Handler handler, String name, int id, int flags) { 1533cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStack == null) { 1534cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 1535cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1536cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) { 1537cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int last = mBackStack.size()-1; 1538cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (last < 0) { 1539cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 1540cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1541cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final BackStackRecord bss = mBackStack.remove(last); 15429f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>(); 15439f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>(); 15449f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount bss.calculateBackFragments(firstOutFragments, lastInFragments); 15459f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount bss.popFromBackStack(true, null, firstOutFragments, lastInFragments); 1546cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn reportBackStackChanged(); 1547cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1548cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int index = -1; 1549cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (name != null || id >= 0) { 1550cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // If a name or ID is specified, look for that place in 1551cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // the stack. 1552cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn index = mBackStack.size()-1; 1553cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn while (index >= 0) { 1554cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackRecord bss = mBackStack.get(index); 1555cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (name != null && name.equals(bss.getName())) { 1556cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 1557cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1558cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (id >= 0 && id == bss.mIndex) { 1559cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 1560cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1561cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn index--; 1562cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1563cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (index < 0) { 1564cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 1565cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1566cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) { 1567cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn index--; 1568cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Consume all following entries that match. 1569cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn while (index >= 0) { 1570cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackRecord bss = mBackStack.get(index); 1571cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if ((name != null && name.equals(bss.getName())) 1572cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn || (id >= 0 && id == bss.mIndex)) { 1573cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn index--; 1574cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn continue; 1575cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1576cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 1577cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1578cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1579cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1580cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (index == mBackStack.size()-1) { 1581cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 1582cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1583cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final ArrayList<BackStackRecord> states 1584cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn = new ArrayList<BackStackRecord>(); 1585cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=mBackStack.size()-1; i>index; i--) { 1586cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn states.add(mBackStack.remove(i)); 1587cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1588cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final int LAST = states.size()-1; 15899f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>(); 15909f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>(); 15919f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount for (int i=0; i<=LAST; i++) { 15929f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount states.get(i).calculateBackFragments(firstOutFragments, lastInFragments); 15939f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount } 15949f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount BackStackRecord.TransitionState state = null; 1595cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<=LAST; i++) { 1596cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i)); 15979f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount state = states.get(i).popFromBackStack(i == LAST, state, 15989f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount firstOutFragments, lastInFragments); 1599cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1600cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn reportBackStackChanged(); 1601cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1602cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return true; 1603cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1604cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1605cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> retainNonConfig() { 1606cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> fragments = null; 1607cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive != null) { 1608cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mActive.size(); i++) { 1609cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 1610cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null && f.mRetainInstance) { 1611cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fragments == null) { 1612cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragments = new ArrayList<Fragment>(); 1613cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1614cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fragments.add(f); 1615cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mRetaining = true; 16162c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1; 1617be2c79d9a5439922030d2a3846c81c61f0e16912Dianne Hackborn if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f); 1618cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1619cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1620cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1621cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return fragments; 1622cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1623cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1624cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void saveFragmentViewState(Fragment f) { 1625cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mInnerView == null) { 1626cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return; 1627cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1628cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mStateArray == null) { 1629cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateArray = new SparseArray<Parcelable>(); 1630ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn } else { 1631ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn mStateArray.clear(); 1632cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1633cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInnerView.saveHierarchyState(mStateArray); 1634cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mStateArray.size() > 0) { 1635cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mSavedViewState = mStateArray; 1636cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateArray = null; 1637cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1638cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1639cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 16405c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn Bundle saveFragmentBasicState(Fragment f) { 16415c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn Bundle result = null; 16425c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 16435c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (mStateBundle == null) { 16445c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mStateBundle = new Bundle(); 16455c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 16460adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performSaveInstanceState(mStateBundle); 16475c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (!mStateBundle.isEmpty()) { 16485c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn result = mStateBundle; 16495c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mStateBundle = null; 16505c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 16515c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 16525c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (f.mView != null) { 16535c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn saveFragmentViewState(f); 16545c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 16555c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (f.mSavedViewState != null) { 16565c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (result == null) { 16575c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn result = new Bundle(); 16585c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 16595c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn result.putSparseParcelableArray( 16605c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); 16615c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 166279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell if (!f.mUserVisibleHint) { 1663f4c0cf637ba73374a206cec26c09d4dfa4c1a364Jake Wharton if (result == null) { 1664f4c0cf637ba73374a206cec26c09d4dfa4c1a364Jake Wharton result = new Bundle(); 1665f4c0cf637ba73374a206cec26c09d4dfa4c1a364Jake Wharton } 166679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell // Only add this if it's not the default value 166779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint); 166879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell } 16695c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 16705c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn return result; 16715c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 16725c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 1673cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Parcelable saveAllState() { 1674cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Make sure all pending operations have now been executed to get 1675cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // our state update-to-date. 1676cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn execPendingActions(); 1677cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1678681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn if (HONEYCOMB) { 1679681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // As of Honeycomb, we save state after pausing. Prior to that 1680681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // it is before pausing. With fragments this is an issue, since 1681681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // there are many things you may do after pausing but before 1682681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // stopping that change the fragment state. For those older 1683681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // devices, we will not at this point say that we have saved 1684681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // the state, so we will allow them to continue doing fragment 1685681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // transactions. This retains the same semantics as Honeycomb, 1686681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // though you do have the risk of losing the very most recent state 1687681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // if the process is killed... we'll live with that. 1688681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mStateSaved = true; 1689681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 1690cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1691cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mActive == null || mActive.size() <= 0) { 1692cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 1693cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1694cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1695cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // First collect all active fragments. 1696cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int N = mActive.size(); 1697cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentState[] active = new FragmentState[N]; 1698cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean haveFragments = false; 1699cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 1700cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(i); 1701cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null) { 17021b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn if (f.mIndex < 0) { 170313fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalStateException( 170413fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn "Failure saving state: active " + f 170513fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + " has cleared index: " + f.mIndex)); 17061b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn } 17071b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn 1708cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn haveFragments = true; 1709cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1710cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentState fs = new FragmentState(f); 1711cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn active[i] = fs; 1712cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1713cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) { 17145c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn fs.mSavedFragmentState = saveFragmentBasicState(f); 1715cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1716cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mTarget != null) { 1717cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mTarget.mIndex < 0) { 171813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalStateException( 171913fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn "Failure saving state: " + f 172013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + " has target not in fragment manager: " + f.mTarget)); 1721cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1722cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fs.mSavedFragmentState == null) { 1723cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fs.mSavedFragmentState = new Bundle(); 1724cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1725cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn putFragment(fs.mSavedFragmentState, 1726cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget); 1727cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f.mTargetRequestCode != 0) { 1728cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fs.mSavedFragmentState.putInt( 1729cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 1730cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mTargetRequestCode); 1731cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1732cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1733cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1734cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1735cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fs.mSavedFragmentState = f.mSavedFragmentState; 1736cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1737cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1738cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "Saved state of " + f + ": " 1739cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + fs.mSavedFragmentState); 1740cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1741cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1742cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1743cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!haveFragments) { 1744cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "saveAllState: no fragments!"); 1745cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return null; 1746cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1747cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1748cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int[] added = null; 1749cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackState[] backStack = null; 1750cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1751cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Build list of currently added fragments. 1752cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAdded != null) { 1753cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mAdded.size(); 1754cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 1755cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn added = new int[N]; 1756cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 1757cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn added[i] = mAdded.get(i).mIndex; 17581b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn if (added[i] < 0) { 175913fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalStateException( 176013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn "Failure saving state: active " + mAdded.get(i) 176113fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn + " has cleared index: " + added[i])); 17621b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn } 1763cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i 1764cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + ": " + mAdded.get(i)); 1765cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1766cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1767cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1768cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1769cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Now save back stack. 1770cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mBackStack != null) { 1771cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn N = mBackStack.size(); 1772cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (N > 0) { 1773cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn backStack = new BackStackState[N]; 1774cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<N; i++) { 1775d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy backStack[i] = new BackStackState(mBackStack.get(i)); 1776cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i 1777cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + ": " + mBackStack.get(i)); 1778cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1779cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1780cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1781cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1782cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerState fms = new FragmentManagerState(); 1783cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fms.mActive = active; 1784cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fms.mAdded = added; 1785cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fms.mBackStack = backStack; 1786cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return fms; 1787cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1788cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1789cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn void restoreAllState(Parcelable state, ArrayList<Fragment> nonConfig) { 1790cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // If there is no saved state at all, then there can not be 1791cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // any nonConfig fragments either, so that is that. 1792cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (state == null) return; 1793cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerState fms = (FragmentManagerState)state; 1794cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fms.mActive == null) return; 1795cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1796cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // First re-attach any non-config instances we are retaining back 1797cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // to their saved state, so we don't try to instantiate them again. 1798cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (nonConfig != null) { 1799cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<nonConfig.size(); i++) { 1800cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = nonConfig.get(i); 1801cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f); 1802cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentState fs = fms.mActive[f.mIndex]; 1803cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fs.mInstance = f; 1804cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mSavedViewState = null; 1805cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mBackStackNesting = 0; 1806cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mInLayout = false; 1807cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mAdded = false; 18082c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn f.mTarget = null; 1809cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fs.mSavedFragmentState != null) { 1810d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy fs.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader()); 1811cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray( 1812cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentManagerImpl.VIEW_STATE_TAG); 18138e4a59b54e9225b77151805dd6b8867dcd8e60a4Craig Mautner f.mSavedFragmentState = fs.mSavedFragmentState; 1814cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1815cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1816cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1817cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1818cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Build the full list of active fragments, instantiating them from 1819cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // their saved state. 1820cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive = new ArrayList<Fragment>(fms.mActive.length); 1821cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailIndices != null) { 1822cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailIndices.clear(); 1823cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1824cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<fms.mActive.length; i++) { 1825cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn FragmentState fs = fms.mActive[i]; 1826cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fs != null) { 1827d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy Fragment f = fs.instantiate(mHost, mParent); 18283a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f); 1829cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive.add(f); 1830cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Now that the fragment is instantiated (or came from being 1831cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // retained above), clear mInstance in case we end up re-restoring 1832cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // from this FragmentState again. 1833cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn fs.mInstance = null; 1834cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1835cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mActive.add(null); 1836cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mAvailIndices == null) { 1837cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailIndices = new ArrayList<Integer>(); 1838cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 18393a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i); 1840cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAvailIndices.add(i); 1841cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1842cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1843cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1844cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Update the target of all retained fragments. 1845cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (nonConfig != null) { 1846cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<nonConfig.size(); i++) { 1847cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = nonConfig.get(i); 18482c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn if (f.mTargetIndex >= 0) { 18492c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn if (f.mTargetIndex < mActive.size()) { 18502c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn f.mTarget = mActive.get(f.mTargetIndex); 1851cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1852cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Log.w(TAG, "Re-attaching retained fragment " + f 18532c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn + " target no longer exists: " + f.mTargetIndex); 1854cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mTarget = null; 1855cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1856cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1857cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1858cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1859cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1860cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Build the list of currently added fragments. 1861cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fms.mAdded != null) { 1862cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdded = new ArrayList<Fragment>(fms.mAdded.length); 1863cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<fms.mAdded.length; i++) { 1864cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mActive.get(fms.mAdded[i]); 1865cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f == null) { 186613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn throwException(new IllegalStateException( 186713fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn "No instantiated fragment for index #" + fms.mAdded[i])); 1868cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1869cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.mAdded = true; 18703a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f); 18713a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (mAdded.contains(f)) { 18723a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn throw new IllegalStateException("Already added!"); 18733a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn } 1874cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdded.add(f); 1875cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1876cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1877cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mAdded = null; 1878cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1879cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1880cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn // Build the back stack. 1881cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (fms.mBackStack != null) { 1882cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length); 1883cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<fms.mBackStack.length; i++) { 1884cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn BackStackRecord bse = fms.mBackStack[i].instantiate(this); 18853a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn if (DEBUG) { 18863a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn Log.v(TAG, "restoreAllState: back stack #" + i 1887cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn + " (index " + bse.mIndex + "): " + bse); 18883a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn LogWriter logw = new LogWriter(TAG); 18893a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn PrintWriter pw = new PrintWriter(logw); 18903a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn bse.dump(" ", pw, false); 18913a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn } 1892cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack.add(bse); 1893cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (bse.mIndex >= 0) { 1894cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setBackStackIndex(bse.mIndex, bse); 1895cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1896cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1897cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1898cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mBackStack = null; 1899cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1900cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1901d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy 1902d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy public void attachController(FragmentHostCallbacks host, 19030adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn FragmentContainer container, Fragment parent) { 1904d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (mHost != null) throw new IllegalStateException("Already attached"); 1905d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost = host; 19060adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn mContainer = container; 19070adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn mParent = parent; 1908cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1909cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1910cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void noteStateNotSaved() { 1911cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateSaved = false; 1912cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1913cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1914cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchCreate() { 1915cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateSaved = false; 1916cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.CREATED, false); 1917cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1918cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1919cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchActivityCreated() { 1920cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateSaved = false; 1921cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.ACTIVITY_CREATED, false); 1922cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1923cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1924cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchStart() { 1925cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateSaved = false; 1926cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.STARTED, false); 1927cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1928cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1929cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchResume() { 1930cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStateSaved = false; 1931cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.RESUMED, false); 1932cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1933cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1934cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchPause() { 1935cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.STARTED, false); 1936cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1937cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1938cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchStop() { 1939681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // See saveAllState() for the explanation of this. We do this for 1940681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // all platform versions, to keep our behavior more consistent between 1941681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn // them. 1942681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn mStateSaved = true; 1943681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 1944e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn moveToState(Fragment.STOPPED, false); 1945cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1946cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1947218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn public void dispatchReallyStop() { 1948218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn moveToState(Fragment.ACTIVITY_CREATED, false); 1949681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn } 1950681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn 19510adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn public void dispatchDestroyView() { 19520adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn moveToState(Fragment.CREATED, false); 19530adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 19540adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn 1955cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchDestroy() { 1956cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mDestroyed = true; 1957e8b402b00c0cbdac050c349a5fc89c34580f3185Dianne Hackborn execPendingActions(); 1958cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn moveToState(Fragment.INITIALIZING, false); 1959d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost = null; 19600adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn mContainer = null; 19610adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn mParent = null; 1962cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1963cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1964cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchConfigurationChanged(Configuration newConfig) { 1965464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 1966cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 1967cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 1968cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null) { 19690adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performConfigurationChanged(newConfig); 1970cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1971cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1972cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1973cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1974cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1975cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchLowMemory() { 1976464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 1977cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 1978cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 1979cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (f != null) { 19800adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performLowMemory(); 1981cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1982cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1983cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1984cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 1985cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 1986cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) { 1987cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean show = false; 1988cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn ArrayList<Fragment> newMenus = null; 1989464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 1990cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 1991cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 19920adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null) { 19930adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f.performCreateOptionsMenu(menu, inflater)) { 19940adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn show = true; 19950adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (newMenus == null) { 19960adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn newMenus = new ArrayList<Fragment>(); 19970adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 19980adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn newMenus.add(f); 1999cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2000cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2001cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2002cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2003c1e87ac17c772ee3c5fd3b3f08321226e2c7ffa4Chris Banes 2004cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mCreatedMenus != null) { 2005cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mCreatedMenus.size(); i++) { 2006cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mCreatedMenus.get(i); 2007cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (newMenus == null || !newMenus.contains(f)) { 2008cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn f.onDestroyOptionsMenu(); 2009cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2010cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2011cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2012c1e87ac17c772ee3c5fd3b3f08321226e2c7ffa4Chris Banes 2013cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mCreatedMenus = newMenus; 2014c1e87ac17c772ee3c5fd3b3f08321226e2c7ffa4Chris Banes 2015cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return show; 2016cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2017cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2018cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean dispatchPrepareOptionsMenu(Menu menu) { 2019cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean show = false; 2020464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2021cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2022cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 20230adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null) { 20240adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f.performPrepareOptionsMenu(menu)) { 20250adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn show = true; 20260adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn } 2027cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2028cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2029cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2030cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return show; 2031cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2032cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2033cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean dispatchOptionsItemSelected(MenuItem item) { 2034464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2035cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2036cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 20370adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null) { 20380adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f.performOptionsItemSelected(item)) { 2039cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return true; 2040cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2041cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2042cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2043cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2044cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 2045cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2046cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2047cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public boolean dispatchContextItemSelected(MenuItem item) { 2048464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2049cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2050cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 20510adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null) { 20520adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f.performContextItemSelected(item)) { 2053cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return true; 2054cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2055cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2056cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2057cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2058cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return false; 2059cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2060cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2061cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void dispatchOptionsMenuClosed(Menu menu) { 2062464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn if (mAdded != null) { 2063cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i=0; i<mAdded.size(); i++) { 2064cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn Fragment f = mAdded.get(i); 20650adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn if (f != null) { 20660adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn f.performOptionsMenuClosed(menu); 2067cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2068cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2069cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2070cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2071461b48b4588ac21b97aa40553f04222c2c0344e7Chris Banes 2072cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public static int reverseTransit(int transit) { 2073cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int rev = 0; 2074cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn switch (transit) { 2075cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: 2076cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE; 2077cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2078cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: 2079cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN; 2080cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2081cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_FADE: 2082cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE; 2083cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2084cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2085cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return rev; 2086cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 2087cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2088cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 20899277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_OPEN_ENTER = 1; 20909277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_OPEN_EXIT = 2; 20919277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_CLOSE_ENTER = 3; 20929277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_CLOSE_EXIT = 4; 20939277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_FADE_ENTER = 5; 20949277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn public static final int ANIM_STYLE_FADE_EXIT = 6; 20959277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn 2096cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public static int transitToStyleIndex(int transit, boolean enter) { 2097cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int animAttr = -1; 2098cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn switch (transit) { 2099cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: 21009277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn animAttr = enter ? ANIM_STYLE_OPEN_ENTER : ANIM_STYLE_OPEN_EXIT; 2101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: 21039277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn animAttr = enter ? ANIM_STYLE_CLOSE_ENTER : ANIM_STYLE_CLOSE_EXIT; 2104cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn case FragmentTransaction.TRANSIT_FRAGMENT_FADE: 21069277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn animAttr = enter ? ANIM_STYLE_FADE_ENTER : ANIM_STYLE_FADE_EXIT; 2107cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn break; 2108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 2109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return animAttr; 2110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 21110f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 21120f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell @Override 2113bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banes public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 21140f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (!"fragment".equals(name)) { 21150f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell return null; 21160f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21170f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 21180f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell String fname = attrs.getAttributeValue(null, "class"); 21190f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell TypedArray a = context.obtainStyledAttributes(attrs, FragmentTag.Fragment); 21200f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fname == null) { 21210f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fname = a.getString(FragmentTag.Fragment_name); 21220f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21230f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID); 21240f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell String tag = a.getString(FragmentTag.Fragment_tag); 21250f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell a.recycle(); 21260f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 2127d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy if (!Fragment.isSupportFragmentClass(mHost.getContext(), fname)) { 21280f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // Invalid support lib fragment; let the device's framework handle it. 21290f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // This will allow android.app.Fragments to do the right thing. 21300f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell return null; 21310f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21320f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 21330f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell int containerId = parent != null ? parent.getId() : 0; 21340f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (containerId == View.NO_ID && id == View.NO_ID && tag == null) { 21350f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell throw new IllegalArgumentException(attrs.getPositionDescription() 21360f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname); 21370f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21380f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 21390f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // If we restored from a previous state, we may already have 21400f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // instantiated this fragment from the state and should use 21410f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // that instance instead of making a new one. 21420f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null; 21430f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fragment == null && tag != null) { 21440f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment = findFragmentByTag(tag); 21450f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21460f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fragment == null && containerId != View.NO_ID) { 21470f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment = findFragmentById(containerId); 21480f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21490f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 21500f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x" 21510f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + Integer.toHexString(id) + " fname=" + fname 21520f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + " existing=" + fragment); 21530f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fragment == null) { 21540f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment = Fragment.instantiate(context, fname); 21550f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mFromLayout = true; 21560f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mFragmentId = id != 0 ? id : containerId; 21570f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mContainerId = containerId; 21580f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mTag = tag; 21590f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mInLayout = true; 21600f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mFragmentManager = this; 2161d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.onFragmentInflate(fragment, attrs, fragment.mSavedFragmentState); 21620f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell addFragment(fragment, true); 21630f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 21640f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } else if (fragment.mInLayout) { 21650f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // A fragment already exists and it is not one we restored from 21660f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // previous state. 21670f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell throw new IllegalArgumentException(attrs.getPositionDescription() 21680f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + ": Duplicate id 0x" + Integer.toHexString(id) 21690f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId) 21700f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + " with another fragment for " + fname); 21710f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } else { 21720f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // This fragment was retained from a previous instance; get it 21730f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // going now. 21740f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mInLayout = true; 21750f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // If this fragment is newly instantiated (either right now, or 21760f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // from last saved state), then give it the attributes to 21770f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // initialize itself. 21780f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (!fragment.mRetaining) { 2179d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy mHost.onFragmentInflate(fragment, attrs, fragment.mSavedFragmentState); 21800f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21810f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21820f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 21830f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // If we haven't finished entering the CREATED state ourselves yet, 21840f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell // push the inflated child fragment along. 21850f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (mCurState < Fragment.CREATED && fragment.mFromLayout) { 21860f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell moveToState(fragment, Fragment.CREATED, 0, 0, false); 21870f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } else { 21880f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell moveToState(fragment); 21890f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21900f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 21910f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fragment.mView == null) { 21920f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell throw new IllegalStateException("Fragment " + fname 21930f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell + " did not create a view."); 21940f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21950f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (id != 0) { 21960f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mView.setId(id); 21970f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 21980f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell if (fragment.mView.getTag() == null) { 21990f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell fragment.mView.setTag(tag); 22000f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 22010f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell return fragment.mView; 22020f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 22030f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 2204bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banes LayoutInflaterFactory getLayoutInflaterFactory() { 22050f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell return this; 22060f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 22070f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 22080f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell static class FragmentTag { 22090f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell public static final int[] Fragment = { 22100f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell 0x01010003, 0x010100d0, 0x010100d1 22110f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell }; 22120f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell public static final int Fragment_id = 1; 22130f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell public static final int Fragment_name = 0; 22140f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell public static final int Fragment_tag = 2; 22150f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell } 2216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn} 2217