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