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; |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 22 | import android.annotation.StringRes; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 23 | import android.content.ComponentCallbacks2; |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 27 | import android.content.res.Resources; |
George Mount | c03da0e | 2014-08-22 17:04:02 -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 | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 30 | import android.os.Parcel; |
| 31 | import android.os.Parcelable; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 32 | import android.transition.Transition; |
| 33 | import android.transition.TransitionInflater; |
| 34 | import android.transition.TransitionSet; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 35 | import android.util.AndroidRuntimeException; |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 36 | import android.util.ArrayMap; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 37 | import android.util.AttributeSet; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 38 | import android.util.DebugUtils; |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 39 | import android.util.Log; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 40 | import android.util.SparseArray; |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 41 | import android.util.SuperNotCalledException; |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 42 | import android.view.ContextMenu; |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 43 | import android.view.ContextMenu.ContextMenuInfo; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 44 | import android.view.LayoutInflater; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 45 | import android.view.Menu; |
| 46 | import android.view.MenuInflater; |
| 47 | import android.view.MenuItem; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 48 | import android.view.View; |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 49 | import android.view.View.OnCreateContextMenuListener; |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 50 | import android.view.ViewGroup; |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 51 | import android.widget.AdapterView; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 52 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 53 | import java.io.FileDescriptor; |
| 54 | import java.io.PrintWriter; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 55 | |
| 56 | final class FragmentState implements Parcelable { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 57 | final String mClassName; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 58 | final int mIndex; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 59 | final boolean mFromLayout; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 60 | final int mFragmentId; |
| 61 | final int mContainerId; |
| 62 | final String mTag; |
| 63 | final boolean mRetainInstance; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 64 | final boolean mDetached; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 65 | final Bundle mArguments; |
Todd Kennedy | d6d1858 | 2015-07-23 10:19:46 -0700 | [diff] [blame] | 66 | final boolean mHidden; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 67 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 68 | Bundle mSavedFragmentState; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 69 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 70 | Fragment mInstance; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 71 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 72 | public FragmentState(Fragment frag) { |
| 73 | mClassName = frag.getClass().getName(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 74 | mIndex = frag.mIndex; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 75 | mFromLayout = frag.mFromLayout; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 76 | mFragmentId = frag.mFragmentId; |
| 77 | mContainerId = frag.mContainerId; |
| 78 | mTag = frag.mTag; |
| 79 | mRetainInstance = frag.mRetainInstance; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 80 | mDetached = frag.mDetached; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 81 | mArguments = frag.mArguments; |
Todd Kennedy | d6d1858 | 2015-07-23 10:19:46 -0700 | [diff] [blame] | 82 | mHidden = frag.mHidden; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 83 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 84 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 85 | public FragmentState(Parcel in) { |
| 86 | mClassName = in.readString(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 87 | mIndex = in.readInt(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 88 | mFromLayout = in.readInt() != 0; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 89 | mFragmentId = in.readInt(); |
| 90 | mContainerId = in.readInt(); |
| 91 | mTag = in.readString(); |
| 92 | mRetainInstance = in.readInt() != 0; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 93 | mDetached = in.readInt() != 0; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 94 | mArguments = in.readBundle(); |
Todd Kennedy | d6d1858 | 2015-07-23 10:19:46 -0700 | [diff] [blame] | 95 | mHidden = in.readInt() != 0; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 96 | mSavedFragmentState = in.readBundle(); |
| 97 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 98 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 99 | public Fragment instantiate(FragmentHostCallback host, Fragment parent) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 100 | if (mInstance != null) { |
| 101 | return mInstance; |
| 102 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 103 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 104 | final Context context = host.getContext(); |
Dianne Hackborn | 5164246 | 2010-10-28 10:32:37 -0700 | [diff] [blame] | 105 | if (mArguments != null) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 106 | mArguments.setClassLoader(context.getClassLoader()); |
Dianne Hackborn | 5164246 | 2010-10-28 10:32:37 -0700 | [diff] [blame] | 107 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 108 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 109 | mInstance = Fragment.instantiate(context, mClassName, mArguments); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 110 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 111 | if (mSavedFragmentState != null) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 112 | mSavedFragmentState.setClassLoader(context.getClassLoader()); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 113 | mInstance.mSavedFragmentState = mSavedFragmentState; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 114 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 115 | mInstance.setIndex(mIndex, parent); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 116 | mInstance.mFromLayout = mFromLayout; |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame] | 117 | mInstance.mRestored = true; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 118 | mInstance.mFragmentId = mFragmentId; |
| 119 | mInstance.mContainerId = mContainerId; |
| 120 | mInstance.mTag = mTag; |
| 121 | mInstance.mRetainInstance = mRetainInstance; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 122 | mInstance.mDetached = mDetached; |
Todd Kennedy | d6d1858 | 2015-07-23 10:19:46 -0700 | [diff] [blame] | 123 | mInstance.mHidden = mHidden; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 124 | mInstance.mFragmentManager = host.mFragmentManager; |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 125 | if (FragmentManagerImpl.DEBUG) Log.v(FragmentManagerImpl.TAG, |
| 126 | "Instantiated fragment " + mInstance); |
| 127 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 128 | return mInstance; |
| 129 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 130 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 131 | public int describeContents() { |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | public void writeToParcel(Parcel dest, int flags) { |
| 136 | dest.writeString(mClassName); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 137 | dest.writeInt(mIndex); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 138 | dest.writeInt(mFromLayout ? 1 : 0); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 139 | dest.writeInt(mFragmentId); |
| 140 | dest.writeInt(mContainerId); |
| 141 | dest.writeString(mTag); |
| 142 | dest.writeInt(mRetainInstance ? 1 : 0); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 143 | dest.writeInt(mDetached ? 1 : 0); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 144 | dest.writeBundle(mArguments); |
Todd Kennedy | d6d1858 | 2015-07-23 10:19:46 -0700 | [diff] [blame] | 145 | dest.writeInt(mHidden ? 1 : 0); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 146 | dest.writeBundle(mSavedFragmentState); |
| 147 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 148 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 149 | public static final Parcelable.Creator<FragmentState> CREATOR |
| 150 | = new Parcelable.Creator<FragmentState>() { |
| 151 | public FragmentState createFromParcel(Parcel in) { |
| 152 | return new FragmentState(in); |
| 153 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 154 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 155 | public FragmentState[] newArray(int size) { |
| 156 | return new FragmentState[size]; |
| 157 | } |
| 158 | }; |
| 159 | } |
| 160 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 161 | /** |
| 162 | * A Fragment is a piece of an application's user interface or behavior |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 163 | * that can be placed in an {@link Activity}. Interaction with fragments |
| 164 | * is done through {@link FragmentManager}, which can be obtained via |
| 165 | * {@link Activity#getFragmentManager() Activity.getFragmentManager()} and |
| 166 | * {@link Fragment#getFragmentManager() Fragment.getFragmentManager()}. |
| 167 | * |
| 168 | * <p>The Fragment class can be used many ways to achieve a wide variety of |
felipeal | 1b4e4b6 | 2012-02-27 12:09:13 -0800 | [diff] [blame] | 169 | * results. In its core, it represents a particular operation or interface |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 170 | * that is running within a larger {@link Activity}. A Fragment is closely |
| 171 | * tied to the Activity it is in, and can not be used apart from one. Though |
| 172 | * Fragment defines its own lifecycle, that lifecycle is dependent on its |
| 173 | * activity: if the activity is stopped, no fragments inside of it can be |
| 174 | * started; when the activity is destroyed, all fragments will be destroyed. |
| 175 | * |
Jeff Davidson | 2a12978 | 2014-09-09 18:10:57 -0700 | [diff] [blame] | 176 | * <p>All subclasses of Fragment must include a public no-argument constructor. |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 177 | * The framework will often re-instantiate a fragment class when needed, |
| 178 | * in particular during state restore, and needs to be able to find this |
Jeff Davidson | 2a12978 | 2014-09-09 18:10:57 -0700 | [diff] [blame] | 179 | * constructor to instantiate it. If the no-argument constructor is not |
| 180 | * available, a runtime exception will occur in some cases during state |
| 181 | * restore. |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 182 | * |
| 183 | * <p>Topics covered here: |
| 184 | * <ol> |
Dianne Hackborn | f9dd34f | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 185 | * <li><a href="#OlderPlatforms">Older Platforms</a> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 186 | * <li><a href="#Lifecycle">Lifecycle</a> |
| 187 | * <li><a href="#Layout">Layout</a> |
| 188 | * <li><a href="#BackStack">Back Stack</a> |
| 189 | * </ol> |
| 190 | * |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 191 | * <div class="special reference"> |
| 192 | * <h3>Developer Guides</h3> |
| 193 | * <p>For more information about using fragments, read the |
| 194 | * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p> |
| 195 | * </div> |
| 196 | * |
Dianne Hackborn | f9dd34f | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 197 | * <a name="OlderPlatforms"></a> |
| 198 | * <h3>Older Platforms</h3> |
| 199 | * |
| 200 | * While the Fragment API was introduced in |
| 201 | * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API |
Dianne Hackborn | 7871bad | 2011-12-12 15:19:26 -0800 | [diff] [blame] | 202 | * at is also available for use on older platforms through |
| 203 | * {@link android.support.v4.app.FragmentActivity}. See the blog post |
Dianne Hackborn | f9dd34f | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 204 | * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html"> |
| 205 | * Fragments For All</a> for more details. |
| 206 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 207 | * <a name="Lifecycle"></a> |
| 208 | * <h3>Lifecycle</h3> |
| 209 | * |
| 210 | * <p>Though a Fragment's lifecycle is tied to its owning activity, it has |
| 211 | * its own wrinkle on the standard activity lifecycle. It includes basic |
| 212 | * activity lifecycle methods such as {@link #onResume}, but also important |
| 213 | * are methods related to interactions with the activity and UI generation. |
| 214 | * |
| 215 | * <p>The core series of lifecycle methods that are called to bring a fragment |
| 216 | * up to resumed state (interacting with the user) are: |
| 217 | * |
| 218 | * <ol> |
| 219 | * <li> {@link #onAttach} called once the fragment is associated with its activity. |
| 220 | * <li> {@link #onCreate} called to do initial creation of the fragment. |
| 221 | * <li> {@link #onCreateView} creates and returns the view hierarchy associated |
| 222 | * with the fragment. |
| 223 | * <li> {@link #onActivityCreated} tells the fragment that its activity has |
Quddus Chong | 3790001 | 2012-04-11 11:57:50 -0700 | [diff] [blame] | 224 | * completed its own {@link Activity#onCreate Activity.onCreate()}. |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 225 | * <li> {@link #onViewStateRestored} tells the fragment that all of the saved |
| 226 | * state of its view hierarchy has been restored. |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 227 | * <li> {@link #onStart} makes the fragment visible to the user (based on its |
| 228 | * containing activity being started). |
Mark Doliner | d0646dc | 2014-08-27 16:04:02 -0700 | [diff] [blame] | 229 | * <li> {@link #onResume} makes the fragment begin interacting with the user |
| 230 | * (based on its containing activity being resumed). |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 231 | * </ol> |
| 232 | * |
| 233 | * <p>As a fragment is no longer being used, it goes through a reverse |
| 234 | * series of callbacks: |
| 235 | * |
| 236 | * <ol> |
| 237 | * <li> {@link #onPause} fragment is no longer interacting with the user either |
| 238 | * because its activity is being paused or a fragment operation is modifying it |
| 239 | * in the activity. |
| 240 | * <li> {@link #onStop} fragment is no longer visible to the user either |
| 241 | * because its activity is being stopped or a fragment operation is modifying it |
| 242 | * in the activity. |
| 243 | * <li> {@link #onDestroyView} allows the fragment to clean up resources |
| 244 | * associated with its View. |
| 245 | * <li> {@link #onDestroy} called to do final cleanup of the fragment's state. |
| 246 | * <li> {@link #onDetach} called immediately prior to the fragment no longer |
| 247 | * being associated with its activity. |
| 248 | * </ol> |
| 249 | * |
| 250 | * <a name="Layout"></a> |
| 251 | * <h3>Layout</h3> |
| 252 | * |
| 253 | * <p>Fragments can be used as part of your application's layout, allowing |
| 254 | * you to better modularize your code and more easily adjust your user |
| 255 | * interface to the screen it is running on. As an example, we can look |
| 256 | * at a simple program consisting of a list of items, and display of the |
| 257 | * details of each item.</p> |
| 258 | * |
| 259 | * <p>An activity's layout XML can include <code><fragment></code> tags |
| 260 | * to embed fragment instances inside of the layout. For example, here is |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 261 | * a simple layout that embeds one fragment:</p> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 262 | * |
| 263 | * {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout} |
| 264 | * |
| 265 | * <p>The layout is installed in the activity in the normal way:</p> |
| 266 | * |
| 267 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java |
| 268 | * main} |
| 269 | * |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 270 | * <p>The titles fragment, showing a list of titles, is fairly simple, relying |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 271 | * on {@link ListFragment} for most of its work. Note the implementation of |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 272 | * clicking an item: depending on the current activity's layout, it can either |
| 273 | * create and display a new fragment to show the details in-place (more about |
Ben Dodson | 542f240 | 2011-06-14 16:40:23 -0700 | [diff] [blame] | 274 | * this later), or start a new activity to show the details.</p> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 275 | * |
| 276 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java |
| 277 | * titles} |
| 278 | * |
Ben Dodson | 542f240 | 2011-06-14 16:40:23 -0700 | [diff] [blame] | 279 | * <p>The details fragment showing the contents of a selected item just |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 280 | * displays a string of text based on an index of a string array built in to |
| 281 | * the app:</p> |
| 282 | * |
| 283 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java |
| 284 | * details} |
| 285 | * |
| 286 | * <p>In this case when the user clicks on a title, there is no details |
Ben Dodson | 542f240 | 2011-06-14 16:40:23 -0700 | [diff] [blame] | 287 | * container in the current activity, so the titles fragment's click code will |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 288 | * launch a new activity to display the details fragment:</p> |
| 289 | * |
| 290 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java |
| 291 | * details_activity} |
| 292 | * |
| 293 | * <p>However the screen may be large enough to show both the list of titles |
| 294 | * and details about the currently selected title. To use such a layout on |
| 295 | * a landscape screen, this alternative layout can be placed under layout-land:</p> |
| 296 | * |
| 297 | * {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout} |
| 298 | * |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 299 | * <p>Note how the prior code will adjust to this alternative UI flow: the titles |
| 300 | * fragment will now embed the details fragment inside of this activity, and the |
| 301 | * details activity will finish itself if it is running in a configuration |
| 302 | * where the details can be shown in-place. |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 303 | * |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 304 | * <p>When a configuration change causes the activity hosting these fragments |
| 305 | * to restart, its new instance may use a different layout that doesn't |
| 306 | * include the same fragments as the previous layout. In this case all of |
| 307 | * the previous fragments will still be instantiated and running in the new |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 308 | * instance. However, any that are no longer associated with a <fragment> |
| 309 | * tag in the view hierarchy will not have their content view created |
| 310 | * and will return false from {@link #isInLayout}. (The code here also shows |
| 311 | * how you can determine if a fragment placed in a container is no longer |
| 312 | * running in a layout with that container and avoid creating its view hierarchy |
| 313 | * in that case.) |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 314 | * |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 315 | * <p>The attributes of the <fragment> tag are used to control the |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 316 | * LayoutParams provided when attaching the fragment's view to the parent |
| 317 | * container. They can also be parsed by the fragment in {@link #onInflate} |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 318 | * as parameters. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 319 | * |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 320 | * <p>The fragment being instantiated must have some kind of unique identifier |
| 321 | * so that it can be re-associated with a previous instance if the parent |
| 322 | * activity needs to be destroyed and recreated. This can be provided these |
| 323 | * ways: |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 324 | * |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 325 | * <ul> |
| 326 | * <li>If nothing is explicitly supplied, the view ID of the container will |
| 327 | * be used. |
| 328 | * <li><code>android:tag</code> can be used in <fragment> to provide |
| 329 | * a specific tag name for the fragment. |
| 330 | * <li><code>android:id</code> can be used in <fragment> to provide |
| 331 | * a specific identifier for the fragment. |
| 332 | * </ul> |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 333 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 334 | * <a name="BackStack"></a> |
| 335 | * <h3>Back Stack</h3> |
| 336 | * |
| 337 | * <p>The transaction in which fragments are modified can be placed on an |
| 338 | * internal back-stack of the owning activity. When the user presses back |
| 339 | * in the activity, any transactions on the back stack are popped off before |
| 340 | * the activity itself is finished. |
| 341 | * |
| 342 | * <p>For example, consider this simple fragment that is instantiated with |
| 343 | * an integer argument and displays that in a TextView in its UI:</p> |
| 344 | * |
| 345 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java |
| 346 | * fragment} |
| 347 | * |
| 348 | * <p>A function that creates a new instance of the fragment, replacing |
| 349 | * whatever current fragment instance is being shown and pushing that change |
| 350 | * on to the back stack could be written as: |
| 351 | * |
| 352 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java |
| 353 | * add_stack} |
| 354 | * |
| 355 | * <p>After each call to this function, a new entry is on the stack, and |
| 356 | * pressing back will pop it to return the user to whatever previous state |
| 357 | * the activity UI was in. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 358 | */ |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 359 | public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 360 | private static final ArrayMap<String, Class<?>> sClassMap = |
| 361 | new ArrayMap<String, Class<?>>(); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 362 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 363 | static final int INVALID_STATE = -1; // Invalid state used as a null value. |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 364 | static final int INITIALIZING = 0; // Not yet created. |
| 365 | static final int CREATED = 1; // Created. |
| 366 | static final int ACTIVITY_CREATED = 2; // The activity has finished its creation. |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 367 | static final int STOPPED = 3; // Fully created, not started. |
| 368 | static final int STARTED = 4; // Created and started, not resumed. |
| 369 | static final int RESUMED = 5; // Created started and resumed. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 370 | |
| 371 | private static final Transition USE_DEFAULT_TRANSITION = new TransitionSet(); |
| 372 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 373 | int mState = INITIALIZING; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 374 | |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 375 | // Non-null if the fragment's view hierarchy is currently animating away, |
| 376 | // meaning we need to wait a bit on completely destroying it. This is the |
| 377 | // animation that is running. |
| 378 | Animator mAnimatingAway; |
| 379 | |
| 380 | // If mAnimatingAway != null, this is the state we should move to once the |
| 381 | // animation is done. |
| 382 | int mStateAfterAnimating; |
| 383 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 384 | // When instantiated from saved state, this is the saved state. |
| 385 | Bundle mSavedFragmentState; |
| 386 | SparseArray<Parcelable> mSavedViewState; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 387 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 388 | // Index into active fragment array. |
| 389 | int mIndex = -1; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 390 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 391 | // Internal unique name for this fragment; |
| 392 | String mWho; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 393 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 394 | // Construction arguments; |
| 395 | Bundle mArguments; |
| 396 | |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 397 | // Target fragment. |
| 398 | Fragment mTarget; |
| 399 | |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 400 | // For use when retaining a fragment: this is the index of the last mTarget. |
| 401 | int mTargetIndex = -1; |
| 402 | |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 403 | // Target request code. |
| 404 | int mTargetRequestCode; |
| 405 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 406 | // True if the fragment is in the list of added fragments. |
| 407 | boolean mAdded; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 408 | |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 409 | // If set this fragment is being removed from its activity. |
| 410 | boolean mRemoving; |
| 411 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 412 | // Set to true if this fragment was instantiated from a layout file. |
| 413 | boolean mFromLayout; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 414 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 415 | // Set to true when the view has actually been inflated in its layout. |
| 416 | boolean mInLayout; |
| 417 | |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame] | 418 | // True if this fragment has been restored from previously saved state. |
| 419 | boolean mRestored; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 420 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 421 | // Number of active back stack entries this fragment is in. |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 422 | int mBackStackNesting; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 423 | |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 424 | // The fragment manager we are associated with. Set as soon as the |
| 425 | // fragment is used in a transaction; cleared after it has been removed |
| 426 | // from all transactions. |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 427 | FragmentManagerImpl mFragmentManager; |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 428 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 429 | // Activity this fragment is attached to. |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 430 | FragmentHostCallback mHost; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 431 | |
| 432 | // Private fragment manager for child fragments inside of this one. |
| 433 | FragmentManagerImpl mChildFragmentManager; |
| 434 | |
| 435 | // If this Fragment is contained in another Fragment, this is that container. |
| 436 | Fragment mParentFragment; |
| 437 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 438 | // The optional identifier for this fragment -- either the container ID if it |
| 439 | // was dynamically added to the view hierarchy, or the ID supplied in |
| 440 | // layout. |
| 441 | int mFragmentId; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 442 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 443 | // When a fragment is being dynamically added to the view hierarchy, this |
| 444 | // is the identifier of the parent container it is being added to. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 445 | int mContainerId; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 446 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 447 | // The optional named tag for this fragment -- usually used to find |
| 448 | // fragments that are not part of the layout. |
| 449 | String mTag; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 450 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 451 | // Set to true when the app has requested that this fragment be hidden |
| 452 | // from the user. |
| 453 | boolean mHidden; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 454 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 455 | // Set to true when the app has requested that this fragment be detached. |
| 456 | boolean mDetached; |
| 457 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 458 | // If set this fragment would like its instance retained across |
| 459 | // configuration changes. |
| 460 | boolean mRetainInstance; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 461 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 462 | // If set this fragment is being retained across the current config change. |
| 463 | boolean mRetaining; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 464 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 465 | // If set this fragment has menu items to contribute. |
| 466 | boolean mHasMenu; |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 467 | |
| 468 | // Set to true to allow the fragment's menu to be shown. |
| 469 | boolean mMenuVisible = true; |
| 470 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 471 | // Used to verify that subclasses call through to super class. |
| 472 | boolean mCalled; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 473 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 474 | // If app has requested a specific animation, this is the one to use. |
| 475 | int mNextAnim; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 476 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 477 | // The parent container of the fragment after dynamically added to UI. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 478 | ViewGroup mContainer; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 479 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 480 | // The View generated for this fragment. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 481 | View mView; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 482 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 483 | // Whether this fragment should defer starting until after other fragments |
| 484 | // have been started and their loaders are finished. |
| 485 | boolean mDeferStart; |
| 486 | |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 487 | // Hint provided by the app that this fragment is currently visible to the user. |
| 488 | boolean mUserVisibleHint = true; |
| 489 | |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 490 | LoaderManagerImpl mLoaderManager; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 491 | boolean mLoadersStarted; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 492 | boolean mCheckedForLoaderManager; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 493 | |
| 494 | private Transition mEnterTransition = null; |
| 495 | private Transition mReturnTransition = USE_DEFAULT_TRANSITION; |
| 496 | private Transition mExitTransition = null; |
| 497 | private Transition mReenterTransition = USE_DEFAULT_TRANSITION; |
| 498 | private Transition mSharedElementEnterTransition = null; |
| 499 | private Transition mSharedElementReturnTransition = USE_DEFAULT_TRANSITION; |
| 500 | private Boolean mAllowReturnTransitionOverlap; |
| 501 | private Boolean mAllowEnterTransitionOverlap; |
| 502 | |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 503 | SharedElementCallback mEnterTransitionCallback = SharedElementCallback.NULL_CALLBACK; |
| 504 | SharedElementCallback mExitTransitionCallback = SharedElementCallback.NULL_CALLBACK; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 505 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 506 | /** |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 507 | * State information that has been retrieved from a fragment instance |
| 508 | * through {@link FragmentManager#saveFragmentInstanceState(Fragment) |
| 509 | * FragmentManager.saveFragmentInstanceState}. |
| 510 | */ |
| 511 | public static class SavedState implements Parcelable { |
| 512 | final Bundle mState; |
| 513 | |
| 514 | SavedState(Bundle state) { |
| 515 | mState = state; |
| 516 | } |
| 517 | |
| 518 | SavedState(Parcel in, ClassLoader loader) { |
| 519 | mState = in.readBundle(); |
| 520 | if (loader != null && mState != null) { |
| 521 | mState.setClassLoader(loader); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public int describeContents() { |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public void writeToParcel(Parcel dest, int flags) { |
| 532 | dest.writeBundle(mState); |
| 533 | } |
| 534 | |
| 535 | public static final Parcelable.ClassLoaderCreator<SavedState> CREATOR |
| 536 | = new Parcelable.ClassLoaderCreator<SavedState>() { |
| 537 | public SavedState createFromParcel(Parcel in) { |
| 538 | return new SavedState(in, null); |
| 539 | } |
| 540 | |
| 541 | public SavedState createFromParcel(Parcel in, ClassLoader loader) { |
| 542 | return new SavedState(in, loader); |
| 543 | } |
| 544 | |
| 545 | public SavedState[] newArray(int size) { |
| 546 | return new SavedState[size]; |
| 547 | } |
| 548 | }; |
| 549 | } |
| 550 | |
| 551 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 552 | * Thrown by {@link Fragment#instantiate(Context, String, Bundle)} when |
| 553 | * there is an instantiation failure. |
| 554 | */ |
| 555 | static public class InstantiationException extends AndroidRuntimeException { |
| 556 | public InstantiationException(String msg, Exception cause) { |
| 557 | super(msg, cause); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /** |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 562 | * Default constructor. <strong>Every</strong> fragment must have an |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 563 | * empty constructor, so it can be instantiated when restoring its |
| 564 | * activity's state. It is strongly recommended that subclasses do not |
| 565 | * have other constructors with parameters, since these constructors |
| 566 | * will not be called when the fragment is re-instantiated; instead, |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 567 | * arguments can be supplied by the caller with {@link #setArguments} |
| 568 | * and later retrieved by the Fragment with {@link #getArguments}. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 569 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 570 | * <p>Applications should generally not implement a constructor. The |
Mark Doliner | d0646dc | 2014-08-27 16:04:02 -0700 | [diff] [blame] | 571 | * first place application code can run where the fragment is ready to |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 572 | * be used is in {@link #onAttach(Activity)}, the point where the fragment |
| 573 | * is actually associated with its activity. Some applications may also |
| 574 | * want to implement {@link #onInflate} to retrieve attributes from a |
| 575 | * layout resource, though should take care here because this happens for |
| 576 | * the fragment is attached to its activity. |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 577 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 578 | public Fragment() { |
| 579 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 580 | |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 581 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 582 | * Like {@link #instantiate(Context, String, Bundle)} but with a null |
| 583 | * argument Bundle. |
| 584 | */ |
| 585 | public static Fragment instantiate(Context context, String fname) { |
| 586 | return instantiate(context, fname, null); |
| 587 | } |
| 588 | |
| 589 | /** |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 590 | * Create a new instance of a Fragment with the given class name. This is |
| 591 | * the same as calling its empty constructor. |
| 592 | * |
| 593 | * @param context The calling context being used to instantiate the fragment. |
| 594 | * This is currently just used to get its ClassLoader. |
| 595 | * @param fname The class name of the fragment to instantiate. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 596 | * @param args Bundle of arguments to supply to the fragment, which it |
| 597 | * can retrieve with {@link #getArguments()}. May be null. |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 598 | * @return Returns a new fragment instance. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 599 | * @throws InstantiationException If there is a failure in instantiating |
| 600 | * the given fragment class. This is a runtime exception; it is not |
| 601 | * normally expected to happen. |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 602 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 603 | public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 604 | try { |
| 605 | Class<?> clazz = sClassMap.get(fname); |
| 606 | if (clazz == null) { |
| 607 | // Class not found in the cache, see if it's real, and try to add it |
| 608 | clazz = context.getClassLoader().loadClass(fname); |
Amith Yamasani | 364ed4d | 2013-07-26 13:37:56 -0700 | [diff] [blame] | 609 | if (!Fragment.class.isAssignableFrom(clazz)) { |
| 610 | throw new InstantiationException("Trying to instantiate a class " + fname |
| 611 | + " that is not a Fragment", new ClassCastException()); |
| 612 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 613 | sClassMap.put(fname, clazz); |
| 614 | } |
| 615 | Fragment f = (Fragment)clazz.newInstance(); |
| 616 | if (args != null) { |
| 617 | args.setClassLoader(f.getClass().getClassLoader()); |
| 618 | f.mArguments = args; |
| 619 | } |
| 620 | return f; |
| 621 | } catch (ClassNotFoundException e) { |
| 622 | throw new InstantiationException("Unable to instantiate fragment " + fname |
| 623 | + ": make sure class name exists, is public, and has an" |
| 624 | + " empty constructor that is public", e); |
| 625 | } catch (java.lang.InstantiationException e) { |
| 626 | throw new InstantiationException("Unable to instantiate fragment " + fname |
| 627 | + ": make sure class name exists, is public, and has an" |
| 628 | + " empty constructor that is public", e); |
| 629 | } catch (IllegalAccessException e) { |
| 630 | throw new InstantiationException("Unable to instantiate fragment " + fname |
| 631 | + ": make sure class name exists, is public, and has an" |
| 632 | + " empty constructor that is public", e); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 633 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 634 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 635 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 636 | final void restoreViewState(Bundle savedInstanceState) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 637 | if (mSavedViewState != null) { |
| 638 | mView.restoreHierarchyState(mSavedViewState); |
| 639 | mSavedViewState = null; |
| 640 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 641 | mCalled = false; |
| 642 | onViewStateRestored(savedInstanceState); |
| 643 | if (!mCalled) { |
| 644 | throw new SuperNotCalledException("Fragment " + this |
| 645 | + " did not call through to super.onViewStateRestored()"); |
| 646 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 647 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 648 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 649 | final void setIndex(int index, Fragment parent) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 650 | mIndex = index; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 651 | if (parent != null) { |
| 652 | mWho = parent.mWho + ":" + mIndex; |
| 653 | } else { |
| 654 | mWho = "android:fragment:" + mIndex; |
| 655 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 658 | final boolean isInBackStack() { |
| 659 | return mBackStackNesting > 0; |
| 660 | } |
| 661 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 662 | /** |
| 663 | * Subclasses can not override equals(). |
| 664 | */ |
| 665 | @Override final public boolean equals(Object o) { |
| 666 | return super.equals(o); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Subclasses can not override hashCode(). |
| 671 | */ |
| 672 | @Override final public int hashCode() { |
| 673 | return super.hashCode(); |
| 674 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 675 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 676 | @Override |
| 677 | public String toString() { |
| 678 | StringBuilder sb = new StringBuilder(128); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 679 | DebugUtils.buildShortClassTag(this, sb); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 680 | if (mIndex >= 0) { |
| 681 | sb.append(" #"); |
| 682 | sb.append(mIndex); |
| 683 | } |
| 684 | if (mFragmentId != 0) { |
| 685 | sb.append(" id=0x"); |
| 686 | sb.append(Integer.toHexString(mFragmentId)); |
| 687 | } |
| 688 | if (mTag != null) { |
| 689 | sb.append(" "); |
| 690 | sb.append(mTag); |
| 691 | } |
| 692 | sb.append('}'); |
| 693 | return sb.toString(); |
| 694 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 695 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 696 | /** |
| 697 | * Return the identifier this fragment is known by. This is either |
| 698 | * the android:id value supplied in a layout or the container view ID |
| 699 | * supplied when adding the fragment. |
| 700 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 701 | final public int getId() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 702 | return mFragmentId; |
| 703 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 704 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 705 | /** |
| 706 | * Get the tag name of the fragment, if specified. |
| 707 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 708 | final public String getTag() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 709 | return mTag; |
| 710 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 711 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 712 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 713 | * Supply the construction arguments for this fragment. This can only |
| 714 | * be called before the fragment has been attached to its activity; that |
| 715 | * is, you should call it immediately after constructing the fragment. The |
| 716 | * arguments supplied here will be retained across fragment destroy and |
| 717 | * creation. |
| 718 | */ |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 719 | public void setArguments(Bundle args) { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 720 | if (mIndex >= 0) { |
| 721 | throw new IllegalStateException("Fragment already active"); |
| 722 | } |
| 723 | mArguments = args; |
| 724 | } |
| 725 | |
| 726 | /** |
Mark Doliner | d0646dc | 2014-08-27 16:04:02 -0700 | [diff] [blame] | 727 | * Return the arguments supplied to {@link #setArguments}, if any. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 728 | */ |
| 729 | final public Bundle getArguments() { |
| 730 | return mArguments; |
| 731 | } |
| 732 | |
| 733 | /** |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 734 | * Set the initial saved state that this Fragment should restore itself |
| 735 | * from when first being constructed, as returned by |
| 736 | * {@link FragmentManager#saveFragmentInstanceState(Fragment) |
| 737 | * FragmentManager.saveFragmentInstanceState}. |
| 738 | * |
| 739 | * @param state The state the fragment should be restored from. |
| 740 | */ |
| 741 | public void setInitialSavedState(SavedState state) { |
| 742 | if (mIndex >= 0) { |
| 743 | throw new IllegalStateException("Fragment already active"); |
| 744 | } |
| 745 | mSavedFragmentState = state != null && state.mState != null |
| 746 | ? state.mState : null; |
| 747 | } |
| 748 | |
| 749 | /** |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 750 | * Optional target for this fragment. This may be used, for example, |
| 751 | * if this fragment is being started by another, and when done wants to |
| 752 | * give a result back to the first. The target set here is retained |
| 753 | * across instances via {@link FragmentManager#putFragment |
| 754 | * FragmentManager.putFragment()}. |
| 755 | * |
| 756 | * @param fragment The fragment that is the target of this one. |
| 757 | * @param requestCode Optional request code, for convenience if you |
| 758 | * are going to call back with {@link #onActivityResult(int, int, Intent)}. |
| 759 | */ |
| 760 | public void setTargetFragment(Fragment fragment, int requestCode) { |
| 761 | mTarget = fragment; |
| 762 | mTargetRequestCode = requestCode; |
| 763 | } |
| 764 | |
| 765 | /** |
Dianne Hackborn | 3f00be5 | 2010-08-15 18:03:27 -0700 | [diff] [blame] | 766 | * Return the target fragment set by {@link #setTargetFragment}. |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 767 | */ |
| 768 | final public Fragment getTargetFragment() { |
| 769 | return mTarget; |
| 770 | } |
| 771 | |
| 772 | /** |
Dianne Hackborn | 3f00be5 | 2010-08-15 18:03:27 -0700 | [diff] [blame] | 773 | * Return the target request code set by {@link #setTargetFragment}. |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 774 | */ |
| 775 | final public int getTargetRequestCode() { |
| 776 | return mTargetRequestCode; |
| 777 | } |
| 778 | |
| 779 | /** |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 780 | * Return the {@link Context} this fragment is currently associated with. |
| 781 | */ |
| 782 | public Context getContext() { |
| 783 | return mHost == null ? null : mHost.getContext(); |
| 784 | } |
| 785 | |
| 786 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 787 | * Return the Activity this fragment is currently associated with. |
| 788 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 789 | final public Activity getActivity() { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 790 | return mHost == null ? null : mHost.getActivity(); |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * Return the host object of this fragment. May return {@code null} if the fragment |
| 795 | * isn't currently being hosted. |
| 796 | */ |
| 797 | @Nullable |
| 798 | final public Object getHost() { |
| 799 | return mHost == null ? null : mHost.onGetHost(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 800 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 801 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 802 | /** |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 803 | * Return <code>getActivity().getResources()</code>. |
| 804 | */ |
| 805 | final public Resources getResources() { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 806 | if (mHost == null) { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 807 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 808 | } |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 809 | return mHost.getContext().getResources(); |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 810 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 811 | |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 812 | /** |
| 813 | * Return a localized, styled CharSequence from the application's package's |
| 814 | * default string table. |
| 815 | * |
| 816 | * @param resId Resource id for the CharSequence text |
| 817 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 818 | public final CharSequence getText(@StringRes int resId) { |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 819 | return getResources().getText(resId); |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * Return a localized string from the application's package's |
| 824 | * default string table. |
| 825 | * |
| 826 | * @param resId Resource id for the string |
| 827 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 828 | public final String getString(@StringRes int resId) { |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 829 | return getResources().getString(resId); |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * Return a localized formatted string from the application's package's |
| 834 | * default string table, substituting the format arguments as defined in |
| 835 | * {@link java.util.Formatter} and {@link java.lang.String#format}. |
| 836 | * |
| 837 | * @param resId Resource id for the format string |
| 838 | * @param formatArgs The format arguments that will be used for substitution. |
| 839 | */ |
| 840 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 841 | public final String getString(@StringRes int resId, Object... formatArgs) { |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 842 | return getResources().getString(resId, formatArgs); |
| 843 | } |
| 844 | |
| 845 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 846 | * Return the FragmentManager for interacting with fragments associated |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 847 | * with this fragment's activity. Note that this will be non-null slightly |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 848 | * before {@link #getActivity()}, during the time from when the fragment is |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 849 | * placed in a {@link FragmentTransaction} until it is committed and |
| 850 | * attached to its activity. |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 851 | * |
| 852 | * <p>If this Fragment is a child of another Fragment, the FragmentManager |
| 853 | * returned here will be the parent's {@link #getChildFragmentManager()}. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 854 | */ |
| 855 | final public FragmentManager getFragmentManager() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 856 | return mFragmentManager; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /** |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 860 | * Return a private FragmentManager for placing and managing Fragments |
| 861 | * inside of this Fragment. |
| 862 | */ |
| 863 | final public FragmentManager getChildFragmentManager() { |
| 864 | if (mChildFragmentManager == null) { |
| 865 | instantiateChildFragmentManager(); |
| 866 | if (mState >= RESUMED) { |
| 867 | mChildFragmentManager.dispatchResume(); |
| 868 | } else if (mState >= STARTED) { |
| 869 | mChildFragmentManager.dispatchStart(); |
| 870 | } else if (mState >= ACTIVITY_CREATED) { |
| 871 | mChildFragmentManager.dispatchActivityCreated(); |
| 872 | } else if (mState >= CREATED) { |
| 873 | mChildFragmentManager.dispatchCreate(); |
| 874 | } |
| 875 | } |
| 876 | return mChildFragmentManager; |
| 877 | } |
| 878 | |
| 879 | /** |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 880 | * Returns the parent Fragment containing this Fragment. If this Fragment |
| 881 | * is attached directly to an Activity, returns null. |
| 882 | */ |
| 883 | final public Fragment getParentFragment() { |
| 884 | return mParentFragment; |
| 885 | } |
| 886 | |
| 887 | /** |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 888 | * Return true if the fragment is currently added to its activity. |
| 889 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 890 | final public boolean isAdded() { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 891 | return mHost != null && mAdded; |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | /** |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 895 | * Return true if the fragment has been explicitly detached from the UI. |
| 896 | * That is, {@link FragmentTransaction#detach(Fragment) |
| 897 | * FragmentTransaction.detach(Fragment)} has been used on it. |
| 898 | */ |
| 899 | final public boolean isDetached() { |
| 900 | return mDetached; |
| 901 | } |
| 902 | |
| 903 | /** |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 904 | * Return true if this fragment is currently being removed from its |
| 905 | * activity. This is <em>not</em> whether its activity is finishing, but |
| 906 | * rather whether it is in the process of being removed from its activity. |
| 907 | */ |
| 908 | final public boolean isRemoving() { |
| 909 | return mRemoving; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 910 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 911 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 912 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 913 | * Return true if the layout is included as part of an activity view |
| 914 | * hierarchy via the <fragment> tag. This will always be true when |
| 915 | * fragments are created through the <fragment> tag, <em>except</em> |
| 916 | * in the case where an old fragment is restored from a previous state and |
| 917 | * it does not appear in the layout of the current state. |
| 918 | */ |
| 919 | final public boolean isInLayout() { |
| 920 | return mInLayout; |
| 921 | } |
| 922 | |
| 923 | /** |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 924 | * Return true if the fragment is in the resumed state. This is true |
| 925 | * for the duration of {@link #onResume()} and {@link #onPause()} as well. |
| 926 | */ |
| 927 | final public boolean isResumed() { |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 928 | return mState >= RESUMED; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 929 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 930 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 931 | /** |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 932 | * Return true if the fragment is currently visible to the user. This means |
| 933 | * it: (1) has been added, (2) has its view attached to the window, and |
| 934 | * (3) is not hidden. |
| 935 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 936 | final public boolean isVisible() { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 937 | return isAdded() && !isHidden() && mView != null |
| 938 | && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE; |
| 939 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 940 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 941 | /** |
| 942 | * Return true if the fragment has been hidden. By default fragments |
| 943 | * are shown. You can find out about changes to this state with |
Dianne Hackborn | cddfa6d | 2010-05-19 22:56:37 -0700 | [diff] [blame] | 944 | * {@link #onHiddenChanged}. Note that the hidden state is orthogonal |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 945 | * to other states -- that is, to be visible to the user, a fragment |
| 946 | * must be both started and not hidden. |
| 947 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 948 | final public boolean isHidden() { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 949 | return mHidden; |
| 950 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 951 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 952 | /** |
| 953 | * Called when the hidden state (as returned by {@link #isHidden()} of |
| 954 | * the fragment has changed. Fragments start out not hidden; this will |
| 955 | * be called whenever the fragment changes state from that. |
| 956 | * @param hidden True if the fragment is now hidden, false if it is not |
| 957 | * visible. |
| 958 | */ |
| 959 | public void onHiddenChanged(boolean hidden) { |
| 960 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 961 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 962 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 963 | * Control whether a fragment instance is retained across Activity |
| 964 | * re-creation (such as from a configuration change). This can only |
| 965 | * be used with fragments not in the back stack. If set, the fragment |
| 966 | * lifecycle will be slightly different when an activity is recreated: |
| 967 | * <ul> |
| 968 | * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still |
| 969 | * will be, because the fragment is being detached from its current activity). |
| 970 | * <li> {@link #onCreate(Bundle)} will not be called since the fragment |
| 971 | * is not being re-created. |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 972 | * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b> |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 973 | * still be called. |
| 974 | * </ul> |
| 975 | */ |
| 976 | public void setRetainInstance(boolean retain) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 977 | if (retain && mParentFragment != null) { |
| 978 | throw new IllegalStateException( |
| 979 | "Can't retain fragements that are nested in other fragments"); |
| 980 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 981 | mRetainInstance = retain; |
| 982 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 983 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 984 | final public boolean getRetainInstance() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 985 | return mRetainInstance; |
| 986 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 987 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 988 | /** |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 989 | * Report that this fragment would like to participate in populating |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame] | 990 | * the options menu by receiving a call to {@link #onCreateOptionsMenu} |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 991 | * and related methods. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 992 | * |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 993 | * @param hasMenu If true, the fragment has menu items to contribute. |
| 994 | */ |
| 995 | public void setHasOptionsMenu(boolean hasMenu) { |
| 996 | if (mHasMenu != hasMenu) { |
| 997 | mHasMenu = hasMenu; |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 998 | if (isAdded() && !isHidden()) { |
| 999 | mFragmentManager.invalidateOptionsMenu(); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | } |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1003 | |
| 1004 | /** |
| 1005 | * Set a hint for whether this fragment's menu should be visible. This |
| 1006 | * is useful if you know that a fragment has been placed in your view |
| 1007 | * hierarchy so that the user can not currently seen it, so any menu items |
| 1008 | * it has should also not be shown. |
| 1009 | * |
| 1010 | * @param menuVisible The default is true, meaning the fragment's menu will |
| 1011 | * be shown as usual. If false, the user will not see the menu. |
| 1012 | */ |
| 1013 | public void setMenuVisibility(boolean menuVisible) { |
| 1014 | if (mMenuVisible != menuVisible) { |
| 1015 | mMenuVisible = menuVisible; |
| 1016 | if (mHasMenu && isAdded() && !isHidden()) { |
| 1017 | mFragmentManager.invalidateOptionsMenu(); |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1022 | /** |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1023 | * Set a hint to the system about whether this fragment's UI is currently visible |
| 1024 | * to the user. This hint defaults to true and is persistent across fragment instance |
| 1025 | * state save and restore. |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1026 | * |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1027 | * <p>An app may set this to false to indicate that the fragment's UI is |
| 1028 | * scrolled out of visibility or is otherwise not directly visible to the user. |
| 1029 | * This may be used by the system to prioritize operations such as fragment lifecycle updates |
| 1030 | * or loader ordering behavior.</p> |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1031 | * |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1032 | * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default), |
| 1033 | * false if it is not. |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1034 | */ |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1035 | public void setUserVisibleHint(boolean isVisibleToUser) { |
| 1036 | if (!mUserVisibleHint && isVisibleToUser && mState < STARTED) { |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1037 | mFragmentManager.performPendingDeferredStart(this); |
| 1038 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1039 | mUserVisibleHint = isVisibleToUser; |
| 1040 | mDeferStart = !isVisibleToUser; |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /** |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1044 | * @return The current value of the user-visible hint on this fragment. |
| 1045 | * @see #setUserVisibleHint(boolean) |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1046 | */ |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1047 | public boolean getUserVisibleHint() { |
| 1048 | return mUserVisibleHint; |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /** |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1052 | * Return the LoaderManager for this fragment, creating it if needed. |
| 1053 | */ |
| 1054 | public LoaderManager getLoaderManager() { |
| 1055 | if (mLoaderManager != null) { |
| 1056 | return mLoaderManager; |
| 1057 | } |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1058 | if (mHost == null) { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1059 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 1060 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1061 | mCheckedForLoaderManager = true; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1062 | mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, true); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1063 | return mLoaderManager; |
| 1064 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1065 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1066 | /** |
Scott Main | 87bff97 | 2013-02-27 15:46:34 -0800 | [diff] [blame] | 1067 | * Call {@link Activity#startActivity(Intent)} from the fragment's |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1068 | * containing Activity. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 1069 | * |
| 1070 | * @param intent The intent to start. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1071 | */ |
| 1072 | public void startActivity(Intent intent) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1073 | startActivity(intent, null); |
| 1074 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1075 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1076 | /** |
Scott Main | 87bff97 | 2013-02-27 15:46:34 -0800 | [diff] [blame] | 1077 | * Call {@link Activity#startActivity(Intent, Bundle)} from the fragment's |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1078 | * containing Activity. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 1079 | * |
| 1080 | * @param intent The intent to start. |
| 1081 | * @param options Additional options for how the Activity should be started. |
| 1082 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 1083 | * Context.startActivity(Intent, Bundle)} for more details. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1084 | */ |
| 1085 | public void startActivity(Intent intent, Bundle options) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1086 | if (mHost == null) { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1087 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 1088 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1089 | if (options != null) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1090 | mHost.onStartActivityFromFragment(this, intent, -1, options); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1091 | } else { |
| 1092 | // Note we want to go through this call for compatibility with |
| 1093 | // applications that may have overridden the method. |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1094 | mHost.onStartActivityFromFragment(this, intent, -1, null /*options*/); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1095 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1096 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1097 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1098 | /** |
Scott Main | 87bff97 | 2013-02-27 15:46:34 -0800 | [diff] [blame] | 1099 | * Call {@link Activity#startActivityForResult(Intent, int)} from the fragment's |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1100 | * containing Activity. |
| 1101 | */ |
| 1102 | public void startActivityForResult(Intent intent, int requestCode) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1103 | startActivityForResult(intent, requestCode, null); |
| 1104 | } |
| 1105 | |
| 1106 | /** |
Scott Main | 87bff97 | 2013-02-27 15:46:34 -0800 | [diff] [blame] | 1107 | * Call {@link Activity#startActivityForResult(Intent, int, Bundle)} from the fragment's |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1108 | * containing Activity. |
| 1109 | */ |
| 1110 | public void startActivityForResult(Intent intent, int requestCode, Bundle options) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1111 | if (mHost == null) { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1112 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 1113 | } |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1114 | mHost.onStartActivityFromFragment(this, intent, requestCode, options); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1115 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1116 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1117 | /** |
| 1118 | * Receive the result from a previous call to |
| 1119 | * {@link #startActivityForResult(Intent, int)}. This follows the |
| 1120 | * related Activity API as described there in |
| 1121 | * {@link Activity#onActivityResult(int, int, Intent)}. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1122 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1123 | * @param requestCode The integer request code originally supplied to |
| 1124 | * startActivityForResult(), allowing you to identify who this |
| 1125 | * result came from. |
| 1126 | * @param resultCode The integer result code returned by the child activity |
| 1127 | * through its setResult(). |
| 1128 | * @param data An Intent, which can return result data to the caller |
| 1129 | * (various data can be attached to Intent "extras"). |
| 1130 | */ |
| 1131 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 1132 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1133 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1134 | /** |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 1135 | * Requests permissions to be granted to this application. These permissions |
| 1136 | * must be requested in your manifest, they should not be granted to your app, |
| 1137 | * and they should have protection level {@link android.content.pm.PermissionInfo |
| 1138 | * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by |
| 1139 | * the platform or a third-party app. |
| 1140 | * <p> |
| 1141 | * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL} |
| 1142 | * are granted at install time if requested in the manifest. Signature permissions |
| 1143 | * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at |
| 1144 | * install time if requested in the manifest and the signature of your app matches |
| 1145 | * the signature of the app declaring the permissions. |
| 1146 | * </p> |
| 1147 | * <p> |
| 1148 | * If your app does not have the requested permissions the user will be presented |
| 1149 | * with UI for accepting them. After the user has accepted or rejected the |
| 1150 | * requested permissions you will receive a callback on {@link |
| 1151 | * #onRequestPermissionsResult(int, String[], int[])} reporting whether the |
| 1152 | * permissions were granted or not. |
| 1153 | * </p> |
| 1154 | * <p> |
| 1155 | * Note that requesting a permission does not guarantee it will be granted and |
| 1156 | * your app should be able to run without having this permission. |
| 1157 | * </p> |
| 1158 | * <p> |
| 1159 | * This method may start an activity allowing the user to choose which permissions |
| 1160 | * to grant and which to reject. Hence, you should be prepared that your activity |
| 1161 | * may be paused and resumed. Further, granting some permissions may require |
| 1162 | * a restart of you application. In such a case, the system will recreate the |
| 1163 | * activity stack before delivering the result to {@link |
| 1164 | * #onRequestPermissionsResult(int, String[], int[])}. |
| 1165 | * </p> |
| 1166 | * <p> |
| 1167 | * When checking whether you have a permission you should use {@link |
| 1168 | * android.content.Context#checkSelfPermission(String)}. |
| 1169 | * </p> |
| 1170 | * <p> |
Svet Ganov | d7ad54e | 2015-08-21 06:18:09 -0700 | [diff] [blame] | 1171 | * Calling this API for permissions already granted to your app would show UI |
| 1172 | * to the user to decide whether the app can still hold these permissions. This |
| 1173 | * can be useful if the way your app uses data guarded by the permissions |
| 1174 | * changes significantly. |
| 1175 | * </p> |
| 1176 | * <p> |
Svet Ganov | 255aece | 2015-07-03 16:03:53 -0700 | [diff] [blame] | 1177 | * You cannot request a permission if your activity sets {@link |
| 1178 | * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to |
| 1179 | * <code>true</code> because in this case the activity would not receive |
| 1180 | * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}. |
| 1181 | * </p> |
| 1182 | * <p> |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 1183 | * A sample permissions request looks like this: |
| 1184 | * </p> |
| 1185 | * <code><pre><p> |
| 1186 | * private void showContacts() { |
| 1187 | * if (getActivity().checkSelfPermission(Manifest.permission.READ_CONTACTS) |
| 1188 | * != PackageManager.PERMISSION_GRANTED) { |
| 1189 | * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, |
| 1190 | * PERMISSIONS_REQUEST_READ_CONTACTS); |
| 1191 | * } else { |
| 1192 | * doShowContacts(); |
| 1193 | * } |
| 1194 | * } |
| 1195 | * |
| 1196 | * {@literal @}Override |
| 1197 | * public void onRequestPermissionsResult(int requestCode, String[] permissions, |
| 1198 | * int[] grantResults) { |
| 1199 | * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS |
| 1200 | * && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 1201 | * doShowContacts(); |
| 1202 | * } |
| 1203 | * } |
| 1204 | * </code></pre></p> |
| 1205 | * |
| 1206 | * @param permissions The requested permissions. |
| 1207 | * @param requestCode Application specific request code to match with a result |
| 1208 | * reported to {@link #onRequestPermissionsResult(int, String[], int[])}. |
Svet Ganov | 255aece | 2015-07-03 16:03:53 -0700 | [diff] [blame] | 1209 | * Should be >= 0. |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 1210 | * |
| 1211 | * @see #onRequestPermissionsResult(int, String[], int[]) |
| 1212 | * @see android.content.Context#checkSelfPermission(String) |
| 1213 | */ |
| 1214 | public final void requestPermissions(@NonNull String[] permissions, int requestCode) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1215 | if (mHost == null) { |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 1216 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 1217 | } |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 1218 | mHost.onRequestPermissionsFromFragment(this, permissions,requestCode); |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | /** |
| 1222 | * Callback for the result from requesting permissions. This method |
| 1223 | * is invoked for every call on {@link #requestPermissions(String[], int)}. |
Svet Ganov | 0f45c7c | 2015-07-16 16:28:51 -0700 | [diff] [blame] | 1224 | * <p> |
| 1225 | * <strong>Note:</strong> It is possible that the permissions request interaction |
| 1226 | * with the user is interrupted. In this case you will receive empty permissions |
| 1227 | * and results arrays which should be treated as a cancellation. |
| 1228 | * </p> |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 1229 | * |
| 1230 | * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}. |
| 1231 | * @param permissions The requested permissions. Never null. |
| 1232 | * @param grantResults The grant results for the corresponding permissions |
| 1233 | * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED} |
| 1234 | * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null. |
| 1235 | * |
| 1236 | * @see #requestPermissions(String[], int) |
| 1237 | */ |
| 1238 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
| 1239 | @NonNull int[] grantResults) { |
| 1240 | /* callback - do nothing */ |
| 1241 | } |
| 1242 | |
| 1243 | /** |
Svetoslav | a2011d2 | 2015-06-03 15:43:59 -0700 | [diff] [blame] | 1244 | * Gets whether you should show UI with rationale for requesting a permission. |
| 1245 | * You should do this only if you do not have the permission and the context in |
| 1246 | * which the permission is requested does not clearly communicate to the user |
| 1247 | * what would be the benefit from granting this permission. |
| 1248 | * <p> |
| 1249 | * For example, if you write a camera app, requesting the camera permission |
| 1250 | * would be expected by the user and no rationale for why it is requested is |
| 1251 | * needed. If however, the app needs location for tagging photos then a non-tech |
| 1252 | * savvy user may wonder how location is related to taking photos. In this case |
| 1253 | * you may choose to show UI with rationale of requesting this permission. |
| 1254 | * </p> |
| 1255 | * |
| 1256 | * @param permission A permission your app wants to request. |
| 1257 | * @return Whether you can show permission rationale UI. |
| 1258 | * |
| 1259 | * @see Context#checkSelfPermission(String) |
| 1260 | * @see #requestPermissions(String[], int) |
| 1261 | * @see #onRequestPermissionsResult(int, String[], int[]) |
| 1262 | */ |
| 1263 | public boolean shouldShowRequestPermissionRationale(@NonNull String permission) { |
| 1264 | if (mHost != null) { |
Svet Ganov | 9e136b8 | 2015-07-16 09:58:38 -0700 | [diff] [blame] | 1265 | return mHost.getContext().getPackageManager() |
| 1266 | .shouldShowRequestPermissionRationale(permission); |
Svetoslav | a2011d2 | 2015-06-03 15:43:59 -0700 | [diff] [blame] | 1267 | } |
| 1268 | return false; |
| 1269 | } |
| 1270 | |
| 1271 | /** |
Dianne Hackborn | 7187ccb | 2011-01-24 23:58:13 -0800 | [diff] [blame] | 1272 | * @hide Hack so that DialogFragment can make its Dialog before creating |
| 1273 | * its views, and the view construction can use the dialog's context for |
| 1274 | * inflation. Maybe this should become a public API. Note sure. |
| 1275 | */ |
| 1276 | public LayoutInflater getLayoutInflater(Bundle savedInstanceState) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1277 | final LayoutInflater result = mHost.onGetLayoutInflater(); |
| 1278 | if (mHost.onUseFragmentManagerInflaterFactory()) { |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 1279 | getChildFragmentManager(); // Init if needed; use raw implementation below. |
| 1280 | result.setPrivateFactory(mChildFragmentManager.getLayoutInflaterFactory()); |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 1281 | } |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1282 | return result; |
Dianne Hackborn | 7187ccb | 2011-01-24 23:58:13 -0800 | [diff] [blame] | 1283 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1284 | |
Dianne Hackborn | 7187ccb | 2011-01-24 23:58:13 -0800 | [diff] [blame] | 1285 | /** |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1286 | * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead. |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1287 | */ |
| 1288 | @Deprecated |
| 1289 | public void onInflate(AttributeSet attrs, Bundle savedInstanceState) { |
| 1290 | mCalled = true; |
| 1291 | } |
| 1292 | |
| 1293 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1294 | * Called when a fragment is being created as part of a view layout |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1295 | * inflation, typically from setting the content view of an activity. This |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1296 | * may be called immediately after the fragment is created from a <fragment> |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1297 | * tag in a layout file. Note this is <em>before</em> the fragment's |
| 1298 | * {@link #onAttach(Activity)} has been called; all you should do here is |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1299 | * parse the attributes and save them away. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1300 | * |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1301 | * <p>This is called every time the fragment is inflated, even if it is |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1302 | * being inflated into a new instance with saved state. It typically makes |
| 1303 | * sense to re-parse the parameters each time, to allow them to change with |
| 1304 | * different configurations.</p> |
| 1305 | * |
| 1306 | * <p>Here is a typical implementation of a fragment that can take parameters |
| 1307 | * both through attributes supplied here as well from {@link #getArguments()}:</p> |
| 1308 | * |
| 1309 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java |
| 1310 | * fragment} |
| 1311 | * |
| 1312 | * <p>Note that parsing the XML attributes uses a "styleable" resource. The |
| 1313 | * declaration for the styleable used here is:</p> |
| 1314 | * |
| 1315 | * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments} |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1316 | * |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1317 | * <p>The fragment can then be declared within its activity's content layout |
| 1318 | * through a tag like this:</p> |
| 1319 | * |
| 1320 | * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes} |
| 1321 | * |
| 1322 | * <p>This fragment can also be created dynamically from arguments given |
| 1323 | * at runtime in the arguments Bundle; here is an example of doing so at |
| 1324 | * creation of the containing activity:</p> |
| 1325 | * |
| 1326 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java |
| 1327 | * create} |
| 1328 | * |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1329 | * @param context The Context that is inflating this fragment. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1330 | * @param attrs The attributes at the tag where the fragment is |
| 1331 | * being created. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1332 | * @param savedInstanceState If the fragment is being re-created from |
| 1333 | * a previous saved state, this is the state. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1334 | */ |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1335 | public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) { |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1336 | onInflate(attrs, savedInstanceState); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1337 | mCalled = true; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1338 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1339 | TypedArray a = context.obtainStyledAttributes(attrs, |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1340 | com.android.internal.R.styleable.Fragment); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1341 | mEnterTransition = loadTransition(context, a, mEnterTransition, null, |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1342 | com.android.internal.R.styleable.Fragment_fragmentEnterTransition); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1343 | mReturnTransition = loadTransition(context, a, mReturnTransition, USE_DEFAULT_TRANSITION, |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1344 | com.android.internal.R.styleable.Fragment_fragmentReturnTransition); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1345 | mExitTransition = loadTransition(context, a, mExitTransition, null, |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1346 | com.android.internal.R.styleable.Fragment_fragmentExitTransition); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1347 | mReenterTransition = loadTransition(context, a, mReenterTransition, USE_DEFAULT_TRANSITION, |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1348 | com.android.internal.R.styleable.Fragment_fragmentReenterTransition); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1349 | mSharedElementEnterTransition = loadTransition(context, a, mSharedElementEnterTransition, |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1350 | null, com.android.internal.R.styleable.Fragment_fragmentSharedElementEnterTransition); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1351 | mSharedElementReturnTransition = loadTransition(context, a, mSharedElementReturnTransition, |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1352 | USE_DEFAULT_TRANSITION, |
| 1353 | com.android.internal.R.styleable.Fragment_fragmentSharedElementReturnTransition); |
| 1354 | if (mAllowEnterTransitionOverlap == null) { |
| 1355 | mAllowEnterTransitionOverlap = a.getBoolean( |
| 1356 | com.android.internal.R.styleable.Fragment_fragmentAllowEnterTransitionOverlap, true); |
| 1357 | } |
| 1358 | if (mAllowReturnTransitionOverlap == null) { |
| 1359 | mAllowReturnTransitionOverlap = a.getBoolean( |
| 1360 | com.android.internal.R.styleable.Fragment_fragmentAllowReturnTransitionOverlap, true); |
| 1361 | } |
| 1362 | a.recycle(); |
Todd Kennedy | 434bd65 | 2015-05-04 12:29:50 -0700 | [diff] [blame] | 1363 | |
| 1364 | final Activity hostActivity = mHost == null ? null : mHost.getActivity(); |
| 1365 | if (hostActivity != null) { |
| 1366 | mCalled = false; |
| 1367 | onInflate(hostActivity, attrs, savedInstanceState); |
| 1368 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1369 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1370 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1371 | /** |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1372 | * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead. |
| 1373 | */ |
| 1374 | @Deprecated |
| 1375 | public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { |
| 1376 | mCalled = true; |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * Called when a fragment is first attached to its context. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1381 | * {@link #onCreate(Bundle)} will be called after this. |
| 1382 | */ |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1383 | public void onAttach(Context context) { |
| 1384 | mCalled = true; |
| 1385 | final Activity hostActivity = mHost == null ? null : mHost.getActivity(); |
| 1386 | if (hostActivity != null) { |
| 1387 | mCalled = false; |
| 1388 | onAttach(hostActivity); |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | /** |
| 1393 | * @deprecated Use {@link #onAttach(Context)} instead. |
| 1394 | */ |
| 1395 | @Deprecated |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1396 | public void onAttach(Activity activity) { |
| 1397 | mCalled = true; |
| 1398 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1399 | |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1400 | /** |
| 1401 | * Called when a fragment loads an animation. |
| 1402 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1403 | public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1404 | return null; |
| 1405 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1406 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1407 | /** |
| 1408 | * Called to do initial creation of a fragment. This is called after |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1409 | * {@link #onAttach(Activity)} and before |
Felipe Leme | b144457 | 2015-11-03 17:39:07 -0800 | [diff] [blame] | 1410 | * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, but is not called if the fragment |
| 1411 | * instance is retained across Activity re-creation (see {@link #setRetainInstance(boolean)}). |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1412 | * |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1413 | * <p>Note that this can be called while the fragment's activity is |
| 1414 | * still in the process of being created. As such, you can not rely |
| 1415 | * on things like the activity's content view hierarchy being initialized |
| 1416 | * at this point. If you want to do work once the activity itself is |
| 1417 | * created, see {@link #onActivityCreated(Bundle)}. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1418 | * |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1419 | * @param savedInstanceState If the fragment is being re-created from |
| 1420 | * a previous saved state, this is the state. |
| 1421 | */ |
Scott Kennedy | 3c32b62 | 2015-02-22 16:58:58 -0800 | [diff] [blame] | 1422 | public void onCreate(@Nullable Bundle savedInstanceState) { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1423 | mCalled = true; |
| 1424 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1425 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1426 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1427 | * Called to have the fragment instantiate its user interface view. |
| 1428 | * This is optional, and non-graphical fragments can return null (which |
| 1429 | * is the default implementation). This will be called between |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1430 | * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1431 | * |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1432 | * <p>If you return a View from here, you will later be called in |
| 1433 | * {@link #onDestroyView} when the view is being released. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1434 | * |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1435 | * @param inflater The LayoutInflater object that can be used to inflate |
| 1436 | * any views in the fragment, |
| 1437 | * @param container If non-null, this is the parent view that the fragment's |
| 1438 | * UI should be attached to. The fragment should not add the view itself, |
| 1439 | * but this can be used to generate the LayoutParams of the view. |
| 1440 | * @param savedInstanceState If non-null, this fragment is being re-constructed |
| 1441 | * from a previous saved state as given here. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1442 | * |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1443 | * @return Return the View for the fragment's UI, or null. |
| 1444 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1445 | @Nullable |
| 1446 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1447 | Bundle savedInstanceState) { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1448 | return null; |
| 1449 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1450 | |
| 1451 | /** |
| 1452 | * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} |
| 1453 | * has returned, but before any saved state has been restored in to the view. |
| 1454 | * This gives subclasses a chance to initialize themselves once |
| 1455 | * they know their view hierarchy has been completely created. The fragment's |
| 1456 | * view hierarchy is not however attached to its parent at this point. |
| 1457 | * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}. |
| 1458 | * @param savedInstanceState If non-null, this fragment is being re-constructed |
| 1459 | * from a previous saved state as given here. |
| 1460 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1461 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1462 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1463 | |
Scott Main | 662cc7a4 | 2011-01-17 15:02:07 -0800 | [diff] [blame] | 1464 | /** |
| 1465 | * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}), |
| 1466 | * if provided. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1467 | * |
Scott Main | 662cc7a4 | 2011-01-17 15:02:07 -0800 | [diff] [blame] | 1468 | * @return The fragment's root view, or null if it has no layout. |
| 1469 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1470 | @Nullable |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1471 | public View getView() { |
| 1472 | return mView; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1473 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1474 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1475 | /** |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1476 | * Called when the fragment's activity has been created and this |
| 1477 | * fragment's view hierarchy instantiated. It can be used to do final |
| 1478 | * initialization once these pieces are in place, such as retrieving |
| 1479 | * views or restoring state. It is also useful for fragments that use |
| 1480 | * {@link #setRetainInstance(boolean)} to retain their instance, |
| 1481 | * as this callback tells the fragment when it is fully associated with |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1482 | * the new activity instance. This is called after {@link #onCreateView} |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1483 | * and before {@link #onViewStateRestored(Bundle)}. |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1484 | * |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1485 | * @param savedInstanceState If the fragment is being re-created from |
| 1486 | * a previous saved state, this is the state. |
| 1487 | */ |
Scott Kennedy | 3c32b62 | 2015-02-22 16:58:58 -0800 | [diff] [blame] | 1488 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1489 | mCalled = true; |
| 1490 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1491 | |
| 1492 | /** |
| 1493 | * Called when all saved state has been restored into the view hierarchy |
| 1494 | * of the fragment. This can be used to do initialization based on saved |
| 1495 | * state that you are letting the view hierarchy track itself, such as |
| 1496 | * whether check box widgets are currently checked. This is called |
| 1497 | * after {@link #onActivityCreated(Bundle)} and before |
| 1498 | * {@link #onStart()}. |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1499 | * |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1500 | * @param savedInstanceState If the fragment is being re-created from |
| 1501 | * a previous saved state, this is the state. |
| 1502 | */ |
| 1503 | public void onViewStateRestored(Bundle savedInstanceState) { |
| 1504 | mCalled = true; |
| 1505 | } |
| 1506 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1507 | /** |
| 1508 | * Called when the Fragment is visible to the user. This is generally |
| 1509 | * tied to {@link Activity#onStart() Activity.onStart} of the containing |
| 1510 | * Activity's lifecycle. |
| 1511 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1512 | public void onStart() { |
| 1513 | mCalled = true; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1514 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1515 | if (!mLoadersStarted) { |
| 1516 | mLoadersStarted = true; |
| 1517 | if (!mCheckedForLoaderManager) { |
| 1518 | mCheckedForLoaderManager = true; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1519 | mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1520 | } |
| 1521 | if (mLoaderManager != null) { |
| 1522 | mLoaderManager.doStart(); |
| 1523 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1524 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1525 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1526 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1527 | /** |
| 1528 | * Called when the fragment is visible to the user and actively running. |
| 1529 | * This is generally |
| 1530 | * tied to {@link Activity#onResume() Activity.onResume} of the containing |
| 1531 | * Activity's lifecycle. |
| 1532 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1533 | public void onResume() { |
| 1534 | mCalled = true; |
| 1535 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1536 | |
Dianne Hackborn | 7277820 | 2010-08-20 18:26:01 -0700 | [diff] [blame] | 1537 | /** |
| 1538 | * Called to ask the fragment to save its current dynamic state, so it |
| 1539 | * can later be reconstructed in a new instance of its process is |
| 1540 | * restarted. If a new instance of the fragment later needs to be |
| 1541 | * created, the data you place in the Bundle here will be available |
| 1542 | * in the Bundle given to {@link #onCreate(Bundle)}, |
| 1543 | * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and |
| 1544 | * {@link #onActivityCreated(Bundle)}. |
| 1545 | * |
| 1546 | * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle) |
Daisuke Miyakawa | 2f76176 | 2010-09-12 16:53:17 -0700 | [diff] [blame] | 1547 | * Activity.onSaveInstanceState(Bundle)} and most of the discussion there |
Dianne Hackborn | 7277820 | 2010-08-20 18:26:01 -0700 | [diff] [blame] | 1548 | * applies here as well. Note however: <em>this method may be called |
| 1549 | * at any time before {@link #onDestroy()}</em>. There are many situations |
| 1550 | * where a fragment may be mostly torn down (such as when placed on the |
| 1551 | * back stack with no UI showing), but its state will not be saved until |
| 1552 | * its owning activity actually needs to save its state. |
| 1553 | * |
| 1554 | * @param outState Bundle in which to place your saved state. |
| 1555 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1556 | public void onSaveInstanceState(Bundle outState) { |
| 1557 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1558 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1559 | public void onConfigurationChanged(Configuration newConfig) { |
| 1560 | mCalled = true; |
| 1561 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1562 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1563 | /** |
| 1564 | * Called when the Fragment is no longer resumed. This is generally |
| 1565 | * tied to {@link Activity#onPause() Activity.onPause} of the containing |
| 1566 | * Activity's lifecycle. |
| 1567 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1568 | public void onPause() { |
| 1569 | mCalled = true; |
| 1570 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1571 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1572 | /** |
| 1573 | * Called when the Fragment is no longer started. This is generally |
| 1574 | * tied to {@link Activity#onStop() Activity.onStop} of the containing |
| 1575 | * Activity's lifecycle. |
| 1576 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1577 | public void onStop() { |
| 1578 | mCalled = true; |
| 1579 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1580 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1581 | public void onLowMemory() { |
| 1582 | mCalled = true; |
| 1583 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1584 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1585 | public void onTrimMemory(int level) { |
| 1586 | mCalled = true; |
| 1587 | } |
| 1588 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1589 | /** |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1590 | * Called when the view previously created by {@link #onCreateView} has |
| 1591 | * been detached from the fragment. The next time the fragment needs |
| 1592 | * to be displayed, a new view will be created. This is called |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1593 | * after {@link #onStop()} and before {@link #onDestroy()}. It is called |
| 1594 | * <em>regardless</em> of whether {@link #onCreateView} returned a |
| 1595 | * non-null view. Internally it is called after the view's state has |
| 1596 | * been saved but before it has been removed from its parent. |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1597 | */ |
| 1598 | public void onDestroyView() { |
| 1599 | mCalled = true; |
| 1600 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1601 | |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1602 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1603 | * Called when the fragment is no longer in use. This is called |
| 1604 | * after {@link #onStop()} and before {@link #onDetach()}. |
| 1605 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1606 | public void onDestroy() { |
| 1607 | mCalled = true; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1608 | //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager |
| 1609 | // + " mLoaderManager=" + mLoaderManager); |
| 1610 | if (!mCheckedForLoaderManager) { |
| 1611 | mCheckedForLoaderManager = true; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1612 | mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1613 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1614 | if (mLoaderManager != null) { |
| 1615 | mLoaderManager.doDestroy(); |
| 1616 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1617 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1618 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1619 | /** |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1620 | * Called by the fragment manager once this fragment has been removed, |
| 1621 | * so that we don't have any left-over state if the application decides |
| 1622 | * to re-use the instance. This only clears state that the framework |
| 1623 | * internally manages, not things the application sets. |
| 1624 | */ |
| 1625 | void initState() { |
| 1626 | mIndex = -1; |
| 1627 | mWho = null; |
| 1628 | mAdded = false; |
| 1629 | mRemoving = false; |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1630 | mFromLayout = false; |
| 1631 | mInLayout = false; |
| 1632 | mRestored = false; |
| 1633 | mBackStackNesting = 0; |
| 1634 | mFragmentManager = null; |
Tim Kilbourn | 7071786 | 2014-02-28 01:00:06 -0800 | [diff] [blame] | 1635 | mChildFragmentManager = null; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1636 | mHost = null; |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1637 | mFragmentId = 0; |
| 1638 | mContainerId = 0; |
| 1639 | mTag = null; |
| 1640 | mHidden = false; |
| 1641 | mDetached = false; |
| 1642 | mRetaining = false; |
| 1643 | mLoaderManager = null; |
| 1644 | mLoadersStarted = false; |
| 1645 | mCheckedForLoaderManager = false; |
| 1646 | } |
| 1647 | |
| 1648 | /** |
Felipe Leme | b144457 | 2015-11-03 17:39:07 -0800 | [diff] [blame] | 1649 | * Called when the fragment is no longer attached to its activity. This is called after |
| 1650 | * {@link #onDestroy()}, except in the cases where the fragment instance is retained across |
| 1651 | * Activity re-creation (see {@link #setRetainInstance(boolean)}), in which case it is called |
| 1652 | * after {@link #onStop()}. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1653 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1654 | public void onDetach() { |
| 1655 | mCalled = true; |
| 1656 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1657 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1658 | /** |
| 1659 | * Initialize the contents of the Activity's standard options menu. You |
| 1660 | * should place your menu items in to <var>menu</var>. For this method |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1661 | * to be called, you must have first called {@link #setHasOptionsMenu}. See |
| 1662 | * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu} |
| 1663 | * for more information. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1664 | * |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1665 | * @param menu The options menu in which you place your items. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1666 | * |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame] | 1667 | * @see #setHasOptionsMenu |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1668 | * @see #onPrepareOptionsMenu |
| 1669 | * @see #onOptionsItemSelected |
| 1670 | */ |
| 1671 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 1672 | } |
| 1673 | |
| 1674 | /** |
| 1675 | * Prepare the Screen's standard options menu to be displayed. This is |
| 1676 | * called right before the menu is shown, every time it is shown. You can |
| 1677 | * use this method to efficiently enable/disable items or otherwise |
| 1678 | * dynamically modify the contents. See |
| 1679 | * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu} |
| 1680 | * for more information. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1681 | * |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1682 | * @param menu The options menu as last shown or first initialized by |
| 1683 | * onCreateOptionsMenu(). |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1684 | * |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame] | 1685 | * @see #setHasOptionsMenu |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1686 | * @see #onCreateOptionsMenu |
| 1687 | */ |
| 1688 | public void onPrepareOptionsMenu(Menu menu) { |
| 1689 | } |
| 1690 | |
| 1691 | /** |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 1692 | * Called when this fragment's option menu items are no longer being |
| 1693 | * included in the overall options menu. Receiving this call means that |
| 1694 | * the menu needed to be rebuilt, but this fragment's items were not |
| 1695 | * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)} |
| 1696 | * was not called). |
| 1697 | */ |
| 1698 | public void onDestroyOptionsMenu() { |
| 1699 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1700 | |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 1701 | /** |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1702 | * This hook is called whenever an item in your options menu is selected. |
| 1703 | * The default implementation simply returns false to have the normal |
| 1704 | * processing happen (calling the item's Runnable or sending a message to |
| 1705 | * its Handler as appropriate). You can use this method for any items |
| 1706 | * for which you would like to do processing without those other |
| 1707 | * facilities. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1708 | * |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1709 | * <p>Derived classes should call through to the base class for it to |
| 1710 | * perform the default menu handling. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1711 | * |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1712 | * @param item The menu item that was selected. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1713 | * |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1714 | * @return boolean Return false to allow normal menu processing to |
| 1715 | * proceed, true to consume it here. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1716 | * |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1717 | * @see #onCreateOptionsMenu |
| 1718 | */ |
| 1719 | public boolean onOptionsItemSelected(MenuItem item) { |
| 1720 | return false; |
| 1721 | } |
| 1722 | |
| 1723 | /** |
| 1724 | * This hook is called whenever the options menu is being closed (either by the user canceling |
| 1725 | * the menu with the back/menu button, or when an item is selected). |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1726 | * |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1727 | * @param menu The options menu as last shown or first initialized by |
| 1728 | * onCreateOptionsMenu(). |
| 1729 | */ |
| 1730 | public void onOptionsMenuClosed(Menu menu) { |
| 1731 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1732 | |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1733 | /** |
| 1734 | * Called when a context menu for the {@code view} is about to be shown. |
| 1735 | * Unlike {@link #onCreateOptionsMenu}, this will be called every |
| 1736 | * time the context menu is about to be shown and should be populated for |
| 1737 | * the view (or item inside the view for {@link AdapterView} subclasses, |
| 1738 | * this can be found in the {@code menuInfo})). |
| 1739 | * <p> |
| 1740 | * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an |
| 1741 | * item has been selected. |
| 1742 | * <p> |
| 1743 | * The default implementation calls up to |
| 1744 | * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though |
| 1745 | * you can not call this implementation if you don't want that behavior. |
| 1746 | * <p> |
| 1747 | * It is not safe to hold onto the context menu after this method returns. |
| 1748 | * {@inheritDoc} |
| 1749 | */ |
| 1750 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 1751 | getActivity().onCreateContextMenu(menu, v, menuInfo); |
| 1752 | } |
| 1753 | |
| 1754 | /** |
| 1755 | * Registers a context menu to be shown for the given view (multiple views |
| 1756 | * can show the context menu). This method will set the |
| 1757 | * {@link OnCreateContextMenuListener} on the view to this fragment, so |
| 1758 | * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be |
| 1759 | * called when it is time to show the context menu. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1760 | * |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1761 | * @see #unregisterForContextMenu(View) |
| 1762 | * @param view The view that should show a context menu. |
| 1763 | */ |
| 1764 | public void registerForContextMenu(View view) { |
| 1765 | view.setOnCreateContextMenuListener(this); |
| 1766 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1767 | |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1768 | /** |
| 1769 | * Prevents a context menu to be shown for the given view. This method will |
| 1770 | * remove the {@link OnCreateContextMenuListener} on the view. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1771 | * |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1772 | * @see #registerForContextMenu(View) |
| 1773 | * @param view The view that should stop showing a context menu. |
| 1774 | */ |
| 1775 | public void unregisterForContextMenu(View view) { |
| 1776 | view.setOnCreateContextMenuListener(null); |
| 1777 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1778 | |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1779 | /** |
| 1780 | * This hook is called whenever an item in a context menu is selected. The |
| 1781 | * default implementation simply returns false to have the normal processing |
| 1782 | * happen (calling the item's Runnable or sending a message to its Handler |
| 1783 | * as appropriate). You can use this method for any items for which you |
| 1784 | * would like to do processing without those other facilities. |
| 1785 | * <p> |
| 1786 | * Use {@link MenuItem#getMenuInfo()} to get extra information set by the |
| 1787 | * View that added this menu item. |
| 1788 | * <p> |
| 1789 | * Derived classes should call through to the base class for it to perform |
| 1790 | * the default menu handling. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1791 | * |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1792 | * @param item The context menu item that was selected. |
| 1793 | * @return boolean Return false to allow normal context menu processing to |
| 1794 | * proceed, true to consume it here. |
| 1795 | */ |
| 1796 | public boolean onContextItemSelected(MenuItem item) { |
| 1797 | return false; |
| 1798 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1799 | |
| 1800 | /** |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 1801 | * When custom transitions are used with Fragments, the enter transition callback |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1802 | * is called when this Fragment is attached or detached when not popping the back stack. |
| 1803 | * |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 1804 | * @param callback Used to manipulate the shared element transitions on this Fragment |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1805 | * when added not as a pop from the back stack. |
| 1806 | */ |
George Mount | 85ea498 | 2014-09-12 14:16:18 -0700 | [diff] [blame] | 1807 | public void setEnterSharedElementCallback(SharedElementCallback callback) { |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 1808 | if (callback == null) { |
| 1809 | callback = SharedElementCallback.NULL_CALLBACK; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1810 | } |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 1811 | mEnterTransitionCallback = callback; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | /** |
George Mount | 85ea498 | 2014-09-12 14:16:18 -0700 | [diff] [blame] | 1815 | * @hide |
| 1816 | */ |
| 1817 | public void setEnterSharedElementTransitionCallback(SharedElementCallback callback) { |
| 1818 | setEnterSharedElementCallback(callback); |
| 1819 | } |
| 1820 | |
| 1821 | /** |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 1822 | * When custom transitions are used with Fragments, the exit transition callback |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1823 | * is called when this Fragment is attached or detached when popping the back stack. |
| 1824 | * |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 1825 | * @param callback Used to manipulate the shared element transitions on this Fragment |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1826 | * when added as a pop from the back stack. |
| 1827 | */ |
George Mount | 85ea498 | 2014-09-12 14:16:18 -0700 | [diff] [blame] | 1828 | public void setExitSharedElementCallback(SharedElementCallback callback) { |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 1829 | if (callback == null) { |
| 1830 | callback = SharedElementCallback.NULL_CALLBACK; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1831 | } |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 1832 | mExitTransitionCallback = callback; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /** |
George Mount | 85ea498 | 2014-09-12 14:16:18 -0700 | [diff] [blame] | 1836 | * @hide |
| 1837 | */ |
| 1838 | public void setExitSharedElementTransitionCallback(SharedElementCallback callback) { |
| 1839 | setExitSharedElementCallback(callback); |
| 1840 | } |
| 1841 | |
| 1842 | /** |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1843 | * Sets the Transition that will be used to move Views into the initial scene. The entering |
| 1844 | * Views will be those that are regular Views or ViewGroups that have |
| 1845 | * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend |
| 1846 | * {@link android.transition.Visibility} as entering is governed by changing visibility from |
| 1847 | * {@link View#INVISIBLE} to {@link View#VISIBLE}. If <code>transition</code> is null, |
| 1848 | * entering Views will remain unaffected. |
| 1849 | * |
| 1850 | * @param transition The Transition to use to move Views into the initial Scene. |
| 1851 | * @attr ref android.R.styleable#Fragment_fragmentEnterTransition |
| 1852 | */ |
| 1853 | public void setEnterTransition(Transition transition) { |
| 1854 | mEnterTransition = transition; |
| 1855 | } |
| 1856 | |
| 1857 | /** |
| 1858 | * Returns the Transition that will be used to move Views into the initial scene. The entering |
| 1859 | * Views will be those that are regular Views or ViewGroups that have |
| 1860 | * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend |
| 1861 | * {@link android.transition.Visibility} as entering is governed by changing visibility from |
| 1862 | * {@link View#INVISIBLE} to {@link View#VISIBLE}. |
| 1863 | * |
| 1864 | * @return the Transition to use to move Views into the initial Scene. |
| 1865 | * @attr ref android.R.styleable#Fragment_fragmentEnterTransition |
| 1866 | */ |
| 1867 | public Transition getEnterTransition() { |
| 1868 | return mEnterTransition; |
| 1869 | } |
| 1870 | |
| 1871 | /** |
| 1872 | * Sets the Transition that will be used to move Views out of the scene when the Fragment is |
| 1873 | * preparing to be removed, hidden, or detached because of popping the back stack. The exiting |
| 1874 | * Views will be those that are regular Views or ViewGroups that have |
| 1875 | * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend |
| 1876 | * {@link android.transition.Visibility} as entering is governed by changing visibility from |
| 1877 | * {@link View#VISIBLE} to {@link View#INVISIBLE}. If <code>transition</code> is null, |
| 1878 | * entering Views will remain unaffected. If nothing is set, the default will be to |
| 1879 | * use the same value as set in {@link #setEnterTransition(android.transition.Transition)}. |
| 1880 | * |
| 1881 | * @param transition The Transition to use to move Views out of the Scene when the Fragment |
| 1882 | * is preparing to close. |
| 1883 | * @attr ref android.R.styleable#Fragment_fragmentExitTransition |
| 1884 | */ |
| 1885 | public void setReturnTransition(Transition transition) { |
| 1886 | mReturnTransition = transition; |
| 1887 | } |
| 1888 | |
| 1889 | /** |
| 1890 | * Returns the Transition that will be used to move Views out of the scene when the Fragment is |
| 1891 | * preparing to be removed, hidden, or detached because of popping the back stack. The exiting |
| 1892 | * Views will be those that are regular Views or ViewGroups that have |
| 1893 | * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend |
| 1894 | * {@link android.transition.Visibility} as entering is governed by changing visibility from |
| 1895 | * {@link View#VISIBLE} to {@link View#INVISIBLE}. If <code>transition</code> is null, |
| 1896 | * entering Views will remain unaffected. |
| 1897 | * |
| 1898 | * @return the Transition to use to move Views out of the Scene when the Fragment |
| 1899 | * is preparing to close. |
| 1900 | * @attr ref android.R.styleable#Fragment_fragmentExitTransition |
| 1901 | */ |
| 1902 | public Transition getReturnTransition() { |
| 1903 | return mReturnTransition == USE_DEFAULT_TRANSITION ? getEnterTransition() |
| 1904 | : mReturnTransition; |
| 1905 | } |
| 1906 | |
| 1907 | /** |
| 1908 | * Sets the Transition that will be used to move Views out of the scene when the |
| 1909 | * fragment is removed, hidden, or detached when not popping the back stack. |
| 1910 | * The exiting Views will be those that are regular Views or ViewGroups that |
| 1911 | * have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend |
| 1912 | * {@link android.transition.Visibility} as exiting is governed by changing visibility |
| 1913 | * from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, the views will |
| 1914 | * remain unaffected. |
| 1915 | * |
| 1916 | * @param transition The Transition to use to move Views out of the Scene when the Fragment |
| 1917 | * is being closed not due to popping the back stack. |
| 1918 | * @attr ref android.R.styleable#Fragment_fragmentExitTransition |
| 1919 | */ |
| 1920 | public void setExitTransition(Transition transition) { |
| 1921 | mExitTransition = transition; |
| 1922 | } |
| 1923 | |
| 1924 | /** |
| 1925 | * Returns the Transition that will be used to move Views out of the scene when the |
| 1926 | * fragment is removed, hidden, or detached when not popping the back stack. |
| 1927 | * The exiting Views will be those that are regular Views or ViewGroups that |
| 1928 | * have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend |
| 1929 | * {@link android.transition.Visibility} as exiting is governed by changing visibility |
| 1930 | * from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, the views will |
| 1931 | * remain unaffected. |
| 1932 | * |
| 1933 | * @return the Transition to use to move Views out of the Scene when the Fragment |
| 1934 | * is being closed not due to popping the back stack. |
| 1935 | * @attr ref android.R.styleable#Fragment_fragmentExitTransition |
| 1936 | */ |
| 1937 | public Transition getExitTransition() { |
| 1938 | return mExitTransition; |
| 1939 | } |
| 1940 | |
| 1941 | /** |
| 1942 | * Sets the Transition that will be used to move Views in to the scene when returning due |
| 1943 | * to popping a back stack. The entering Views will be those that are regular Views |
| 1944 | * or ViewGroups that have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions |
| 1945 | * will extend {@link android.transition.Visibility} as exiting is governed by changing |
| 1946 | * visibility from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, |
| 1947 | * the views will remain unaffected. If nothing is set, the default will be to use the same |
| 1948 | * transition as {@link #setExitTransition(android.transition.Transition)}. |
| 1949 | * |
| 1950 | * @param transition The Transition to use to move Views into the scene when reentering from a |
| 1951 | * previously-started Activity. |
| 1952 | * @attr ref android.R.styleable#Fragment_fragmentReenterTransition |
| 1953 | */ |
| 1954 | public void setReenterTransition(Transition transition) { |
| 1955 | mReenterTransition = transition; |
| 1956 | } |
| 1957 | |
| 1958 | /** |
| 1959 | * Returns the Transition that will be used to move Views in to the scene when returning due |
| 1960 | * to popping a back stack. The entering Views will be those that are regular Views |
| 1961 | * or ViewGroups that have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions |
| 1962 | * will extend {@link android.transition.Visibility} as exiting is governed by changing |
| 1963 | * visibility from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, |
| 1964 | * the views will remain unaffected. If nothing is set, the default will be to use the same |
| 1965 | * transition as {@link #setExitTransition(android.transition.Transition)}. |
| 1966 | * |
| 1967 | * @return the Transition to use to move Views into the scene when reentering from a |
| 1968 | * previously-started Activity. |
| 1969 | * @attr ref android.R.styleable#Fragment_fragmentReenterTransition |
| 1970 | */ |
| 1971 | public Transition getReenterTransition() { |
| 1972 | return mReenterTransition == USE_DEFAULT_TRANSITION ? getExitTransition() |
| 1973 | : mReenterTransition; |
| 1974 | } |
| 1975 | |
| 1976 | /** |
| 1977 | * Sets the Transition that will be used for shared elements transferred into the content |
| 1978 | * Scene. Typical Transitions will affect size and location, such as |
| 1979 | * {@link android.transition.ChangeBounds}. A null |
| 1980 | * value will cause transferred shared elements to blink to the final position. |
| 1981 | * |
| 1982 | * @param transition The Transition to use for shared elements transferred into the content |
| 1983 | * Scene. |
| 1984 | * @attr ref android.R.styleable#Fragment_fragmentSharedElementEnterTransition |
| 1985 | */ |
| 1986 | public void setSharedElementEnterTransition(Transition transition) { |
| 1987 | mSharedElementEnterTransition = transition; |
| 1988 | } |
| 1989 | |
| 1990 | /** |
| 1991 | * Returns the Transition that will be used for shared elements transferred into the content |
| 1992 | * Scene. Typical Transitions will affect size and location, such as |
| 1993 | * {@link android.transition.ChangeBounds}. A null |
| 1994 | * value will cause transferred shared elements to blink to the final position. |
| 1995 | * |
| 1996 | * @return The Transition to use for shared elements transferred into the content |
| 1997 | * Scene. |
| 1998 | * @attr ref android.R.styleable#Fragment_fragmentSharedElementEnterTransition |
| 1999 | */ |
| 2000 | public Transition getSharedElementEnterTransition() { |
| 2001 | return mSharedElementEnterTransition; |
| 2002 | } |
| 2003 | |
| 2004 | /** |
| 2005 | * Sets the Transition that will be used for shared elements transferred back during a |
| 2006 | * pop of the back stack. This Transition acts in the leaving Fragment. |
| 2007 | * Typical Transitions will affect size and location, such as |
| 2008 | * {@link android.transition.ChangeBounds}. A null |
| 2009 | * value will cause transferred shared elements to blink to the final position. |
| 2010 | * If no value is set, the default will be to use the same value as |
| 2011 | * {@link #setSharedElementEnterTransition(android.transition.Transition)}. |
| 2012 | * |
| 2013 | * @param transition The Transition to use for shared elements transferred out of the content |
| 2014 | * Scene. |
| 2015 | * @attr ref android.R.styleable#Fragment_fragmentSharedElementReturnTransition |
| 2016 | */ |
| 2017 | public void setSharedElementReturnTransition(Transition transition) { |
| 2018 | mSharedElementReturnTransition = transition; |
| 2019 | } |
| 2020 | |
| 2021 | /** |
| 2022 | * Return the Transition that will be used for shared elements transferred back during a |
| 2023 | * pop of the back stack. This Transition acts in the leaving Fragment. |
| 2024 | * Typical Transitions will affect size and location, such as |
| 2025 | * {@link android.transition.ChangeBounds}. A null |
| 2026 | * value will cause transferred shared elements to blink to the final position. |
| 2027 | * If no value is set, the default will be to use the same value as |
| 2028 | * {@link #setSharedElementEnterTransition(android.transition.Transition)}. |
| 2029 | * |
| 2030 | * @return The Transition to use for shared elements transferred out of the content |
| 2031 | * Scene. |
| 2032 | * @attr ref android.R.styleable#Fragment_fragmentSharedElementReturnTransition |
| 2033 | */ |
| 2034 | public Transition getSharedElementReturnTransition() { |
| 2035 | return mSharedElementReturnTransition == USE_DEFAULT_TRANSITION ? |
| 2036 | getSharedElementEnterTransition() : mSharedElementReturnTransition; |
| 2037 | } |
| 2038 | |
| 2039 | /** |
| 2040 | * Sets whether the the exit transition and enter transition overlap or not. |
| 2041 | * When true, the enter transition will start as soon as possible. When false, the |
| 2042 | * enter transition will wait until the exit transition completes before starting. |
| 2043 | * |
| 2044 | * @param allow true to start the enter transition when possible or false to |
| 2045 | * wait until the exiting transition completes. |
| 2046 | * @attr ref android.R.styleable#Fragment_fragmentAllowEnterTransitionOverlap |
| 2047 | */ |
| 2048 | public void setAllowEnterTransitionOverlap(boolean allow) { |
| 2049 | mAllowEnterTransitionOverlap = allow; |
| 2050 | } |
| 2051 | |
| 2052 | /** |
| 2053 | * Returns whether the the exit transition and enter transition overlap or not. |
| 2054 | * When true, the enter transition will start as soon as possible. When false, the |
| 2055 | * enter transition will wait until the exit transition completes before starting. |
| 2056 | * |
| 2057 | * @return true when the enter transition should start as soon as possible or false to |
| 2058 | * when it should wait until the exiting transition completes. |
| 2059 | * @attr ref android.R.styleable#Fragment_fragmentAllowEnterTransitionOverlap |
| 2060 | */ |
| 2061 | public boolean getAllowEnterTransitionOverlap() { |
| 2062 | return (mAllowEnterTransitionOverlap == null) ? true : mAllowEnterTransitionOverlap; |
| 2063 | } |
| 2064 | |
| 2065 | /** |
| 2066 | * Sets whether the the return transition and reenter transition overlap or not. |
| 2067 | * When true, the reenter transition will start as soon as possible. When false, the |
| 2068 | * reenter transition will wait until the return transition completes before starting. |
| 2069 | * |
| 2070 | * @param allow true to start the reenter transition when possible or false to wait until the |
| 2071 | * return transition completes. |
| 2072 | * @attr ref android.R.styleable#Fragment_fragmentAllowReturnTransitionOverlap |
| 2073 | */ |
| 2074 | public void setAllowReturnTransitionOverlap(boolean allow) { |
| 2075 | mAllowReturnTransitionOverlap = allow; |
| 2076 | } |
| 2077 | |
| 2078 | /** |
| 2079 | * Returns whether the the return transition and reenter transition overlap or not. |
| 2080 | * When true, the reenter transition will start as soon as possible. When false, the |
| 2081 | * reenter transition will wait until the return transition completes before starting. |
| 2082 | * |
| 2083 | * @return true to start the reenter transition when possible or false to wait until the |
| 2084 | * return transition completes. |
| 2085 | * @attr ref android.R.styleable#Fragment_fragmentAllowReturnTransitionOverlap |
| 2086 | */ |
| 2087 | public boolean getAllowReturnTransitionOverlap() { |
| 2088 | return (mAllowReturnTransitionOverlap == null) ? true : mAllowReturnTransitionOverlap; |
| 2089 | } |
| 2090 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2091 | /** |
| 2092 | * Print the Fragments's state into the given stream. |
| 2093 | * |
| 2094 | * @param prefix Text to print at the front of each line. |
| 2095 | * @param fd The raw file descriptor that the dump is being sent to. |
| 2096 | * @param writer The PrintWriter to which you should dump your state. This will be |
| 2097 | * closed for you after you return. |
| 2098 | * @param args additional arguments to the dump request. |
| 2099 | */ |
| 2100 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 2101 | writer.print(prefix); writer.print("mFragmentId=#"); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2102 | writer.print(Integer.toHexString(mFragmentId)); |
| 2103 | writer.print(" mContainerId=#"); |
| 2104 | writer.print(Integer.toHexString(mContainerId)); |
| 2105 | writer.print(" mTag="); writer.println(mTag); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2106 | writer.print(prefix); writer.print("mState="); writer.print(mState); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2107 | writer.print(" mIndex="); writer.print(mIndex); |
| 2108 | writer.print(" mWho="); writer.print(mWho); |
| 2109 | writer.print(" mBackStackNesting="); writer.println(mBackStackNesting); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2110 | writer.print(prefix); writer.print("mAdded="); writer.print(mAdded); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2111 | writer.print(" mRemoving="); writer.print(mRemoving); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2112 | writer.print(" mFromLayout="); writer.print(mFromLayout); |
| 2113 | writer.print(" mInLayout="); writer.println(mInLayout); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2114 | writer.print(prefix); writer.print("mHidden="); writer.print(mHidden); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2115 | writer.print(" mDetached="); writer.print(mDetached); |
| 2116 | writer.print(" mMenuVisible="); writer.print(mMenuVisible); |
| 2117 | writer.print(" mHasMenu="); writer.println(mHasMenu); |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 2118 | writer.print(prefix); writer.print("mRetainInstance="); writer.print(mRetainInstance); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2119 | writer.print(" mRetaining="); writer.print(mRetaining); |
| 2120 | writer.print(" mUserVisibleHint="); writer.println(mUserVisibleHint); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2121 | if (mFragmentManager != null) { |
| 2122 | writer.print(prefix); writer.print("mFragmentManager="); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2123 | writer.println(mFragmentManager); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2124 | } |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2125 | if (mHost != null) { |
| 2126 | writer.print(prefix); writer.print("mHost="); |
| 2127 | writer.println(mHost); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2128 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2129 | if (mParentFragment != null) { |
| 2130 | writer.print(prefix); writer.print("mParentFragment="); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2131 | writer.println(mParentFragment); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2132 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2133 | if (mArguments != null) { |
| 2134 | writer.print(prefix); writer.print("mArguments="); writer.println(mArguments); |
| 2135 | } |
| 2136 | if (mSavedFragmentState != null) { |
| 2137 | writer.print(prefix); writer.print("mSavedFragmentState="); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2138 | writer.println(mSavedFragmentState); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2139 | } |
| 2140 | if (mSavedViewState != null) { |
| 2141 | writer.print(prefix); writer.print("mSavedViewState="); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2142 | writer.println(mSavedViewState); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2143 | } |
| 2144 | if (mTarget != null) { |
| 2145 | writer.print(prefix); writer.print("mTarget="); writer.print(mTarget); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2146 | writer.print(" mTargetRequestCode="); |
| 2147 | writer.println(mTargetRequestCode); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2148 | } |
| 2149 | if (mNextAnim != 0) { |
| 2150 | writer.print(prefix); writer.print("mNextAnim="); writer.println(mNextAnim); |
| 2151 | } |
| 2152 | if (mContainer != null) { |
| 2153 | writer.print(prefix); writer.print("mContainer="); writer.println(mContainer); |
| 2154 | } |
| 2155 | if (mView != null) { |
| 2156 | writer.print(prefix); writer.print("mView="); writer.println(mView); |
| 2157 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 2158 | if (mAnimatingAway != null) { |
| 2159 | writer.print(prefix); writer.print("mAnimatingAway="); writer.println(mAnimatingAway); |
| 2160 | writer.print(prefix); writer.print("mStateAfterAnimating="); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2161 | writer.println(mStateAfterAnimating); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 2162 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2163 | if (mLoaderManager != null) { |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 2164 | writer.print(prefix); writer.println("Loader Manager:"); |
| 2165 | mLoaderManager.dump(prefix + " ", fd, writer, args); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2166 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2167 | if (mChildFragmentManager != null) { |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 2168 | writer.print(prefix); writer.println("Child " + mChildFragmentManager + ":"); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2169 | mChildFragmentManager.dump(prefix + " ", fd, writer, args); |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | Fragment findFragmentByWho(String who) { |
| 2174 | if (who.equals(mWho)) { |
| 2175 | return this; |
| 2176 | } |
| 2177 | if (mChildFragmentManager != null) { |
| 2178 | return mChildFragmentManager.findFragmentByWho(who); |
| 2179 | } |
| 2180 | return null; |
| 2181 | } |
| 2182 | |
| 2183 | void instantiateChildFragmentManager() { |
| 2184 | mChildFragmentManager = new FragmentManagerImpl(); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2185 | mChildFragmentManager.attachController(mHost, new FragmentContainer() { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2186 | @Override |
Scott Kennedy | c051955 | 2015-02-11 15:33:10 -0800 | [diff] [blame] | 2187 | @Nullable |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2188 | public View onFindViewById(int id) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2189 | if (mView == null) { |
| 2190 | throw new IllegalStateException("Fragment does not have a view"); |
| 2191 | } |
| 2192 | return mView.findViewById(id); |
| 2193 | } |
George Mount | 0b26e4d | 2014-09-17 16:36:42 -0700 | [diff] [blame] | 2194 | |
| 2195 | @Override |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2196 | public boolean onHasView() { |
George Mount | 0b26e4d | 2014-09-17 16:36:42 -0700 | [diff] [blame] | 2197 | return (mView != null); |
| 2198 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2199 | }, this); |
| 2200 | } |
| 2201 | |
| 2202 | void performCreate(Bundle savedInstanceState) { |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 2203 | if (mChildFragmentManager != null) { |
| 2204 | mChildFragmentManager.noteStateNotSaved(); |
| 2205 | } |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 2206 | mState = CREATED; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2207 | mCalled = false; |
| 2208 | onCreate(savedInstanceState); |
| 2209 | if (!mCalled) { |
| 2210 | throw new SuperNotCalledException("Fragment " + this |
| 2211 | + " did not call through to super.onCreate()"); |
| 2212 | } |
| 2213 | if (savedInstanceState != null) { |
| 2214 | Parcelable p = savedInstanceState.getParcelable(Activity.FRAGMENTS_TAG); |
| 2215 | if (p != null) { |
| 2216 | if (mChildFragmentManager == null) { |
| 2217 | instantiateChildFragmentManager(); |
| 2218 | } |
| 2219 | mChildFragmentManager.restoreAllState(p, null); |
| 2220 | mChildFragmentManager.dispatchCreate(); |
| 2221 | } |
| 2222 | } |
| 2223 | } |
| 2224 | |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 2225 | View performCreateView(LayoutInflater inflater, ViewGroup container, |
| 2226 | Bundle savedInstanceState) { |
| 2227 | if (mChildFragmentManager != null) { |
| 2228 | mChildFragmentManager.noteStateNotSaved(); |
| 2229 | } |
| 2230 | return onCreateView(inflater, container, savedInstanceState); |
| 2231 | } |
| 2232 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2233 | void performActivityCreated(Bundle savedInstanceState) { |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 2234 | if (mChildFragmentManager != null) { |
| 2235 | mChildFragmentManager.noteStateNotSaved(); |
| 2236 | } |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 2237 | mState = ACTIVITY_CREATED; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2238 | mCalled = false; |
| 2239 | onActivityCreated(savedInstanceState); |
| 2240 | if (!mCalled) { |
| 2241 | throw new SuperNotCalledException("Fragment " + this |
| 2242 | + " did not call through to super.onActivityCreated()"); |
| 2243 | } |
| 2244 | if (mChildFragmentManager != null) { |
| 2245 | mChildFragmentManager.dispatchActivityCreated(); |
| 2246 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 2249 | void performStart() { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2250 | if (mChildFragmentManager != null) { |
| 2251 | mChildFragmentManager.noteStateNotSaved(); |
| 2252 | mChildFragmentManager.execPendingActions(); |
| 2253 | } |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 2254 | mState = STARTED; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2255 | mCalled = false; |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 2256 | onStart(); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2257 | if (!mCalled) { |
| 2258 | throw new SuperNotCalledException("Fragment " + this |
| 2259 | + " did not call through to super.onStart()"); |
| 2260 | } |
| 2261 | if (mChildFragmentManager != null) { |
| 2262 | mChildFragmentManager.dispatchStart(); |
| 2263 | } |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 2264 | if (mLoaderManager != null) { |
| 2265 | mLoaderManager.doReportStart(); |
| 2266 | } |
| 2267 | } |
| 2268 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2269 | void performResume() { |
| 2270 | if (mChildFragmentManager != null) { |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 2271 | mChildFragmentManager.noteStateNotSaved(); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2272 | mChildFragmentManager.execPendingActions(); |
| 2273 | } |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 2274 | mState = RESUMED; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2275 | mCalled = false; |
| 2276 | onResume(); |
| 2277 | if (!mCalled) { |
| 2278 | throw new SuperNotCalledException("Fragment " + this |
| 2279 | + " did not call through to super.onResume()"); |
| 2280 | } |
| 2281 | if (mChildFragmentManager != null) { |
| 2282 | mChildFragmentManager.dispatchResume(); |
| 2283 | mChildFragmentManager.execPendingActions(); |
| 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | void performConfigurationChanged(Configuration newConfig) { |
| 2288 | onConfigurationChanged(newConfig); |
| 2289 | if (mChildFragmentManager != null) { |
| 2290 | mChildFragmentManager.dispatchConfigurationChanged(newConfig); |
| 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | void performLowMemory() { |
| 2295 | onLowMemory(); |
| 2296 | if (mChildFragmentManager != null) { |
| 2297 | mChildFragmentManager.dispatchLowMemory(); |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | void performTrimMemory(int level) { |
| 2302 | onTrimMemory(level); |
| 2303 | if (mChildFragmentManager != null) { |
| 2304 | mChildFragmentManager.dispatchTrimMemory(level); |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | boolean performCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 2309 | boolean show = false; |
| 2310 | if (!mHidden) { |
| 2311 | if (mHasMenu && mMenuVisible) { |
| 2312 | show = true; |
| 2313 | onCreateOptionsMenu(menu, inflater); |
| 2314 | } |
| 2315 | if (mChildFragmentManager != null) { |
| 2316 | show |= mChildFragmentManager.dispatchCreateOptionsMenu(menu, inflater); |
| 2317 | } |
| 2318 | } |
| 2319 | return show; |
| 2320 | } |
| 2321 | |
| 2322 | boolean performPrepareOptionsMenu(Menu menu) { |
| 2323 | boolean show = false; |
| 2324 | if (!mHidden) { |
| 2325 | if (mHasMenu && mMenuVisible) { |
| 2326 | show = true; |
| 2327 | onPrepareOptionsMenu(menu); |
| 2328 | } |
| 2329 | if (mChildFragmentManager != null) { |
| 2330 | show |= mChildFragmentManager.dispatchPrepareOptionsMenu(menu); |
| 2331 | } |
| 2332 | } |
| 2333 | return show; |
| 2334 | } |
| 2335 | |
| 2336 | boolean performOptionsItemSelected(MenuItem item) { |
| 2337 | if (!mHidden) { |
| 2338 | if (mHasMenu && mMenuVisible) { |
| 2339 | if (onOptionsItemSelected(item)) { |
| 2340 | return true; |
| 2341 | } |
| 2342 | } |
| 2343 | if (mChildFragmentManager != null) { |
| 2344 | if (mChildFragmentManager.dispatchOptionsItemSelected(item)) { |
| 2345 | return true; |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | return false; |
| 2350 | } |
| 2351 | |
| 2352 | boolean performContextItemSelected(MenuItem item) { |
| 2353 | if (!mHidden) { |
| 2354 | if (onContextItemSelected(item)) { |
| 2355 | return true; |
| 2356 | } |
| 2357 | if (mChildFragmentManager != null) { |
| 2358 | if (mChildFragmentManager.dispatchContextItemSelected(item)) { |
| 2359 | return true; |
| 2360 | } |
| 2361 | } |
| 2362 | } |
| 2363 | return false; |
| 2364 | } |
| 2365 | |
| 2366 | void performOptionsMenuClosed(Menu menu) { |
| 2367 | if (!mHidden) { |
| 2368 | if (mHasMenu && mMenuVisible) { |
| 2369 | onOptionsMenuClosed(menu); |
| 2370 | } |
| 2371 | if (mChildFragmentManager != null) { |
| 2372 | mChildFragmentManager.dispatchOptionsMenuClosed(menu); |
| 2373 | } |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | void performSaveInstanceState(Bundle outState) { |
| 2378 | onSaveInstanceState(outState); |
| 2379 | if (mChildFragmentManager != null) { |
| 2380 | Parcelable p = mChildFragmentManager.saveAllState(); |
| 2381 | if (p != null) { |
| 2382 | outState.putParcelable(Activity.FRAGMENTS_TAG, p); |
| 2383 | } |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | void performPause() { |
| 2388 | if (mChildFragmentManager != null) { |
| 2389 | mChildFragmentManager.dispatchPause(); |
| 2390 | } |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 2391 | mState = STARTED; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2392 | mCalled = false; |
| 2393 | onPause(); |
| 2394 | if (!mCalled) { |
| 2395 | throw new SuperNotCalledException("Fragment " + this |
| 2396 | + " did not call through to super.onPause()"); |
| 2397 | } |
| 2398 | } |
| 2399 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 2400 | void performStop() { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2401 | if (mChildFragmentManager != null) { |
| 2402 | mChildFragmentManager.dispatchStop(); |
| 2403 | } |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 2404 | mState = STOPPED; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2405 | mCalled = false; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 2406 | onStop(); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2407 | if (!mCalled) { |
| 2408 | throw new SuperNotCalledException("Fragment " + this |
| 2409 | + " did not call through to super.onStop()"); |
| 2410 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2411 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 2412 | if (mLoadersStarted) { |
| 2413 | mLoadersStarted = false; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 2414 | if (!mCheckedForLoaderManager) { |
| 2415 | mCheckedForLoaderManager = true; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2416 | mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 2417 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 2418 | if (mLoaderManager != null) { |
Todd Kennedy | 2ee19c7f | 2015-10-19 15:31:51 -0700 | [diff] [blame] | 2419 | if (mHost.getRetainLoaders()) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 2420 | mLoaderManager.doRetain(); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2421 | } else { |
| 2422 | mLoaderManager.doStop(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 2423 | } |
| 2424 | } |
| 2425 | } |
| 2426 | } |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 2427 | |
| 2428 | void performDestroyView() { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2429 | if (mChildFragmentManager != null) { |
| 2430 | mChildFragmentManager.dispatchDestroyView(); |
| 2431 | } |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 2432 | mState = CREATED; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2433 | mCalled = false; |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 2434 | onDestroyView(); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2435 | if (!mCalled) { |
| 2436 | throw new SuperNotCalledException("Fragment " + this |
| 2437 | + " did not call through to super.onDestroyView()"); |
| 2438 | } |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 2439 | if (mLoaderManager != null) { |
| 2440 | mLoaderManager.doReportNextStart(); |
| 2441 | } |
| 2442 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2443 | |
| 2444 | void performDestroy() { |
| 2445 | if (mChildFragmentManager != null) { |
| 2446 | mChildFragmentManager.dispatchDestroy(); |
| 2447 | } |
Adam Powell | d1d4d9c | 2016-01-12 10:11:42 -0800 | [diff] [blame] | 2448 | mState = INITIALIZING; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2449 | mCalled = false; |
| 2450 | onDestroy(); |
| 2451 | if (!mCalled) { |
| 2452 | throw new SuperNotCalledException("Fragment " + this |
| 2453 | + " did not call through to super.onDestroy()"); |
| 2454 | } |
| 2455 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 2456 | |
| 2457 | private static Transition loadTransition(Context context, TypedArray typedArray, |
| 2458 | Transition currentValue, Transition defaultValue, int id) { |
| 2459 | if (currentValue != defaultValue) { |
| 2460 | return currentValue; |
| 2461 | } |
| 2462 | int transitionId = typedArray.getResourceId(id, 0); |
| 2463 | Transition transition = defaultValue; |
| 2464 | if (transitionId != 0 && transitionId != com.android.internal.R.transition.no_transition) { |
| 2465 | TransitionInflater inflater = TransitionInflater.from(context); |
| 2466 | transition = inflater.inflateTransition(transitionId); |
| 2467 | if (transition instanceof TransitionSet && |
| 2468 | ((TransitionSet)transition).getTransitionCount() == 0) { |
| 2469 | transition = null; |
| 2470 | } |
| 2471 | } |
| 2472 | return transition; |
| 2473 | } |
| 2474 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2475 | } |