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; |
Doris Liu | de9284d | 2015-05-22 15:41:46 -0700 | [diff] [blame] | 22 | import android.animation.AnimatorSet; |
| 23 | import android.animation.PropertyValuesHolder; |
| 24 | import android.animation.ValueAnimator; |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Adam Powell | e01f595 | 2016-02-23 15:25:42 -0800 | [diff] [blame] | 27 | import android.content.res.Resources.NotFoundException; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 30 | import android.os.Debug; |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 31 | import android.os.Looper; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 32 | import android.os.Parcel; |
| 33 | import android.os.Parcelable; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 34 | import android.transition.Transition; |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 35 | import android.util.AttributeSet; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 36 | import android.util.DebugUtils; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 38 | import android.util.LogWriter; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 39 | import android.util.SparseArray; |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 40 | import android.util.SuperNotCalledException; |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 41 | import android.view.LayoutInflater; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 42 | import android.view.Menu; |
| 43 | import android.view.MenuInflater; |
| 44 | import android.view.MenuItem; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 45 | import android.view.View; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 46 | import android.view.ViewGroup; |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 47 | import com.android.internal.util.FastPrintWriter; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 48 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 49 | import java.io.FileDescriptor; |
| 50 | import java.io.PrintWriter; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 51 | import java.util.ArrayList; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 52 | import java.util.Arrays; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 53 | import java.util.List; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 54 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Interface for interacting with {@link Fragment} objects inside of an |
| 57 | * {@link Activity} |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 58 | * |
| 59 | * <div class="special reference"> |
| 60 | * <h3>Developer Guides</h3> |
| 61 | * <p>For more information about using fragments, read the |
Andrew Solovay | f539680 | 2016-08-26 15:46:38 -0700 | [diff] [blame] | 62 | * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p> |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 63 | * </div> |
Dianne Hackborn | 7871bad | 2011-12-12 15:19:26 -0800 | [diff] [blame] | 64 | * |
| 65 | * While the FragmentManager API was introduced in |
| 66 | * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API |
| 67 | * at is also available for use on older platforms through |
| 68 | * {@link android.support.v4.app.FragmentActivity}. See the blog post |
| 69 | * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html"> |
| 70 | * Fragments For All</a> for more details. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 71 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 72 | public abstract class FragmentManager { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 73 | /** |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 74 | * Representation of an entry on the fragment back stack, as created |
| 75 | * with {@link FragmentTransaction#addToBackStack(String) |
| 76 | * FragmentTransaction.addToBackStack()}. Entries can later be |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 77 | * retrieved with {@link FragmentManager#getBackStackEntryAt(int) |
Mark Doliner | d0646dc | 2014-08-27 16:04:02 -0700 | [diff] [blame] | 78 | * FragmentManager.getBackStackEntryAt()}. |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 79 | * |
| 80 | * <p>Note that you should never hold on to a BackStackEntry object; |
| 81 | * the identifier as returned by {@link #getId} is the only thing that |
| 82 | * will be persisted across activity instances. |
| 83 | */ |
| 84 | public interface BackStackEntry { |
| 85 | /** |
| 86 | * Return the unique identifier for the entry. This is the only |
| 87 | * representation of the entry that will persist across activity |
| 88 | * instances. |
| 89 | */ |
| 90 | public int getId(); |
| 91 | |
| 92 | /** |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 93 | * Get the name that was supplied to |
| 94 | * {@link FragmentTransaction#addToBackStack(String) |
| 95 | * FragmentTransaction.addToBackStack(String)} when creating this entry. |
| 96 | */ |
| 97 | public String getName(); |
| 98 | |
| 99 | /** |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 100 | * Return the full bread crumb title resource identifier for the entry, |
| 101 | * or 0 if it does not have one. |
| 102 | */ |
| 103 | public int getBreadCrumbTitleRes(); |
| 104 | |
| 105 | /** |
| 106 | * Return the short bread crumb title resource identifier for the entry, |
| 107 | * or 0 if it does not have one. |
| 108 | */ |
| 109 | public int getBreadCrumbShortTitleRes(); |
| 110 | |
| 111 | /** |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 112 | * Return the full bread crumb title for the entry, or null if it |
| 113 | * does not have one. |
| 114 | */ |
| 115 | public CharSequence getBreadCrumbTitle(); |
| 116 | |
| 117 | /** |
| 118 | * Return the short bread crumb title for the entry, or null if it |
| 119 | * does not have one. |
| 120 | */ |
| 121 | public CharSequence getBreadCrumbShortTitle(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Interface to watch for changes to the back stack. |
| 126 | */ |
| 127 | public interface OnBackStackChangedListener { |
| 128 | /** |
| 129 | * Called whenever the contents of the back stack change. |
| 130 | */ |
| 131 | public void onBackStackChanged(); |
| 132 | } |
| 133 | |
| 134 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 135 | * Start a series of edit operations on the Fragments associated with |
| 136 | * this FragmentManager. |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 137 | * |
| 138 | * <p>Note: A fragment transaction can only be created/committed prior |
| 139 | * to an activity saving its state. If you try to commit a transaction |
| 140 | * after {@link Activity#onSaveInstanceState Activity.onSaveInstanceState()} |
| 141 | * (and prior to a following {@link Activity#onStart Activity.onStart} |
| 142 | * or {@link Activity#onResume Activity.onResume()}, you will get an error. |
| 143 | * This is because the framework takes care of saving your current fragments |
| 144 | * in the state, and if changes are made after the state is saved then they |
| 145 | * will be lost.</p> |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 146 | */ |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 147 | public abstract FragmentTransaction beginTransaction(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 148 | |
Dianne Hackborn | 17b9b81 | 2011-01-17 17:16:02 -0800 | [diff] [blame] | 149 | /** @hide -- remove once prebuilts are in. */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 150 | @Deprecated |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 151 | public FragmentTransaction openTransaction() { |
| 152 | return beginTransaction(); |
| 153 | } |
| 154 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 155 | /** |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 156 | * After a {@link FragmentTransaction} is committed with |
| 157 | * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it |
| 158 | * is scheduled to be executed asynchronously on the process's main thread. |
| 159 | * If you want to immediately executing any such pending operations, you |
| 160 | * can call this function (only from the main thread) to do so. Note that |
| 161 | * all callbacks and other related behavior will be done from within this |
| 162 | * call, so be careful about where this is called from. |
| 163 | * |
| 164 | * @return Returns true if there were any pending transactions to be |
| 165 | * executed. |
| 166 | */ |
| 167 | public abstract boolean executePendingTransactions(); |
| 168 | |
| 169 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 170 | * Finds a fragment that was identified by the given id either when inflated |
| 171 | * from XML or as the container ID when added in a transaction. This first |
| 172 | * searches through fragments that are currently added to the manager's |
| 173 | * activity; if no such fragment is found, then all fragments currently |
| 174 | * on the back stack associated with this ID are searched. |
| 175 | * @return The fragment if found or null otherwise. |
| 176 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 177 | public abstract Fragment findFragmentById(int id); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * Finds a fragment that was identified by the given tag either when inflated |
| 181 | * from XML or as supplied when added in a transaction. This first |
| 182 | * searches through fragments that are currently added to the manager's |
| 183 | * activity; if no such fragment is found, then all fragments currently |
| 184 | * on the back stack are searched. |
| 185 | * @return The fragment if found or null otherwise. |
| 186 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 187 | public abstract Fragment findFragmentByTag(String tag); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Flag for {@link #popBackStack(String, int)} |
| 191 | * and {@link #popBackStack(int, int)}: If set, and the name or ID of |
| 192 | * a back stack entry has been supplied, then all matching entries will |
| 193 | * be consumed until one that doesn't match is found or the bottom of |
| 194 | * the stack is reached. Otherwise, all entries up to but not including that entry |
| 195 | * will be removed. |
| 196 | */ |
| 197 | public static final int POP_BACK_STACK_INCLUSIVE = 1<<0; |
| 198 | |
| 199 | /** |
Ben Komalo | 87ffa20 | 2011-02-28 12:41:42 -0800 | [diff] [blame] | 200 | * Pop the top state off the back stack. This function is asynchronous -- it |
| 201 | * enqueues the request to pop, but the action will not be performed until the |
| 202 | * application returns to its event loop. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 203 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 204 | public abstract void popBackStack(); |
| 205 | |
| 206 | /** |
| 207 | * Like {@link #popBackStack()}, but performs the operation immediately |
| 208 | * inside of the call. This is like calling {@link #executePendingTransactions()} |
| 209 | * afterwards. |
| 210 | * @return Returns true if there was something popped, else false. |
| 211 | */ |
| 212 | public abstract boolean popBackStackImmediate(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * Pop the last fragment transition from the manager's fragment |
| 216 | * back stack. If there is nothing to pop, false is returned. |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 217 | * This function is asynchronous -- it enqueues the |
| 218 | * request to pop, but the action will not be performed until the application |
| 219 | * returns to its event loop. |
| 220 | * |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 221 | * @param name If non-null, this is the name of a previous back state |
| 222 | * to look for; if found, all states up to that state will be popped. The |
| 223 | * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether |
| 224 | * the named state itself is popped. If null, only the top state is popped. |
| 225 | * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. |
| 226 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 227 | public abstract void popBackStack(String name, int flags); |
| 228 | |
| 229 | /** |
| 230 | * Like {@link #popBackStack(String, int)}, but performs the operation immediately |
| 231 | * inside of the call. This is like calling {@link #executePendingTransactions()} |
| 232 | * afterwards. |
| 233 | * @return Returns true if there was something popped, else false. |
| 234 | */ |
| 235 | public abstract boolean popBackStackImmediate(String name, int flags); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * 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] | 239 | * This function is asynchronous -- it enqueues the |
| 240 | * request to pop, but the action will not be performed until the application |
| 241 | * returns to its event loop. |
| 242 | * |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 243 | * @param id Identifier of the stated to be popped. If no identifier exists, |
| 244 | * false is returned. |
| 245 | * The identifier is the number returned by |
| 246 | * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The |
| 247 | * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether |
| 248 | * the named state itself is popped. |
| 249 | * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. |
| 250 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 251 | public abstract void popBackStack(int id, int flags); |
| 252 | |
| 253 | /** |
| 254 | * Like {@link #popBackStack(int, int)}, but performs the operation immediately |
| 255 | * inside of the call. This is like calling {@link #executePendingTransactions()} |
| 256 | * afterwards. |
| 257 | * @return Returns true if there was something popped, else false. |
| 258 | */ |
| 259 | public abstract boolean popBackStackImmediate(int id, int flags); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 260 | |
| 261 | /** |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 262 | * Return the number of entries currently in the back stack. |
| 263 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 264 | public abstract int getBackStackEntryCount(); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Return the BackStackEntry at index <var>index</var> in the back stack; |
Mark Doliner | d0646dc | 2014-08-27 16:04:02 -0700 | [diff] [blame] | 268 | * where the item on the bottom of the stack has index 0. |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 269 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 270 | public abstract BackStackEntry getBackStackEntryAt(int index); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * Add a new listener for changes to the fragment back stack. |
| 274 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 275 | public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * Remove a listener that was previously added with |
| 279 | * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}. |
| 280 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 281 | public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 282 | |
| 283 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 284 | * Put a reference to a fragment in a Bundle. This Bundle can be |
| 285 | * persisted as saved state, and when later restoring |
| 286 | * {@link #getFragment(Bundle, String)} will return the current |
| 287 | * instance of the same fragment. |
| 288 | * |
| 289 | * @param bundle The bundle in which to put the fragment reference. |
| 290 | * @param key The name of the entry in the bundle. |
| 291 | * @param fragment The Fragment whose reference is to be stored. |
| 292 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 293 | public abstract void putFragment(Bundle bundle, String key, Fragment fragment); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Retrieve the current Fragment instance for a reference previously |
| 297 | * placed with {@link #putFragment(Bundle, String, Fragment)}. |
| 298 | * |
| 299 | * @param bundle The bundle from which to retrieve the fragment reference. |
| 300 | * @param key The name of the entry in the bundle. |
| 301 | * @return Returns the current Fragment instance that is associated with |
| 302 | * the given reference. |
| 303 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 304 | public abstract Fragment getFragment(Bundle bundle, String key); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 305 | |
| 306 | /** |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 307 | * Save the current instance state of the given Fragment. This can be |
| 308 | * used later when creating a new instance of the Fragment and adding |
| 309 | * it to the fragment manager, to have it create itself to match the |
| 310 | * current state returned here. Note that there are limits on how |
| 311 | * this can be used: |
| 312 | * |
| 313 | * <ul> |
| 314 | * <li>The Fragment must currently be attached to the FragmentManager. |
| 315 | * <li>A new Fragment created using this saved state must be the same class |
| 316 | * type as the Fragment it was created from. |
| 317 | * <li>The saved state can not contain dependencies on other fragments -- |
| 318 | * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to |
| 319 | * store a fragment reference because that reference may not be valid when |
| 320 | * this saved state is later used. Likewise the Fragment's target and |
| 321 | * result code are not included in this state. |
| 322 | * </ul> |
| 323 | * |
| 324 | * @param f The Fragment whose state is to be saved. |
| 325 | * @return The generated state. This will be null if there was no |
| 326 | * interesting state created by the fragment. |
| 327 | */ |
| 328 | public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f); |
| 329 | |
| 330 | /** |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 331 | * Returns true if the final {@link Activity#onDestroy() Activity.onDestroy()} |
| 332 | * call has been made on the FragmentManager's Activity, so this instance is now dead. |
| 333 | */ |
| 334 | public abstract boolean isDestroyed(); |
| 335 | |
| 336 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 337 | * Print the FragmentManager's state into the given stream. |
| 338 | * |
| 339 | * @param prefix Text to print at the front of each line. |
| 340 | * @param fd The raw file descriptor that the dump is being sent to. |
| 341 | * @param writer A PrintWriter to which the dump is to be set. |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 342 | * @param args Additional arguments to the dump request. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 343 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 344 | public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 345 | |
| 346 | /** |
| 347 | * Control whether the framework's internal fragment manager debugging |
| 348 | * logs are turned on. If enabled, you will see output in logcat as |
| 349 | * the framework performs fragment operations. |
| 350 | */ |
| 351 | public static void enableDebugLogging(boolean enabled) { |
| 352 | FragmentManagerImpl.DEBUG = enabled; |
| 353 | } |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 354 | |
| 355 | /** |
| 356 | * Invalidate the attached activity's options menu as necessary. |
| 357 | * This may end up being deferred until we move to the resumed state. |
| 358 | */ |
| 359 | public void invalidateOptionsMenu() { } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 362 | final class FragmentManagerState implements Parcelable { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 363 | FragmentState[] mActive; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 364 | int[] mAdded; |
| 365 | BackStackState[] mBackStack; |
| 366 | |
| 367 | public FragmentManagerState() { |
| 368 | } |
| 369 | |
| 370 | public FragmentManagerState(Parcel in) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 371 | mActive = in.createTypedArray(FragmentState.CREATOR); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 372 | mAdded = in.createIntArray(); |
| 373 | mBackStack = in.createTypedArray(BackStackState.CREATOR); |
| 374 | } |
| 375 | |
| 376 | public int describeContents() { |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | public void writeToParcel(Parcel dest, int flags) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 381 | dest.writeTypedArray(mActive, flags); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 382 | dest.writeIntArray(mAdded); |
| 383 | dest.writeTypedArray(mBackStack, flags); |
| 384 | } |
| 385 | |
| 386 | public static final Parcelable.Creator<FragmentManagerState> CREATOR |
| 387 | = new Parcelable.Creator<FragmentManagerState>() { |
| 388 | public FragmentManagerState createFromParcel(Parcel in) { |
| 389 | return new FragmentManagerState(in); |
| 390 | } |
| 391 | |
| 392 | public FragmentManagerState[] newArray(int size) { |
| 393 | return new FragmentManagerState[size]; |
| 394 | } |
| 395 | }; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 398 | /** |
| 399 | * Container for fragments associated with an activity. |
| 400 | */ |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 401 | final class FragmentManagerImpl extends FragmentManager implements LayoutInflater.Factory2 { |
Craig Mautner | 1c43719 | 2012-08-01 10:01:16 -0700 | [diff] [blame] | 402 | static boolean DEBUG = false; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 403 | static final String TAG = "FragmentManager"; |
| 404 | |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 405 | static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state"; |
| 406 | static final String TARGET_STATE_TAG = "android:target_state"; |
| 407 | static final String VIEW_STATE_TAG = "android:view_state"; |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 408 | static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint"; |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 409 | |
Doris Liu | de9284d | 2015-05-22 15:41:46 -0700 | [diff] [blame] | 410 | static class AnimateOnHWLayerIfNeededListener implements Animator.AnimatorListener { |
| 411 | private boolean mShouldRunOnHWLayer = false; |
| 412 | private View mView; |
| 413 | public AnimateOnHWLayerIfNeededListener(final View v) { |
| 414 | if (v == null) { |
| 415 | return; |
| 416 | } |
| 417 | mView = v; |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public void onAnimationStart(Animator animation) { |
| 422 | mShouldRunOnHWLayer = shouldRunOnHWLayer(mView, animation); |
| 423 | if (mShouldRunOnHWLayer) { |
| 424 | mView.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public void onAnimationEnd(Animator animation) { |
| 430 | if (mShouldRunOnHWLayer) { |
| 431 | mView.setLayerType(View.LAYER_TYPE_NONE, null); |
| 432 | } |
| 433 | mView = null; |
| 434 | animation.removeListener(this); |
| 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public void onAnimationCancel(Animator animation) { |
| 439 | |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public void onAnimationRepeat(Animator animation) { |
| 444 | |
| 445 | } |
| 446 | } |
| 447 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 448 | ArrayList<OpGenerator> mPendingActions; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 449 | boolean mExecutingActions; |
| 450 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 451 | ArrayList<Fragment> mActive; |
| 452 | ArrayList<Fragment> mAdded; |
| 453 | ArrayList<Integer> mAvailIndices; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 454 | ArrayList<BackStackRecord> mBackStack; |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 455 | ArrayList<Fragment> mCreatedMenus; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 456 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 457 | // Must be accessed while locked. |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 458 | ArrayList<BackStackRecord> mBackStackIndices; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 459 | ArrayList<Integer> mAvailBackStackIndices; |
| 460 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 461 | ArrayList<OnBackStackChangedListener> mBackStackChangeListeners; |
| 462 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 463 | int mCurState = Fragment.INITIALIZING; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 464 | FragmentHostCallback<?> mHost; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 465 | FragmentContainer mContainer; |
| 466 | Fragment mParent; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 467 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 468 | boolean mNeedMenuInvalidate; |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 469 | boolean mStateSaved; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 470 | boolean mDestroyed; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 471 | String mNoTransactionsBecause; |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 472 | boolean mHavePendingDeferredStart; |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 473 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 474 | // Temporary vars for optimizing execution of BackStackRecords: |
| 475 | ArrayList<BackStackRecord> mTmpRecords; |
| 476 | ArrayList<Boolean> mTmpIsPop; |
| 477 | SparseArray<BackStackRecord.FragmentContainerTransition> mTmpFragmentsContainerTransitions; |
| 478 | ArrayList<Fragment> mTmpAddedFragments; |
| 479 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 480 | // Temporary vars for state save and restore. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 481 | Bundle mStateBundle = null; |
| 482 | SparseArray<Parcelable> mStateArray = null; |
| 483 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 484 | Runnable mExecCommit = new Runnable() { |
| 485 | @Override |
| 486 | public void run() { |
| 487 | execPendingActions(); |
| 488 | } |
| 489 | }; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 490 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 491 | private void throwException(RuntimeException ex) { |
| 492 | Log.e(TAG, ex.getMessage()); |
| 493 | LogWriter logw = new LogWriter(Log.ERROR, TAG); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 494 | PrintWriter pw = new FastPrintWriter(logw, false, 1024); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 495 | if (mHost != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 496 | Log.e(TAG, "Activity state:"); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 497 | try { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 498 | mHost.onDump(" ", null, pw, new String[] { }); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 499 | } catch (Exception e) { |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 500 | pw.flush(); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 501 | Log.e(TAG, "Failed dumping state", e); |
| 502 | } |
| 503 | } else { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 504 | Log.e(TAG, "Fragment manager state:"); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 505 | try { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 506 | dump(" ", null, pw, new String[] { }); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 507 | } catch (Exception e) { |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 508 | pw.flush(); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 509 | Log.e(TAG, "Failed dumping state", e); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 510 | } |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 511 | } |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 512 | pw.flush(); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 513 | throw ex; |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Doris Liu | de9284d | 2015-05-22 15:41:46 -0700 | [diff] [blame] | 516 | static boolean modifiesAlpha(Animator anim) { |
| 517 | if (anim == null) { |
| 518 | return false; |
| 519 | } |
| 520 | if (anim instanceof ValueAnimator) { |
| 521 | ValueAnimator valueAnim = (ValueAnimator) anim; |
| 522 | PropertyValuesHolder[] values = valueAnim.getValues(); |
| 523 | for (int i = 0; i < values.length; i++) { |
| 524 | if (("alpha").equals(values[i].getPropertyName())) { |
| 525 | return true; |
| 526 | } |
| 527 | } |
| 528 | } else if (anim instanceof AnimatorSet) { |
| 529 | List<Animator> animList = ((AnimatorSet) anim).getChildAnimations(); |
| 530 | for (int i = 0; i < animList.size(); i++) { |
| 531 | if (modifiesAlpha(animList.get(i))) { |
| 532 | return true; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | static boolean shouldRunOnHWLayer(View v, Animator anim) { |
| 540 | if (v == null || anim == null) { |
| 541 | return false; |
| 542 | } |
| 543 | return v.getLayerType() == View.LAYER_TYPE_NONE |
| 544 | && v.hasOverlappingRendering() |
| 545 | && modifiesAlpha(anim); |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Sets the to be animated view on hardware layer during the animation. |
| 550 | */ |
| 551 | private void setHWLayerAnimListenerIfAlpha(final View v, Animator anim) { |
| 552 | if (v == null || anim == null) { |
| 553 | return; |
| 554 | } |
| 555 | if (shouldRunOnHWLayer(v, anim)) { |
| 556 | anim.addListener(new AnimateOnHWLayerIfNeededListener(v)); |
| 557 | } |
| 558 | } |
| 559 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 560 | @Override |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 561 | public FragmentTransaction beginTransaction() { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 562 | return new BackStackRecord(this); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 565 | @Override |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 566 | public boolean executePendingTransactions() { |
| 567 | return execPendingActions(); |
| 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public void popBackStack() { |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 572 | enqueueAction(new PopBackStackState(null, -1, 0), false); |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | @Override |
| 576 | public boolean popBackStackImmediate() { |
| 577 | checkStateLoss(); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 578 | return popBackStackImmediate(null, -1, 0); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 581 | @Override |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 582 | public void popBackStack(String name, int flags) { |
| 583 | enqueueAction(new PopBackStackState(name, -1, flags), false); |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | @Override |
| 587 | public boolean popBackStackImmediate(String name, int flags) { |
| 588 | checkStateLoss(); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 589 | return popBackStackImmediate(name, -1, flags); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 592 | @Override |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 593 | public void popBackStack(int id, int flags) { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 594 | if (id < 0) { |
| 595 | throw new IllegalArgumentException("Bad id: " + id); |
| 596 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 597 | enqueueAction(new PopBackStackState(null, id, flags), false); |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | @Override |
| 601 | public boolean popBackStackImmediate(int id, int flags) { |
| 602 | checkStateLoss(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 603 | if (id < 0) { |
| 604 | throw new IllegalArgumentException("Bad id: " + id); |
| 605 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 606 | return popBackStackImmediate(null, id, flags); |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * Used by all public popBackStackImmediate methods, this executes pending transactions and |
| 611 | * returns true if the pop action did anything, regardless of what other pending |
| 612 | * transactions did. |
| 613 | * |
| 614 | * @return true if the pop operation did anything or false otherwise. |
| 615 | */ |
| 616 | private boolean popBackStackImmediate(String name, int id, int flags) { |
| 617 | executePendingTransactions(); |
| 618 | ensureExecReady(true); |
| 619 | |
| 620 | boolean executePop = popBackStackState(mTmpRecords, mTmpIsPop, name, id, flags); |
| 621 | if (executePop) { |
| 622 | mExecutingActions = true; |
| 623 | try { |
| 624 | optimizeAndExecuteOps(mTmpRecords, mTmpIsPop); |
| 625 | } finally { |
| 626 | cleanupExec(); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | doPendingDeferredStart(); |
| 631 | return executePop; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 634 | @Override |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 635 | public int getBackStackEntryCount() { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 636 | return mBackStack != null ? mBackStack.size() : 0; |
| 637 | } |
| 638 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 639 | @Override |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 640 | public BackStackEntry getBackStackEntryAt(int index) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 641 | return mBackStack.get(index); |
| 642 | } |
| 643 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 644 | @Override |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 645 | public void addOnBackStackChangedListener(OnBackStackChangedListener listener) { |
| 646 | if (mBackStackChangeListeners == null) { |
| 647 | mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>(); |
| 648 | } |
| 649 | mBackStackChangeListeners.add(listener); |
| 650 | } |
| 651 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 652 | @Override |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 653 | public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) { |
| 654 | if (mBackStackChangeListeners != null) { |
| 655 | mBackStackChangeListeners.remove(listener); |
| 656 | } |
| 657 | } |
| 658 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 659 | @Override |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 660 | public void putFragment(Bundle bundle, String key, Fragment fragment) { |
| 661 | if (fragment.mIndex < 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 662 | throwException(new IllegalStateException("Fragment " + fragment |
| 663 | + " is not currently in the FragmentManager")); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 664 | } |
| 665 | bundle.putInt(key, fragment.mIndex); |
| 666 | } |
| 667 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 668 | @Override |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 669 | public Fragment getFragment(Bundle bundle, String key) { |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 670 | int index = bundle.getInt(key, -1); |
| 671 | if (index == -1) { |
| 672 | return null; |
| 673 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 674 | if (index >= mActive.size()) { |
Cyril Mottier | 2de5082 | 2013-09-30 22:42:26 +0200 | [diff] [blame] | 675 | throwException(new IllegalStateException("Fragment no longer exists for key " |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 676 | + key + ": index " + index)); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 677 | } |
| 678 | Fragment f = mActive.get(index); |
| 679 | if (f == null) { |
Cyril Mottier | 2de5082 | 2013-09-30 22:42:26 +0200 | [diff] [blame] | 680 | throwException(new IllegalStateException("Fragment no longer exists for key " |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 681 | + key + ": index " + index)); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 682 | } |
| 683 | return f; |
| 684 | } |
| 685 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 686 | @Override |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 687 | public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) { |
| 688 | if (fragment.mIndex < 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 689 | throwException(new IllegalStateException("Fragment " + fragment |
| 690 | + " is not currently in the FragmentManager")); |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 691 | } |
| 692 | if (fragment.mState > Fragment.INITIALIZING) { |
| 693 | Bundle result = saveFragmentBasicState(fragment); |
| 694 | return result != null ? new Fragment.SavedState(result) : null; |
| 695 | } |
| 696 | return null; |
| 697 | } |
| 698 | |
| 699 | @Override |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 700 | public boolean isDestroyed() { |
| 701 | return mDestroyed; |
| 702 | } |
| 703 | |
| 704 | @Override |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 705 | public String toString() { |
| 706 | StringBuilder sb = new StringBuilder(128); |
| 707 | sb.append("FragmentManager{"); |
| 708 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 709 | sb.append(" in "); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 710 | if (mParent != null) { |
| 711 | DebugUtils.buildShortClassTag(mParent, sb); |
| 712 | } else { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 713 | DebugUtils.buildShortClassTag(mHost, sb); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 714 | } |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 715 | sb.append("}}"); |
| 716 | return sb.toString(); |
| 717 | } |
| 718 | |
| 719 | @Override |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 720 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 721 | String innerPrefix = prefix + " "; |
| 722 | |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 723 | int N; |
| 724 | if (mActive != null) { |
| 725 | N = mActive.size(); |
| 726 | if (N > 0) { |
| 727 | writer.print(prefix); writer.print("Active Fragments in "); |
| 728 | writer.print(Integer.toHexString(System.identityHashCode(this))); |
| 729 | writer.println(":"); |
| 730 | for (int i=0; i<N; i++) { |
| 731 | Fragment f = mActive.get(i); |
| 732 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 733 | writer.print(": "); writer.println(f); |
| 734 | if (f != null) { |
| 735 | f.dump(innerPrefix, fd, writer, args); |
| 736 | } |
| 737 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
| 741 | if (mAdded != null) { |
| 742 | N = mAdded.size(); |
| 743 | if (N > 0) { |
| 744 | writer.print(prefix); writer.println("Added Fragments:"); |
| 745 | for (int i=0; i<N; i++) { |
| 746 | Fragment f = mAdded.get(i); |
| 747 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 748 | writer.print(": "); writer.println(f.toString()); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 753 | if (mCreatedMenus != null) { |
| 754 | N = mCreatedMenus.size(); |
| 755 | if (N > 0) { |
| 756 | writer.print(prefix); writer.println("Fragments Created Menus:"); |
| 757 | for (int i=0; i<N; i++) { |
| 758 | Fragment f = mCreatedMenus.get(i); |
| 759 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 760 | writer.print(": "); writer.println(f.toString()); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 765 | if (mBackStack != null) { |
| 766 | N = mBackStack.size(); |
| 767 | if (N > 0) { |
| 768 | writer.print(prefix); writer.println("Back Stack:"); |
| 769 | for (int i=0; i<N; i++) { |
| 770 | BackStackRecord bs = mBackStack.get(i); |
| 771 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 772 | writer.print(": "); writer.println(bs.toString()); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 773 | bs.dump(innerPrefix, fd, writer, args); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 777 | |
| 778 | synchronized (this) { |
| 779 | if (mBackStackIndices != null) { |
| 780 | N = mBackStackIndices.size(); |
| 781 | if (N > 0) { |
| 782 | writer.print(prefix); writer.println("Back Stack Indices:"); |
| 783 | for (int i=0; i<N; i++) { |
| 784 | BackStackRecord bs = mBackStackIndices.get(i); |
| 785 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 786 | writer.print(": "); writer.println(bs); |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) { |
| 792 | writer.print(prefix); writer.print("mAvailBackStackIndices: "); |
| 793 | writer.println(Arrays.toString(mAvailBackStackIndices.toArray())); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | if (mPendingActions != null) { |
| 798 | N = mPendingActions.size(); |
| 799 | if (N > 0) { |
| 800 | writer.print(prefix); writer.println("Pending Actions:"); |
| 801 | for (int i=0; i<N; i++) { |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 802 | OpGenerator r = mPendingActions.get(i); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 803 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 804 | writer.print(": "); writer.println(r); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | writer.print(prefix); writer.println("FragmentManager misc state:"); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 810 | writer.print(prefix); writer.print(" mHost="); writer.println(mHost); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 811 | writer.print(prefix); writer.print(" mContainer="); writer.println(mContainer); |
| 812 | if (mParent != null) { |
| 813 | writer.print(prefix); writer.print(" mParent="); writer.println(mParent); |
| 814 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 815 | writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState); |
| 816 | writer.print(" mStateSaved="); writer.print(mStateSaved); |
| 817 | writer.print(" mDestroyed="); writer.println(mDestroyed); |
| 818 | if (mNeedMenuInvalidate) { |
| 819 | writer.print(prefix); writer.print(" mNeedMenuInvalidate="); |
| 820 | writer.println(mNeedMenuInvalidate); |
| 821 | } |
| 822 | if (mNoTransactionsBecause != null) { |
| 823 | writer.print(prefix); writer.print(" mNoTransactionsBecause="); |
| 824 | writer.println(mNoTransactionsBecause); |
| 825 | } |
| 826 | if (mAvailIndices != null && mAvailIndices.size() > 0) { |
| 827 | writer.print(prefix); writer.print(" mAvailIndices: "); |
| 828 | writer.println(Arrays.toString(mAvailIndices.toArray())); |
| 829 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 832 | Animator loadAnimator(Fragment fragment, int transit, boolean enter, |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 833 | int transitionStyle) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 834 | Animator animObj = fragment.onCreateAnimator(transit, enter, |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 835 | fragment.mNextAnim); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 836 | if (animObj != null) { |
| 837 | return animObj; |
| 838 | } |
| 839 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 840 | if (fragment.mNextAnim != 0) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 841 | Animator anim = AnimatorInflater.loadAnimator(mHost.getContext(), fragment.mNextAnim); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 842 | if (anim != null) { |
| 843 | return anim; |
| 844 | } |
| 845 | } |
| 846 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 847 | if (transit == 0) { |
| 848 | return null; |
| 849 | } |
| 850 | |
| 851 | int styleIndex = transitToStyleIndex(transit, enter); |
| 852 | if (styleIndex < 0) { |
| 853 | return null; |
| 854 | } |
| 855 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 856 | if (transitionStyle == 0 && mHost.onHasWindowAnimations()) { |
| 857 | transitionStyle = mHost.onGetWindowAnimations(); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 858 | } |
| 859 | if (transitionStyle == 0) { |
| 860 | return null; |
| 861 | } |
| 862 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 863 | TypedArray attrs = mHost.getContext().obtainStyledAttributes(transitionStyle, |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 864 | com.android.internal.R.styleable.FragmentAnimation); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 865 | int anim = attrs.getResourceId(styleIndex, 0); |
| 866 | attrs.recycle(); |
| 867 | |
| 868 | if (anim == 0) { |
| 869 | return null; |
| 870 | } |
| 871 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 872 | return AnimatorInflater.loadAnimator(mHost.getContext(), anim); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 875 | public void performPendingDeferredStart(Fragment f) { |
| 876 | if (f.mDeferStart) { |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 877 | if (mExecutingActions) { |
| 878 | // Wait until we're done executing our pending transactions |
| 879 | mHavePendingDeferredStart = true; |
| 880 | return; |
| 881 | } |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 882 | f.mDeferStart = false; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 883 | moveToState(f, mCurState, 0, 0, false); |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
Adam Powell | 467cc6f | 2016-05-11 13:45:33 -0700 | [diff] [blame] | 887 | boolean isStateAtLeast(int state) { |
| 888 | return mCurState >= state; |
| 889 | } |
| 890 | |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 891 | void moveToState(Fragment f, int newState, int transit, int transitionStyle, |
| 892 | boolean keepActive) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 893 | if (DEBUG && false) Log.v(TAG, "moveToState: " + f |
| 894 | + " oldState=" + f.mState + " newState=" + newState |
| 895 | + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5)); |
Craig Mautner | 1c43719 | 2012-08-01 10:01:16 -0700 | [diff] [blame] | 896 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 897 | // Fragments that are not currently added will sit in the onCreate() state. |
Dianne Hackborn | e181bd9 | 2012-09-25 14:15:15 -0700 | [diff] [blame] | 898 | if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 899 | newState = Fragment.CREATED; |
| 900 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 901 | if (f.mRemoving && newState > f.mState) { |
| 902 | // While removing a fragment, we can't change it to a higher state. |
| 903 | newState = f.mState; |
| 904 | } |
Adam Powell | 2db4e4b | 2011-11-02 14:30:47 -0700 | [diff] [blame] | 905 | // Defer start if requested; don't allow it to move to STARTED or higher |
| 906 | // if it's not already started. |
| 907 | if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) { |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 908 | newState = Fragment.STOPPED; |
| 909 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 910 | if (f.mState < newState) { |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 911 | // For fragments that are created from a layout, when restoring from |
| 912 | // state we don't want to allow them to be created until they are |
| 913 | // being reloaded from the layout. |
| 914 | if (f.mFromLayout && !f.mInLayout) { |
| 915 | return; |
| 916 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 917 | if (f.mAnimatingAway != null) { |
| 918 | // The fragment is currently being animated... but! Now we |
| 919 | // want to move our state back up. Give up on waiting for the |
| 920 | // animation, move to whatever the final state should be once |
| 921 | // the animation is done, and then we can proceed from there. |
| 922 | f.mAnimatingAway = null; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 923 | moveToState(f, f.mStateAfterAnimating, 0, 0, true); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 924 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 925 | switch (f.mState) { |
| 926 | case Fragment.INITIALIZING: |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 927 | if (DEBUG) Log.v(TAG, "moveto CREATED: " + f); |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 928 | if (f.mSavedFragmentState != null) { |
| 929 | f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray( |
| 930 | FragmentManagerImpl.VIEW_STATE_TAG); |
| 931 | f.mTarget = getFragment(f.mSavedFragmentState, |
| 932 | FragmentManagerImpl.TARGET_STATE_TAG); |
| 933 | if (f.mTarget != null) { |
| 934 | f.mTargetRequestCode = f.mSavedFragmentState.getInt( |
| 935 | FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0); |
| 936 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 937 | f.mUserVisibleHint = f.mSavedFragmentState.getBoolean( |
| 938 | FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true); |
| 939 | if (!f.mUserVisibleHint) { |
| 940 | f.mDeferStart = true; |
| 941 | if (newState > Fragment.STOPPED) { |
| 942 | newState = Fragment.STOPPED; |
| 943 | } |
| 944 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 945 | } |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 946 | f.mHost = mHost; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 947 | f.mParentFragment = mParent; |
| 948 | f.mFragmentManager = mParent != null |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 949 | ? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 950 | f.mCalled = false; |
Todd Kennedy | 434bd65 | 2015-05-04 12:29:50 -0700 | [diff] [blame] | 951 | f.onAttach(mHost.getContext()); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 952 | if (!f.mCalled) { |
| 953 | throw new SuperNotCalledException("Fragment " + f |
| 954 | + " did not call through to super.onAttach()"); |
| 955 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 956 | if (f.mParentFragment == null) { |
Todd Kennedy | 434bd65 | 2015-05-04 12:29:50 -0700 | [diff] [blame] | 957 | mHost.onAttachFragment(f); |
Adam Powell | e30299f | 2016-05-13 11:04:39 -0700 | [diff] [blame] | 958 | } else { |
| 959 | f.mParentFragment.onAttachFragment(f); |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 960 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 961 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 962 | if (!f.mRetaining) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 963 | f.performCreate(f.mSavedFragmentState); |
Adam Powell | eacacb5 | 2016-03-23 13:07:27 -0700 | [diff] [blame] | 964 | } else { |
| 965 | f.restoreChildFragmentState(f.mSavedFragmentState, true); |
| 966 | f.mState = Fragment.CREATED; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 967 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 968 | f.mRetaining = false; |
| 969 | if (f.mFromLayout) { |
| 970 | // For fragments that are part of the content view |
| 971 | // layout, we need to instantiate the view immediately |
| 972 | // and the inflater will take care of adding it. |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 973 | f.mView = f.performCreateView(f.getLayoutInflater( |
| 974 | f.mSavedFragmentState), null, f.mSavedFragmentState); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 975 | if (f.mView != null) { |
| 976 | f.mView.setSaveFromParentEnabled(false); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 977 | if (f.mHidden) f.mView.setVisibility(View.GONE); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 978 | f.onViewCreated(f.mView, f.mSavedFragmentState); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 979 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 980 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 981 | case Fragment.CREATED: |
| 982 | if (newState > Fragment.CREATED) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 983 | if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 984 | if (!f.mFromLayout) { |
| 985 | ViewGroup container = null; |
| 986 | if (f.mContainerId != 0) { |
Adam Powell | e01f595 | 2016-02-23 15:25:42 -0800 | [diff] [blame] | 987 | if (f.mContainerId == View.NO_ID) { |
| 988 | throwException(new IllegalArgumentException( |
| 989 | "Cannot create fragment " |
| 990 | + f |
| 991 | + " for a container view with no id")); |
| 992 | } |
| 993 | container = (ViewGroup) mContainer.onFindViewById(f.mContainerId); |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame] | 994 | if (container == null && !f.mRestored) { |
Adam Powell | e01f595 | 2016-02-23 15:25:42 -0800 | [diff] [blame] | 995 | String resName; |
| 996 | try { |
| 997 | resName = f.getResources().getResourceName(f.mContainerId); |
| 998 | } catch (NotFoundException e) { |
| 999 | resName = "unknown"; |
| 1000 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1001 | throwException(new IllegalArgumentException( |
| 1002 | "No view found for id 0x" |
| 1003 | + Integer.toHexString(f.mContainerId) + " (" |
Adam Powell | e01f595 | 2016-02-23 15:25:42 -0800 | [diff] [blame] | 1004 | + resName |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1005 | + ") for fragment " + f)); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | f.mContainer = container; |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1009 | f.mView = f.performCreateView(f.getLayoutInflater( |
| 1010 | f.mSavedFragmentState), container, f.mSavedFragmentState); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1011 | if (f.mView != null) { |
| 1012 | f.mView.setSaveFromParentEnabled(false); |
| 1013 | if (container != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1014 | Animator anim = loadAnimator(f, transit, true, |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1015 | transitionStyle); |
| 1016 | if (anim != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1017 | anim.setTarget(f.mView); |
Doris Liu | de9284d | 2015-05-22 15:41:46 -0700 | [diff] [blame] | 1018 | setHWLayerAnimListenerIfAlpha(f.mView, anim); |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1019 | anim.start(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1020 | } |
| 1021 | container.addView(f.mView); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1022 | } |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1023 | if (f.mHidden) f.mView.setVisibility(View.GONE); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1024 | f.onViewCreated(f.mView, f.mSavedFragmentState); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1025 | } |
| 1026 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1027 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1028 | f.performActivityCreated(f.mSavedFragmentState); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1029 | if (f.mView != null) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1030 | f.restoreViewState(f.mSavedFragmentState); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1031 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1032 | f.mSavedFragmentState = null; |
| 1033 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1034 | case Fragment.ACTIVITY_CREATED: |
Adam Powell | eacacb5 | 2016-03-23 13:07:27 -0700 | [diff] [blame] | 1035 | if (newState > Fragment.ACTIVITY_CREATED) { |
| 1036 | f.mState = Fragment.STOPPED; |
| 1037 | } |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1038 | case Fragment.STOPPED: |
| 1039 | if (newState > Fragment.STOPPED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1040 | if (DEBUG) Log.v(TAG, "moveto STARTED: " + f); |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1041 | f.performStart(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1042 | } |
| 1043 | case Fragment.STARTED: |
| 1044 | if (newState > Fragment.STARTED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1045 | if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1046 | f.performResume(); |
Adam Powell | 9520251 | 2011-08-07 17:20:17 -0700 | [diff] [blame] | 1047 | // Get rid of this in case we saved it and never needed it. |
| 1048 | f.mSavedFragmentState = null; |
| 1049 | f.mSavedViewState = null; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | } else if (f.mState > newState) { |
| 1053 | switch (f.mState) { |
| 1054 | case Fragment.RESUMED: |
| 1055 | if (newState < Fragment.RESUMED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1056 | if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1057 | f.performPause(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1058 | } |
| 1059 | case Fragment.STARTED: |
| 1060 | if (newState < Fragment.STARTED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1061 | if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1062 | f.performStop(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1063 | } |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1064 | case Fragment.STOPPED: |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1065 | case Fragment.ACTIVITY_CREATED: |
| 1066 | if (newState < Fragment.ACTIVITY_CREATED) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1067 | if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1068 | if (f.mView != null) { |
| 1069 | // Need to save the current view state if not |
| 1070 | // done already. |
Todd Kennedy | 46d168f | 2015-05-13 11:13:58 -0700 | [diff] [blame] | 1071 | if (mHost.onShouldSaveFragmentState(f) && f.mSavedViewState == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1072 | saveFragmentViewState(f); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1073 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1074 | } |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1075 | f.performDestroyView(); |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1076 | if (f.mView != null && f.mContainer != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1077 | Animator anim = null; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1078 | if (mCurState > Fragment.INITIALIZING && !mDestroyed) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1079 | anim = loadAnimator(f, transit, false, |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1080 | transitionStyle); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1081 | } |
| 1082 | if (anim != null) { |
| 1083 | final ViewGroup container = f.mContainer; |
| 1084 | final View view = f.mView; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1085 | final Fragment fragment = f; |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1086 | container.startViewTransition(view); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1087 | f.mAnimatingAway = anim; |
| 1088 | f.mStateAfterAnimating = newState; |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1089 | anim.addListener(new AnimatorListenerAdapter() { |
| 1090 | @Override |
| 1091 | public void onAnimationEnd(Animator anim) { |
| 1092 | container.endViewTransition(view); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1093 | if (fragment.mAnimatingAway != null) { |
| 1094 | fragment.mAnimatingAway = null; |
| 1095 | moveToState(fragment, fragment.mStateAfterAnimating, |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1096 | 0, 0, false); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1097 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1098 | } |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1099 | }); |
| 1100 | anim.setTarget(f.mView); |
Doris Liu | de9284d | 2015-05-22 15:41:46 -0700 | [diff] [blame] | 1101 | setHWLayerAnimListenerIfAlpha(f.mView, anim); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1102 | anim.start(); |
| 1103 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1104 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1105 | f.mContainer.removeView(f.mView); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1106 | } |
| 1107 | f.mContainer = null; |
| 1108 | f.mView = null; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1109 | } |
| 1110 | case Fragment.CREATED: |
| 1111 | if (newState < Fragment.CREATED) { |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1112 | if (mDestroyed) { |
| 1113 | if (f.mAnimatingAway != null) { |
| 1114 | // The fragment's containing activity is |
| 1115 | // being destroyed, but this fragment is |
| 1116 | // currently animating away. Stop the |
| 1117 | // animation right now -- it is not needed, |
| 1118 | // and we can't wait any more on destroying |
| 1119 | // the fragment. |
Dianne Hackborn | 1b39e22 | 2010-12-28 14:17:18 -0800 | [diff] [blame] | 1120 | Animator anim = f.mAnimatingAway; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1121 | f.mAnimatingAway = null; |
Dianne Hackborn | 1b39e22 | 2010-12-28 14:17:18 -0800 | [diff] [blame] | 1122 | anim.cancel(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1123 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1124 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1125 | if (f.mAnimatingAway != null) { |
| 1126 | // We are waiting for the fragment's view to finish |
| 1127 | // animating away. Just make a note of the state |
| 1128 | // the fragment now should move to once the animation |
| 1129 | // is done. |
| 1130 | f.mStateAfterAnimating = newState; |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 1131 | newState = Fragment.CREATED; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1132 | } else { |
| 1133 | if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f); |
| 1134 | if (!f.mRetaining) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1135 | f.performDestroy(); |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 1136 | } else { |
Adam Powell | cbade7f | 2016-04-15 11:14:37 -0700 | [diff] [blame] | 1137 | f.mState = Fragment.INITIALIZING; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Adam Powell | a9bab98 | 2016-04-21 11:04:41 -0700 | [diff] [blame] | 1140 | f.performDetach(); |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1141 | if (!keepActive) { |
| 1142 | if (!f.mRetaining) { |
| 1143 | makeInactive(f); |
| 1144 | } else { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1145 | f.mHost = null; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1146 | f.mParentFragment = null; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1147 | f.mFragmentManager = null; |
| 1148 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 1149 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1150 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 1155 | if (f.mState != newState) { |
| 1156 | Log.w(TAG, "moveToState: Fragment state for " + f + " not updated inline; " |
| 1157 | + "expected state " + newState + " found " + f.mState); |
| 1158 | f.mState = newState; |
| 1159 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1162 | void moveToState(Fragment f) { |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1163 | moveToState(f, mCurState, 0, 0, false); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1166 | /** |
| 1167 | * Fragments that have been shown or hidden don't have their visibility changed or |
| 1168 | * animations run during the {@link #showFragment(Fragment)} or {@link #hideFragment(Fragment)} |
| 1169 | * calls. After fragments are brought to their final state in |
| 1170 | * {@link #moveFragmentToExpectedState(Fragment)} the fragments that have been shown or |
| 1171 | * hidden must have their visibility changed and their animations started here. |
| 1172 | * |
| 1173 | * @param fragment The fragment with mHiddenChanged = true that should change its View's |
| 1174 | * visibility and start the show or hide animation. |
| 1175 | */ |
| 1176 | void completeShowHideFragment(final Fragment fragment) { |
| 1177 | if (fragment.mView != null) { |
| 1178 | Animator anim = loadAnimator(fragment, fragment.mNextTransition, !fragment.mHidden, |
| 1179 | fragment.mNextTransitionStyle); |
| 1180 | if (anim != null) { |
| 1181 | anim.setTarget(fragment.mView); |
| 1182 | if (fragment.mHidden) { |
| 1183 | // Delay the actual hide operation until the animation finishes, otherwise |
| 1184 | // the fragment will just immediately disappear |
| 1185 | anim.addListener(new AnimatorListenerAdapter() { |
| 1186 | @Override |
| 1187 | public void onAnimationEnd(Animator animation) { |
| 1188 | animation.removeListener(this); |
| 1189 | if (fragment.mView != null) { |
| 1190 | fragment.mView.setVisibility(View.GONE); |
| 1191 | } |
| 1192 | } |
| 1193 | }); |
| 1194 | } |
| 1195 | setHWLayerAnimListenerIfAlpha(fragment.mView, anim); |
| 1196 | anim.start(); |
| 1197 | } else { |
| 1198 | final int visibility = fragment.mHidden ? View.GONE : View.VISIBLE; |
| 1199 | fragment.mView.setVisibility(visibility); |
| 1200 | } |
| 1201 | } |
| 1202 | if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) { |
| 1203 | mNeedMenuInvalidate = true; |
| 1204 | } |
| 1205 | fragment.mHiddenChanged = false; |
| 1206 | fragment.onHiddenChanged(fragment.mHidden); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1207 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1208 | |
| 1209 | /** |
| 1210 | * Moves a fragment to its expected final state or the fragment manager's state, depending |
| 1211 | * on whether the fragment manager's state is raised properly. |
| 1212 | * |
| 1213 | * @param f The fragment to change. |
| 1214 | */ |
| 1215 | void moveFragmentToExpectedState(Fragment f) { |
| 1216 | if (f == null) { |
| 1217 | return; |
| 1218 | } |
| 1219 | int nextState = mCurState; |
| 1220 | if (f.mRemoving) { |
| 1221 | if (f.isInBackStack()) { |
| 1222 | nextState = Fragment.CREATED; |
| 1223 | } else { |
| 1224 | nextState = Fragment.INITIALIZING; |
| 1225 | } |
| 1226 | } |
| 1227 | moveToState(f, nextState, f.mNextTransition, f.mNextTransitionStyle, false); |
| 1228 | |
| 1229 | if (f.mView != null) { |
| 1230 | Fragment underFragment = findFragmentUnder(f); |
| 1231 | if (underFragment != null) { |
| 1232 | final View underView = underFragment.mView; |
| 1233 | // make sure this fragment is in the right order. |
| 1234 | final ViewGroup container = f.mContainer; |
| 1235 | int underIndex = container.indexOfChild(underView); |
| 1236 | int viewIndex = container.indexOfChild(f.mView); |
| 1237 | if (viewIndex < underIndex) { |
| 1238 | container.removeViewAt(viewIndex); |
| 1239 | container.addView(f.mView, underIndex); |
| 1240 | } |
| 1241 | } |
| 1242 | } |
| 1243 | if (f.mHiddenChanged) { |
| 1244 | completeShowHideFragment(f); |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | void moveToState(int newState) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1249 | if (mHost == null && newState != Fragment.INITIALIZING) { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1250 | throw new IllegalStateException("No activity"); |
| 1251 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1252 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1253 | mCurState = newState; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1254 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1255 | if (mActive != null) { |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1256 | boolean loadersRunning = false; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1257 | |
| 1258 | // Must add them in the proper order. mActive fragments may be out of order |
| 1259 | final int numAdded = mAdded.size(); |
| 1260 | for (int i = 0; i < numAdded; i++) { |
| 1261 | Fragment f = mAdded.get(i); |
| 1262 | moveFragmentToExpectedState(f); |
| 1263 | if (f.mLoaderManager != null) { |
| 1264 | loadersRunning |= f.mLoaderManager.hasRunningLoaders(); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | // Now iterate through all active fragments. These will include those that are removed |
| 1269 | // and detached. |
| 1270 | final int numActive = mActive.size(); |
| 1271 | for (int i = 0; i < numActive; i++) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1272 | Fragment f = mActive.get(i); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1273 | if (f != null && (f.mRemoving || f.mDetached)) { |
| 1274 | moveFragmentToExpectedState(f); |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1275 | if (f.mLoaderManager != null) { |
| 1276 | loadersRunning |= f.mLoaderManager.hasRunningLoaders(); |
| 1277 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1278 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1279 | } |
Dianne Hackborn | 5f36c96 | 2010-08-26 15:54:17 -0700 | [diff] [blame] | 1280 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1281 | if (!loadersRunning) { |
| 1282 | startPendingDeferredFragments(); |
| 1283 | } |
| 1284 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1285 | if (mNeedMenuInvalidate && mHost != null && mCurState == Fragment.RESUMED) { |
| 1286 | mHost.onInvalidateOptionsMenu(); |
Dianne Hackborn | 5f36c96 | 2010-08-26 15:54:17 -0700 | [diff] [blame] | 1287 | mNeedMenuInvalidate = false; |
| 1288 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1292 | void startPendingDeferredFragments() { |
Adam Powell | 3751090 | 2011-10-31 11:48:24 -0700 | [diff] [blame] | 1293 | if (mActive == null) return; |
| 1294 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1295 | for (int i=0; i<mActive.size(); i++) { |
| 1296 | Fragment f = mActive.get(i); |
| 1297 | if (f != null) { |
| 1298 | performPendingDeferredStart(f); |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1303 | void makeActive(Fragment f) { |
| 1304 | if (f.mIndex >= 0) { |
| 1305 | return; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1306 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1307 | |
| 1308 | if (mAvailIndices == null || mAvailIndices.size() <= 0) { |
| 1309 | if (mActive == null) { |
| 1310 | mActive = new ArrayList<Fragment>(); |
| 1311 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1312 | f.setIndex(mActive.size(), mParent); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1313 | mActive.add(f); |
| 1314 | |
| 1315 | } else { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1316 | f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1317 | mActive.set(f.mIndex, f); |
| 1318 | } |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 1319 | if (DEBUG) Log.v(TAG, "Allocated fragment index " + f); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | void makeInactive(Fragment f) { |
| 1323 | if (f.mIndex < 0) { |
| 1324 | return; |
| 1325 | } |
| 1326 | |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 1327 | if (DEBUG) Log.v(TAG, "Freeing fragment index " + f); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1328 | mActive.set(f.mIndex, null); |
| 1329 | if (mAvailIndices == null) { |
| 1330 | mAvailIndices = new ArrayList<Integer>(); |
| 1331 | } |
| 1332 | mAvailIndices.add(f.mIndex); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1333 | mHost.inactivateFragment(f.mWho); |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1334 | f.initState(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | public void addFragment(Fragment fragment, boolean moveToStateNow) { |
| 1338 | if (mAdded == null) { |
| 1339 | mAdded = new ArrayList<Fragment>(); |
| 1340 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1341 | if (DEBUG) Log.v(TAG, "add: " + fragment); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1342 | makeActive(fragment); |
| 1343 | if (!fragment.mDetached) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1344 | if (mAdded.contains(fragment)) { |
| 1345 | throw new IllegalStateException("Fragment already added: " + fragment); |
| 1346 | } |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1347 | mAdded.add(fragment); |
| 1348 | fragment.mAdded = true; |
| 1349 | fragment.mRemoving = false; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1350 | if (fragment.mView == null) { |
| 1351 | fragment.mHiddenChanged = false; |
| 1352 | } |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1353 | if (fragment.mHasMenu && fragment.mMenuVisible) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1354 | mNeedMenuInvalidate = true; |
| 1355 | } |
| 1356 | if (moveToStateNow) { |
| 1357 | moveToState(fragment); |
| 1358 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1359 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1360 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1361 | |
| 1362 | public void removeFragment(Fragment fragment) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1363 | if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1364 | final boolean inactive = !fragment.isInBackStack(); |
| 1365 | if (!fragment.mDetached || inactive) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1366 | if (false) { |
| 1367 | // Would be nice to catch a bad remove here, but we need |
| 1368 | // time to test this to make sure we aren't crashes cases |
| 1369 | // where it is not a problem. |
| 1370 | if (!mAdded.contains(fragment)) { |
| 1371 | throw new IllegalStateException("Fragment not added: " + fragment); |
| 1372 | } |
| 1373 | } |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1374 | if (mAdded != null) { |
| 1375 | mAdded.remove(fragment); |
| 1376 | } |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1377 | if (fragment.mHasMenu && fragment.mMenuVisible) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1378 | mNeedMenuInvalidate = true; |
| 1379 | } |
| 1380 | fragment.mAdded = false; |
| 1381 | fragment.mRemoving = true; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1382 | } |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1383 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1384 | |
| 1385 | /** |
| 1386 | * Marks a fragment as hidden to be later animated in with |
| 1387 | * {@link #completeShowHideFragment(Fragment)}. |
| 1388 | * |
| 1389 | * @param fragment The fragment to be shown. |
| 1390 | */ |
| 1391 | public void hideFragment(Fragment fragment) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1392 | if (DEBUG) Log.v(TAG, "hide: " + fragment); |
| 1393 | if (!fragment.mHidden) { |
| 1394 | fragment.mHidden = true; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1395 | // Toggle hidden changed so that if a fragment goes through show/hide/show |
| 1396 | // it doesn't go through the animation. |
| 1397 | fragment.mHiddenChanged = !fragment.mHiddenChanged; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1398 | } |
| 1399 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1400 | |
| 1401 | /** |
| 1402 | * Marks a fragment as shown to be later animated in with |
| 1403 | * {@link #completeShowHideFragment(Fragment)}. |
| 1404 | * |
| 1405 | * @param fragment The fragment to be shown. |
| 1406 | */ |
| 1407 | public void showFragment(Fragment fragment) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1408 | if (DEBUG) Log.v(TAG, "show: " + fragment); |
| 1409 | if (fragment.mHidden) { |
| 1410 | fragment.mHidden = false; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1411 | // Toggle hidden changed so that if a fragment goes through show/hide/show |
| 1412 | // it doesn't go through the animation. |
| 1413 | fragment.mHiddenChanged = !fragment.mHiddenChanged; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1414 | } |
| 1415 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1416 | |
| 1417 | public void detachFragment(Fragment fragment) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1418 | if (DEBUG) Log.v(TAG, "detach: " + fragment); |
| 1419 | if (!fragment.mDetached) { |
| 1420 | fragment.mDetached = true; |
| 1421 | if (fragment.mAdded) { |
| 1422 | // 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] | 1423 | if (mAdded != null) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1424 | if (DEBUG) Log.v(TAG, "remove from detach: " + fragment); |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1425 | mAdded.remove(fragment); |
| 1426 | } |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1427 | if (fragment.mHasMenu && fragment.mMenuVisible) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1428 | mNeedMenuInvalidate = true; |
| 1429 | } |
| 1430 | fragment.mAdded = false; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1435 | public void attachFragment(Fragment fragment) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1436 | if (DEBUG) Log.v(TAG, "attach: " + fragment); |
| 1437 | if (fragment.mDetached) { |
| 1438 | fragment.mDetached = false; |
| 1439 | if (!fragment.mAdded) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1440 | if (mAdded == null) { |
| 1441 | mAdded = new ArrayList<Fragment>(); |
| 1442 | } |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1443 | if (mAdded.contains(fragment)) { |
| 1444 | throw new IllegalStateException("Fragment already added: " + fragment); |
| 1445 | } |
| 1446 | if (DEBUG) Log.v(TAG, "add from attach: " + fragment); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1447 | mAdded.add(fragment); |
| 1448 | fragment.mAdded = true; |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1449 | if (fragment.mHasMenu && fragment.mMenuVisible) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1450 | mNeedMenuInvalidate = true; |
| 1451 | } |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1456 | public Fragment findFragmentById(int id) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1457 | if (mAdded != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1458 | // First look through added fragments. |
| 1459 | for (int i=mAdded.size()-1; i>=0; i--) { |
| 1460 | Fragment f = mAdded.get(i); |
| 1461 | if (f != null && f.mFragmentId == id) { |
| 1462 | return f; |
| 1463 | } |
| 1464 | } |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1465 | } |
| 1466 | if (mActive != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1467 | // Now for any known fragment. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1468 | for (int i=mActive.size()-1; i>=0; i--) { |
| 1469 | Fragment f = mActive.get(i); |
| 1470 | if (f != null && f.mFragmentId == id) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1471 | return f; |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | return null; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1478 | public Fragment findFragmentByTag(String tag) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1479 | if (mAdded != null && tag != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1480 | // First look through added fragments. |
| 1481 | for (int i=mAdded.size()-1; i>=0; i--) { |
| 1482 | Fragment f = mAdded.get(i); |
| 1483 | if (f != null && tag.equals(f.mTag)) { |
| 1484 | return f; |
| 1485 | } |
| 1486 | } |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1487 | } |
| 1488 | if (mActive != null && tag != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1489 | // Now for any known fragment. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1490 | for (int i=mActive.size()-1; i>=0; i--) { |
| 1491 | Fragment f = mActive.get(i); |
| 1492 | if (f != null && tag.equals(f.mTag)) { |
| 1493 | return f; |
| 1494 | } |
| 1495 | } |
| 1496 | } |
| 1497 | return null; |
| 1498 | } |
| 1499 | |
| 1500 | public Fragment findFragmentByWho(String who) { |
| 1501 | if (mActive != null && who != null) { |
| 1502 | for (int i=mActive.size()-1; i>=0; i--) { |
| 1503 | Fragment f = mActive.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1504 | if (f != null && (f=f.findFragmentByWho(who)) != null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1505 | return f; |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | return null; |
| 1510 | } |
| 1511 | |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1512 | private void checkStateLoss() { |
| 1513 | if (mStateSaved) { |
| 1514 | throw new IllegalStateException( |
| 1515 | "Can not perform this action after onSaveInstanceState"); |
| 1516 | } |
| 1517 | if (mNoTransactionsBecause != null) { |
| 1518 | throw new IllegalStateException( |
| 1519 | "Can not perform this action inside of " + mNoTransactionsBecause); |
| 1520 | } |
| 1521 | } |
| 1522 | |
Alan Viverette | 95a4609 | 2013-08-14 11:17:25 -0700 | [diff] [blame] | 1523 | /** |
| 1524 | * Adds an action to the queue of pending actions. |
| 1525 | * |
| 1526 | * @param action the action to add |
| 1527 | * @param allowStateLoss whether to allow loss of state information |
| 1528 | * @throws IllegalStateException if the activity has been destroyed |
| 1529 | */ |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1530 | public void enqueueAction(OpGenerator action, boolean allowStateLoss) { |
Dianne Hackborn | 6908cd1 | 2010-11-08 15:11:16 -0800 | [diff] [blame] | 1531 | if (!allowStateLoss) { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1532 | checkStateLoss(); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1533 | } |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1534 | synchronized (this) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1535 | if (mDestroyed || mHost == null) { |
Dianne Hackborn | 6908cd1 | 2010-11-08 15:11:16 -0800 | [diff] [blame] | 1536 | throw new IllegalStateException("Activity has been destroyed"); |
| 1537 | } |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1538 | if (mPendingActions == null) { |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1539 | mPendingActions = new ArrayList<>(); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1540 | } |
| 1541 | mPendingActions.add(action); |
| 1542 | if (mPendingActions.size() == 1) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1543 | mHost.getHandler().removeCallbacks(mExecCommit); |
| 1544 | mHost.getHandler().post(mExecCommit); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1549 | public int allocBackStackIndex(BackStackRecord bse) { |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1550 | synchronized (this) { |
| 1551 | if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) { |
| 1552 | if (mBackStackIndices == null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1553 | mBackStackIndices = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1554 | } |
| 1555 | int index = mBackStackIndices.size(); |
| 1556 | if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); |
| 1557 | mBackStackIndices.add(bse); |
| 1558 | return index; |
| 1559 | |
| 1560 | } else { |
| 1561 | int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1); |
| 1562 | if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); |
| 1563 | mBackStackIndices.set(index, bse); |
| 1564 | return index; |
| 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1569 | public void setBackStackIndex(int index, BackStackRecord bse) { |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1570 | synchronized (this) { |
| 1571 | if (mBackStackIndices == null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1572 | mBackStackIndices = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1573 | } |
| 1574 | int N = mBackStackIndices.size(); |
| 1575 | if (index < N) { |
| 1576 | if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); |
| 1577 | mBackStackIndices.set(index, bse); |
| 1578 | } else { |
| 1579 | while (N < index) { |
| 1580 | mBackStackIndices.add(null); |
| 1581 | if (mAvailBackStackIndices == null) { |
| 1582 | mAvailBackStackIndices = new ArrayList<Integer>(); |
| 1583 | } |
| 1584 | if (DEBUG) Log.v(TAG, "Adding available back stack index " + N); |
| 1585 | mAvailBackStackIndices.add(N); |
| 1586 | N++; |
| 1587 | } |
| 1588 | if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); |
| 1589 | mBackStackIndices.add(bse); |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | public void freeBackStackIndex(int index) { |
| 1595 | synchronized (this) { |
| 1596 | mBackStackIndices.set(index, null); |
| 1597 | if (mAvailBackStackIndices == null) { |
| 1598 | mAvailBackStackIndices = new ArrayList<Integer>(); |
| 1599 | } |
| 1600 | if (DEBUG) Log.v(TAG, "Freeing back stack index " + index); |
| 1601 | mAvailBackStackIndices.add(index); |
| 1602 | } |
| 1603 | } |
| 1604 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1605 | /** |
| 1606 | * Broken out from exec*, this prepares for gathering and executing operations. |
| 1607 | * |
| 1608 | * @param allowStateLoss true if state loss should be ignored or false if it should be |
| 1609 | * checked. |
| 1610 | */ |
| 1611 | private void ensureExecReady(boolean allowStateLoss) { |
Adam Powell | 8585ed6 | 2016-02-04 15:38:20 -0800 | [diff] [blame] | 1612 | if (mExecutingActions) { |
| 1613 | throw new IllegalStateException("FragmentManager is already executing transactions"); |
| 1614 | } |
| 1615 | |
| 1616 | if (Looper.myLooper() != mHost.getHandler().getLooper()) { |
| 1617 | throw new IllegalStateException("Must be called from main thread of fragment host"); |
| 1618 | } |
| 1619 | |
Adam Powell | 3518fed | 2016-03-01 09:07:44 -0800 | [diff] [blame] | 1620 | if (!allowStateLoss) { |
Adam Powell | 8585ed6 | 2016-02-04 15:38:20 -0800 | [diff] [blame] | 1621 | checkStateLoss(); |
| 1622 | } |
| 1623 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1624 | if (mTmpRecords == null) { |
| 1625 | mTmpRecords = new ArrayList<>(); |
| 1626 | mTmpIsPop = new ArrayList<>(); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | public void execSingleAction(OpGenerator action, boolean allowStateLoss) { |
| 1631 | ensureExecReady(allowStateLoss); |
| 1632 | if (action.generateOps(mTmpRecords, mTmpIsPop)) { |
| 1633 | mExecutingActions = true; |
| 1634 | try { |
| 1635 | optimizeAndExecuteOps(mTmpRecords, mTmpIsPop); |
| 1636 | } finally { |
| 1637 | cleanupExec(); |
| 1638 | } |
George Mount | c8c2bdf | 2016-06-30 17:08:27 -0700 | [diff] [blame] | 1639 | } |
Adam Powell | 8585ed6 | 2016-02-04 15:38:20 -0800 | [diff] [blame] | 1640 | |
| 1641 | doPendingDeferredStart(); |
| 1642 | } |
| 1643 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1644 | /** |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1645 | * Broken out of exec*, this cleans up the mExecutingActions and the temporary structures |
| 1646 | * used in executing operations. |
| 1647 | */ |
| 1648 | private void cleanupExec() { |
| 1649 | mExecutingActions = false; |
| 1650 | mTmpIsPop.clear(); |
| 1651 | mTmpRecords.clear(); |
| 1652 | } |
| 1653 | |
| 1654 | /** |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1655 | * Only call from main thread! |
| 1656 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1657 | public boolean execPendingActions() { |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1658 | ensureExecReady(true); |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1659 | |
| 1660 | boolean didSomething = false; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1661 | while (generateOpsForPendingActions(mTmpRecords, mTmpIsPop)) { |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1662 | mExecutingActions = true; |
George Mount | c8c2bdf | 2016-06-30 17:08:27 -0700 | [diff] [blame] | 1663 | try { |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1664 | optimizeAndExecuteOps(mTmpRecords, mTmpIsPop); |
George Mount | c8c2bdf | 2016-06-30 17:08:27 -0700 | [diff] [blame] | 1665 | } finally { |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1666 | cleanupExec(); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1667 | } |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1668 | didSomething = true; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1669 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1670 | |
Adam Powell | 8585ed6 | 2016-02-04 15:38:20 -0800 | [diff] [blame] | 1671 | doPendingDeferredStart(); |
| 1672 | |
| 1673 | return didSomething; |
| 1674 | } |
| 1675 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1676 | /** |
| 1677 | * Optimizes BackStackRecord operations. This method merges operations of proximate records |
| 1678 | * that allow optimization. See {@link FragmentTransaction#setAllowOptimization(boolean)}. |
| 1679 | * <p> |
| 1680 | * For example, a transaction that adds to the back stack and then another that pops that |
| 1681 | * back stack record will be optimized. |
| 1682 | * <p> |
| 1683 | * Likewise, two transactions committed that are executed at the same time will be optimized |
| 1684 | * as well as two pop operations executed together. |
| 1685 | * |
| 1686 | * @param records The records pending execution |
| 1687 | * @param isRecordPop The direction that these records are being run. |
| 1688 | */ |
| 1689 | private void optimizeAndExecuteOps(ArrayList<BackStackRecord> records, |
| 1690 | ArrayList<Boolean> isRecordPop) { |
| 1691 | if (records == null || records.isEmpty()) { |
| 1692 | return; |
| 1693 | } |
| 1694 | |
| 1695 | if (isRecordPop == null || records.size() != isRecordPop.size()) { |
| 1696 | throw new IllegalStateException("Internal error with the back stack records"); |
| 1697 | } |
| 1698 | |
| 1699 | final int numRecords = records.size(); |
| 1700 | int startIndex = 0; |
| 1701 | for (int recordNum = 0; recordNum < numRecords; recordNum++) { |
| 1702 | final boolean canOptimize = records.get(recordNum).mAllowOptimization; |
| 1703 | if (!canOptimize) { |
| 1704 | // execute all previous transactions |
| 1705 | if (startIndex != recordNum) { |
| 1706 | executeOpsTogether(records, isRecordPop, startIndex, recordNum); |
| 1707 | } |
| 1708 | // execute all unoptimized together |
| 1709 | int optimizeEnd; |
| 1710 | for (optimizeEnd = recordNum + 1; optimizeEnd < numRecords; optimizeEnd++) { |
| 1711 | if (records.get(optimizeEnd).mAllowOptimization) { |
| 1712 | break; |
| 1713 | } |
| 1714 | } |
| 1715 | executeOpsTogether(records, isRecordPop, recordNum, optimizeEnd); |
| 1716 | startIndex = optimizeEnd; |
| 1717 | recordNum = optimizeEnd - 1; |
| 1718 | } |
| 1719 | } |
| 1720 | if (startIndex != numRecords) { |
| 1721 | executeOpsTogether(records, isRecordPop, startIndex, numRecords); |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | /** |
| 1726 | * Optimizes a subset of a list of BackStackRecords, all of which either allow optimization or |
| 1727 | * do not allow optimization. |
| 1728 | * @param records A list of BackStackRecords that are to be optimized |
| 1729 | * @param isRecordPop The direction that these records are being run. |
| 1730 | * @param startIndex The index of the first record in <code>records</code> to be optimized |
| 1731 | * @param endIndex One more than the final record index in <code>records</code> to optimize. |
| 1732 | */ |
| 1733 | private void executeOpsTogether(ArrayList<BackStackRecord> records, |
| 1734 | ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) { |
| 1735 | final boolean allowOptimization = records.get(startIndex).mAllowOptimization; |
| 1736 | boolean addToBackStack = false; |
| 1737 | if (mTmpAddedFragments == null) { |
| 1738 | mTmpAddedFragments = new ArrayList<>(); |
| 1739 | mTmpFragmentsContainerTransitions = new SparseArray<>(); |
| 1740 | } else { |
| 1741 | mTmpAddedFragments.clear(); |
| 1742 | mTmpFragmentsContainerTransitions.clear(); |
| 1743 | } |
| 1744 | if (mAdded != null) { |
| 1745 | mTmpAddedFragments.addAll(mAdded); |
| 1746 | } |
| 1747 | for (int recordNum = startIndex; recordNum < endIndex; recordNum++) { |
| 1748 | final BackStackRecord record = records.get(recordNum); |
| 1749 | final boolean isPop = isRecordPop.get(recordNum); |
| 1750 | if (!isPop) { |
| 1751 | record.expandReplaceOps(mTmpAddedFragments); |
| 1752 | } |
| 1753 | final int bumpAmount = isPop ? -1 : 1; |
| 1754 | record.bumpBackStackNesting(bumpAmount); |
| 1755 | addToBackStack = addToBackStack || record.mAddToBackStack; |
| 1756 | |
| 1757 | if (mCurState >= Fragment.CREATED) { |
| 1758 | if (isPop) { |
| 1759 | record.calculatePopFragments(mTmpFragmentsContainerTransitions); |
| 1760 | } else { |
| 1761 | record.calculateFragments(mTmpFragmentsContainerTransitions); |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | mTmpAddedFragments.clear(); |
| 1766 | |
| 1767 | if (!allowOptimization) { |
| 1768 | startTransitions(records, isRecordPop, startIndex, endIndex); |
| 1769 | } |
| 1770 | executeOps(records, isRecordPop, startIndex, endIndex); |
| 1771 | |
| 1772 | if (allowOptimization) { |
| 1773 | moveFragmentsToAtLeastCreated(); |
| 1774 | startTransitions(records, isRecordPop, startIndex, endIndex); |
| 1775 | moveToState(mCurState); |
| 1776 | } |
| 1777 | |
| 1778 | for (int recordNum = startIndex; recordNum < endIndex; recordNum++) { |
| 1779 | final BackStackRecord record = records.get(recordNum); |
| 1780 | final boolean isPop = isRecordPop.get(recordNum); |
| 1781 | if (isPop && record.mIndex >= 0) { |
| 1782 | freeBackStackIndex(record.mIndex); |
| 1783 | record.mIndex = -1; |
| 1784 | } |
| 1785 | } |
| 1786 | if (addToBackStack) { |
| 1787 | reportBackStackChanged(); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | /** |
| 1792 | * Find a fragment within the fragment's container whose View should be below the passed |
| 1793 | * fragment. {@code null} is returned when the fragment has no View or if there should be |
| 1794 | * no fragment with a View below the given fragment. |
| 1795 | * |
| 1796 | * As an example, if mAdded has two Fragments with Views sharing the same container: |
| 1797 | * FragmentA |
| 1798 | * FragmentB |
| 1799 | * |
| 1800 | * Then, when processing FragmentB, FragmentA will be returned. If, however, FragmentA |
| 1801 | * had no View, null would be returned. |
| 1802 | * |
| 1803 | * @param f The fragment that may be on top of another fragment. |
| 1804 | * @return The fragment with a View under f, if one exists or null if f has no View or |
| 1805 | * there are no fragments with Views in the same container. |
| 1806 | */ |
| 1807 | private Fragment findFragmentUnder(Fragment f) { |
| 1808 | final ViewGroup container = f.mContainer; |
| 1809 | final View view = f.mView; |
| 1810 | |
| 1811 | if (container == null || view == null) { |
| 1812 | return null; |
| 1813 | } |
| 1814 | |
| 1815 | final int fragmentIndex = mAdded.indexOf(f); |
| 1816 | for (int i = fragmentIndex - 1; i >= 0; i--) { |
| 1817 | Fragment underFragment = mAdded.get(i); |
| 1818 | if (underFragment.mContainer == container && underFragment.mView != null) { |
| 1819 | // Found the fragment under this one |
| 1820 | return underFragment; |
| 1821 | } |
| 1822 | } |
| 1823 | return null; |
| 1824 | } |
| 1825 | |
| 1826 | /** |
| 1827 | * Run the operations in the BackStackRecords, either to push or pop. |
| 1828 | * |
| 1829 | * @param records The list of records whose operations should be run. |
| 1830 | * @param isRecordPop The direction that these records are being run. |
| 1831 | * @param startIndex The index of the first entry in records to run. |
| 1832 | * @param endIndex One past the index of the final entry in records to run. |
| 1833 | */ |
| 1834 | private static void executeOps(ArrayList<BackStackRecord> records, |
| 1835 | ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) { |
| 1836 | for (int i = startIndex; i < endIndex; i++) { |
| 1837 | final BackStackRecord record = records.get(i); |
| 1838 | final boolean isPop = isRecordPop.get(i); |
| 1839 | if (isPop) { |
| 1840 | record.executePopOps(); |
| 1841 | } else { |
| 1842 | record.executeOps(); |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | /** |
| 1848 | * Prepares the fragments for Transitions and starts it. If the FragmentManager is not |
| 1849 | * at Fragment.CREATED, no transition will be run. |
| 1850 | * |
| 1851 | * This is explicitly for {@link Fragment#setEnterTransition(Transition)} and its siblings, |
| 1852 | * not for {@link FragmentTransaction#setTransition(int)}. |
| 1853 | * |
| 1854 | * @param records The entries to examine for transitions. |
| 1855 | * @param isRecordPop The direction that these records are being run. |
| 1856 | * @param startIndex The index of the first entry in records to run transitions for. |
| 1857 | * @param endIndex One past the index of the final entry in records to run transitions for. |
| 1858 | */ |
| 1859 | private void startTransitions(ArrayList<BackStackRecord> records, |
| 1860 | ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) { |
| 1861 | if (mCurState >= Fragment.CREATED) { |
| 1862 | if (mTmpFragmentsContainerTransitions.size() != 0) { |
| 1863 | BackStackRecord record = records.get(0); |
| 1864 | BackStackRecord.TransitionState state = |
| 1865 | record.beginTransition(mTmpFragmentsContainerTransitions); |
| 1866 | if (state != null) { |
| 1867 | for (int i = startIndex + 1; i < endIndex - 1; i++) { |
| 1868 | final BackStackRecord nameRecord = records.get(i); |
| 1869 | final boolean isPop = isRecordPop.get(i); |
| 1870 | ArrayList<String> sourceNames = isPop |
| 1871 | ? nameRecord.mSharedElementTargetNames |
| 1872 | : record.mSharedElementSourceNames; |
| 1873 | ArrayList<String> targetNames = isPop |
| 1874 | ? nameRecord.mSharedElementSourceNames |
| 1875 | : record.mSharedElementTargetNames; |
| 1876 | BackStackRecord.setNameOverrides(state, sourceNames, targetNames); |
| 1877 | } |
| 1878 | } |
| 1879 | mTmpFragmentsContainerTransitions.clear(); |
| 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | /** |
| 1885 | * Ensure that fragments that are added are at least at the CREATED state |
| 1886 | * so that they may load Transitions using TransitionInflater. When the transaction |
| 1887 | * cannot be optimized, this is executed in |
| 1888 | * {@link BackStackRecord#setLastIn(SparseArray, SparseArray, Fragment)} instead. Prior to |
| 1889 | * N, this wasn't supported, so no out-of-order creation can be done for compatibility. |
| 1890 | * <p> |
| 1891 | * This won't change the state of the fragment manager, nor will it change the fragment's |
| 1892 | * state if the fragment manager isn't at least at the CREATED state. |
| 1893 | */ |
| 1894 | private void moveFragmentsToAtLeastCreated() { |
| 1895 | if (mCurState < Fragment.CREATED) { |
| 1896 | return; |
| 1897 | } |
| 1898 | final int numAdded = mAdded == null ? 0 : mAdded.size(); |
| 1899 | for (int i = 0; i < numAdded; i++) { |
| 1900 | Fragment fragment = mAdded.get(i); |
| 1901 | if (fragment.mState < Fragment.CREATED) { |
| 1902 | moveToState(fragment, Fragment.CREATED, 0, 0, false); |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | /** |
| 1908 | * Adds all records in the pending actions to records and whether they are add or pop |
| 1909 | * operations to isPop. After executing, the pending actions will be empty. |
| 1910 | * |
| 1911 | * @param records All pending actions will generate BackStackRecords added to this. |
| 1912 | * This contains the transactions, in order, to execute. |
| 1913 | * @param isPop All pending actions will generate booleans to add to this. This contains |
| 1914 | * an entry for each entry in records to indicate whether or not it is a |
| 1915 | * pop action. |
| 1916 | */ |
| 1917 | private boolean generateOpsForPendingActions(ArrayList<BackStackRecord> records, |
| 1918 | ArrayList<Boolean> isPop) { |
| 1919 | int numActions; |
| 1920 | synchronized (this) { |
| 1921 | if (mPendingActions == null || mPendingActions.size() == 0) { |
| 1922 | return false; |
| 1923 | } |
| 1924 | |
| 1925 | numActions = mPendingActions.size(); |
| 1926 | for (int i = 0; i < numActions; i++) { |
| 1927 | mPendingActions.get(i).generateOps(records, isPop); |
| 1928 | } |
| 1929 | mPendingActions.clear(); |
| 1930 | mHost.getHandler().removeCallbacks(mExecCommit); |
| 1931 | } |
| 1932 | return numActions > 0; |
| 1933 | } |
| 1934 | |
Adam Powell | 8585ed6 | 2016-02-04 15:38:20 -0800 | [diff] [blame] | 1935 | void doPendingDeferredStart() { |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1936 | if (mHavePendingDeferredStart) { |
| 1937 | boolean loadersRunning = false; |
| 1938 | for (int i=0; i<mActive.size(); i++) { |
| 1939 | Fragment f = mActive.get(i); |
| 1940 | if (f != null && f.mLoaderManager != null) { |
| 1941 | loadersRunning |= f.mLoaderManager.hasRunningLoaders(); |
| 1942 | } |
| 1943 | } |
| 1944 | if (!loadersRunning) { |
| 1945 | mHavePendingDeferredStart = false; |
| 1946 | startPendingDeferredFragments(); |
| 1947 | } |
| 1948 | } |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1949 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1950 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1951 | void reportBackStackChanged() { |
| 1952 | if (mBackStackChangeListeners != null) { |
| 1953 | for (int i=0; i<mBackStackChangeListeners.size(); i++) { |
| 1954 | mBackStackChangeListeners.get(i).onBackStackChanged(); |
| 1955 | } |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | void addBackStackState(BackStackRecord state) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1960 | if (mBackStack == null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1961 | mBackStack = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1962 | } |
| 1963 | mBackStack.add(state); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1964 | reportBackStackChanged(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1965 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1966 | |
| 1967 | boolean popBackStackState(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop, |
| 1968 | String name, int id, int flags) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1969 | if (mBackStack == null) { |
| 1970 | return false; |
| 1971 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1972 | if (name == null && id < 0 && (flags & POP_BACK_STACK_INCLUSIVE) == 0) { |
| 1973 | int last = mBackStack.size() - 1; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1974 | if (last < 0) { |
| 1975 | return false; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1976 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 1977 | records.add(mBackStack.remove(last)); |
| 1978 | isRecordPop.add(true); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1979 | } else { |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1980 | int index = -1; |
| 1981 | if (name != null || id >= 0) { |
| 1982 | // If a name or ID is specified, look for that place in |
| 1983 | // the stack. |
| 1984 | index = mBackStack.size()-1; |
| 1985 | while (index >= 0) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1986 | BackStackRecord bss = mBackStack.get(index); |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1987 | if (name != null && name.equals(bss.getName())) { |
| 1988 | break; |
| 1989 | } |
| 1990 | if (id >= 0 && id == bss.mIndex) { |
| 1991 | break; |
| 1992 | } |
| 1993 | index--; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1994 | } |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1995 | if (index < 0) { |
| 1996 | return false; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1997 | } |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1998 | if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) { |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1999 | index--; |
| 2000 | // Consume all following entries that match. |
| 2001 | while (index >= 0) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 2002 | BackStackRecord bss = mBackStack.get(index); |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 2003 | if ((name != null && name.equals(bss.getName())) |
| 2004 | || (id >= 0 && id == bss.mIndex)) { |
| 2005 | index--; |
| 2006 | continue; |
| 2007 | } |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 2011 | } |
| 2012 | if (index == mBackStack.size()-1) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2013 | return false; |
| 2014 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 2015 | for (int i = mBackStack.size() - 1; i > index; i--) { |
| 2016 | records.add(mBackStack.remove(i)); |
| 2017 | isRecordPop.add(true); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2018 | } |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2019 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 2020 | return true; |
| 2021 | } |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2022 | |
| 2023 | FragmentManagerNonConfig retainNonConfig() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2024 | ArrayList<Fragment> fragments = null; |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2025 | ArrayList<FragmentManagerNonConfig> childFragments = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2026 | if (mActive != null) { |
| 2027 | for (int i=0; i<mActive.size(); i++) { |
| 2028 | Fragment f = mActive.get(i); |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2029 | if (f != null) { |
| 2030 | if (f.mRetainInstance) { |
| 2031 | if (fragments == null) { |
| 2032 | fragments = new ArrayList<>(); |
| 2033 | } |
| 2034 | fragments.add(f); |
| 2035 | f.mRetaining = true; |
| 2036 | f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1; |
| 2037 | if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2038 | } |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2039 | boolean addedChild = false; |
| 2040 | if (f.mChildFragmentManager != null) { |
| 2041 | FragmentManagerNonConfig child = f.mChildFragmentManager.retainNonConfig(); |
| 2042 | if (child != null) { |
| 2043 | if (childFragments == null) { |
| 2044 | childFragments = new ArrayList<>(); |
| 2045 | for (int j = 0; j < i; j++) { |
| 2046 | childFragments.add(null); |
| 2047 | } |
| 2048 | } |
| 2049 | childFragments.add(child); |
| 2050 | addedChild = true; |
| 2051 | } |
| 2052 | } |
| 2053 | if (childFragments != null && !addedChild) { |
| 2054 | childFragments.add(null); |
| 2055 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | } |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2059 | if (fragments == null && childFragments == null) { |
| 2060 | return null; |
| 2061 | } |
| 2062 | return new FragmentManagerNonConfig(fragments, childFragments); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | void saveFragmentViewState(Fragment f) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2066 | if (f.mView == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2067 | return; |
| 2068 | } |
| 2069 | if (mStateArray == null) { |
| 2070 | mStateArray = new SparseArray<Parcelable>(); |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 2071 | } else { |
| 2072 | mStateArray.clear(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2073 | } |
| 2074 | f.mView.saveHierarchyState(mStateArray); |
| 2075 | if (mStateArray.size() > 0) { |
| 2076 | f.mSavedViewState = mStateArray; |
| 2077 | mStateArray = null; |
| 2078 | } |
| 2079 | } |
| 2080 | |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 2081 | Bundle saveFragmentBasicState(Fragment f) { |
| 2082 | Bundle result = null; |
| 2083 | |
| 2084 | if (mStateBundle == null) { |
| 2085 | mStateBundle = new Bundle(); |
| 2086 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2087 | f.performSaveInstanceState(mStateBundle); |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 2088 | if (!mStateBundle.isEmpty()) { |
| 2089 | result = mStateBundle; |
| 2090 | mStateBundle = null; |
| 2091 | } |
| 2092 | |
| 2093 | if (f.mView != null) { |
| 2094 | saveFragmentViewState(f); |
Dianne Hackborn | 1333276 | 2011-06-03 17:34:45 -0700 | [diff] [blame] | 2095 | } |
| 2096 | if (f.mSavedViewState != null) { |
| 2097 | if (result == null) { |
| 2098 | result = new Bundle(); |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 2099 | } |
Dianne Hackborn | 1333276 | 2011-06-03 17:34:45 -0700 | [diff] [blame] | 2100 | result.putSparseParcelableArray( |
| 2101 | FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 2102 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 2103 | if (!f.mUserVisibleHint) { |
Jake Wharton | 258029e | 2012-04-22 17:17:01 -0400 | [diff] [blame] | 2104 | if (result == null) { |
| 2105 | result = new Bundle(); |
| 2106 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 2107 | // Only add this if it's not the default value |
| 2108 | result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint); |
| 2109 | } |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 2110 | |
| 2111 | return result; |
| 2112 | } |
| 2113 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2114 | Parcelable saveAllState() { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 2115 | // Make sure all pending operations have now been executed to get |
| 2116 | // our state update-to-date. |
| 2117 | execPendingActions(); |
| 2118 | |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 2119 | mStateSaved = true; |
| 2120 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2121 | if (mActive == null || mActive.size() <= 0) { |
| 2122 | return null; |
| 2123 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2124 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2125 | // First collect all active fragments. |
| 2126 | int N = mActive.size(); |
| 2127 | FragmentState[] active = new FragmentState[N]; |
| 2128 | boolean haveFragments = false; |
| 2129 | for (int i=0; i<N; i++) { |
| 2130 | Fragment f = mActive.get(i); |
| 2131 | if (f != null) { |
Dianne Hackborn | 61af8a8 | 2012-05-30 16:38:30 -0700 | [diff] [blame] | 2132 | if (f.mIndex < 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2133 | throwException(new IllegalStateException( |
| 2134 | "Failure saving state: active " + f |
| 2135 | + " has cleared index: " + f.mIndex)); |
Dianne Hackborn | 61af8a8 | 2012-05-30 16:38:30 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2138 | haveFragments = true; |
Dianne Hackborn | 61af8a8 | 2012-05-30 16:38:30 -0700 | [diff] [blame] | 2139 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2140 | FragmentState fs = new FragmentState(f); |
| 2141 | active[i] = fs; |
| 2142 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2143 | if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) { |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 2144 | fs.mSavedFragmentState = saveFragmentBasicState(f); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2145 | |
| 2146 | if (f.mTarget != null) { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 2147 | if (f.mTarget.mIndex < 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2148 | throwException(new IllegalStateException( |
| 2149 | "Failure saving state: " + f |
| 2150 | + " has target not in fragment manager: " + f.mTarget)); |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 2151 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2152 | if (fs.mSavedFragmentState == null) { |
| 2153 | fs.mSavedFragmentState = new Bundle(); |
| 2154 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2155 | putFragment(fs.mSavedFragmentState, |
| 2156 | FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget); |
| 2157 | if (f.mTargetRequestCode != 0) { |
| 2158 | fs.mSavedFragmentState.putInt( |
| 2159 | FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, |
| 2160 | f.mTargetRequestCode); |
| 2161 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 2162 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 2163 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2164 | } else { |
| 2165 | fs.mSavedFragmentState = f.mSavedFragmentState; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 2168 | if (DEBUG) Log.v(TAG, "Saved state of " + f + ": " |
| 2169 | + fs.mSavedFragmentState); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | if (!haveFragments) { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 2174 | if (DEBUG) Log.v(TAG, "saveAllState: no fragments!"); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2175 | return null; |
| 2176 | } |
| 2177 | |
| 2178 | int[] added = null; |
| 2179 | BackStackState[] backStack = null; |
| 2180 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2181 | // Build list of currently added fragments. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2182 | if (mAdded != null) { |
| 2183 | N = mAdded.size(); |
| 2184 | if (N > 0) { |
| 2185 | added = new int[N]; |
| 2186 | for (int i=0; i<N; i++) { |
| 2187 | added[i] = mAdded.get(i).mIndex; |
Dianne Hackborn | 61af8a8 | 2012-05-30 16:38:30 -0700 | [diff] [blame] | 2188 | if (added[i] < 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2189 | throwException(new IllegalStateException( |
| 2190 | "Failure saving state: active " + mAdded.get(i) |
| 2191 | + " has cleared index: " + added[i])); |
Dianne Hackborn | 61af8a8 | 2012-05-30 16:38:30 -0700 | [diff] [blame] | 2192 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2193 | if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i |
| 2194 | + ": " + mAdded.get(i)); |
| 2195 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | // Now save back stack. |
| 2200 | if (mBackStack != null) { |
| 2201 | N = mBackStack.size(); |
| 2202 | if (N > 0) { |
| 2203 | backStack = new BackStackState[N]; |
| 2204 | for (int i=0; i<N; i++) { |
| 2205 | backStack[i] = new BackStackState(this, mBackStack.get(i)); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 2206 | if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i |
| 2207 | + ": " + mBackStack.get(i)); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2212 | FragmentManagerState fms = new FragmentManagerState(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2213 | fms.mActive = active; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2214 | fms.mAdded = added; |
| 2215 | fms.mBackStack = backStack; |
| 2216 | return fms; |
| 2217 | } |
| 2218 | |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2219 | void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2220 | // If there is no saved state at all, then there can not be |
| 2221 | // any nonConfig fragments either, so that is that. |
| 2222 | if (state == null) return; |
| 2223 | FragmentManagerState fms = (FragmentManagerState)state; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2224 | if (fms.mActive == null) return; |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2225 | |
| 2226 | List<FragmentManagerNonConfig> childNonConfigs = null; |
| 2227 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2228 | // First re-attach any non-config instances we are retaining back |
| 2229 | // 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] | 2230 | if (nonConfig != null) { |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2231 | List<Fragment> nonConfigFragments = nonConfig.getFragments(); |
| 2232 | childNonConfigs = nonConfig.getChildNonConfigs(); |
| 2233 | final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0; |
| 2234 | for (int i = 0; i < count; i++) { |
| 2235 | Fragment f = nonConfigFragments.get(i); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 2236 | if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2237 | FragmentState fs = fms.mActive[f.mIndex]; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2238 | fs.mInstance = f; |
| 2239 | f.mSavedViewState = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2240 | f.mBackStackNesting = 0; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2241 | f.mInLayout = false; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2242 | f.mAdded = false; |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 2243 | f.mTarget = null; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2244 | if (fs.mSavedFragmentState != null) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2245 | fs.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader()); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2246 | f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray( |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 2247 | FragmentManagerImpl.VIEW_STATE_TAG); |
Craig Mautner | 33acfbe | 2014-06-28 14:00:53 -0700 | [diff] [blame] | 2248 | f.mSavedFragmentState = fs.mSavedFragmentState; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2249 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2250 | } |
| 2251 | } |
| 2252 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2253 | // Build the full list of active fragments, instantiating them from |
| 2254 | // their saved state. |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2255 | mActive = new ArrayList<>(fms.mActive.length); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2256 | if (mAvailIndices != null) { |
| 2257 | mAvailIndices.clear(); |
| 2258 | } |
| 2259 | for (int i=0; i<fms.mActive.length; i++) { |
| 2260 | FragmentState fs = fms.mActive[i]; |
| 2261 | if (fs != null) { |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2262 | FragmentManagerNonConfig childNonConfig = null; |
| 2263 | if (childNonConfigs != null && i < childNonConfigs.size()) { |
| 2264 | childNonConfig = childNonConfigs.get(i); |
| 2265 | } |
| 2266 | Fragment f = fs.instantiate(mHost, mParent, childNonConfig); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 2267 | if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 2268 | mActive.add(f); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 2269 | // Now that the fragment is instantiated (or came from being |
| 2270 | // retained above), clear mInstance in case we end up re-restoring |
| 2271 | // from this FragmentState again. |
| 2272 | fs.mInstance = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2273 | } else { |
| 2274 | mActive.add(null); |
| 2275 | if (mAvailIndices == null) { |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2276 | mAvailIndices = new ArrayList<>(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2277 | } |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 2278 | if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2279 | mAvailIndices.add(i); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2280 | } |
| 2281 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2282 | |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 2283 | // Update the target of all retained fragments. |
| 2284 | if (nonConfig != null) { |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2285 | List<Fragment> nonConfigFragments = nonConfig.getFragments(); |
| 2286 | final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0; |
| 2287 | for (int i = 0; i < count; i++) { |
| 2288 | Fragment f = nonConfigFragments.get(i); |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 2289 | if (f.mTargetIndex >= 0) { |
| 2290 | if (f.mTargetIndex < mActive.size()) { |
| 2291 | f.mTarget = mActive.get(f.mTargetIndex); |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 2292 | } else { |
| 2293 | Log.w(TAG, "Re-attaching retained fragment " + f |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 2294 | + " target no longer exists: " + f.mTargetIndex); |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 2295 | f.mTarget = null; |
| 2296 | } |
| 2297 | } |
| 2298 | } |
| 2299 | } |
| 2300 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2301 | // Build the list of currently added fragments. |
| 2302 | if (fms.mAdded != null) { |
| 2303 | mAdded = new ArrayList<Fragment>(fms.mAdded.length); |
| 2304 | for (int i=0; i<fms.mAdded.length; i++) { |
| 2305 | Fragment f = mActive.get(fms.mAdded[i]); |
| 2306 | if (f == null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2307 | throwException(new IllegalStateException( |
| 2308 | "No instantiated fragment for index #" + fms.mAdded[i])); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2309 | } |
| 2310 | f.mAdded = true; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 2311 | if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f); |
| 2312 | if (mAdded.contains(f)) { |
| 2313 | throw new IllegalStateException("Already added!"); |
| 2314 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2315 | mAdded.add(f); |
| 2316 | } |
| 2317 | } else { |
| 2318 | mAdded = null; |
| 2319 | } |
| 2320 | |
| 2321 | // Build the back stack. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2322 | if (fms.mBackStack != null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 2323 | mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2324 | for (int i=0; i<fms.mBackStack.length; i++) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 2325 | BackStackRecord bse = fms.mBackStack[i].instantiate(this); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 2326 | if (DEBUG) { |
| 2327 | Log.v(TAG, "restoreAllState: back stack #" + i |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 2328 | + " (index " + bse.mIndex + "): " + bse); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 2329 | LogWriter logw = new LogWriter(Log.VERBOSE, TAG); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 2330 | PrintWriter pw = new FastPrintWriter(logw, false, 1024); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 2331 | bse.dump(" ", pw, false); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 2332 | pw.flush(); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 2333 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2334 | mBackStack.add(bse); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 2335 | if (bse.mIndex >= 0) { |
| 2336 | setBackStackIndex(bse.mIndex, bse); |
| 2337 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2338 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 2339 | } else { |
| 2340 | mBackStack = null; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2344 | public void attachController(FragmentHostCallback<?> host, FragmentContainer container, |
| 2345 | Fragment parent) { |
| 2346 | if (mHost != null) throw new IllegalStateException("Already attached"); |
| 2347 | mHost = host; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2348 | mContainer = container; |
| 2349 | mParent = parent; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 2352 | public void noteStateNotSaved() { |
| 2353 | mStateSaved = false; |
| 2354 | } |
| 2355 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2356 | public void dispatchCreate() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 2357 | mStateSaved = false; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 2358 | moveToState(Fragment.CREATED); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 2361 | public void dispatchActivityCreated() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 2362 | mStateSaved = false; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 2363 | moveToState(Fragment.ACTIVITY_CREATED); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 2364 | } |
| 2365 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2366 | public void dispatchStart() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 2367 | mStateSaved = false; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 2368 | moveToState(Fragment.STARTED); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | public void dispatchResume() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 2372 | mStateSaved = false; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 2373 | moveToState(Fragment.RESUMED); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | public void dispatchPause() { |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 2377 | moveToState(Fragment.STARTED); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | public void dispatchStop() { |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 2381 | moveToState(Fragment.STOPPED); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2384 | public void dispatchDestroyView() { |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 2385 | moveToState(Fragment.CREATED); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2386 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 2387 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2388 | public void dispatchDestroy() { |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 2389 | mDestroyed = true; |
Dianne Hackborn | c693823 | 2011-07-21 16:25:26 -0700 | [diff] [blame] | 2390 | execPendingActions(); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 2391 | moveToState(Fragment.INITIALIZING); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2392 | mHost = null; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2393 | mContainer = null; |
| 2394 | mParent = null; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2395 | } |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 2396 | |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 2397 | public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) { |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 2398 | if (mAdded == null) { |
| 2399 | return; |
| 2400 | } |
| 2401 | for (int i = mAdded.size() - 1; i >= 0; --i) { |
| 2402 | final Fragment f = mAdded.get(i); |
| 2403 | if (f != null) { |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 2404 | f.performMultiWindowModeChanged(isInMultiWindowMode); |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 2405 | } |
| 2406 | } |
| 2407 | } |
| 2408 | |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 2409 | public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) { |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 2410 | if (mAdded == null) { |
| 2411 | return; |
| 2412 | } |
| 2413 | for (int i = mAdded.size() - 1; i >= 0; --i) { |
| 2414 | final Fragment f = mAdded.get(i); |
| 2415 | if (f != null) { |
Andrii Kulian | 933076d | 2016-03-29 17:04:42 -0700 | [diff] [blame] | 2416 | f.performPictureInPictureModeChanged(isInPictureInPictureMode); |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | } |
| 2420 | |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 2421 | public void dispatchConfigurationChanged(Configuration newConfig) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 2422 | if (mAdded != null) { |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 2423 | for (int i=0; i<mAdded.size(); i++) { |
| 2424 | Fragment f = mAdded.get(i); |
| 2425 | if (f != null) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2426 | f.performConfigurationChanged(newConfig); |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 2427 | } |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | |
| 2432 | public void dispatchLowMemory() { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 2433 | if (mAdded != null) { |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 2434 | for (int i=0; i<mAdded.size(); i++) { |
| 2435 | Fragment f = mAdded.get(i); |
| 2436 | if (f != null) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2437 | f.performLowMemory(); |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | } |
| 2441 | } |
| 2442 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2443 | public void dispatchTrimMemory(int level) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 2444 | if (mAdded != null) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2445 | for (int i=0; i<mAdded.size(); i++) { |
| 2446 | Fragment f = mAdded.get(i); |
| 2447 | if (f != null) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2448 | f.performTrimMemory(level); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2449 | } |
| 2450 | } |
| 2451 | } |
| 2452 | } |
| 2453 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2454 | public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 2455 | boolean show = false; |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 2456 | ArrayList<Fragment> newMenus = null; |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 2457 | if (mAdded != null) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2458 | for (int i=0; i<mAdded.size(); i++) { |
| 2459 | Fragment f = mAdded.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2460 | if (f != null) { |
| 2461 | if (f.performCreateOptionsMenu(menu, inflater)) { |
| 2462 | show = true; |
| 2463 | if (newMenus == null) { |
| 2464 | newMenus = new ArrayList<Fragment>(); |
| 2465 | } |
| 2466 | newMenus.add(f); |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 2467 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2468 | } |
| 2469 | } |
| 2470 | } |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 2471 | |
| 2472 | if (mCreatedMenus != null) { |
| 2473 | for (int i=0; i<mCreatedMenus.size(); i++) { |
| 2474 | Fragment f = mCreatedMenus.get(i); |
| 2475 | if (newMenus == null || !newMenus.contains(f)) { |
| 2476 | f.onDestroyOptionsMenu(); |
| 2477 | } |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | mCreatedMenus = newMenus; |
| 2482 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2483 | return show; |
| 2484 | } |
| 2485 | |
| 2486 | public boolean dispatchPrepareOptionsMenu(Menu menu) { |
| 2487 | boolean show = false; |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 2488 | if (mAdded != null) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2489 | for (int i=0; i<mAdded.size(); i++) { |
| 2490 | Fragment f = mAdded.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2491 | if (f != null) { |
| 2492 | if (f.performPrepareOptionsMenu(menu)) { |
| 2493 | show = true; |
| 2494 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2495 | } |
| 2496 | } |
| 2497 | } |
| 2498 | return show; |
| 2499 | } |
| 2500 | |
| 2501 | public boolean dispatchOptionsItemSelected(MenuItem item) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 2502 | if (mAdded != null) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2503 | for (int i=0; i<mAdded.size(); i++) { |
| 2504 | Fragment f = mAdded.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2505 | if (f != null) { |
| 2506 | if (f.performOptionsItemSelected(item)) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2507 | return true; |
| 2508 | } |
| 2509 | } |
| 2510 | } |
| 2511 | } |
| 2512 | return false; |
| 2513 | } |
| 2514 | |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 2515 | public boolean dispatchContextItemSelected(MenuItem item) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 2516 | if (mAdded != null) { |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 2517 | for (int i=0; i<mAdded.size(); i++) { |
| 2518 | Fragment f = mAdded.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2519 | if (f != null) { |
| 2520 | if (f.performContextItemSelected(item)) { |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 2521 | return true; |
| 2522 | } |
| 2523 | } |
| 2524 | } |
| 2525 | } |
| 2526 | return false; |
| 2527 | } |
| 2528 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2529 | public void dispatchOptionsMenuClosed(Menu menu) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 2530 | if (mAdded != null) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2531 | for (int i=0; i<mAdded.size(); i++) { |
| 2532 | Fragment f = mAdded.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2533 | if (f != null) { |
| 2534 | f.performOptionsMenuClosed(menu); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2535 | } |
| 2536 | } |
| 2537 | } |
| 2538 | } |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 2539 | |
| 2540 | @Override |
| 2541 | public void invalidateOptionsMenu() { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2542 | if (mHost != null && mCurState == Fragment.RESUMED) { |
| 2543 | mHost.onInvalidateOptionsMenu(); |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 2544 | } else { |
| 2545 | mNeedMenuInvalidate = true; |
| 2546 | } |
| 2547 | } |
| 2548 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2549 | public static int reverseTransit(int transit) { |
| 2550 | int rev = 0; |
| 2551 | switch (transit) { |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2552 | case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: |
| 2553 | rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2554 | break; |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2555 | case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: |
| 2556 | rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2557 | break; |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 2558 | case FragmentTransaction.TRANSIT_FRAGMENT_FADE: |
| 2559 | rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE; |
Chet Haase | 9ff82bf | 2010-10-05 14:30:51 -0700 | [diff] [blame] | 2560 | break; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2561 | } |
| 2562 | return rev; |
| 2563 | |
| 2564 | } |
| 2565 | |
| 2566 | public static int transitToStyleIndex(int transit, boolean enter) { |
| 2567 | int animAttr = -1; |
| 2568 | switch (transit) { |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2569 | case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2570 | animAttr = enter |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2571 | ? com.android.internal.R.styleable.FragmentAnimation_fragmentOpenEnterAnimation |
| 2572 | : com.android.internal.R.styleable.FragmentAnimation_fragmentOpenExitAnimation; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2573 | break; |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2574 | case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2575 | animAttr = enter |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2576 | ? com.android.internal.R.styleable.FragmentAnimation_fragmentCloseEnterAnimation |
| 2577 | : com.android.internal.R.styleable.FragmentAnimation_fragmentCloseExitAnimation; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2578 | break; |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 2579 | case FragmentTransaction.TRANSIT_FRAGMENT_FADE: |
Chet Haase | 9ff82bf | 2010-10-05 14:30:51 -0700 | [diff] [blame] | 2580 | animAttr = enter |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 2581 | ? com.android.internal.R.styleable.FragmentAnimation_fragmentFadeEnterAnimation |
| 2582 | : com.android.internal.R.styleable.FragmentAnimation_fragmentFadeExitAnimation; |
Chet Haase | 9ff82bf | 2010-10-05 14:30:51 -0700 | [diff] [blame] | 2583 | break; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2584 | } |
| 2585 | return animAttr; |
| 2586 | } |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 2587 | |
| 2588 | @Override |
| 2589 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { |
| 2590 | if (!"fragment".equals(name)) { |
| 2591 | return null; |
| 2592 | } |
| 2593 | |
| 2594 | String fname = attrs.getAttributeValue(null, "class"); |
| 2595 | TypedArray a = |
| 2596 | context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment); |
| 2597 | if (fname == null) { |
| 2598 | fname = a.getString(com.android.internal.R.styleable.Fragment_name); |
| 2599 | } |
| 2600 | int id = a.getResourceId(com.android.internal.R.styleable.Fragment_id, View.NO_ID); |
| 2601 | String tag = a.getString(com.android.internal.R.styleable.Fragment_tag); |
| 2602 | a.recycle(); |
| 2603 | |
| 2604 | int containerId = parent != null ? parent.getId() : 0; |
| 2605 | if (containerId == View.NO_ID && id == View.NO_ID && tag == null) { |
| 2606 | throw new IllegalArgumentException(attrs.getPositionDescription() |
| 2607 | + ": Must specify unique android:id, android:tag, or have a parent with" |
| 2608 | + " an id for " + fname); |
| 2609 | } |
| 2610 | |
| 2611 | // If we restored from a previous state, we may already have |
| 2612 | // instantiated this fragment from the state and should use |
| 2613 | // that instance instead of making a new one. |
| 2614 | Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null; |
| 2615 | if (fragment == null && tag != null) { |
| 2616 | fragment = findFragmentByTag(tag); |
| 2617 | } |
| 2618 | if (fragment == null && containerId != View.NO_ID) { |
| 2619 | fragment = findFragmentById(containerId); |
| 2620 | } |
| 2621 | |
| 2622 | if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x" |
| 2623 | + Integer.toHexString(id) + " fname=" + fname |
| 2624 | + " existing=" + fragment); |
| 2625 | if (fragment == null) { |
| 2626 | fragment = Fragment.instantiate(context, fname); |
| 2627 | fragment.mFromLayout = true; |
| 2628 | fragment.mFragmentId = id != 0 ? id : containerId; |
| 2629 | fragment.mContainerId = containerId; |
| 2630 | fragment.mTag = tag; |
| 2631 | fragment.mInLayout = true; |
| 2632 | fragment.mFragmentManager = this; |
Todd Kennedy | 0aa69b7 | 2015-08-31 14:10:04 -0700 | [diff] [blame] | 2633 | fragment.mHost = mHost; |
Todd Kennedy | 434bd65 | 2015-05-04 12:29:50 -0700 | [diff] [blame] | 2634 | fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState); |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 2635 | addFragment(fragment, true); |
| 2636 | } else if (fragment.mInLayout) { |
| 2637 | // A fragment already exists and it is not one we restored from |
| 2638 | // previous state. |
| 2639 | throw new IllegalArgumentException(attrs.getPositionDescription() |
| 2640 | + ": Duplicate id 0x" + Integer.toHexString(id) |
| 2641 | + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId) |
| 2642 | + " with another fragment for " + fname); |
| 2643 | } else { |
| 2644 | // This fragment was retained from a previous instance; get it |
| 2645 | // going now. |
| 2646 | fragment.mInLayout = true; |
Todd Kennedy | ac0e6ca | 2015-10-19 12:48:55 -0700 | [diff] [blame] | 2647 | fragment.mHost = mHost; |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 2648 | // If this fragment is newly instantiated (either right now, or |
| 2649 | // from last saved state), then give it the attributes to |
| 2650 | // initialize itself. |
| 2651 | if (!fragment.mRetaining) { |
Todd Kennedy | 434bd65 | 2015-05-04 12:29:50 -0700 | [diff] [blame] | 2652 | fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState); |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | // If we haven't finished entering the CREATED state ourselves yet, |
| 2657 | // push the inflated child fragment along. |
| 2658 | if (mCurState < Fragment.CREATED && fragment.mFromLayout) { |
| 2659 | moveToState(fragment, Fragment.CREATED, 0, 0, false); |
| 2660 | } else { |
| 2661 | moveToState(fragment); |
| 2662 | } |
| 2663 | |
| 2664 | if (fragment.mView == null) { |
| 2665 | throw new IllegalStateException("Fragment " + fname |
| 2666 | + " did not create a view."); |
| 2667 | } |
| 2668 | if (id != 0) { |
| 2669 | fragment.mView.setId(id); |
| 2670 | } |
| 2671 | if (fragment.mView.getTag() == null) { |
| 2672 | fragment.mView.setTag(tag); |
| 2673 | } |
| 2674 | return fragment.mView; |
| 2675 | } |
| 2676 | |
| 2677 | @Override |
| 2678 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
| 2679 | return null; |
| 2680 | } |
| 2681 | |
| 2682 | LayoutInflater.Factory2 getLayoutInflaterFactory() { |
| 2683 | return this; |
| 2684 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame^] | 2685 | |
| 2686 | /** |
| 2687 | * An add or pop transaction to be scheduled for the UI thread. |
| 2688 | */ |
| 2689 | interface OpGenerator { |
| 2690 | /** |
| 2691 | * Generate transactions to add to {@code records} and whether or not the transaction is |
| 2692 | * an add or pop to {@code isRecordPop}. |
| 2693 | * |
| 2694 | * records and isRecordPop must be added equally so that each transaction in records |
| 2695 | * matches the boolean for whether or not it is a pop in isRecordPop. |
| 2696 | * |
| 2697 | * @param records A list to add transactions to. |
| 2698 | * @param isRecordPop A list to add whether or not the transactions added to records is |
| 2699 | * a pop transaction. |
| 2700 | * @return true if something was added or false otherwise. |
| 2701 | */ |
| 2702 | boolean generateOps(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop); |
| 2703 | } |
| 2704 | |
| 2705 | /** |
| 2706 | * A pop operation OpGenerator. This will be run on the UI thread and will generate the |
| 2707 | * transactions that will be popped if anything can be popped. |
| 2708 | */ |
| 2709 | private class PopBackStackState implements OpGenerator { |
| 2710 | final String mName; |
| 2711 | final int mId; |
| 2712 | final int mFlags; |
| 2713 | |
| 2714 | public PopBackStackState(String name, int id, int flags) { |
| 2715 | mName = name; |
| 2716 | mId = id; |
| 2717 | mFlags = flags; |
| 2718 | } |
| 2719 | |
| 2720 | @Override |
| 2721 | public boolean generateOps(ArrayList<BackStackRecord> records, |
| 2722 | ArrayList<Boolean> isRecordPop) { |
| 2723 | return popBackStackState(records, isRecordPop, mName, mId, mFlags); |
| 2724 | } |
| 2725 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2726 | } |