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