Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorInflater; |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorListenerAdapter; |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 24 | import android.os.Bundle; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 25 | import android.os.Handler; |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 26 | import android.os.Looper; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 27 | import android.os.Parcel; |
| 28 | import android.os.Parcelable; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 29 | import android.util.DebugUtils; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 31 | import android.util.LogWriter; |
| 32 | import android.util.Slog; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 33 | import android.util.SparseArray; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 34 | import android.view.Menu; |
| 35 | import android.view.MenuInflater; |
| 36 | import android.view.MenuItem; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 37 | import android.view.View; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 38 | import android.view.ViewGroup; |
| 39 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 40 | import java.io.FileDescriptor; |
| 41 | import java.io.PrintWriter; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 43 | import java.util.Arrays; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 44 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Interface for interacting with {@link Fragment} objects inside of an |
| 47 | * {@link Activity} |
| 48 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 49 | public abstract class FragmentManager { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 50 | /** |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 51 | * Representation of an entry on the fragment back stack, as created |
| 52 | * with {@link FragmentTransaction#addToBackStack(String) |
| 53 | * FragmentTransaction.addToBackStack()}. Entries can later be |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 54 | * retrieved with {@link FragmentManager#getBackStackEntryAt(int) |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 55 | * FragmentManager.getBackStackEntry()}. |
| 56 | * |
| 57 | * <p>Note that you should never hold on to a BackStackEntry object; |
| 58 | * the identifier as returned by {@link #getId} is the only thing that |
| 59 | * will be persisted across activity instances. |
| 60 | */ |
| 61 | public interface BackStackEntry { |
| 62 | /** |
| 63 | * Return the unique identifier for the entry. This is the only |
| 64 | * representation of the entry that will persist across activity |
| 65 | * instances. |
| 66 | */ |
| 67 | public int getId(); |
| 68 | |
| 69 | /** |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 70 | * Return the full bread crumb title resource identifier for the entry, |
| 71 | * or 0 if it does not have one. |
| 72 | */ |
| 73 | public int getBreadCrumbTitleRes(); |
| 74 | |
| 75 | /** |
| 76 | * Return the short bread crumb title resource identifier for the entry, |
| 77 | * or 0 if it does not have one. |
| 78 | */ |
| 79 | public int getBreadCrumbShortTitleRes(); |
| 80 | |
| 81 | /** |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 82 | * Return the full bread crumb title for the entry, or null if it |
| 83 | * does not have one. |
| 84 | */ |
| 85 | public CharSequence getBreadCrumbTitle(); |
| 86 | |
| 87 | /** |
| 88 | * Return the short bread crumb title for the entry, or null if it |
| 89 | * does not have one. |
| 90 | */ |
| 91 | public CharSequence getBreadCrumbShortTitle(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Interface to watch for changes to the back stack. |
| 96 | */ |
| 97 | public interface OnBackStackChangedListener { |
| 98 | /** |
| 99 | * Called whenever the contents of the back stack change. |
| 100 | */ |
| 101 | public void onBackStackChanged(); |
| 102 | } |
| 103 | |
| 104 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 105 | * Start a series of edit operations on the Fragments associated with |
| 106 | * this FragmentManager. |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 107 | * |
| 108 | * <p>Note: A fragment transaction can only be created/committed prior |
| 109 | * to an activity saving its state. If you try to commit a transaction |
| 110 | * after {@link Activity#onSaveInstanceState Activity.onSaveInstanceState()} |
| 111 | * (and prior to a following {@link Activity#onStart Activity.onStart} |
| 112 | * or {@link Activity#onResume Activity.onResume()}, you will get an error. |
| 113 | * This is because the framework takes care of saving your current fragments |
| 114 | * in the state, and if changes are made after the state is saved then they |
| 115 | * will be lost.</p> |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 116 | */ |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 117 | public abstract FragmentTransaction beginTransaction(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 118 | |
Dianne Hackborn | 17b9b81 | 2011-01-17 17:16:02 -0800 | [diff] [blame] | 119 | /** @hide -- remove once prebuilts are in. */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 120 | @Deprecated |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 121 | public FragmentTransaction openTransaction() { |
| 122 | return beginTransaction(); |
| 123 | } |
| 124 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 125 | /** |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 126 | * After a {@link FragmentTransaction} is committed with |
| 127 | * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it |
| 128 | * is scheduled to be executed asynchronously on the process's main thread. |
| 129 | * If you want to immediately executing any such pending operations, you |
| 130 | * can call this function (only from the main thread) to do so. Note that |
| 131 | * all callbacks and other related behavior will be done from within this |
| 132 | * call, so be careful about where this is called from. |
| 133 | * |
| 134 | * @return Returns true if there were any pending transactions to be |
| 135 | * executed. |
| 136 | */ |
| 137 | public abstract boolean executePendingTransactions(); |
| 138 | |
| 139 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 140 | * Finds a fragment that was identified by the given id either when inflated |
| 141 | * from XML or as the container ID when added in a transaction. This first |
| 142 | * searches through fragments that are currently added to the manager's |
| 143 | * activity; if no such fragment is found, then all fragments currently |
| 144 | * on the back stack associated with this ID are searched. |
| 145 | * @return The fragment if found or null otherwise. |
| 146 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 147 | public abstract Fragment findFragmentById(int id); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Finds a fragment that was identified by the given tag either when inflated |
| 151 | * from XML or as supplied when added in a transaction. This first |
| 152 | * searches through fragments that are currently added to the manager's |
| 153 | * activity; if no such fragment is found, then all fragments currently |
| 154 | * on the back stack are searched. |
| 155 | * @return The fragment if found or null otherwise. |
| 156 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 157 | public abstract Fragment findFragmentByTag(String tag); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Flag for {@link #popBackStack(String, int)} |
| 161 | * and {@link #popBackStack(int, int)}: If set, and the name or ID of |
| 162 | * a back stack entry has been supplied, then all matching entries will |
| 163 | * be consumed until one that doesn't match is found or the bottom of |
| 164 | * the stack is reached. Otherwise, all entries up to but not including that entry |
| 165 | * will be removed. |
| 166 | */ |
| 167 | public static final int POP_BACK_STACK_INCLUSIVE = 1<<0; |
| 168 | |
| 169 | /** |
Ben Komalo | 87ffa20 | 2011-02-28 12:41:42 -0800 | [diff] [blame] | 170 | * Pop the top state off the back stack. This function is asynchronous -- it |
| 171 | * enqueues the request to pop, but the action will not be performed until the |
| 172 | * application returns to its event loop. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 173 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 174 | public abstract void popBackStack(); |
| 175 | |
| 176 | /** |
| 177 | * Like {@link #popBackStack()}, but performs the operation immediately |
| 178 | * inside of the call. This is like calling {@link #executePendingTransactions()} |
| 179 | * afterwards. |
| 180 | * @return Returns true if there was something popped, else false. |
| 181 | */ |
| 182 | public abstract boolean popBackStackImmediate(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * Pop the last fragment transition from the manager's fragment |
| 186 | * back stack. If there is nothing to pop, false is returned. |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 187 | * This function is asynchronous -- it enqueues the |
| 188 | * request to pop, but the action will not be performed until the application |
| 189 | * returns to its event loop. |
| 190 | * |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 191 | * @param name If non-null, this is the name of a previous back state |
| 192 | * to look for; if found, all states up to that state will be popped. The |
| 193 | * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether |
| 194 | * the named state itself is popped. If null, only the top state is popped. |
| 195 | * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. |
| 196 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 197 | public abstract void popBackStack(String name, int flags); |
| 198 | |
| 199 | /** |
| 200 | * Like {@link #popBackStack(String, int)}, but performs the operation immediately |
| 201 | * inside of the call. This is like calling {@link #executePendingTransactions()} |
| 202 | * afterwards. |
| 203 | * @return Returns true if there was something popped, else false. |
| 204 | */ |
| 205 | public abstract boolean popBackStackImmediate(String name, int flags); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * Pop all back stack states up to the one with the given identifier. |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 209 | * This function is asynchronous -- it enqueues the |
| 210 | * request to pop, but the action will not be performed until the application |
| 211 | * returns to its event loop. |
| 212 | * |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 213 | * @param id Identifier of the stated to be popped. If no identifier exists, |
| 214 | * false is returned. |
| 215 | * The identifier is the number returned by |
| 216 | * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The |
| 217 | * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether |
| 218 | * the named state itself is popped. |
| 219 | * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. |
| 220 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 221 | public abstract void popBackStack(int id, int flags); |
| 222 | |
| 223 | /** |
| 224 | * Like {@link #popBackStack(int, int)}, but performs the operation immediately |
| 225 | * inside of the call. This is like calling {@link #executePendingTransactions()} |
| 226 | * afterwards. |
| 227 | * @return Returns true if there was something popped, else false. |
| 228 | */ |
| 229 | public abstract boolean popBackStackImmediate(int id, int flags); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 230 | |
| 231 | /** |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 232 | * Return the number of entries currently in the back stack. |
| 233 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 234 | public abstract int getBackStackEntryCount(); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 235 | |
| 236 | /** |
| 237 | * Return the BackStackEntry at index <var>index</var> in the back stack; |
| 238 | * entries start index 0 being the bottom of the stack. |
| 239 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 240 | public abstract BackStackEntry getBackStackEntryAt(int index); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Add a new listener for changes to the fragment back stack. |
| 244 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 245 | public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 246 | |
| 247 | /** |
| 248 | * Remove a listener that was previously added with |
| 249 | * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}. |
| 250 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 251 | public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 252 | |
| 253 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 254 | * Put a reference to a fragment in a Bundle. This Bundle can be |
| 255 | * persisted as saved state, and when later restoring |
| 256 | * {@link #getFragment(Bundle, String)} will return the current |
| 257 | * instance of the same fragment. |
| 258 | * |
| 259 | * @param bundle The bundle in which to put the fragment reference. |
| 260 | * @param key The name of the entry in the bundle. |
| 261 | * @param fragment The Fragment whose reference is to be stored. |
| 262 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 263 | public abstract void putFragment(Bundle bundle, String key, Fragment fragment); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 264 | |
| 265 | /** |
| 266 | * Retrieve the current Fragment instance for a reference previously |
| 267 | * placed with {@link #putFragment(Bundle, String, Fragment)}. |
| 268 | * |
| 269 | * @param bundle The bundle from which to retrieve the fragment reference. |
| 270 | * @param key The name of the entry in the bundle. |
| 271 | * @return Returns the current Fragment instance that is associated with |
| 272 | * the given reference. |
| 273 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 274 | public abstract Fragment getFragment(Bundle bundle, String key); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * Print the FragmentManager's state into the given stream. |
| 278 | * |
| 279 | * @param prefix Text to print at the front of each line. |
| 280 | * @param fd The raw file descriptor that the dump is being sent to. |
| 281 | * @param writer A PrintWriter to which the dump is to be set. |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 282 | * @param args Additional arguments to the dump request. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 283 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 284 | public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 285 | |
| 286 | /** |
| 287 | * Control whether the framework's internal fragment manager debugging |
| 288 | * logs are turned on. If enabled, you will see output in logcat as |
| 289 | * the framework performs fragment operations. |
| 290 | */ |
| 291 | public static void enableDebugLogging(boolean enabled) { |
| 292 | FragmentManagerImpl.DEBUG = enabled; |
| 293 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 296 | final class FragmentManagerState implements Parcelable { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 297 | FragmentState[] mActive; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 298 | int[] mAdded; |
| 299 | BackStackState[] mBackStack; |
| 300 | |
| 301 | public FragmentManagerState() { |
| 302 | } |
| 303 | |
| 304 | public FragmentManagerState(Parcel in) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 305 | mActive = in.createTypedArray(FragmentState.CREATOR); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 306 | mAdded = in.createIntArray(); |
| 307 | mBackStack = in.createTypedArray(BackStackState.CREATOR); |
| 308 | } |
| 309 | |
| 310 | public int describeContents() { |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | public void writeToParcel(Parcel dest, int flags) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 315 | dest.writeTypedArray(mActive, flags); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 316 | dest.writeIntArray(mAdded); |
| 317 | dest.writeTypedArray(mBackStack, flags); |
| 318 | } |
| 319 | |
| 320 | public static final Parcelable.Creator<FragmentManagerState> CREATOR |
| 321 | = new Parcelable.Creator<FragmentManagerState>() { |
| 322 | public FragmentManagerState createFromParcel(Parcel in) { |
| 323 | return new FragmentManagerState(in); |
| 324 | } |
| 325 | |
| 326 | public FragmentManagerState[] newArray(int size) { |
| 327 | return new FragmentManagerState[size]; |
| 328 | } |
| 329 | }; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 332 | /** |
| 333 | * Container for fragments associated with an activity. |
| 334 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 335 | final class FragmentManagerImpl extends FragmentManager { |
Dianne Hackborn | ec541e1 | 2011-01-21 16:44:04 -0800 | [diff] [blame] | 336 | static boolean DEBUG = false; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 337 | static final String TAG = "FragmentManager"; |
| 338 | |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 339 | static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state"; |
| 340 | static final String TARGET_STATE_TAG = "android:target_state"; |
| 341 | static final String VIEW_STATE_TAG = "android:view_state"; |
| 342 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 343 | ArrayList<Runnable> mPendingActions; |
| 344 | Runnable[] mTmpActions; |
| 345 | boolean mExecutingActions; |
| 346 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 347 | ArrayList<Fragment> mActive; |
| 348 | ArrayList<Fragment> mAdded; |
| 349 | ArrayList<Integer> mAvailIndices; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 350 | ArrayList<BackStackRecord> mBackStack; |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 351 | ArrayList<Fragment> mCreatedMenus; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 352 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 353 | // Must be accessed while locked. |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 354 | ArrayList<BackStackRecord> mBackStackIndices; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 355 | ArrayList<Integer> mAvailBackStackIndices; |
| 356 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 357 | ArrayList<OnBackStackChangedListener> mBackStackChangeListeners; |
| 358 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 359 | int mCurState = Fragment.INITIALIZING; |
| 360 | Activity mActivity; |
| 361 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 362 | boolean mNeedMenuInvalidate; |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 363 | boolean mStateSaved; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 364 | boolean mDestroyed; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 365 | String mNoTransactionsBecause; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 366 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 367 | // Temporary vars for state save and restore. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 368 | Bundle mStateBundle = null; |
| 369 | SparseArray<Parcelable> mStateArray = null; |
| 370 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 371 | Runnable mExecCommit = new Runnable() { |
| 372 | @Override |
| 373 | public void run() { |
| 374 | execPendingActions(); |
| 375 | } |
| 376 | }; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 377 | |
| 378 | @Override |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 379 | public FragmentTransaction beginTransaction() { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 380 | return new BackStackRecord(this); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 383 | @Override |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 384 | public boolean executePendingTransactions() { |
| 385 | return execPendingActions(); |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void popBackStack() { |
| 390 | enqueueAction(new Runnable() { |
| 391 | @Override public void run() { |
| 392 | popBackStackState(mActivity.mHandler, null, -1, 0); |
| 393 | } |
| 394 | }, false); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public boolean popBackStackImmediate() { |
| 399 | checkStateLoss(); |
| 400 | executePendingTransactions(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 401 | return popBackStackState(mActivity.mHandler, null, -1, 0); |
| 402 | } |
| 403 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 404 | @Override |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 405 | public void popBackStack(final String name, final int flags) { |
| 406 | enqueueAction(new Runnable() { |
| 407 | @Override public void run() { |
| 408 | popBackStackState(mActivity.mHandler, name, -1, flags); |
| 409 | } |
| 410 | }, false); |
| 411 | } |
| 412 | |
| 413 | @Override |
| 414 | public boolean popBackStackImmediate(String name, int flags) { |
| 415 | checkStateLoss(); |
| 416 | executePendingTransactions(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 417 | return popBackStackState(mActivity.mHandler, name, -1, flags); |
| 418 | } |
| 419 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 420 | @Override |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 421 | public void popBackStack(final int id, final int flags) { |
| 422 | if (id < 0) { |
| 423 | throw new IllegalArgumentException("Bad id: " + id); |
| 424 | } |
| 425 | enqueueAction(new Runnable() { |
| 426 | @Override public void run() { |
| 427 | popBackStackState(mActivity.mHandler, null, id, flags); |
| 428 | } |
| 429 | }, false); |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public boolean popBackStackImmediate(int id, int flags) { |
| 434 | checkStateLoss(); |
| 435 | executePendingTransactions(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 436 | if (id < 0) { |
| 437 | throw new IllegalArgumentException("Bad id: " + id); |
| 438 | } |
| 439 | return popBackStackState(mActivity.mHandler, null, id, flags); |
| 440 | } |
| 441 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 442 | @Override |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 443 | public int getBackStackEntryCount() { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 444 | return mBackStack != null ? mBackStack.size() : 0; |
| 445 | } |
| 446 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 447 | @Override |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 448 | public BackStackEntry getBackStackEntryAt(int index) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 449 | return mBackStack.get(index); |
| 450 | } |
| 451 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 452 | @Override |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 453 | public void addOnBackStackChangedListener(OnBackStackChangedListener listener) { |
| 454 | if (mBackStackChangeListeners == null) { |
| 455 | mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>(); |
| 456 | } |
| 457 | mBackStackChangeListeners.add(listener); |
| 458 | } |
| 459 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 460 | @Override |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 461 | public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) { |
| 462 | if (mBackStackChangeListeners != null) { |
| 463 | mBackStackChangeListeners.remove(listener); |
| 464 | } |
| 465 | } |
| 466 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 467 | @Override |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 468 | public void putFragment(Bundle bundle, String key, Fragment fragment) { |
| 469 | if (fragment.mIndex < 0) { |
| 470 | throw new IllegalStateException("Fragment " + fragment |
| 471 | + " is not currently in the FragmentManager"); |
| 472 | } |
| 473 | bundle.putInt(key, fragment.mIndex); |
| 474 | } |
| 475 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 476 | @Override |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 477 | public Fragment getFragment(Bundle bundle, String key) { |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 478 | int index = bundle.getInt(key, -1); |
| 479 | if (index == -1) { |
| 480 | return null; |
| 481 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 482 | if (index >= mActive.size()) { |
| 483 | throw new IllegalStateException("Fragement no longer exists for key " |
| 484 | + key + ": index " + index); |
| 485 | } |
| 486 | Fragment f = mActive.get(index); |
| 487 | if (f == null) { |
| 488 | throw new IllegalStateException("Fragement no longer exists for key " |
| 489 | + key + ": index " + index); |
| 490 | } |
| 491 | return f; |
| 492 | } |
| 493 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 494 | @Override |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 495 | public String toString() { |
| 496 | StringBuilder sb = new StringBuilder(128); |
| 497 | sb.append("FragmentManager{"); |
| 498 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 499 | sb.append(" in "); |
| 500 | DebugUtils.buildShortClassTag(mActivity, sb); |
| 501 | sb.append("}}"); |
| 502 | return sb.toString(); |
| 503 | } |
| 504 | |
| 505 | @Override |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 506 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 507 | String innerPrefix = prefix + " "; |
| 508 | |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 509 | int N; |
| 510 | if (mActive != null) { |
| 511 | N = mActive.size(); |
| 512 | if (N > 0) { |
| 513 | writer.print(prefix); writer.print("Active Fragments in "); |
| 514 | writer.print(Integer.toHexString(System.identityHashCode(this))); |
| 515 | writer.println(":"); |
| 516 | for (int i=0; i<N; i++) { |
| 517 | Fragment f = mActive.get(i); |
| 518 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 519 | writer.print(": "); writer.println(f); |
| 520 | if (f != null) { |
| 521 | f.dump(innerPrefix, fd, writer, args); |
| 522 | } |
| 523 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | if (mAdded != null) { |
| 528 | N = mAdded.size(); |
| 529 | if (N > 0) { |
| 530 | writer.print(prefix); writer.println("Added Fragments:"); |
| 531 | for (int i=0; i<N; i++) { |
| 532 | Fragment f = mAdded.get(i); |
| 533 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 534 | writer.print(": "); writer.println(f.toString()); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 539 | if (mCreatedMenus != null) { |
| 540 | N = mCreatedMenus.size(); |
| 541 | if (N > 0) { |
| 542 | writer.print(prefix); writer.println("Fragments Created Menus:"); |
| 543 | for (int i=0; i<N; i++) { |
| 544 | Fragment f = mCreatedMenus.get(i); |
| 545 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 546 | writer.print(": "); writer.println(f.toString()); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 551 | if (mBackStack != null) { |
| 552 | N = mBackStack.size(); |
| 553 | if (N > 0) { |
| 554 | writer.print(prefix); writer.println("Back Stack:"); |
| 555 | for (int i=0; i<N; i++) { |
| 556 | BackStackRecord bs = mBackStack.get(i); |
| 557 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 558 | writer.print(": "); writer.println(bs.toString()); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 559 | bs.dump(innerPrefix, fd, writer, args); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 563 | |
| 564 | synchronized (this) { |
| 565 | if (mBackStackIndices != null) { |
| 566 | N = mBackStackIndices.size(); |
| 567 | if (N > 0) { |
| 568 | writer.print(prefix); writer.println("Back Stack Indices:"); |
| 569 | for (int i=0; i<N; i++) { |
| 570 | BackStackRecord bs = mBackStackIndices.get(i); |
| 571 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 572 | writer.print(": "); writer.println(bs); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) { |
| 578 | writer.print(prefix); writer.print("mAvailBackStackIndices: "); |
| 579 | writer.println(Arrays.toString(mAvailBackStackIndices.toArray())); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | if (mPendingActions != null) { |
| 584 | N = mPendingActions.size(); |
| 585 | if (N > 0) { |
| 586 | writer.print(prefix); writer.println("Pending Actions:"); |
| 587 | for (int i=0; i<N; i++) { |
| 588 | Runnable r = mPendingActions.get(i); |
| 589 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 590 | writer.print(": "); writer.println(r); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | writer.print(prefix); writer.println("FragmentManager misc state:"); |
| 596 | writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState); |
| 597 | writer.print(" mStateSaved="); writer.print(mStateSaved); |
| 598 | writer.print(" mDestroyed="); writer.println(mDestroyed); |
| 599 | if (mNeedMenuInvalidate) { |
| 600 | writer.print(prefix); writer.print(" mNeedMenuInvalidate="); |
| 601 | writer.println(mNeedMenuInvalidate); |
| 602 | } |
| 603 | if (mNoTransactionsBecause != null) { |
| 604 | writer.print(prefix); writer.print(" mNoTransactionsBecause="); |
| 605 | writer.println(mNoTransactionsBecause); |
| 606 | } |
| 607 | if (mAvailIndices != null && mAvailIndices.size() > 0) { |
| 608 | writer.print(prefix); writer.print(" mAvailIndices: "); |
| 609 | writer.println(Arrays.toString(mAvailIndices.toArray())); |
| 610 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 613 | Animator loadAnimator(Fragment fragment, int transit, boolean enter, |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 614 | int transitionStyle) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 615 | Animator animObj = fragment.onCreateAnimator(transit, enter, |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 616 | fragment.mNextAnim); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 617 | if (animObj != null) { |
| 618 | return animObj; |
| 619 | } |
| 620 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 621 | if (fragment.mNextAnim != 0) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 622 | Animator anim = AnimatorInflater.loadAnimator(mActivity, fragment.mNextAnim); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 623 | if (anim != null) { |
| 624 | return anim; |
| 625 | } |
| 626 | } |
| 627 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 628 | if (transit == 0) { |
| 629 | return null; |
| 630 | } |
| 631 | |
| 632 | int styleIndex = transitToStyleIndex(transit, enter); |
| 633 | if (styleIndex < 0) { |
| 634 | return null; |
| 635 | } |
| 636 | |
| 637 | if (transitionStyle == 0 && mActivity.getWindow() != null) { |
| 638 | transitionStyle = mActivity.getWindow().getAttributes().windowAnimations; |
| 639 | } |
| 640 | if (transitionStyle == 0) { |
| 641 | return null; |
| 642 | } |
| 643 | |
| 644 | TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle, |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 645 | com.android.internal.R.styleable.FragmentAnimation); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 646 | int anim = attrs.getResourceId(styleIndex, 0); |
| 647 | attrs.recycle(); |
| 648 | |
| 649 | if (anim == 0) { |
| 650 | return null; |
| 651 | } |
| 652 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 653 | return AnimatorInflater.loadAnimator(mActivity, anim); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void moveToState(Fragment f, int newState, int transit, int transitionStyle) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 657 | // Fragments that are not currently added will sit in the onCreate() state. |
| 658 | if (!f.mAdded && newState > Fragment.CREATED) { |
| 659 | newState = Fragment.CREATED; |
| 660 | } |
| 661 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 662 | if (f.mState < newState) { |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 663 | // For fragments that are created from a layout, when restoring from |
| 664 | // state we don't want to allow them to be created until they are |
| 665 | // being reloaded from the layout. |
| 666 | if (f.mFromLayout && !f.mInLayout) { |
| 667 | return; |
| 668 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 669 | if (f.mAnimatingAway != null) { |
| 670 | // The fragment is currently being animated... but! Now we |
| 671 | // want to move our state back up. Give up on waiting for the |
| 672 | // animation, move to whatever the final state should be once |
| 673 | // the animation is done, and then we can proceed from there. |
| 674 | f.mAnimatingAway = null; |
| 675 | moveToState(f, f.mStateAfterAnimating, 0, 0); |
| 676 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 677 | switch (f.mState) { |
| 678 | case Fragment.INITIALIZING: |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 679 | if (DEBUG) Log.v(TAG, "moveto CREATED: " + f); |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 680 | if (f.mSavedFragmentState != null) { |
| 681 | f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray( |
| 682 | FragmentManagerImpl.VIEW_STATE_TAG); |
| 683 | f.mTarget = getFragment(f.mSavedFragmentState, |
| 684 | FragmentManagerImpl.TARGET_STATE_TAG); |
| 685 | if (f.mTarget != null) { |
| 686 | f.mTargetRequestCode = f.mSavedFragmentState.getInt( |
| 687 | FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0); |
| 688 | } |
| 689 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 690 | f.mActivity = mActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 691 | f.mFragmentManager = mActivity.mFragments; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 692 | f.mCalled = false; |
| 693 | f.onAttach(mActivity); |
| 694 | if (!f.mCalled) { |
| 695 | throw new SuperNotCalledException("Fragment " + f |
| 696 | + " did not call through to super.onAttach()"); |
| 697 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 698 | mActivity.onAttachFragment(f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 699 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 700 | if (!f.mRetaining) { |
| 701 | f.mCalled = false; |
| 702 | f.onCreate(f.mSavedFragmentState); |
| 703 | if (!f.mCalled) { |
| 704 | throw new SuperNotCalledException("Fragment " + f |
| 705 | + " did not call through to super.onCreate()"); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 708 | f.mRetaining = false; |
| 709 | if (f.mFromLayout) { |
| 710 | // For fragments that are part of the content view |
| 711 | // layout, we need to instantiate the view immediately |
| 712 | // and the inflater will take care of adding it. |
Dianne Hackborn | 7187ccb | 2011-01-24 23:58:13 -0800 | [diff] [blame] | 713 | f.mView = f.onCreateView(f.getLayoutInflater(f.mSavedFragmentState), |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 714 | null, f.mSavedFragmentState); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 715 | if (f.mView != null) { |
| 716 | f.mView.setSaveFromParentEnabled(false); |
Dianne Hackborn | d071ec2 | 2011-04-18 14:08:33 -0700 | [diff] [blame] | 717 | if (f.mHidden) f.mView.setVisibility(View.GONE); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 718 | f.restoreViewState(); |
Dianne Hackborn | d071ec2 | 2011-04-18 14:08:33 -0700 | [diff] [blame] | 719 | f.onViewCreated(f.mView, f.mSavedFragmentState); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 720 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 721 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 722 | case Fragment.CREATED: |
| 723 | if (newState > Fragment.CREATED) { |
Dianne Hackborn | d071ec2 | 2011-04-18 14:08:33 -0700 | [diff] [blame] | 724 | if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 725 | if (!f.mFromLayout) { |
| 726 | ViewGroup container = null; |
| 727 | if (f.mContainerId != 0) { |
| 728 | container = (ViewGroup)mActivity.findViewById(f.mContainerId); |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame] | 729 | if (container == null && !f.mRestored) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 730 | throw new IllegalArgumentException("No view found for id 0x" |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 731 | + Integer.toHexString(f.mContainerId) |
| 732 | + " for fragment " + f); |
| 733 | } |
| 734 | } |
| 735 | f.mContainer = container; |
Dianne Hackborn | 7187ccb | 2011-01-24 23:58:13 -0800 | [diff] [blame] | 736 | f.mView = f.onCreateView(f.getLayoutInflater(f.mSavedFragmentState), |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 737 | container, f.mSavedFragmentState); |
| 738 | if (f.mView != null) { |
| 739 | f.mView.setSaveFromParentEnabled(false); |
| 740 | if (container != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 741 | Animator anim = loadAnimator(f, transit, true, |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 742 | transitionStyle); |
| 743 | if (anim != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 744 | anim.setTarget(f.mView); |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 745 | anim.start(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 746 | } |
| 747 | container.addView(f.mView); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 748 | } |
Dianne Hackborn | d071ec2 | 2011-04-18 14:08:33 -0700 | [diff] [blame] | 749 | if (f.mHidden) f.mView.setVisibility(View.GONE); |
| 750 | f.restoreViewState(); |
| 751 | f.onViewCreated(f.mView, f.mSavedFragmentState); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
| 755 | f.mCalled = false; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 756 | f.onActivityCreated(f.mSavedFragmentState); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 757 | if (!f.mCalled) { |
| 758 | throw new SuperNotCalledException("Fragment " + f |
Dianne Hackborn | ec541e1 | 2011-01-21 16:44:04 -0800 | [diff] [blame] | 759 | + " did not call through to super.onActivityCreated()"); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 760 | } |
Dianne Hackborn | d071ec2 | 2011-04-18 14:08:33 -0700 | [diff] [blame] | 761 | if (f.mView != null) { |
| 762 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 763 | f.mSavedFragmentState = null; |
| 764 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 765 | case Fragment.ACTIVITY_CREATED: |
Dianne Hackborn | d071ec2 | 2011-04-18 14:08:33 -0700 | [diff] [blame] | 766 | case Fragment.STOPPED: |
| 767 | if (newState > Fragment.STOPPED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 768 | if (DEBUG) Log.v(TAG, "moveto STARTED: " + f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 769 | f.mCalled = false; |
| 770 | f.onStart(); |
| 771 | if (!f.mCalled) { |
| 772 | throw new SuperNotCalledException("Fragment " + f |
| 773 | + " did not call through to super.onStart()"); |
| 774 | } |
| 775 | } |
| 776 | case Fragment.STARTED: |
| 777 | if (newState > Fragment.STARTED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 778 | if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 779 | f.mCalled = false; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 780 | f.mResumed = true; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 781 | f.onResume(); |
| 782 | if (!f.mCalled) { |
| 783 | throw new SuperNotCalledException("Fragment " + f |
| 784 | + " did not call through to super.onResume()"); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } else if (f.mState > newState) { |
| 789 | switch (f.mState) { |
| 790 | case Fragment.RESUMED: |
| 791 | if (newState < Fragment.RESUMED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 792 | if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 793 | f.mCalled = false; |
| 794 | f.onPause(); |
| 795 | if (!f.mCalled) { |
| 796 | throw new SuperNotCalledException("Fragment " + f |
| 797 | + " did not call through to super.onPause()"); |
| 798 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 799 | f.mResumed = false; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 800 | } |
| 801 | case Fragment.STARTED: |
| 802 | if (newState < Fragment.STARTED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 803 | if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 804 | f.mCalled = false; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 805 | f.performStop(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 806 | if (!f.mCalled) { |
| 807 | throw new SuperNotCalledException("Fragment " + f |
| 808 | + " did not call through to super.onStop()"); |
| 809 | } |
| 810 | } |
Dianne Hackborn | d071ec2 | 2011-04-18 14:08:33 -0700 | [diff] [blame] | 811 | case Fragment.STOPPED: |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 812 | case Fragment.ACTIVITY_CREATED: |
| 813 | if (newState < Fragment.ACTIVITY_CREATED) { |
Dianne Hackborn | d071ec2 | 2011-04-18 14:08:33 -0700 | [diff] [blame] | 814 | if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 815 | if (f.mView != null) { |
| 816 | // Need to save the current view state if not |
| 817 | // done already. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 818 | if (!mActivity.isFinishing() && f.mSavedViewState == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 819 | saveFragmentViewState(f); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 820 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 821 | } |
| 822 | f.mCalled = false; |
| 823 | f.onDestroyView(); |
| 824 | if (!f.mCalled) { |
| 825 | throw new SuperNotCalledException("Fragment " + f |
Dianne Hackborn | ec541e1 | 2011-01-21 16:44:04 -0800 | [diff] [blame] | 826 | + " did not call through to super.onDestroyView()"); |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 827 | } |
| 828 | if (f.mView != null && f.mContainer != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 829 | Animator anim = null; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 830 | if (mCurState > Fragment.INITIALIZING && !mDestroyed) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 831 | anim = loadAnimator(f, transit, false, |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 832 | transitionStyle); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 833 | } |
| 834 | if (anim != null) { |
| 835 | final ViewGroup container = f.mContainer; |
| 836 | final View view = f.mView; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 837 | final Fragment fragment = f; |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 838 | container.startViewTransition(view); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 839 | f.mAnimatingAway = anim; |
| 840 | f.mStateAfterAnimating = newState; |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 841 | anim.addListener(new AnimatorListenerAdapter() { |
| 842 | @Override |
| 843 | public void onAnimationEnd(Animator anim) { |
| 844 | container.endViewTransition(view); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 845 | if (fragment.mAnimatingAway != null) { |
| 846 | fragment.mAnimatingAway = null; |
| 847 | moveToState(fragment, fragment.mStateAfterAnimating, |
| 848 | 0, 0); |
| 849 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 850 | } |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 851 | }); |
| 852 | anim.setTarget(f.mView); |
| 853 | anim.start(); |
| 854 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 855 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 856 | f.mContainer.removeView(f.mView); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 857 | } |
| 858 | f.mContainer = null; |
| 859 | f.mView = null; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 860 | } |
| 861 | case Fragment.CREATED: |
| 862 | if (newState < Fragment.CREATED) { |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 863 | if (mDestroyed) { |
| 864 | if (f.mAnimatingAway != null) { |
| 865 | // The fragment's containing activity is |
| 866 | // being destroyed, but this fragment is |
| 867 | // currently animating away. Stop the |
| 868 | // animation right now -- it is not needed, |
| 869 | // and we can't wait any more on destroying |
| 870 | // the fragment. |
Dianne Hackborn | 1b39e22 | 2010-12-28 14:17:18 -0800 | [diff] [blame] | 871 | Animator anim = f.mAnimatingAway; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 872 | f.mAnimatingAway = null; |
Dianne Hackborn | 1b39e22 | 2010-12-28 14:17:18 -0800 | [diff] [blame] | 873 | anim.cancel(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 874 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 875 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 876 | if (f.mAnimatingAway != null) { |
| 877 | // We are waiting for the fragment's view to finish |
| 878 | // animating away. Just make a note of the state |
| 879 | // the fragment now should move to once the animation |
| 880 | // is done. |
| 881 | f.mStateAfterAnimating = newState; |
| 882 | } else { |
| 883 | if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f); |
| 884 | if (!f.mRetaining) { |
| 885 | f.mCalled = false; |
| 886 | f.onDestroy(); |
| 887 | if (!f.mCalled) { |
| 888 | throw new SuperNotCalledException("Fragment " + f |
| 889 | + " did not call through to super.onDestroy()"); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | f.mCalled = false; |
| 894 | f.onDetach(); |
| 895 | if (!f.mCalled) { |
| 896 | throw new SuperNotCalledException("Fragment " + f |
| 897 | + " did not call through to super.onDetach()"); |
| 898 | } |
| 899 | f.mImmediateActivity = null; |
| 900 | f.mActivity = null; |
| 901 | f.mFragmentManager = null; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 902 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | f.mState = newState; |
| 908 | } |
| 909 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 910 | void moveToState(Fragment f) { |
| 911 | moveToState(f, mCurState, 0, 0); |
| 912 | } |
| 913 | |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 914 | void moveToState(int newState, boolean always) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 915 | moveToState(newState, 0, 0, always); |
| 916 | } |
| 917 | |
| 918 | void moveToState(int newState, int transit, int transitStyle, boolean always) { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 919 | if (mActivity == null && newState != Fragment.INITIALIZING) { |
| 920 | throw new IllegalStateException("No activity"); |
| 921 | } |
| 922 | |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 923 | if (!always && mCurState == newState) { |
| 924 | return; |
| 925 | } |
| 926 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 927 | mCurState = newState; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 928 | if (mActive != null) { |
| 929 | for (int i=0; i<mActive.size(); i++) { |
| 930 | Fragment f = mActive.get(i); |
| 931 | if (f != null) { |
| 932 | moveToState(f, newState, transit, transitStyle); |
| 933 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 934 | } |
Dianne Hackborn | 5f36c96 | 2010-08-26 15:54:17 -0700 | [diff] [blame] | 935 | |
| 936 | if (mNeedMenuInvalidate && mActivity != null) { |
| 937 | mActivity.invalidateOptionsMenu(); |
| 938 | mNeedMenuInvalidate = false; |
| 939 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 943 | void makeActive(Fragment f) { |
| 944 | if (f.mIndex >= 0) { |
| 945 | return; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 946 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 947 | |
| 948 | if (mAvailIndices == null || mAvailIndices.size() <= 0) { |
| 949 | if (mActive == null) { |
| 950 | mActive = new ArrayList<Fragment>(); |
| 951 | } |
| 952 | f.setIndex(mActive.size()); |
| 953 | mActive.add(f); |
| 954 | |
| 955 | } else { |
| 956 | f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1)); |
| 957 | mActive.set(f.mIndex, f); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | void makeInactive(Fragment f) { |
| 962 | if (f.mIndex < 0) { |
| 963 | return; |
| 964 | } |
| 965 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 966 | if (DEBUG) Log.v(TAG, "Freeing fragment index " + f.mIndex); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 967 | mActive.set(f.mIndex, null); |
| 968 | if (mAvailIndices == null) { |
| 969 | mAvailIndices = new ArrayList<Integer>(); |
| 970 | } |
| 971 | mAvailIndices.add(f.mIndex); |
Dianne Hackborn | 9e14e9f3 | 2010-07-14 11:07:38 -0700 | [diff] [blame] | 972 | mActivity.invalidateFragmentIndex(f.mIndex); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 973 | f.clearIndex(); |
| 974 | } |
| 975 | |
| 976 | public void addFragment(Fragment fragment, boolean moveToStateNow) { |
| 977 | if (mAdded == null) { |
| 978 | mAdded = new ArrayList<Fragment>(); |
| 979 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 980 | if (DEBUG) Log.v(TAG, "add: " + fragment); |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 981 | makeActive(fragment); |
| 982 | if (!fragment.mDetached) { |
| 983 | mAdded.add(fragment); |
| 984 | fragment.mAdded = true; |
| 985 | fragment.mRemoving = false; |
| 986 | if (fragment.mHasMenu) { |
| 987 | mNeedMenuInvalidate = true; |
| 988 | } |
| 989 | if (moveToStateNow) { |
| 990 | moveToState(fragment); |
| 991 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 992 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 995 | public void removeFragment(Fragment fragment, int transition, int transitionStyle) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 996 | if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting); |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 997 | final boolean inactive = !fragment.isInBackStack(); |
| 998 | if (!fragment.mDetached || inactive) { |
| 999 | mAdded.remove(fragment); |
| 1000 | if (fragment.mHasMenu) { |
| 1001 | mNeedMenuInvalidate = true; |
| 1002 | } |
| 1003 | fragment.mAdded = false; |
| 1004 | fragment.mRemoving = true; |
| 1005 | moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED, |
| 1006 | transition, transitionStyle); |
| 1007 | if (inactive) { |
| 1008 | makeInactive(fragment); |
| 1009 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1010 | } |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1013 | public void hideFragment(Fragment fragment, int transition, int transitionStyle) { |
| 1014 | if (DEBUG) Log.v(TAG, "hide: " + fragment); |
| 1015 | if (!fragment.mHidden) { |
| 1016 | fragment.mHidden = true; |
| 1017 | if (fragment.mView != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1018 | Animator anim = loadAnimator(fragment, transition, true, |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1019 | transitionStyle); |
| 1020 | if (anim != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1021 | anim.setTarget(fragment.mView); |
Chet Haase | 61eb40d | 2010-12-28 13:59:17 -0800 | [diff] [blame] | 1022 | // Delay the actual hide operation until the animation finishes, otherwise |
| 1023 | // the fragment will just immediately disappear |
| 1024 | final Fragment finalFragment = fragment; |
| 1025 | anim.addListener(new AnimatorListenerAdapter() { |
| 1026 | @Override |
| 1027 | public void onAnimationEnd(Animator animation) { |
Chet Haase | b29407f | 2011-01-11 14:09:34 -0800 | [diff] [blame] | 1028 | if (finalFragment.mView != null) { |
| 1029 | finalFragment.mView.setVisibility(View.GONE); |
| 1030 | } |
Chet Haase | 61eb40d | 2010-12-28 13:59:17 -0800 | [diff] [blame] | 1031 | } |
| 1032 | }); |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1033 | anim.start(); |
Chet Haase | e646b28c | 2010-12-28 14:48:32 -0800 | [diff] [blame] | 1034 | } else { |
| 1035 | fragment.mView.setVisibility(View.GONE); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1036 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1037 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1038 | if (fragment.mAdded && fragment.mHasMenu) { |
| 1039 | mNeedMenuInvalidate = true; |
| 1040 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1041 | fragment.onHiddenChanged(true); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | public void showFragment(Fragment fragment, int transition, int transitionStyle) { |
| 1046 | if (DEBUG) Log.v(TAG, "show: " + fragment); |
| 1047 | if (fragment.mHidden) { |
| 1048 | fragment.mHidden = false; |
| 1049 | if (fragment.mView != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1050 | Animator anim = loadAnimator(fragment, transition, true, |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1051 | transitionStyle); |
| 1052 | if (anim != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1053 | anim.setTarget(fragment.mView); |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1054 | anim.start(); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1055 | } |
| 1056 | fragment.mView.setVisibility(View.VISIBLE); |
| 1057 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1058 | if (fragment.mAdded && fragment.mHasMenu) { |
| 1059 | mNeedMenuInvalidate = true; |
| 1060 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1061 | fragment.onHiddenChanged(false); |
| 1062 | } |
| 1063 | } |
| 1064 | |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1065 | public void detachFragment(Fragment fragment, int transition, int transitionStyle) { |
| 1066 | if (DEBUG) Log.v(TAG, "detach: " + fragment); |
| 1067 | if (!fragment.mDetached) { |
| 1068 | fragment.mDetached = true; |
| 1069 | if (fragment.mAdded) { |
| 1070 | // We are not already in back stack, so need to remove the fragment. |
| 1071 | mAdded.remove(fragment); |
| 1072 | if (fragment.mHasMenu) { |
| 1073 | mNeedMenuInvalidate = true; |
| 1074 | } |
| 1075 | fragment.mAdded = false; |
| 1076 | fragment.mRemoving = true; |
| 1077 | moveToState(fragment, Fragment.CREATED, transition, transitionStyle); |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | public void attachFragment(Fragment fragment, int transition, int transitionStyle) { |
| 1083 | if (DEBUG) Log.v(TAG, "attach: " + fragment); |
| 1084 | if (fragment.mDetached) { |
| 1085 | fragment.mDetached = false; |
| 1086 | if (!fragment.mAdded) { |
| 1087 | mAdded.add(fragment); |
| 1088 | fragment.mAdded = true; |
| 1089 | fragment.mRemoving = false; |
| 1090 | if (fragment.mHasMenu) { |
| 1091 | mNeedMenuInvalidate = true; |
| 1092 | } |
| 1093 | moveToState(fragment, mCurState, transition, transitionStyle); |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1098 | public Fragment findFragmentById(int id) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1099 | if (mActive != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1100 | // First look through added fragments. |
| 1101 | for (int i=mAdded.size()-1; i>=0; i--) { |
| 1102 | Fragment f = mAdded.get(i); |
| 1103 | if (f != null && f.mFragmentId == id) { |
| 1104 | return f; |
| 1105 | } |
| 1106 | } |
| 1107 | // Now for any known fragment. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1108 | for (int i=mActive.size()-1; i>=0; i--) { |
| 1109 | Fragment f = mActive.get(i); |
| 1110 | if (f != null && f.mFragmentId == id) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1111 | return f; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | return null; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1118 | public Fragment findFragmentByTag(String tag) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1119 | if (mActive != null && tag != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1120 | // First look through added fragments. |
| 1121 | for (int i=mAdded.size()-1; i>=0; i--) { |
| 1122 | Fragment f = mAdded.get(i); |
| 1123 | if (f != null && tag.equals(f.mTag)) { |
| 1124 | return f; |
| 1125 | } |
| 1126 | } |
| 1127 | // Now for any known fragment. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1128 | for (int i=mActive.size()-1; i>=0; i--) { |
| 1129 | Fragment f = mActive.get(i); |
| 1130 | if (f != null && tag.equals(f.mTag)) { |
| 1131 | return f; |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | return null; |
| 1136 | } |
| 1137 | |
| 1138 | public Fragment findFragmentByWho(String who) { |
| 1139 | if (mActive != null && who != null) { |
| 1140 | for (int i=mActive.size()-1; i>=0; i--) { |
| 1141 | Fragment f = mActive.get(i); |
| 1142 | if (f != null && who.equals(f.mWho)) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1143 | return f; |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | return null; |
| 1148 | } |
| 1149 | |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1150 | private void checkStateLoss() { |
| 1151 | if (mStateSaved) { |
| 1152 | throw new IllegalStateException( |
| 1153 | "Can not perform this action after onSaveInstanceState"); |
| 1154 | } |
| 1155 | if (mNoTransactionsBecause != null) { |
| 1156 | throw new IllegalStateException( |
| 1157 | "Can not perform this action inside of " + mNoTransactionsBecause); |
| 1158 | } |
| 1159 | } |
| 1160 | |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 1161 | public void enqueueAction(Runnable action, boolean allowStateLoss) { |
Dianne Hackborn | 6908cd1 | 2010-11-08 15:11:16 -0800 | [diff] [blame] | 1162 | if (!allowStateLoss) { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1163 | checkStateLoss(); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1164 | } |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1165 | synchronized (this) { |
Dianne Hackborn | 6908cd1 | 2010-11-08 15:11:16 -0800 | [diff] [blame] | 1166 | if (mActivity == null) { |
| 1167 | throw new IllegalStateException("Activity has been destroyed"); |
| 1168 | } |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1169 | if (mPendingActions == null) { |
| 1170 | mPendingActions = new ArrayList<Runnable>(); |
| 1171 | } |
| 1172 | mPendingActions.add(action); |
| 1173 | if (mPendingActions.size() == 1) { |
| 1174 | mActivity.mHandler.removeCallbacks(mExecCommit); |
| 1175 | mActivity.mHandler.post(mExecCommit); |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1180 | public int allocBackStackIndex(BackStackRecord bse) { |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1181 | synchronized (this) { |
| 1182 | if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) { |
| 1183 | if (mBackStackIndices == null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1184 | mBackStackIndices = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1185 | } |
| 1186 | int index = mBackStackIndices.size(); |
| 1187 | if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); |
| 1188 | mBackStackIndices.add(bse); |
| 1189 | return index; |
| 1190 | |
| 1191 | } else { |
| 1192 | int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1); |
| 1193 | if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); |
| 1194 | mBackStackIndices.set(index, bse); |
| 1195 | return index; |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1200 | public void setBackStackIndex(int index, BackStackRecord bse) { |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1201 | synchronized (this) { |
| 1202 | if (mBackStackIndices == null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1203 | mBackStackIndices = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1204 | } |
| 1205 | int N = mBackStackIndices.size(); |
| 1206 | if (index < N) { |
| 1207 | if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); |
| 1208 | mBackStackIndices.set(index, bse); |
| 1209 | } else { |
| 1210 | while (N < index) { |
| 1211 | mBackStackIndices.add(null); |
| 1212 | if (mAvailBackStackIndices == null) { |
| 1213 | mAvailBackStackIndices = new ArrayList<Integer>(); |
| 1214 | } |
| 1215 | if (DEBUG) Log.v(TAG, "Adding available back stack index " + N); |
| 1216 | mAvailBackStackIndices.add(N); |
| 1217 | N++; |
| 1218 | } |
| 1219 | if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); |
| 1220 | mBackStackIndices.add(bse); |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | public void freeBackStackIndex(int index) { |
| 1226 | synchronized (this) { |
| 1227 | mBackStackIndices.set(index, null); |
| 1228 | if (mAvailBackStackIndices == null) { |
| 1229 | mAvailBackStackIndices = new ArrayList<Integer>(); |
| 1230 | } |
| 1231 | if (DEBUG) Log.v(TAG, "Freeing back stack index " + index); |
| 1232 | mAvailBackStackIndices.add(index); |
| 1233 | } |
| 1234 | } |
| 1235 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1236 | /** |
| 1237 | * Only call from main thread! |
| 1238 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1239 | public boolean execPendingActions() { |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1240 | if (mExecutingActions) { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1241 | throw new IllegalStateException("Recursive entry to executePendingTransactions"); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Dianne Hackborn | d9b3b7e | 2010-11-16 18:22:49 -0800 | [diff] [blame] | 1244 | if (Looper.myLooper() != mActivity.mHandler.getLooper()) { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1245 | throw new IllegalStateException("Must be called from main thread of process"); |
| 1246 | } |
| 1247 | |
| 1248 | boolean didSomething = false; |
| 1249 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1250 | while (true) { |
| 1251 | int numActions; |
| 1252 | |
| 1253 | synchronized (this) { |
| 1254 | if (mPendingActions == null || mPendingActions.size() == 0) { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1255 | return didSomething; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | numActions = mPendingActions.size(); |
| 1259 | if (mTmpActions == null || mTmpActions.length < numActions) { |
| 1260 | mTmpActions = new Runnable[numActions]; |
| 1261 | } |
| 1262 | mPendingActions.toArray(mTmpActions); |
| 1263 | mPendingActions.clear(); |
| 1264 | mActivity.mHandler.removeCallbacks(mExecCommit); |
| 1265 | } |
| 1266 | |
| 1267 | mExecutingActions = true; |
| 1268 | for (int i=0; i<numActions; i++) { |
| 1269 | mTmpActions[i].run(); |
| 1270 | } |
| 1271 | mExecutingActions = false; |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1272 | didSomething = true; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1276 | void reportBackStackChanged() { |
| 1277 | if (mBackStackChangeListeners != null) { |
| 1278 | for (int i=0; i<mBackStackChangeListeners.size(); i++) { |
| 1279 | mBackStackChangeListeners.get(i).onBackStackChanged(); |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | void addBackStackState(BackStackRecord state) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1285 | if (mBackStack == null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1286 | mBackStack = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1287 | } |
| 1288 | mBackStack.add(state); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1289 | reportBackStackChanged(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1292 | boolean popBackStackState(Handler handler, String name, int id, int flags) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1293 | if (mBackStack == null) { |
| 1294 | return false; |
| 1295 | } |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1296 | if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1297 | int last = mBackStack.size()-1; |
| 1298 | if (last < 0) { |
| 1299 | return false; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1300 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1301 | final BackStackRecord bss = mBackStack.remove(last); |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1302 | bss.popFromBackStack(true); |
| 1303 | reportBackStackChanged(); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1304 | } else { |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1305 | int index = -1; |
| 1306 | if (name != null || id >= 0) { |
| 1307 | // If a name or ID is specified, look for that place in |
| 1308 | // the stack. |
| 1309 | index = mBackStack.size()-1; |
| 1310 | while (index >= 0) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1311 | BackStackRecord bss = mBackStack.get(index); |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1312 | if (name != null && name.equals(bss.getName())) { |
| 1313 | break; |
| 1314 | } |
| 1315 | if (id >= 0 && id == bss.mIndex) { |
| 1316 | break; |
| 1317 | } |
| 1318 | index--; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1319 | } |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1320 | if (index < 0) { |
| 1321 | return false; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1322 | } |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1323 | if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) { |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1324 | index--; |
| 1325 | // Consume all following entries that match. |
| 1326 | while (index >= 0) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1327 | BackStackRecord bss = mBackStack.get(index); |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1328 | if ((name != null && name.equals(bss.getName())) |
| 1329 | || (id >= 0 && id == bss.mIndex)) { |
| 1330 | index--; |
| 1331 | continue; |
| 1332 | } |
| 1333 | break; |
| 1334 | } |
| 1335 | } |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1336 | } |
| 1337 | if (index == mBackStack.size()-1) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1338 | return false; |
| 1339 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1340 | final ArrayList<BackStackRecord> states |
| 1341 | = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1342 | for (int i=mBackStack.size()-1; i>index; i--) { |
| 1343 | states.add(mBackStack.remove(i)); |
| 1344 | } |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1345 | final int LAST = states.size()-1; |
| 1346 | for (int i=0; i<=LAST; i++) { |
| 1347 | if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i)); |
| 1348 | states.get(i).popFromBackStack(i == LAST); |
| 1349 | } |
| 1350 | reportBackStackChanged(); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1351 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1352 | return true; |
| 1353 | } |
| 1354 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1355 | ArrayList<Fragment> retainNonConfig() { |
| 1356 | ArrayList<Fragment> fragments = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1357 | if (mActive != null) { |
| 1358 | for (int i=0; i<mActive.size(); i++) { |
| 1359 | Fragment f = mActive.get(i); |
| 1360 | if (f != null && f.mRetainInstance) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1361 | if (fragments == null) { |
| 1362 | fragments = new ArrayList<Fragment>(); |
| 1363 | } |
| 1364 | fragments.add(f); |
| 1365 | f.mRetaining = true; |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | return fragments; |
| 1370 | } |
| 1371 | |
| 1372 | void saveFragmentViewState(Fragment f) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1373 | if (f.mView == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1374 | return; |
| 1375 | } |
| 1376 | if (mStateArray == null) { |
| 1377 | mStateArray = new SparseArray<Parcelable>(); |
| 1378 | } |
| 1379 | f.mView.saveHierarchyState(mStateArray); |
| 1380 | if (mStateArray.size() > 0) { |
| 1381 | f.mSavedViewState = mStateArray; |
| 1382 | mStateArray = null; |
| 1383 | } |
| 1384 | } |
| 1385 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1386 | Parcelable saveAllState() { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1387 | // Make sure all pending operations have now been executed to get |
| 1388 | // our state update-to-date. |
| 1389 | execPendingActions(); |
| 1390 | |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1391 | mStateSaved = true; |
| 1392 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1393 | if (mActive == null || mActive.size() <= 0) { |
| 1394 | return null; |
| 1395 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1396 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1397 | // First collect all active fragments. |
| 1398 | int N = mActive.size(); |
| 1399 | FragmentState[] active = new FragmentState[N]; |
| 1400 | boolean haveFragments = false; |
| 1401 | for (int i=0; i<N; i++) { |
| 1402 | Fragment f = mActive.get(i); |
| 1403 | if (f != null) { |
| 1404 | haveFragments = true; |
| 1405 | |
| 1406 | FragmentState fs = new FragmentState(f); |
| 1407 | active[i] = fs; |
| 1408 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1409 | if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) { |
| 1410 | if (mStateBundle == null) { |
| 1411 | mStateBundle = new Bundle(); |
| 1412 | } |
| 1413 | f.onSaveInstanceState(mStateBundle); |
| 1414 | if (!mStateBundle.isEmpty()) { |
| 1415 | fs.mSavedFragmentState = mStateBundle; |
| 1416 | mStateBundle = null; |
| 1417 | } |
| 1418 | |
| 1419 | if (f.mView != null) { |
| 1420 | saveFragmentViewState(f); |
| 1421 | if (f.mSavedViewState != null) { |
| 1422 | if (fs.mSavedFragmentState == null) { |
| 1423 | fs.mSavedFragmentState = new Bundle(); |
| 1424 | } |
| 1425 | fs.mSavedFragmentState.putSparseParcelableArray( |
| 1426 | FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | if (f.mTarget != null) { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1431 | if (f.mTarget.mIndex < 0) { |
| 1432 | String msg = "Failure saving state: " + f |
| 1433 | + " has target not in fragment manager: " + f.mTarget; |
| 1434 | Slog.e(TAG, msg); |
| 1435 | dump(" ", null, new PrintWriter(new LogWriter( |
| 1436 | Log.ERROR, TAG, Log.LOG_ID_SYSTEM)), new String[] { }); |
| 1437 | throw new IllegalStateException(msg); |
| 1438 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1439 | if (fs.mSavedFragmentState == null) { |
| 1440 | fs.mSavedFragmentState = new Bundle(); |
| 1441 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1442 | putFragment(fs.mSavedFragmentState, |
| 1443 | FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget); |
| 1444 | if (f.mTargetRequestCode != 0) { |
| 1445 | fs.mSavedFragmentState.putInt( |
| 1446 | FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, |
| 1447 | f.mTargetRequestCode); |
| 1448 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1449 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1450 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1451 | } else { |
| 1452 | fs.mSavedFragmentState = f.mSavedFragmentState; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1453 | } |
| 1454 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1455 | if (DEBUG) Log.v(TAG, "Saved state of " + f + ": " |
| 1456 | + fs.mSavedFragmentState); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | if (!haveFragments) { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1461 | if (DEBUG) Log.v(TAG, "saveAllState: no fragments!"); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1462 | return null; |
| 1463 | } |
| 1464 | |
| 1465 | int[] added = null; |
| 1466 | BackStackState[] backStack = null; |
| 1467 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1468 | // Build list of currently added fragments. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1469 | if (mAdded != null) { |
| 1470 | N = mAdded.size(); |
| 1471 | if (N > 0) { |
| 1472 | added = new int[N]; |
| 1473 | for (int i=0; i<N; i++) { |
| 1474 | added[i] = mAdded.get(i).mIndex; |
| 1475 | if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i |
| 1476 | + ": " + mAdded.get(i)); |
| 1477 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | // Now save back stack. |
| 1482 | if (mBackStack != null) { |
| 1483 | N = mBackStack.size(); |
| 1484 | if (N > 0) { |
| 1485 | backStack = new BackStackState[N]; |
| 1486 | for (int i=0; i<N; i++) { |
| 1487 | backStack[i] = new BackStackState(this, mBackStack.get(i)); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1488 | if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i |
| 1489 | + ": " + mBackStack.get(i)); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1494 | FragmentManagerState fms = new FragmentManagerState(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1495 | fms.mActive = active; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1496 | fms.mAdded = added; |
| 1497 | fms.mBackStack = backStack; |
| 1498 | return fms; |
| 1499 | } |
| 1500 | |
| 1501 | void restoreAllState(Parcelable state, ArrayList<Fragment> nonConfig) { |
| 1502 | // If there is no saved state at all, then there can not be |
| 1503 | // any nonConfig fragments either, so that is that. |
| 1504 | if (state == null) return; |
| 1505 | FragmentManagerState fms = (FragmentManagerState)state; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1506 | if (fms.mActive == null) return; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1507 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1508 | // First re-attach any non-config instances we are retaining back |
| 1509 | // to their saved state, so we don't try to instantiate them again. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1510 | if (nonConfig != null) { |
| 1511 | for (int i=0; i<nonConfig.size(); i++) { |
| 1512 | Fragment f = nonConfig.get(i); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1513 | if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1514 | FragmentState fs = fms.mActive[f.mIndex]; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1515 | fs.mInstance = f; |
| 1516 | f.mSavedViewState = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1517 | f.mBackStackNesting = 0; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1518 | f.mInLayout = false; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1519 | f.mAdded = false; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1520 | if (fs.mSavedFragmentState != null) { |
Dianne Hackborn | 5164246 | 2010-10-28 10:32:37 -0700 | [diff] [blame] | 1521 | fs.mSavedFragmentState.setClassLoader(mActivity.getClassLoader()); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1522 | f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray( |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1523 | FragmentManagerImpl.VIEW_STATE_TAG); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1524 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1528 | // Build the full list of active fragments, instantiating them from |
| 1529 | // their saved state. |
| 1530 | mActive = new ArrayList<Fragment>(fms.mActive.length); |
| 1531 | if (mAvailIndices != null) { |
| 1532 | mAvailIndices.clear(); |
| 1533 | } |
| 1534 | for (int i=0; i<fms.mActive.length; i++) { |
| 1535 | FragmentState fs = fms.mActive[i]; |
| 1536 | if (fs != null) { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1537 | Fragment f = fs.instantiate(mActivity); |
| 1538 | if (DEBUG) Log.v(TAG, "restoreAllState: adding #" + i + ": " + f); |
| 1539 | mActive.add(f); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 1540 | // Now that the fragment is instantiated (or came from being |
| 1541 | // retained above), clear mInstance in case we end up re-restoring |
| 1542 | // from this FragmentState again. |
| 1543 | fs.mInstance = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1544 | } else { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1545 | if (DEBUG) Log.v(TAG, "restoreAllState: adding #" + i + ": (null)"); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1546 | mActive.add(null); |
| 1547 | if (mAvailIndices == null) { |
| 1548 | mAvailIndices = new ArrayList<Integer>(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1549 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1550 | if (DEBUG) Log.v(TAG, "restoreAllState: adding avail #" + i); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1551 | mAvailIndices.add(i); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1552 | } |
| 1553 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1554 | |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1555 | // Update the target of all retained fragments. |
| 1556 | if (nonConfig != null) { |
| 1557 | for (int i=0; i<nonConfig.size(); i++) { |
| 1558 | Fragment f = nonConfig.get(i); |
| 1559 | if (f.mTarget != null) { |
| 1560 | if (f.mTarget.mIndex < mActive.size()) { |
| 1561 | f.mTarget = mActive.get(f.mTarget.mIndex); |
| 1562 | } else { |
| 1563 | Log.w(TAG, "Re-attaching retained fragment " + f |
| 1564 | + " target no longer exists: " + f.mTarget); |
| 1565 | f.mTarget = null; |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1571 | // Build the list of currently added fragments. |
| 1572 | if (fms.mAdded != null) { |
| 1573 | mAdded = new ArrayList<Fragment>(fms.mAdded.length); |
| 1574 | for (int i=0; i<fms.mAdded.length; i++) { |
| 1575 | Fragment f = mActive.get(fms.mAdded[i]); |
| 1576 | if (f == null) { |
| 1577 | throw new IllegalStateException( |
| 1578 | "No instantiated fragment for index #" + fms.mAdded[i]); |
| 1579 | } |
| 1580 | f.mAdded = true; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1581 | f.mImmediateActivity = mActivity; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1582 | if (DEBUG) Log.v(TAG, "restoreAllState: making added #" + i + ": " + f); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1583 | mAdded.add(f); |
| 1584 | } |
| 1585 | } else { |
| 1586 | mAdded = null; |
| 1587 | } |
| 1588 | |
| 1589 | // Build the back stack. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1590 | if (fms.mBackStack != null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1591 | mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1592 | for (int i=0; i<fms.mBackStack.length; i++) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1593 | BackStackRecord bse = fms.mBackStack[i].instantiate(this); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1594 | if (DEBUG) Log.v(TAG, "restoreAllState: adding bse #" + i |
| 1595 | + " (index " + bse.mIndex + "): " + bse); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1596 | mBackStack.add(bse); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1597 | if (bse.mIndex >= 0) { |
| 1598 | setBackStackIndex(bse.mIndex, bse); |
| 1599 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1600 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1601 | } else { |
| 1602 | mBackStack = null; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1606 | public void attachActivity(Activity activity) { |
| 1607 | if (mActivity != null) throw new IllegalStateException(); |
| 1608 | mActivity = activity; |
| 1609 | } |
| 1610 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1611 | public void noteStateNotSaved() { |
| 1612 | mStateSaved = false; |
| 1613 | } |
| 1614 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1615 | public void dispatchCreate() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1616 | mStateSaved = false; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1617 | moveToState(Fragment.CREATED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1620 | public void dispatchActivityCreated() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1621 | mStateSaved = false; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1622 | moveToState(Fragment.ACTIVITY_CREATED, false); |
| 1623 | } |
| 1624 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1625 | public void dispatchStart() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1626 | mStateSaved = false; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1627 | moveToState(Fragment.STARTED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | public void dispatchResume() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1631 | mStateSaved = false; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1632 | moveToState(Fragment.RESUMED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | public void dispatchPause() { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1636 | moveToState(Fragment.STARTED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | public void dispatchStop() { |
Dianne Hackborn | d071ec2 | 2011-04-18 14:08:33 -0700 | [diff] [blame] | 1640 | moveToState(Fragment.STOPPED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | public void dispatchDestroy() { |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1644 | mDestroyed = true; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1645 | moveToState(Fragment.INITIALIZING, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1646 | mActivity = null; |
| 1647 | } |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1648 | |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1649 | public void dispatchConfigurationChanged(Configuration newConfig) { |
| 1650 | if (mActive != null) { |
| 1651 | for (int i=0; i<mAdded.size(); i++) { |
| 1652 | Fragment f = mAdded.get(i); |
| 1653 | if (f != null) { |
| 1654 | f.onConfigurationChanged(newConfig); |
| 1655 | } |
| 1656 | } |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | public void dispatchLowMemory() { |
| 1661 | if (mActive != null) { |
| 1662 | for (int i=0; i<mAdded.size(); i++) { |
| 1663 | Fragment f = mAdded.get(i); |
| 1664 | if (f != null) { |
| 1665 | f.onLowMemory(); |
| 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1671 | public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 1672 | boolean show = false; |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 1673 | ArrayList<Fragment> newMenus = null; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1674 | if (mActive != null) { |
| 1675 | for (int i=0; i<mAdded.size(); i++) { |
| 1676 | Fragment f = mAdded.get(i); |
| 1677 | if (f != null && !f.mHidden && f.mHasMenu) { |
| 1678 | show = true; |
| 1679 | f.onCreateOptionsMenu(menu, inflater); |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 1680 | if (newMenus == null) { |
| 1681 | newMenus = new ArrayList<Fragment>(); |
| 1682 | } |
| 1683 | newMenus.add(f); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | } |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 1687 | |
| 1688 | if (mCreatedMenus != null) { |
| 1689 | for (int i=0; i<mCreatedMenus.size(); i++) { |
| 1690 | Fragment f = mCreatedMenus.get(i); |
| 1691 | if (newMenus == null || !newMenus.contains(f)) { |
| 1692 | f.onDestroyOptionsMenu(); |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | mCreatedMenus = newMenus; |
| 1698 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1699 | return show; |
| 1700 | } |
| 1701 | |
| 1702 | public boolean dispatchPrepareOptionsMenu(Menu menu) { |
| 1703 | boolean show = false; |
| 1704 | if (mActive != null) { |
| 1705 | for (int i=0; i<mAdded.size(); i++) { |
| 1706 | Fragment f = mAdded.get(i); |
| 1707 | if (f != null && !f.mHidden && f.mHasMenu) { |
| 1708 | show = true; |
| 1709 | f.onPrepareOptionsMenu(menu); |
| 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | return show; |
| 1714 | } |
| 1715 | |
| 1716 | public boolean dispatchOptionsItemSelected(MenuItem item) { |
| 1717 | if (mActive != null) { |
| 1718 | for (int i=0; i<mAdded.size(); i++) { |
| 1719 | Fragment f = mAdded.get(i); |
| 1720 | if (f != null && !f.mHidden && f.mHasMenu) { |
| 1721 | if (f.onOptionsItemSelected(item)) { |
| 1722 | return true; |
| 1723 | } |
| 1724 | } |
| 1725 | } |
| 1726 | } |
| 1727 | return false; |
| 1728 | } |
| 1729 | |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1730 | public boolean dispatchContextItemSelected(MenuItem item) { |
| 1731 | if (mActive != null) { |
| 1732 | for (int i=0; i<mAdded.size(); i++) { |
| 1733 | Fragment f = mAdded.get(i); |
| 1734 | if (f != null && !f.mHidden) { |
| 1735 | if (f.onContextItemSelected(item)) { |
| 1736 | return true; |
| 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | return false; |
| 1742 | } |
| 1743 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1744 | public void dispatchOptionsMenuClosed(Menu menu) { |
| 1745 | if (mActive != null) { |
| 1746 | for (int i=0; i<mAdded.size(); i++) { |
| 1747 | Fragment f = mAdded.get(i); |
| 1748 | if (f != null && !f.mHidden && f.mHasMenu) { |
| 1749 | f.onOptionsMenuClosed(menu); |
| 1750 | } |
| 1751 | } |
| 1752 | } |
| 1753 | } |
| 1754 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1755 | public static int reverseTransit(int transit) { |
| 1756 | int rev = 0; |
| 1757 | switch (transit) { |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1758 | case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: |
| 1759 | rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1760 | break; |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1761 | case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: |
| 1762 | rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1763 | break; |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 1764 | case FragmentTransaction.TRANSIT_FRAGMENT_FADE: |
| 1765 | rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE; |
Chet Haase | 9ff82bf | 2010-10-05 14:30:51 -0700 | [diff] [blame] | 1766 | break; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1767 | } |
| 1768 | return rev; |
| 1769 | |
| 1770 | } |
| 1771 | |
| 1772 | public static int transitToStyleIndex(int transit, boolean enter) { |
| 1773 | int animAttr = -1; |
| 1774 | switch (transit) { |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1775 | case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1776 | animAttr = enter |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1777 | ? com.android.internal.R.styleable.FragmentAnimation_fragmentOpenEnterAnimation |
| 1778 | : com.android.internal.R.styleable.FragmentAnimation_fragmentOpenExitAnimation; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1779 | break; |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1780 | case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1781 | animAttr = enter |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1782 | ? com.android.internal.R.styleable.FragmentAnimation_fragmentCloseEnterAnimation |
| 1783 | : com.android.internal.R.styleable.FragmentAnimation_fragmentCloseExitAnimation; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1784 | break; |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 1785 | case FragmentTransaction.TRANSIT_FRAGMENT_FADE: |
Chet Haase | 9ff82bf | 2010-10-05 14:30:51 -0700 | [diff] [blame] | 1786 | animAttr = enter |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 1787 | ? com.android.internal.R.styleable.FragmentAnimation_fragmentFadeEnterAnimation |
| 1788 | : com.android.internal.R.styleable.FragmentAnimation_fragmentFadeExitAnimation; |
Chet Haase | 9ff82bf | 2010-10-05 14:30:51 -0700 | [diff] [blame] | 1789 | break; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1790 | } |
| 1791 | return animAttr; |
| 1792 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1793 | } |