The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 19 | import android.annotation.CallSuper; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 20 | import android.annotation.DrawableRes; |
| 21 | import android.annotation.IdRes; |
| 22 | import android.annotation.IntDef; |
| 23 | import android.annotation.LayoutRes; |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 24 | import android.annotation.NonNull; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 25 | import android.annotation.Nullable; |
| 26 | import android.annotation.StyleRes; |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 27 | import android.os.PersistableBundle; |
Adam Powell | cfbe9be | 2013-11-06 14:58:58 -0800 | [diff] [blame] | 28 | import android.transition.Scene; |
Adam Powell | cfbe9be | 2013-11-06 14:58:58 -0800 | [diff] [blame] | 29 | import android.transition.TransitionManager; |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 30 | import android.util.ArrayMap; |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 31 | import android.util.SuperNotCalledException; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 32 | import android.widget.Toolbar; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 33 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 34 | import com.android.internal.app.IVoiceInteractor; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 35 | import com.android.internal.app.WindowDecorActionBar; |
| 36 | import com.android.internal.app.ToolbarActionBar; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 37 | |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 38 | import android.annotation.SystemApi; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 39 | import android.app.admin.DevicePolicyManager; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 40 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.ComponentName; |
| 42 | import android.content.ContentResolver; |
| 43 | import android.content.Context; |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 44 | import android.content.CursorLoader; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 45 | import android.content.IIntentSender; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 46 | import android.content.Intent; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 47 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.content.SharedPreferences; |
| 49 | import android.content.pm.ActivityInfo; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 50 | import android.content.pm.PackageManager; |
| 51 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.content.res.Configuration; |
| 53 | import android.content.res.Resources; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 54 | import android.content.res.TypedArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.database.Cursor; |
| 56 | import android.graphics.Bitmap; |
| 57 | import android.graphics.Canvas; |
| 58 | import android.graphics.drawable.Drawable; |
| 59 | import android.media.AudioManager; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 60 | import android.media.session.MediaController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.net.Uri; |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 62 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.os.Handler; |
| 65 | import android.os.IBinder; |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 66 | import android.os.Looper; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 67 | import android.os.Parcelable; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 68 | import android.os.RemoteException; |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 69 | import android.os.StrictMode; |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 70 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import android.text.Selection; |
| 72 | import android.text.SpannableStringBuilder; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 73 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | import android.text.method.TextKeyListener; |
| 75 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.util.EventLog; |
| 77 | import android.util.Log; |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 78 | import android.util.PrintWriterPrinter; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 79 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.util.SparseArray; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 81 | import android.view.ActionMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | import android.view.ContextMenu; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 83 | import android.view.ContextMenu.ContextMenuInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.view.ContextThemeWrapper; |
| 85 | import android.view.KeyEvent; |
| 86 | import android.view.LayoutInflater; |
| 87 | import android.view.Menu; |
| 88 | import android.view.MenuInflater; |
| 89 | import android.view.MenuItem; |
| 90 | import android.view.MotionEvent; |
Jorim Jaggi | b10e33f | 2015-02-04 21:57:40 +0100 | [diff] [blame] | 91 | import android.view.PhoneWindow; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.view.View; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 93 | import android.view.View.OnCreateContextMenuListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | import android.view.ViewGroup; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 95 | import android.view.ViewGroup.LayoutParams; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | import android.view.ViewManager; |
| 97 | import android.view.Window; |
| 98 | import android.view.WindowManager; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 99 | import android.view.WindowManagerGlobal; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 100 | import android.view.accessibility.AccessibilityEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.widget.AdapterView; |
| 102 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 103 | import java.io.FileDescriptor; |
| 104 | import java.io.PrintWriter; |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 105 | import java.lang.annotation.Retention; |
| 106 | import java.lang.annotation.RetentionPolicy; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 107 | import java.util.ArrayList; |
| 108 | import java.util.HashMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * An activity is a single, focused thing that the user can do. Almost all |
| 112 | * activities interact with the user, so the Activity class takes care of |
| 113 | * creating a window for you in which you can place your UI with |
| 114 | * {@link #setContentView}. While activities are often presented to the user |
| 115 | * as full-screen windows, they can also be used in other ways: as floating |
| 116 | * windows (via a theme with {@link android.R.attr#windowIsFloating} set) |
| 117 | * or embedded inside of another activity (using {@link ActivityGroup}). |
| 118 | * |
| 119 | * There are two methods almost all subclasses of Activity will implement: |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 120 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | * <ul> |
| 122 | * <li> {@link #onCreate} is where you initialize your activity. Most |
| 123 | * importantly, here you will usually call {@link #setContentView(int)} |
| 124 | * with a layout resource defining your UI, and using {@link #findViewById} |
| 125 | * to retrieve the widgets in that UI that you need to interact with |
| 126 | * programmatically. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 127 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | * <li> {@link #onPause} is where you deal with the user leaving your |
| 129 | * activity. Most importantly, any changes made by the user should at this |
| 130 | * point be committed (usually to the |
| 131 | * {@link android.content.ContentProvider} holding the data). |
| 132 | * </ul> |
| 133 | * |
| 134 | * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all |
| 135 | * activity classes must have a corresponding |
| 136 | * {@link android.R.styleable#AndroidManifestActivity <activity>} |
| 137 | * declaration in their package's <code>AndroidManifest.xml</code>.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 138 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | * <p>Topics covered here: |
| 140 | * <ol> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 141 | * <li><a href="#Fragments">Fragments</a> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | * <li><a href="#ActivityLifecycle">Activity Lifecycle</a> |
| 143 | * <li><a href="#ConfigurationChanges">Configuration Changes</a> |
| 144 | * <li><a href="#StartingActivities">Starting Activities and Getting Results</a> |
| 145 | * <li><a href="#SavingPersistentState">Saving Persistent State</a> |
| 146 | * <li><a href="#Permissions">Permissions</a> |
| 147 | * <li><a href="#ProcessLifecycle">Process Lifecycle</a> |
| 148 | * </ol> |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 149 | * |
| 150 | * <div class="special reference"> |
| 151 | * <h3>Developer Guides</h3> |
| 152 | * <p>The Activity class is an important part of an application's overall lifecycle, |
| 153 | * and the way activities are launched and put together is a fundamental |
| 154 | * part of the platform's application model. For a detailed perspective on the structure of an |
| 155 | * Android application and how activities behave, please read the |
| 156 | * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and |
| 157 | * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a> |
| 158 | * developer guides.</p> |
| 159 | * |
| 160 | * <p>You can also find a detailed discussion about how to create activities in the |
| 161 | * <a href="{@docRoot}guide/topics/fundamentals/activities.html">Activities</a> |
| 162 | * developer guide.</p> |
| 163 | * </div> |
| 164 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 165 | * <a name="Fragments"></a> |
| 166 | * <h3>Fragments</h3> |
| 167 | * |
| 168 | * <p>Starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}, Activity |
| 169 | * implementations can make use of the {@link Fragment} class to better |
| 170 | * modularize their code, build more sophisticated user interfaces for larger |
| 171 | * screens, and help scale their application between small and large screens. |
| 172 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | * <a name="ActivityLifecycle"></a> |
| 174 | * <h3>Activity Lifecycle</h3> |
| 175 | * |
| 176 | * <p>Activities in the system are managed as an <em>activity stack</em>. |
| 177 | * When a new activity is started, it is placed on the top of the stack |
| 178 | * and becomes the running activity -- the previous activity always remains |
| 179 | * below it in the stack, and will not come to the foreground again until |
| 180 | * the new activity exits.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 181 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | * <p>An activity has essentially four states:</p> |
| 183 | * <ul> |
| 184 | * <li> If an activity in the foreground of the screen (at the top of |
| 185 | * the stack), |
| 186 | * it is <em>active</em> or <em>running</em>. </li> |
| 187 | * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 188 | * or transparent activity has focus on top of your activity), it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | * is <em>paused</em>. A paused activity is completely alive (it |
| 190 | * maintains all state and member information and remains attached to |
| 191 | * the window manager), but can be killed by the system in extreme |
| 192 | * low memory situations. |
| 193 | * <li>If an activity is completely obscured by another activity, |
| 194 | * it is <em>stopped</em>. It still retains all state and member information, |
| 195 | * however, it is no longer visible to the user so its window is hidden |
| 196 | * and it will often be killed by the system when memory is needed |
| 197 | * elsewhere.</li> |
| 198 | * <li>If an activity is paused or stopped, the system can drop the activity |
| 199 | * from memory by either asking it to finish, or simply killing its |
| 200 | * process. When it is displayed again to the user, it must be |
| 201 | * completely restarted and restored to its previous state.</li> |
| 202 | * </ul> |
| 203 | * |
| 204 | * <p>The following diagram shows the important state paths of an Activity. |
| 205 | * The square rectangles represent callback methods you can implement to |
| 206 | * perform operations when the Activity moves between states. The colored |
| 207 | * ovals are major states the Activity can be in.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 208 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | * <p><img src="../../../images/activity_lifecycle.png" |
| 210 | * alt="State diagram for an Android Activity Lifecycle." border="0" /></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 211 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | * <p>There are three key loops you may be interested in monitoring within your |
| 213 | * activity: |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 214 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | * <ul> |
| 216 | * <li>The <b>entire lifetime</b> of an activity happens between the first call |
| 217 | * to {@link android.app.Activity#onCreate} through to a single final call |
| 218 | * to {@link android.app.Activity#onDestroy}. An activity will do all setup |
| 219 | * of "global" state in onCreate(), and release all remaining resources in |
| 220 | * onDestroy(). For example, if it has a thread running in the background |
| 221 | * to download data from the network, it may create that thread in onCreate() |
| 222 | * and then stop the thread in onDestroy(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 223 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | * <li>The <b>visible lifetime</b> of an activity happens between a call to |
| 225 | * {@link android.app.Activity#onStart} until a corresponding call to |
| 226 | * {@link android.app.Activity#onStop}. During this time the user can see the |
| 227 | * activity on-screen, though it may not be in the foreground and interacting |
| 228 | * with the user. Between these two methods you can maintain resources that |
| 229 | * are needed to show the activity to the user. For example, you can register |
| 230 | * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 231 | * that impact your UI, and unregister it in onStop() when the user no |
| 232 | * longer sees what you are displaying. The onStart() and onStop() methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | * can be called multiple times, as the activity becomes visible and hidden |
| 234 | * to the user. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 235 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | * <li>The <b>foreground lifetime</b> of an activity happens between a call to |
| 237 | * {@link android.app.Activity#onResume} until a corresponding call to |
| 238 | * {@link android.app.Activity#onPause}. During this time the activity is |
| 239 | * in front of all other activities and interacting with the user. An activity |
| 240 | * can frequently go between the resumed and paused states -- for example when |
| 241 | * the device goes to sleep, when an activity result is delivered, when a new |
| 242 | * intent is delivered -- so the code in these methods should be fairly |
| 243 | * lightweight. |
| 244 | * </ul> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 245 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | * <p>The entire lifecycle of an activity is defined by the following |
| 247 | * Activity methods. All of these are hooks that you can override |
| 248 | * to do appropriate work when the activity changes state. All |
| 249 | * activities will implement {@link android.app.Activity#onCreate} |
| 250 | * to do their initial setup; many will also implement |
| 251 | * {@link android.app.Activity#onPause} to commit changes to data and |
| 252 | * otherwise prepare to stop interacting with the user. You should always |
| 253 | * call up to your superclass when implementing these methods.</p> |
| 254 | * |
| 255 | * </p> |
| 256 | * <pre class="prettyprint"> |
| 257 | * public class Activity extends ApplicationContext { |
| 258 | * protected void onCreate(Bundle savedInstanceState); |
| 259 | * |
| 260 | * protected void onStart(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 261 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | * protected void onRestart(); |
| 263 | * |
| 264 | * protected void onResume(); |
| 265 | * |
| 266 | * protected void onPause(); |
| 267 | * |
| 268 | * protected void onStop(); |
| 269 | * |
| 270 | * protected void onDestroy(); |
| 271 | * } |
| 272 | * </pre> |
| 273 | * |
| 274 | * <p>In general the movement through an activity's lifecycle looks like |
| 275 | * this:</p> |
| 276 | * |
| 277 | * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> |
| 278 | * <colgroup align="left" span="3" /> |
| 279 | * <colgroup align="left" /> |
| 280 | * <colgroup align="center" /> |
| 281 | * <colgroup align="center" /> |
| 282 | * |
| 283 | * <thead> |
| 284 | * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr> |
| 285 | * </thead> |
| 286 | * |
| 287 | * <tbody> |
| 288 | * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onCreate onCreate()}</th> |
| 289 | * <td>Called when the activity is first created. |
| 290 | * This is where you should do all of your normal static set up: |
| 291 | * create views, bind data to lists, etc. This method also |
| 292 | * provides you with a Bundle containing the activity's previously |
| 293 | * frozen state, if there was one. |
| 294 | * <p>Always followed by <code>onStart()</code>.</td> |
| 295 | * <td align="center">No</td> |
| 296 | * <td align="center"><code>onStart()</code></td> |
| 297 | * </tr> |
| 298 | * |
| 299 | * <tr><td rowspan="5" style="border-left: none; border-right: none;"> </td> |
| 300 | * <th colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</th> |
| 301 | * <td>Called after your activity has been stopped, prior to it being |
| 302 | * started again. |
| 303 | * <p>Always followed by <code>onStart()</code></td> |
| 304 | * <td align="center">No</td> |
| 305 | * <td align="center"><code>onStart()</code></td> |
| 306 | * </tr> |
| 307 | * |
| 308 | * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStart onStart()}</th> |
| 309 | * <td>Called when the activity is becoming visible to the user. |
| 310 | * <p>Followed by <code>onResume()</code> if the activity comes |
| 311 | * to the foreground, or <code>onStop()</code> if it becomes hidden.</td> |
| 312 | * <td align="center">No</td> |
| 313 | * <td align="center"><code>onResume()</code> or <code>onStop()</code></td> |
| 314 | * </tr> |
| 315 | * |
| 316 | * <tr><td rowspan="2" style="border-left: none;"> </td> |
| 317 | * <th align="left" border="0">{@link android.app.Activity#onResume onResume()}</th> |
| 318 | * <td>Called when the activity will start |
| 319 | * interacting with the user. At this point your activity is at |
| 320 | * the top of the activity stack, with user input going to it. |
| 321 | * <p>Always followed by <code>onPause()</code>.</td> |
| 322 | * <td align="center">No</td> |
| 323 | * <td align="center"><code>onPause()</code></td> |
| 324 | * </tr> |
| 325 | * |
| 326 | * <tr><th align="left" border="0">{@link android.app.Activity#onPause onPause()}</th> |
| 327 | * <td>Called when the system is about to start resuming a previous |
| 328 | * activity. This is typically used to commit unsaved changes to |
| 329 | * persistent data, stop animations and other things that may be consuming |
| 330 | * CPU, etc. Implementations of this method must be very quick because |
| 331 | * the next activity will not be resumed until this method returns. |
| 332 | * <p>Followed by either <code>onResume()</code> if the activity |
| 333 | * returns back to the front, or <code>onStop()</code> if it becomes |
| 334 | * invisible to the user.</td> |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 335 | * <td align="center"><font color="#800000"><strong>Pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB}</strong></font></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | * <td align="center"><code>onResume()</code> or<br> |
| 337 | * <code>onStop()</code></td> |
| 338 | * </tr> |
| 339 | * |
| 340 | * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</th> |
| 341 | * <td>Called when the activity is no longer visible to the user, because |
| 342 | * another activity has been resumed and is covering this one. This |
| 343 | * may happen either because a new activity is being started, an existing |
| 344 | * one is being brought in front of this one, or this one is being |
| 345 | * destroyed. |
| 346 | * <p>Followed by either <code>onRestart()</code> if |
| 347 | * this activity is coming back to interact with the user, or |
| 348 | * <code>onDestroy()</code> if this activity is going away.</td> |
| 349 | * <td align="center"><font color="#800000"><strong>Yes</strong></font></td> |
| 350 | * <td align="center"><code>onRestart()</code> or<br> |
| 351 | * <code>onDestroy()</code></td> |
| 352 | * </tr> |
| 353 | * |
| 354 | * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onDestroy onDestroy()}</th> |
| 355 | * <td>The final call you receive before your |
| 356 | * activity is destroyed. This can happen either because the |
| 357 | * activity is finishing (someone called {@link Activity#finish} on |
| 358 | * it, or because the system is temporarily destroying this |
| 359 | * instance of the activity to save space. You can distinguish |
| 360 | * between these two scenarios with the {@link |
| 361 | * Activity#isFinishing} method.</td> |
| 362 | * <td align="center"><font color="#800000"><strong>Yes</strong></font></td> |
| 363 | * <td align="center"><em>nothing</em></td> |
| 364 | * </tr> |
| 365 | * </tbody> |
| 366 | * </table> |
| 367 | * |
| 368 | * <p>Note the "Killable" column in the above table -- for those methods that |
| 369 | * are marked as being killable, after that method returns the process hosting the |
Kris Giesing | 8b6216d | 2014-12-16 17:23:15 -0800 | [diff] [blame] | 370 | * activity may be killed by the system <em>at any time</em> without another line |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | * of its code being executed. Because of this, you should use the |
| 372 | * {@link #onPause} method to write any persistent data (such as user edits) |
| 373 | * to storage. In addition, the method |
| 374 | * {@link #onSaveInstanceState(Bundle)} is called before placing the activity |
| 375 | * in such a background state, allowing you to save away any dynamic instance |
| 376 | * state in your activity into the given Bundle, to be later received in |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 377 | * {@link #onCreate} if the activity needs to be re-created. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | * See the <a href="#ProcessLifecycle">Process Lifecycle</a> |
| 379 | * section for more information on how the lifecycle of a process is tied |
| 380 | * to the activities it is hosting. Note that it is important to save |
| 381 | * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState} |
Daisuke Miyakawa | 5c40f3f | 2011-02-15 13:24:36 -0800 | [diff] [blame] | 382 | * because the latter is not part of the lifecycle callbacks, so will not |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | * be called in every situation as described in its documentation.</p> |
| 384 | * |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 385 | * <p class="note">Be aware that these semantics will change slightly between |
| 386 | * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 387 | * vs. those targeting prior platforms. Starting with Honeycomb, an application |
| 388 | * is not in the killable state until its {@link #onStop} has returned. This |
| 389 | * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be |
| 390 | * safely called after {@link #onPause()} and allows and application to safely |
| 391 | * wait until {@link #onStop()} to save persistent state.</p> |
| 392 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | * <p>For those methods that are not marked as being killable, the activity's |
| 394 | * process will not be killed by the system starting from the time the method |
| 395 | * is called and continuing after it returns. Thus an activity is in the killable |
| 396 | * state, for example, between after <code>onPause()</code> to the start of |
| 397 | * <code>onResume()</code>.</p> |
| 398 | * |
| 399 | * <a name="ConfigurationChanges"></a> |
| 400 | * <h3>Configuration Changes</h3> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 401 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | * <p>If the configuration of the device (as defined by the |
| 403 | * {@link Configuration Resources.Configuration} class) changes, |
| 404 | * then anything displaying a user interface will need to update to match that |
| 405 | * configuration. Because Activity is the primary mechanism for interacting |
| 406 | * with the user, it includes special support for handling configuration |
| 407 | * changes.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 408 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | * <p>Unless you specify otherwise, a configuration change (such as a change |
| 410 | * in screen orientation, language, input devices, etc) will cause your |
| 411 | * current activity to be <em>destroyed</em>, going through the normal activity |
| 412 | * lifecycle process of {@link #onPause}, |
| 413 | * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity |
| 414 | * had been in the foreground or visible to the user, once {@link #onDestroy} is |
| 415 | * called in that instance then a new instance of the activity will be |
| 416 | * created, with whatever savedInstanceState the previous instance had generated |
| 417 | * from {@link #onSaveInstanceState}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 418 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | * <p>This is done because any application resource, |
| 420 | * including layout files, can change based on any configuration value. Thus |
| 421 | * the only safe way to handle a configuration change is to re-retrieve all |
| 422 | * resources, including layouts, drawables, and strings. Because activities |
| 423 | * must already know how to save their state and re-create themselves from |
| 424 | * that state, this is a convenient way to have an activity restart itself |
| 425 | * with a new configuration.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 426 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | * <p>In some special cases, you may want to bypass restarting of your |
| 428 | * activity based on one or more types of configuration changes. This is |
| 429 | * done with the {@link android.R.attr#configChanges android:configChanges} |
| 430 | * attribute in its manifest. For any types of configuration changes you say |
| 431 | * that you handle there, you will receive a call to your current activity's |
| 432 | * {@link #onConfigurationChanged} method instead of being restarted. If |
| 433 | * a configuration change involves any that you do not handle, however, the |
| 434 | * activity will still be restarted and {@link #onConfigurationChanged} |
| 435 | * will not be called.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 436 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | * <a name="StartingActivities"></a> |
| 438 | * <h3>Starting Activities and Getting Results</h3> |
| 439 | * |
| 440 | * <p>The {@link android.app.Activity#startActivity} |
| 441 | * method is used to start a |
| 442 | * new activity, which will be placed at the top of the activity stack. It |
| 443 | * takes a single argument, an {@link android.content.Intent Intent}, |
| 444 | * which describes the activity |
| 445 | * to be executed.</p> |
| 446 | * |
| 447 | * <p>Sometimes you want to get a result back from an activity when it |
| 448 | * ends. For example, you may start an activity that lets the user pick |
| 449 | * a person in a list of contacts; when it ends, it returns the person |
| 450 | * that was selected. To do this, you call the |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 451 | * {@link android.app.Activity#startActivityForResult(Intent, int)} |
| 452 | * version with a second integer parameter identifying the call. The result |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | * will come back through your {@link android.app.Activity#onActivityResult} |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 454 | * method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | * |
| 456 | * <p>When an activity exits, it can call |
| 457 | * {@link android.app.Activity#setResult(int)} |
| 458 | * to return data back to its parent. It must always supply a result code, |
| 459 | * which can be the standard results RESULT_CANCELED, RESULT_OK, or any |
| 460 | * custom values starting at RESULT_FIRST_USER. In addition, it can optionally |
| 461 | * return back an Intent containing any additional data it wants. All of this |
| 462 | * information appears back on the |
| 463 | * parent's <code>Activity.onActivityResult()</code>, along with the integer |
| 464 | * identifier it originally supplied.</p> |
| 465 | * |
| 466 | * <p>If a child activity fails for any reason (such as crashing), the parent |
| 467 | * activity will receive a result with the code RESULT_CANCELED.</p> |
| 468 | * |
| 469 | * <pre class="prettyprint"> |
| 470 | * public class MyActivity extends Activity { |
| 471 | * ... |
| 472 | * |
| 473 | * static final int PICK_CONTACT_REQUEST = 0; |
| 474 | * |
Michael Wright | 5438e4e | 2014-07-25 14:05:42 -0700 | [diff] [blame] | 475 | * public boolean onKeyDown(int keyCode, KeyEvent event) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { |
| 477 | * // When the user center presses, let them pick a contact. |
| 478 | * startActivityForResult( |
| 479 | * new Intent(Intent.ACTION_PICK, |
| 480 | * new Uri("content://contacts")), |
| 481 | * PICK_CONTACT_REQUEST); |
| 482 | * return true; |
| 483 | * } |
| 484 | * return false; |
| 485 | * } |
| 486 | * |
| 487 | * protected void onActivityResult(int requestCode, int resultCode, |
| 488 | * Intent data) { |
| 489 | * if (requestCode == PICK_CONTACT_REQUEST) { |
| 490 | * if (resultCode == RESULT_OK) { |
| 491 | * // A contact was picked. Here we will just display it |
| 492 | * // to the user. |
| 493 | * startActivity(new Intent(Intent.ACTION_VIEW, data)); |
| 494 | * } |
| 495 | * } |
| 496 | * } |
| 497 | * } |
| 498 | * </pre> |
| 499 | * |
| 500 | * <a name="SavingPersistentState"></a> |
| 501 | * <h3>Saving Persistent State</h3> |
| 502 | * |
| 503 | * <p>There are generally two kinds of persistent state than an activity |
| 504 | * will deal with: shared document-like data (typically stored in a SQLite |
| 505 | * database using a {@linkplain android.content.ContentProvider content provider}) |
| 506 | * and internal state such as user preferences.</p> |
| 507 | * |
| 508 | * <p>For content provider data, we suggest that activities use a |
| 509 | * "edit in place" user model. That is, any edits a user makes are effectively |
| 510 | * made immediately without requiring an additional confirmation step. |
| 511 | * Supporting this model is generally a simple matter of following two rules:</p> |
| 512 | * |
| 513 | * <ul> |
| 514 | * <li> <p>When creating a new document, the backing database entry or file for |
| 515 | * it is created immediately. For example, if the user chooses to write |
| 516 | * a new e-mail, a new entry for that e-mail is created as soon as they |
| 517 | * start entering data, so that if they go to any other activity after |
| 518 | * that point this e-mail will now appear in the list of drafts.</p> |
| 519 | * <li> <p>When an activity's <code>onPause()</code> method is called, it should |
| 520 | * commit to the backing content provider or file any changes the user |
| 521 | * has made. This ensures that those changes will be seen by any other |
| 522 | * activity that is about to run. You will probably want to commit |
| 523 | * your data even more aggressively at key times during your |
| 524 | * activity's lifecycle: for example before starting a new |
| 525 | * activity, before finishing your own activity, when the user |
| 526 | * switches between input fields, etc.</p> |
| 527 | * </ul> |
| 528 | * |
| 529 | * <p>This model is designed to prevent data loss when a user is navigating |
| 530 | * between activities, and allows the system to safely kill an activity (because |
| 531 | * system resources are needed somewhere else) at any time after it has been |
| 532 | * paused. Note this implies |
| 533 | * that the user pressing BACK from your activity does <em>not</em> |
| 534 | * mean "cancel" -- it means to leave the activity with its current contents |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 535 | * saved away. Canceling edits in an activity must be provided through |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | * some other mechanism, such as an explicit "revert" or "undo" option.</p> |
| 537 | * |
| 538 | * <p>See the {@linkplain android.content.ContentProvider content package} for |
| 539 | * more information about content providers. These are a key aspect of how |
| 540 | * different activities invoke and propagate data between themselves.</p> |
| 541 | * |
| 542 | * <p>The Activity class also provides an API for managing internal persistent state |
| 543 | * associated with an activity. This can be used, for example, to remember |
| 544 | * the user's preferred initial display in a calendar (day view or week view) |
| 545 | * or the user's default home page in a web browser.</p> |
| 546 | * |
| 547 | * <p>Activity persistent state is managed |
| 548 | * with the method {@link #getPreferences}, |
| 549 | * allowing you to retrieve and |
| 550 | * modify a set of name/value pairs associated with the activity. To use |
| 551 | * preferences that are shared across multiple application components |
| 552 | * (activities, receivers, services, providers), you can use the underlying |
| 553 | * {@link Context#getSharedPreferences Context.getSharedPreferences()} method |
| 554 | * to retrieve a preferences |
| 555 | * object stored under a specific name. |
| 556 | * (Note that it is not possible to share settings data across application |
| 557 | * packages -- for that you will need a content provider.)</p> |
| 558 | * |
| 559 | * <p>Here is an excerpt from a calendar activity that stores the user's |
| 560 | * preferred view mode in its persistent settings:</p> |
| 561 | * |
| 562 | * <pre class="prettyprint"> |
| 563 | * public class CalendarActivity extends Activity { |
| 564 | * ... |
| 565 | * |
| 566 | * static final int DAY_VIEW_MODE = 0; |
| 567 | * static final int WEEK_VIEW_MODE = 1; |
| 568 | * |
| 569 | * private SharedPreferences mPrefs; |
| 570 | * private int mCurViewMode; |
| 571 | * |
| 572 | * protected void onCreate(Bundle savedInstanceState) { |
| 573 | * super.onCreate(savedInstanceState); |
| 574 | * |
| 575 | * SharedPreferences mPrefs = getSharedPreferences(); |
Quddus Chong | 3fa98cb | 2012-04-13 11:17:24 -0700 | [diff] [blame] | 576 | * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | * } |
| 578 | * |
| 579 | * protected void onPause() { |
| 580 | * super.onPause(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 581 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | * SharedPreferences.Editor ed = mPrefs.edit(); |
| 583 | * ed.putInt("view_mode", mCurViewMode); |
| 584 | * ed.commit(); |
| 585 | * } |
| 586 | * } |
| 587 | * </pre> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 588 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | * <a name="Permissions"></a> |
| 590 | * <h3>Permissions</h3> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 591 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | * <p>The ability to start a particular Activity can be enforced when it is |
| 593 | * declared in its |
| 594 | * manifest's {@link android.R.styleable#AndroidManifestActivity <activity>} |
| 595 | * tag. By doing so, other applications will need to declare a corresponding |
| 596 | * {@link android.R.styleable#AndroidManifestUsesPermission <uses-permission>} |
| 597 | * element in their own manifest to be able to start that activity. |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 598 | * |
| 599 | * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION |
| 600 | * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION |
| 601 | * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the |
| 602 | * Activity access to the specific URIs in the Intent. Access will remain |
| 603 | * until the Activity has finished (it will remain across the hosting |
| 604 | * process being killed and other temporary destruction). As of |
| 605 | * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity |
| 606 | * was already created and a new Intent is being delivered to |
| 607 | * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added |
| 608 | * to the existing ones it holds. |
| 609 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a> |
| 611 | * document for more information on permissions and security in general. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 612 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | * <a name="ProcessLifecycle"></a> |
| 614 | * <h3>Process Lifecycle</h3> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 615 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | * <p>The Android system attempts to keep application process around for as |
| 617 | * long as possible, but eventually will need to remove old processes when |
| 618 | * memory runs low. As described in <a href="#ActivityLifecycle">Activity |
| 619 | * Lifecycle</a>, the decision about which process to remove is intimately |
| 620 | * tied to the state of the user's interaction with it. In general, there |
| 621 | * are four states a process can be in based on the activities running in it, |
| 622 | * listed here in order of importance. The system will kill less important |
| 623 | * processes (the last ones) before it resorts to killing more important |
| 624 | * processes (the first ones). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 625 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | * <ol> |
| 627 | * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen |
| 628 | * that the user is currently interacting with) is considered the most important. |
| 629 | * Its process will only be killed as a last resort, if it uses more memory |
| 630 | * than is available on the device. Generally at this point the device has |
| 631 | * reached a memory paging state, so this is required in order to keep the user |
| 632 | * interface responsive. |
| 633 | * <li> <p>A <b>visible activity</b> (an activity that is visible to the user |
| 634 | * but not in the foreground, such as one sitting behind a foreground dialog) |
| 635 | * is considered extremely important and will not be killed unless that is |
| 636 | * required to keep the foreground activity running. |
| 637 | * <li> <p>A <b>background activity</b> (an activity that is not visible to |
| 638 | * the user and has been paused) is no longer critical, so the system may |
| 639 | * safely kill its process to reclaim memory for other foreground or |
| 640 | * visible processes. If its process needs to be killed, when the user navigates |
| 641 | * back to the activity (making it visible on the screen again), its |
| 642 | * {@link #onCreate} method will be called with the savedInstanceState it had previously |
| 643 | * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same |
| 644 | * state as the user last left it. |
| 645 | * <li> <p>An <b>empty process</b> is one hosting no activities or other |
| 646 | * application components (such as {@link Service} or |
| 647 | * {@link android.content.BroadcastReceiver} classes). These are killed very |
| 648 | * quickly by the system as memory becomes low. For this reason, any |
| 649 | * background operation you do outside of an activity must be executed in the |
| 650 | * context of an activity BroadcastReceiver or Service to ensure that the system |
| 651 | * knows it needs to keep your process around. |
| 652 | * </ol> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 653 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | * <p>Sometimes an Activity may need to do a long-running operation that exists |
| 655 | * independently of the activity lifecycle itself. An example may be a camera |
| 656 | * application that allows you to upload a picture to a web site. The upload |
| 657 | * may take a long time, and the application should allow the user to leave |
| 658 | * the application will it is executing. To accomplish this, your Activity |
| 659 | * should start a {@link Service} in which the upload takes place. This allows |
| 660 | * the system to properly prioritize your process (considering it to be more |
| 661 | * important than other non-visible applications) for the duration of the |
| 662 | * upload, independent of whether the original activity is paused, stopped, |
| 663 | * or finished. |
| 664 | */ |
| 665 | public class Activity extends ContextThemeWrapper |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 666 | implements LayoutInflater.Factory2, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | Window.Callback, KeyEvent.Callback, |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 668 | OnCreateContextMenuListener, ComponentCallbacks2, |
| 669 | Window.OnWindowDismissedCallback { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | private static final String TAG = "Activity"; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 671 | private static final boolean DEBUG_LIFECYCLE = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | |
| 673 | /** Standard activity result: operation canceled. */ |
| 674 | public static final int RESULT_CANCELED = 0; |
| 675 | /** Standard activity result: operation succeeded. */ |
| 676 | public static final int RESULT_OK = -1; |
| 677 | /** Start of user-defined activity results. */ |
| 678 | public static final int RESULT_FIRST_USER = 1; |
| 679 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 680 | static final String FRAGMENTS_TAG = "android:fragments"; |
| 681 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState"; |
| 683 | private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds"; |
| 684 | private static final String SAVED_DIALOGS_TAG = "android:savedDialogs"; |
| 685 | private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_"; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 686 | private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 688 | private static class ManagedDialog { |
| 689 | Dialog mDialog; |
| 690 | Bundle mArgs; |
| 691 | } |
| 692 | private SparseArray<ManagedDialog> mManagedDialogs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | |
| 694 | // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called. |
| 695 | private Instrumentation mInstrumentation; |
| 696 | private IBinder mToken; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 697 | private int mIdent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | /*package*/ String mEmbeddedID; |
| 699 | private Application mApplication; |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 700 | /*package*/ Intent mIntent; |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 701 | /*package*/ String mReferrer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 702 | private ComponentName mComponent; |
| 703 | /*package*/ ActivityInfo mActivityInfo; |
| 704 | /*package*/ ActivityThread mMainThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | Activity mParent; |
| 706 | boolean mCalled; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 707 | boolean mCheckedForLoaderManager; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 708 | boolean mLoadersStarted; |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 709 | /*package*/ boolean mResumed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | private boolean mStopped; |
| 711 | boolean mFinished; |
| 712 | boolean mStartedActivity; |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 713 | private boolean mDestroyed; |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 714 | private boolean mDoReportFullyDrawn = true; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 715 | /** true if the activity is going through a transient pause */ |
| 716 | /*package*/ boolean mTemporaryPause = false; |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 717 | /** true if the activity is being destroyed in order to recreate it with a new configuration */ |
| 718 | /*package*/ boolean mChangingConfigurations = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | /*package*/ int mConfigChangeFlags; |
| 720 | /*package*/ Configuration mCurrentConfig; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 721 | private SearchManager mSearchManager; |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 722 | private MenuInflater mMenuInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 723 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 724 | static final class NonConfigurationInstances { |
| 725 | Object activity; |
| 726 | HashMap<String, Object> children; |
| 727 | ArrayList<Fragment> fragments; |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 728 | ArrayMap<String, LoaderManagerImpl> loaders; |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 729 | VoiceInteractor voiceInteractor; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 730 | } |
| 731 | /* package */ NonConfigurationInstances mLastNonConfigurationInstances; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 732 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | private Window mWindow; |
| 734 | |
| 735 | private WindowManager mWindowManager; |
| 736 | /*package*/ View mDecor = null; |
| 737 | /*package*/ boolean mWindowAdded = false; |
| 738 | /*package*/ boolean mVisibleFromServer = false; |
| 739 | /*package*/ boolean mVisibleFromClient = true; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 740 | /*package*/ ActionBar mActionBar = null; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 741 | private boolean mEnableDefaultActionBarUp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 743 | private VoiceInteractor mVoiceInteractor; |
| 744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | private CharSequence mTitle; |
| 746 | private int mTitleColor = 0; |
| 747 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 748 | final FragmentManagerImpl mFragments = new FragmentManagerImpl(); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 749 | final FragmentContainer mContainer = new FragmentContainer() { |
| 750 | @Override |
Scott Kennedy | c051955 | 2015-02-11 15:33:10 -0800 | [diff] [blame] | 751 | @Nullable |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 752 | public View findViewById(int id) { |
| 753 | return Activity.this.findViewById(id); |
| 754 | } |
George Mount | 0b26e4d | 2014-09-17 16:36:42 -0700 | [diff] [blame] | 755 | @Override |
| 756 | public boolean hasView() { |
| 757 | Window window = Activity.this.getWindow(); |
| 758 | return (window != null && window.peekDecorView() != null); |
| 759 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 760 | }; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 761 | |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 762 | // Most recent call to requestVisibleBehind(). |
| 763 | boolean mVisibleBehind; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 764 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 765 | ArrayMap<String, LoaderManagerImpl> mAllLoaderManagers; |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 766 | LoaderManagerImpl mLoaderManager; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 767 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | private static final class ManagedCursor { |
| 769 | ManagedCursor(Cursor cursor) { |
| 770 | mCursor = cursor; |
| 771 | mReleased = false; |
| 772 | mUpdated = false; |
| 773 | } |
| 774 | |
| 775 | private final Cursor mCursor; |
| 776 | private boolean mReleased; |
| 777 | private boolean mUpdated; |
| 778 | } |
| 779 | private final ArrayList<ManagedCursor> mManagedCursors = |
| 780 | new ArrayList<ManagedCursor>(); |
| 781 | |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 782 | // protected by synchronized (this) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | int mResultCode = RESULT_CANCELED; |
| 784 | Intent mResultData = null; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 785 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 786 | private TranslucentConversionListener mTranslucentCallback; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 787 | private boolean mChangeCanvasToTranslucent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | |
| 789 | private boolean mTitleReady = false; |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 790 | private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | |
| 792 | private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE; |
| 793 | private SpannableStringBuilder mDefaultKeySsb = null; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 794 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused}; |
| 796 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 797 | @SuppressWarnings("unused") |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 798 | private final Object mInstanceTracker = StrictMode.trackActivity(this); |
| 799 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | private Thread mUiThread; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 801 | final Handler mHandler = new Handler(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 802 | |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 803 | ActivityTransitionState mActivityTransitionState = new ActivityTransitionState(); |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 804 | SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK; |
| 805 | SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | /** Return the intent that started this activity. */ |
| 808 | public Intent getIntent() { |
| 809 | return mIntent; |
| 810 | } |
| 811 | |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 812 | /** |
| 813 | * Change the intent returned by {@link #getIntent}. This holds a |
| 814 | * reference to the given intent; it does not copy it. Often used in |
| 815 | * conjunction with {@link #onNewIntent}. |
| 816 | * |
| 817 | * @param newIntent The new Intent object to return from getIntent |
| 818 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | * @see #getIntent |
| 820 | * @see #onNewIntent |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 821 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | public void setIntent(Intent newIntent) { |
| 823 | mIntent = newIntent; |
| 824 | } |
| 825 | |
| 826 | /** Return the application that owns this activity. */ |
| 827 | public final Application getApplication() { |
| 828 | return mApplication; |
| 829 | } |
| 830 | |
| 831 | /** Is this activity embedded inside of another activity? */ |
| 832 | public final boolean isChild() { |
| 833 | return mParent != null; |
| 834 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 835 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | /** Return the parent activity if this view is an embedded child. */ |
| 837 | public final Activity getParent() { |
| 838 | return mParent; |
| 839 | } |
| 840 | |
| 841 | /** Retrieve the window manager for showing custom windows. */ |
| 842 | public WindowManager getWindowManager() { |
| 843 | return mWindowManager; |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * Retrieve the current {@link android.view.Window} for the activity. |
| 848 | * This can be used to directly access parts of the Window API that |
| 849 | * are not available through Activity/Screen. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 850 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | * @return Window The current window, or null if the activity is not |
| 852 | * visual. |
| 853 | */ |
| 854 | public Window getWindow() { |
| 855 | return mWindow; |
| 856 | } |
| 857 | |
| 858 | /** |
Mark Doliner | 9525f2a | 2014-01-02 11:17:47 -0800 | [diff] [blame] | 859 | * Return the LoaderManager for this activity, creating it if needed. |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 860 | */ |
| 861 | public LoaderManager getLoaderManager() { |
| 862 | if (mLoaderManager != null) { |
| 863 | return mLoaderManager; |
| 864 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 865 | mCheckedForLoaderManager = true; |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 866 | mLoaderManager = getLoaderManager("(root)", mLoadersStarted, true); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 867 | return mLoaderManager; |
| 868 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 869 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 870 | LoaderManagerImpl getLoaderManager(String who, boolean started, boolean create) { |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 871 | if (mAllLoaderManagers == null) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 872 | mAllLoaderManagers = new ArrayMap<String, LoaderManagerImpl>(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 873 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 874 | LoaderManagerImpl lm = mAllLoaderManagers.get(who); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 875 | if (lm == null) { |
| 876 | if (create) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 877 | lm = new LoaderManagerImpl(who, this, started); |
| 878 | mAllLoaderManagers.put(who, lm); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 879 | } |
| 880 | } else { |
| 881 | lm.updateActivity(this); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 882 | } |
| 883 | return lm; |
| 884 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 885 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 886 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | * Calls {@link android.view.Window#getCurrentFocus} on the |
| 888 | * Window of this Activity to return the currently focused view. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 889 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | * @return View The current View with focus or null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 891 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | * @see #getWindow |
| 893 | * @see android.view.Window#getCurrentFocus |
| 894 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 895 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 896 | public View getCurrentFocus() { |
| 897 | return mWindow != null ? mWindow.getCurrentFocus() : null; |
| 898 | } |
| 899 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | /** |
| 901 | * Called when the activity is starting. This is where most initialization |
| 902 | * should go: calling {@link #setContentView(int)} to inflate the |
| 903 | * activity's UI, using {@link #findViewById} to programmatically interact |
| 904 | * with widgets in the UI, calling |
| 905 | * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve |
| 906 | * cursors for data being displayed, etc. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 907 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | * <p>You can call {@link #finish} from within this function, in |
| 909 | * which case onDestroy() will be immediately called without any of the rest |
| 910 | * of the activity lifecycle ({@link #onStart}, {@link #onResume}, |
| 911 | * {@link #onPause}, etc) executing. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 912 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | * <p><em>Derived classes must call through to the super class's |
| 914 | * implementation of this method. If they do not, an exception will be |
| 915 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 916 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | * @param savedInstanceState If the activity is being re-initialized after |
| 918 | * previously being shut down then this Bundle contains the data it most |
| 919 | * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 920 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | * @see #onStart |
| 922 | * @see #onSaveInstanceState |
| 923 | * @see #onRestoreInstanceState |
| 924 | * @see #onPostCreate |
| 925 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 926 | @CallSuper |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 927 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 928 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 929 | if (mLastNonConfigurationInstances != null) { |
| 930 | mAllLoaderManagers = mLastNonConfigurationInstances.loaders; |
| 931 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 932 | if (mActivityInfo.parentActivityName != null) { |
| 933 | if (mActionBar == null) { |
| 934 | mEnableDefaultActionBarUp = true; |
| 935 | } else { |
| 936 | mActionBar.setDefaultDisplayHomeAsUpEnabled(true); |
| 937 | } |
| 938 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 939 | if (savedInstanceState != null) { |
| 940 | Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG); |
| 941 | mFragments.restoreAllState(p, mLastNonConfigurationInstances != null |
| 942 | ? mLastNonConfigurationInstances.fragments : null); |
| 943 | } |
| 944 | mFragments.dispatchCreate(); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 945 | getApplication().dispatchActivityCreated(this, savedInstanceState); |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 946 | if (mVoiceInteractor != null) { |
| 947 | mVoiceInteractor.attachActivity(this); |
| 948 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | mCalled = true; |
| 950 | } |
| 951 | |
| 952 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 953 | * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with |
Craig Mautner | 43e52ed | 2014-06-16 17:18:52 -0700 | [diff] [blame] | 954 | * the attribute {@link android.R.attr#persistableMode} set to |
| 955 | * <code>persistAcrossReboots</code>. |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 956 | * |
| 957 | * @param savedInstanceState if the activity is being re-initialized after |
| 958 | * previously being shut down then this Bundle contains the data it most |
| 959 | * recently supplied in {@link #onSaveInstanceState}. |
| 960 | * <b><i>Note: Otherwise it is null.</i></b> |
| 961 | * @param persistentState if the activity is being re-initialized after |
| 962 | * previously being shut down or powered off then this Bundle contains the data it most |
| 963 | * recently supplied to outPersistentState in {@link #onSaveInstanceState}. |
| 964 | * <b><i>Note: Otherwise it is null.</i></b> |
| 965 | * |
| 966 | * @see #onCreate(android.os.Bundle) |
| 967 | * @see #onStart |
| 968 | * @see #onSaveInstanceState |
| 969 | * @see #onRestoreInstanceState |
| 970 | * @see #onPostCreate |
| 971 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 972 | public void onCreate(@Nullable Bundle savedInstanceState, |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 973 | @Nullable PersistableBundle persistentState) { |
| 974 | onCreate(savedInstanceState); |
| 975 | } |
| 976 | |
| 977 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | * The hook for {@link ActivityThread} to restore the state of this activity. |
| 979 | * |
| 980 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} and |
| 981 | * {@link #restoreManagedDialogs(android.os.Bundle)}. |
| 982 | * |
| 983 | * @param savedInstanceState contains the saved state |
| 984 | */ |
| 985 | final void performRestoreInstanceState(Bundle savedInstanceState) { |
| 986 | onRestoreInstanceState(savedInstanceState); |
| 987 | restoreManagedDialogs(savedInstanceState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 991 | * The hook for {@link ActivityThread} to restore the state of this activity. |
| 992 | * |
| 993 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} and |
| 994 | * {@link #restoreManagedDialogs(android.os.Bundle)}. |
| 995 | * |
| 996 | * @param savedInstanceState contains the saved state |
| 997 | * @param persistentState contains the persistable saved state |
| 998 | */ |
| 999 | final void performRestoreInstanceState(Bundle savedInstanceState, |
| 1000 | PersistableBundle persistentState) { |
| 1001 | onRestoreInstanceState(savedInstanceState, persistentState); |
| 1002 | if (savedInstanceState != null) { |
| 1003 | restoreManagedDialogs(savedInstanceState); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | * This method is called after {@link #onStart} when the activity is |
| 1009 | * being re-initialized from a previously saved state, given here in |
Mike LeBeau | 305de9d | 2010-03-11 09:21:08 -0800 | [diff] [blame] | 1010 | * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | * to restore their state, but it is sometimes convenient to do it here |
| 1012 | * after all of the initialization has been done or to allow subclasses to |
| 1013 | * decide whether to use your default implementation. The default |
| 1014 | * implementation of this method performs a restore of any view state that |
| 1015 | * had previously been frozen by {@link #onSaveInstanceState}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1016 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | * <p>This method is called between {@link #onStart} and |
| 1018 | * {@link #onPostCreate}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1019 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1021 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | * @see #onCreate |
| 1023 | * @see #onPostCreate |
| 1024 | * @see #onResume |
| 1025 | * @see #onSaveInstanceState |
| 1026 | */ |
| 1027 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 1028 | if (mWindow != null) { |
| 1029 | Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG); |
| 1030 | if (windowState != null) { |
| 1031 | mWindow.restoreHierarchyState(windowState); |
| 1032 | } |
| 1033 | } |
| 1034 | } |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1035 | |
| 1036 | /** |
| 1037 | * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities |
Craig Mautner | 43e52ed | 2014-06-16 17:18:52 -0700 | [diff] [blame] | 1038 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1039 | * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed |
| 1040 | * came from the restored PersistableBundle first |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1041 | * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}. |
| 1042 | * |
| 1043 | * <p>This method is called between {@link #onStart} and |
| 1044 | * {@link #onPostCreate}. |
| 1045 | * |
| 1046 | * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called. |
| 1047 | * |
| 1048 | * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}. |
| 1049 | * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}. |
| 1050 | * |
| 1051 | * @see #onRestoreInstanceState(Bundle) |
| 1052 | * @see #onCreate |
| 1053 | * @see #onPostCreate |
| 1054 | * @see #onResume |
| 1055 | * @see #onSaveInstanceState |
| 1056 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1057 | public void onRestoreInstanceState(Bundle savedInstanceState, |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1058 | PersistableBundle persistentState) { |
| 1059 | if (savedInstanceState != null) { |
| 1060 | onRestoreInstanceState(savedInstanceState); |
| 1061 | } |
| 1062 | } |
| 1063 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | /** |
| 1065 | * Restore the state of any saved managed dialogs. |
| 1066 | * |
| 1067 | * @param savedInstanceState The bundle to restore from. |
| 1068 | */ |
| 1069 | private void restoreManagedDialogs(Bundle savedInstanceState) { |
| 1070 | final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG); |
| 1071 | if (b == null) { |
| 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY); |
| 1076 | final int numDialogs = ids.length; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1077 | mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1078 | for (int i = 0; i < numDialogs; i++) { |
| 1079 | final Integer dialogId = ids[i]; |
| 1080 | Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId)); |
| 1081 | if (dialogState != null) { |
Romain Guy | e35c235 | 2009-06-19 13:18:12 -0700 | [diff] [blame] | 1082 | // Calling onRestoreInstanceState() below will invoke dispatchOnCreate |
| 1083 | // so tell createDialog() not to do it, otherwise we get an exception |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1084 | final ManagedDialog md = new ManagedDialog(); |
| 1085 | md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId)); |
| 1086 | md.mDialog = createDialog(dialogId, dialogState, md.mArgs); |
| 1087 | if (md.mDialog != null) { |
| 1088 | mManagedDialogs.put(dialogId, md); |
| 1089 | onPrepareDialog(dialogId, md.mDialog, md.mArgs); |
| 1090 | md.mDialog.onRestoreInstanceState(dialogState); |
| 1091 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1096 | private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) { |
| 1097 | final Dialog dialog = onCreateDialog(dialogId, args); |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1098 | if (dialog == null) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1099 | return null; |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1100 | } |
Romain Guy | 6de4aed | 2009-07-08 10:54:45 -0700 | [diff] [blame] | 1101 | dialog.dispatchOnCreate(state); |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1102 | return dialog; |
| 1103 | } |
| 1104 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1105 | private static String savedDialogKeyFor(int key) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | return SAVED_DIALOG_KEY_PREFIX + key; |
| 1107 | } |
| 1108 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1109 | private static String savedDialogArgsKeyFor(int key) { |
| 1110 | return SAVED_DIALOG_ARGS_KEY_PREFIX + key; |
| 1111 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | |
| 1113 | /** |
| 1114 | * Called when activity start-up is complete (after {@link #onStart} |
| 1115 | * and {@link #onRestoreInstanceState} have been called). Applications will |
| 1116 | * generally not implement this method; it is intended for system |
| 1117 | * classes to do final initialization after application code has run. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1118 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | * <p><em>Derived classes must call through to the super class's |
| 1120 | * implementation of this method. If they do not, an exception will be |
| 1121 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1122 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | * @param savedInstanceState If the activity is being re-initialized after |
| 1124 | * previously being shut down then this Bundle contains the data it most |
| 1125 | * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b> |
| 1126 | * @see #onCreate |
| 1127 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1128 | @CallSuper |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1129 | protected void onPostCreate(@Nullable Bundle savedInstanceState) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | if (!isChild()) { |
| 1131 | mTitleReady = true; |
| 1132 | onTitleChanged(getTitle(), getTitleColor()); |
| 1133 | } |
| 1134 | mCalled = true; |
| 1135 | } |
| 1136 | |
| 1137 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1138 | * This is the same as {@link #onPostCreate(Bundle)} but is called for activities |
Craig Mautner | 43e52ed | 2014-06-16 17:18:52 -0700 | [diff] [blame] | 1139 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1140 | * <code>persistAcrossReboots</code>. |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1141 | * |
| 1142 | * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState} |
| 1143 | * @param persistentState The data caming from the PersistableBundle first |
| 1144 | * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}. |
| 1145 | * |
| 1146 | * @see #onCreate |
| 1147 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1148 | public void onPostCreate(@Nullable Bundle savedInstanceState, |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1149 | @Nullable PersistableBundle persistentState) { |
| 1150 | onPostCreate(savedInstanceState); |
| 1151 | } |
| 1152 | |
| 1153 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1154 | * Called after {@link #onCreate} — or after {@link #onRestart} when |
| 1155 | * the activity had been stopped, but is now again being displayed to the |
John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 1156 | * user. It will be followed by {@link #onResume}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1157 | * |
| 1158 | * <p><em>Derived classes must call through to the super class's |
| 1159 | * implementation of this method. If they do not, an exception will be |
| 1160 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1161 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | * @see #onCreate |
| 1163 | * @see #onStop |
| 1164 | * @see #onResume |
| 1165 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1166 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | protected void onStart() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1168 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | mCalled = true; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1170 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1171 | if (!mLoadersStarted) { |
| 1172 | mLoadersStarted = true; |
| 1173 | if (mLoaderManager != null) { |
| 1174 | mLoaderManager.doStart(); |
| 1175 | } else if (!mCheckedForLoaderManager) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 1176 | mLoaderManager = getLoaderManager("(root)", mLoadersStarted, false); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1177 | } |
| 1178 | mCheckedForLoaderManager = true; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1179 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1180 | |
| 1181 | getApplication().dispatchActivityStarted(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | /** |
| 1185 | * Called after {@link #onStop} when the current activity is being |
| 1186 | * re-displayed to the user (the user has navigated back to it). It will |
| 1187 | * be followed by {@link #onStart} and then {@link #onResume}. |
| 1188 | * |
| 1189 | * <p>For activities that are using raw {@link Cursor} objects (instead of |
| 1190 | * creating them through |
| 1191 | * {@link #managedQuery(android.net.Uri , String[], String, String[], String)}, |
| 1192 | * this is usually the place |
| 1193 | * where the cursor should be requeried (because you had deactivated it in |
| 1194 | * {@link #onStop}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1195 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | * <p><em>Derived classes must call through to the super class's |
| 1197 | * implementation of this method. If they do not, an exception will be |
| 1198 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1199 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | * @see #onStop |
| 1201 | * @see #onStart |
| 1202 | * @see #onResume |
| 1203 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1204 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1205 | protected void onRestart() { |
| 1206 | mCalled = true; |
| 1207 | } |
| 1208 | |
| 1209 | /** |
| 1210 | * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or |
| 1211 | * {@link #onPause}, for your activity to start interacting with the user. |
| 1212 | * This is a good place to begin animations, open exclusive-access devices |
| 1213 | * (such as the camera), etc. |
| 1214 | * |
| 1215 | * <p>Keep in mind that onResume is not the best indicator that your activity |
| 1216 | * is visible to the user; a system window such as the keyguard may be in |
| 1217 | * front. Use {@link #onWindowFocusChanged} to know for certain that your |
| 1218 | * activity is visible to the user (for example, to resume a game). |
| 1219 | * |
| 1220 | * <p><em>Derived classes must call through to the super class's |
| 1221 | * implementation of this method. If they do not, an exception will be |
| 1222 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1223 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | * @see #onRestoreInstanceState |
| 1225 | * @see #onRestart |
| 1226 | * @see #onPostResume |
| 1227 | * @see #onPause |
| 1228 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1229 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | protected void onResume() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1231 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1232 | getApplication().dispatchActivityResumed(this); |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 1233 | mActivityTransitionState.onResume(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1234 | mCalled = true; |
| 1235 | } |
| 1236 | |
| 1237 | /** |
| 1238 | * Called when activity resume is complete (after {@link #onResume} has |
| 1239 | * been called). Applications will generally not implement this method; |
| 1240 | * it is intended for system classes to do final setup after application |
| 1241 | * resume code has run. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1242 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | * <p><em>Derived classes must call through to the super class's |
| 1244 | * implementation of this method. If they do not, an exception will be |
| 1245 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1246 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | * @see #onResume |
| 1248 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1249 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | protected void onPostResume() { |
| 1251 | final Window win = getWindow(); |
| 1252 | if (win != null) win.makeActive(); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 1253 | if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | mCalled = true; |
| 1255 | } |
| 1256 | |
| 1257 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 1258 | * Check whether this activity is running as part of a voice interaction with the user. |
| 1259 | * If true, it should perform its interaction with the user through the |
| 1260 | * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}. |
| 1261 | */ |
| 1262 | public boolean isVoiceInteraction() { |
| 1263 | return mVoiceInteractor != null; |
| 1264 | } |
| 1265 | |
| 1266 | /** |
| 1267 | * Retrieve the active {@link VoiceInteractor} that the user is going through to |
| 1268 | * interact with this activity. |
| 1269 | */ |
| 1270 | public VoiceInteractor getVoiceInteractor() { |
| 1271 | return mVoiceInteractor; |
| 1272 | } |
| 1273 | |
| 1274 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1275 | * This is called for activities that set launchMode to "singleTop" in |
| 1276 | * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} |
| 1277 | * flag when calling {@link #startActivity}. In either case, when the |
| 1278 | * activity is re-launched while at the top of the activity stack instead |
| 1279 | * of a new instance of the activity being started, onNewIntent() will be |
| 1280 | * called on the existing instance with the Intent that was used to |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1281 | * re-launch it. |
| 1282 | * |
| 1283 | * <p>An activity will always be paused before receiving a new intent, so |
| 1284 | * you can count on {@link #onResume} being called after this method. |
| 1285 | * |
| 1286 | * <p>Note that {@link #getIntent} still returns the original Intent. You |
| 1287 | * can use {@link #setIntent} to update it to this new Intent. |
| 1288 | * |
| 1289 | * @param intent The new intent that was started for the activity. |
| 1290 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | * @see #getIntent |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1292 | * @see #setIntent |
| 1293 | * @see #onResume |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1294 | */ |
| 1295 | protected void onNewIntent(Intent intent) { |
| 1296 | } |
| 1297 | |
| 1298 | /** |
| 1299 | * The hook for {@link ActivityThread} to save the state of this activity. |
| 1300 | * |
| 1301 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} |
| 1302 | * and {@link #saveManagedDialogs(android.os.Bundle)}. |
| 1303 | * |
| 1304 | * @param outState The bundle to save the state to. |
| 1305 | */ |
| 1306 | final void performSaveInstanceState(Bundle outState) { |
| 1307 | onSaveInstanceState(outState); |
| 1308 | saveManagedDialogs(outState); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 1309 | mActivityTransitionState.saveState(outState); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1310 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1314 | * The hook for {@link ActivityThread} to save the state of this activity. |
| 1315 | * |
| 1316 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} |
| 1317 | * and {@link #saveManagedDialogs(android.os.Bundle)}. |
| 1318 | * |
| 1319 | * @param outState The bundle to save the state to. |
| 1320 | * @param outPersistentState The bundle to save persistent state to. |
| 1321 | */ |
| 1322 | final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { |
| 1323 | onSaveInstanceState(outState, outPersistentState); |
| 1324 | saveManagedDialogs(outState); |
| 1325 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState + |
| 1326 | ", " + outPersistentState); |
| 1327 | } |
| 1328 | |
| 1329 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1330 | * Called to retrieve per-instance state from an activity before being killed |
| 1331 | * so that the state can be restored in {@link #onCreate} or |
| 1332 | * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method |
| 1333 | * will be passed to both). |
| 1334 | * |
| 1335 | * <p>This method is called before an activity may be killed so that when it |
| 1336 | * comes back some time in the future it can restore its state. For example, |
| 1337 | * if activity B is launched in front of activity A, and at some point activity |
| 1338 | * A is killed to reclaim resources, activity A will have a chance to save the |
| 1339 | * current state of its user interface via this method so that when the user |
| 1340 | * returns to activity A, the state of the user interface can be restored |
| 1341 | * via {@link #onCreate} or {@link #onRestoreInstanceState}. |
| 1342 | * |
| 1343 | * <p>Do not confuse this method with activity lifecycle callbacks such as |
| 1344 | * {@link #onPause}, which is always called when an activity is being placed |
| 1345 | * in the background or on its way to destruction, or {@link #onStop} which |
| 1346 | * is called before destruction. One example of when {@link #onPause} and |
| 1347 | * {@link #onStop} is called and not this method is when a user navigates back |
| 1348 | * from activity B to activity A: there is no need to call {@link #onSaveInstanceState} |
| 1349 | * on B because that particular instance will never be restored, so the |
| 1350 | * system avoids calling it. An example when {@link #onPause} is called and |
| 1351 | * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A: |
| 1352 | * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't |
| 1353 | * killed during the lifetime of B since the state of the user interface of |
| 1354 | * A will stay intact. |
| 1355 | * |
| 1356 | * <p>The default implementation takes care of most of the UI per-instance |
| 1357 | * state for you by calling {@link android.view.View#onSaveInstanceState()} on each |
| 1358 | * view in the hierarchy that has an id, and by saving the id of the currently |
| 1359 | * focused view (all of which is restored by the default implementation of |
| 1360 | * {@link #onRestoreInstanceState}). If you override this method to save additional |
| 1361 | * information not captured by each individual view, you will likely want to |
| 1362 | * call through to the default implementation, otherwise be prepared to save |
| 1363 | * all of the state of each view yourself. |
| 1364 | * |
| 1365 | * <p>If called, this method will occur before {@link #onStop}. There are |
| 1366 | * no guarantees about whether it will occur before or after {@link #onPause}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1367 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1368 | * @param outState Bundle in which to place your saved state. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1369 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1370 | * @see #onCreate |
| 1371 | * @see #onRestoreInstanceState |
| 1372 | * @see #onPause |
| 1373 | */ |
| 1374 | protected void onSaveInstanceState(Bundle outState) { |
| 1375 | outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState()); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1376 | Parcelable p = mFragments.saveAllState(); |
| 1377 | if (p != null) { |
| 1378 | outState.putParcelable(FRAGMENTS_TAG, p); |
| 1379 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1380 | getApplication().dispatchActivitySaveInstanceState(this, outState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1384 | * This is the same as {@link #onSaveInstanceState} but is called for activities |
Craig Mautner | 43e52ed | 2014-06-16 17:18:52 -0700 | [diff] [blame] | 1385 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1386 | * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed |
| 1387 | * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)} |
| 1388 | * the first time that this activity is restarted following the next device reboot. |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1389 | * |
| 1390 | * @param outState Bundle in which to place your saved state. |
| 1391 | * @param outPersistentState State which will be saved across reboots. |
| 1392 | * |
| 1393 | * @see #onSaveInstanceState(Bundle) |
| 1394 | * @see #onCreate |
| 1395 | * @see #onRestoreInstanceState(Bundle, PersistableBundle) |
| 1396 | * @see #onPause |
| 1397 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1398 | public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1399 | onSaveInstanceState(outState); |
| 1400 | } |
| 1401 | |
| 1402 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | * Save the state of any managed dialogs. |
| 1404 | * |
| 1405 | * @param outState place to store the saved state. |
| 1406 | */ |
| 1407 | private void saveManagedDialogs(Bundle outState) { |
| 1408 | if (mManagedDialogs == null) { |
| 1409 | return; |
| 1410 | } |
| 1411 | |
| 1412 | final int numDialogs = mManagedDialogs.size(); |
| 1413 | if (numDialogs == 0) { |
| 1414 | return; |
| 1415 | } |
| 1416 | |
| 1417 | Bundle dialogState = new Bundle(); |
| 1418 | |
| 1419 | int[] ids = new int[mManagedDialogs.size()]; |
| 1420 | |
| 1421 | // save each dialog's bundle, gather the ids |
| 1422 | for (int i = 0; i < numDialogs; i++) { |
| 1423 | final int key = mManagedDialogs.keyAt(i); |
| 1424 | ids[i] = key; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1425 | final ManagedDialog md = mManagedDialogs.valueAt(i); |
| 1426 | dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState()); |
| 1427 | if (md.mArgs != null) { |
| 1428 | dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs); |
| 1429 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids); |
| 1433 | outState.putBundle(SAVED_DIALOGS_TAG, dialogState); |
| 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | /** |
| 1438 | * Called as part of the activity lifecycle when an activity is going into |
| 1439 | * the background, but has not (yet) been killed. The counterpart to |
| 1440 | * {@link #onResume}. |
| 1441 | * |
| 1442 | * <p>When activity B is launched in front of activity A, this callback will |
| 1443 | * be invoked on A. B will not be created until A's {@link #onPause} returns, |
| 1444 | * so be sure to not do anything lengthy here. |
| 1445 | * |
| 1446 | * <p>This callback is mostly used for saving any persistent state the |
| 1447 | * activity is editing, to present a "edit in place" model to the user and |
| 1448 | * making sure nothing is lost if there are not enough resources to start |
| 1449 | * the new activity without first killing this one. This is also a good |
| 1450 | * place to do things like stop animations and other things that consume a |
Pin Ting | 14a9310 | 2012-04-25 11:27:03 +0800 | [diff] [blame] | 1451 | * noticeable amount of CPU in order to make the switch to the next activity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | * as fast as possible, or to close resources that are exclusive access |
| 1453 | * such as the camera. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1454 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1455 | * <p>In situations where the system needs more memory it may kill paused |
| 1456 | * processes to reclaim resources. Because of this, you should be sure |
| 1457 | * that all of your state is saved by the time you return from |
| 1458 | * this function. In general {@link #onSaveInstanceState} is used to save |
| 1459 | * per-instance state in the activity and this method is used to store |
| 1460 | * global persistent data (in content providers, files, etc.) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1461 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | * <p>After receiving this call you will usually receive a following call |
| 1463 | * to {@link #onStop} (after the next activity has been resumed and |
| 1464 | * displayed), however in some cases there will be a direct call back to |
| 1465 | * {@link #onResume} without going through the stopped state. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1466 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1467 | * <p><em>Derived classes must call through to the super class's |
| 1468 | * implementation of this method. If they do not, an exception will be |
| 1469 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1470 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1471 | * @see #onResume |
| 1472 | * @see #onSaveInstanceState |
| 1473 | * @see #onStop |
| 1474 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1475 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1476 | protected void onPause() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1477 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1478 | getApplication().dispatchActivityPaused(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1479 | mCalled = true; |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * Called as part of the activity lifecycle when an activity is about to go |
| 1484 | * into the background as the result of user choice. For example, when the |
| 1485 | * user presses the Home key, {@link #onUserLeaveHint} will be called, but |
| 1486 | * when an incoming phone call causes the in-call Activity to be automatically |
| 1487 | * brought to the foreground, {@link #onUserLeaveHint} will not be called on |
| 1488 | * the activity being interrupted. In cases when it is invoked, this method |
| 1489 | * is called right before the activity's {@link #onPause} callback. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1490 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | * <p>This callback and {@link #onUserInteraction} are intended to help |
| 1492 | * activities manage status bar notifications intelligently; specifically, |
| 1493 | * for helping activities determine the proper time to cancel a notfication. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1494 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | * @see #onUserInteraction() |
| 1496 | */ |
| 1497 | protected void onUserLeaveHint() { |
| 1498 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1499 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1500 | /** |
| 1501 | * Generate a new thumbnail for this activity. This method is called before |
| 1502 | * pausing the activity, and should draw into <var>outBitmap</var> the |
| 1503 | * imagery for the desired thumbnail in the dimensions of that bitmap. It |
| 1504 | * can use the given <var>canvas</var>, which is configured to draw into the |
| 1505 | * bitmap, for rendering if desired. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1506 | * |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 1507 | * <p>The default implementation returns fails and does not draw a thumbnail; |
| 1508 | * this will result in the platform creating its own thumbnail if needed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1509 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | * @param outBitmap The bitmap to contain the thumbnail. |
| 1511 | * @param canvas Can be used to render into the bitmap. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1512 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1513 | * @return Return true if you have drawn into the bitmap; otherwise after |
| 1514 | * you return it will be filled with a default thumbnail. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1515 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1516 | * @see #onCreateDescription |
| 1517 | * @see #onSaveInstanceState |
| 1518 | * @see #onPause |
| 1519 | */ |
| 1520 | public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) { |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 1521 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | /** |
| 1525 | * Generate a new description for this activity. This method is called |
| 1526 | * before pausing the activity and can, if desired, return some textual |
| 1527 | * description of its current state to be displayed to the user. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1528 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | * <p>The default implementation returns null, which will cause you to |
| 1530 | * inherit the description from the previous activity. If all activities |
| 1531 | * return null, generally the label of the top activity will be used as the |
| 1532 | * description. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1533 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1534 | * @return A description of what the user is doing. It should be short and |
| 1535 | * sweet (only a few words). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1536 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1537 | * @see #onCreateThumbnail |
| 1538 | * @see #onSaveInstanceState |
| 1539 | * @see #onPause |
| 1540 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1541 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1542 | public CharSequence onCreateDescription() { |
| 1543 | return null; |
| 1544 | } |
| 1545 | |
| 1546 | /** |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1547 | * This is called when the user is requesting an assist, to build a full |
| 1548 | * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current |
| 1549 | * application. You can override this method to place into the bundle anything |
| 1550 | * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part |
Dianne Hackborn | a83ce1d | 2015-03-11 15:16:13 -0700 | [diff] [blame] | 1551 | * of the assist Intent. |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1552 | * |
| 1553 | * <p>This function will be called after any global assist callbacks that had |
Ying Wang | 4e0eb22 | 2013-04-18 20:39:48 -0700 | [diff] [blame] | 1554 | * been registered with {@link Application#registerOnProvideAssistDataListener |
| 1555 | * Application.registerOnProvideAssistDataListener}. |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1556 | */ |
| 1557 | public void onProvideAssistData(Bundle data) { |
Dianne Hackborn | a83ce1d | 2015-03-11 15:16:13 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | /** |
| 1561 | * This is called when the user is requesting an assist, to provide references |
| 1562 | * to content related to the current activity. Before being called, the |
| 1563 | * {@code outContent} Intent is filled with the base Intent of the activity (the Intent |
| 1564 | * returned by {@link #getIntent()}). The Intent's extras are stripped of any types |
| 1565 | * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and |
| 1566 | * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and |
| 1567 | * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent. |
| 1568 | * |
| 1569 | * <p>Custom implementation may adjust the content intent to better reflect the top-level |
| 1570 | * context of the activity, and fill in its ClipData with additional content of |
| 1571 | * interest that the user is currently viewing. For example, an image gallery application |
| 1572 | * that has launched in to an activity allowing the user to swipe through pictures should |
| 1573 | * modify the intent to reference the current image they are looking it; such an |
| 1574 | * application when showing a list of pictures should add a ClipData that has |
| 1575 | * references to all of the pictures currently visible on screen.</p> |
| 1576 | * |
| 1577 | * @param outContent The assist content to return. |
| 1578 | */ |
| 1579 | public void onProvideAssistContent(AssistContent outContent) { |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1583 | * Called when you are no longer visible to the user. You will next |
| 1584 | * receive either {@link #onRestart}, {@link #onDestroy}, or nothing, |
| 1585 | * depending on later user activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1586 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | * <p>Note that this method may never be called, in low memory situations |
| 1588 | * where the system does not have enough memory to keep your activity's |
| 1589 | * process running after its {@link #onPause} method is called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1590 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | * <p><em>Derived classes must call through to the super class's |
| 1592 | * implementation of this method. If they do not, an exception will be |
| 1593 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1594 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | * @see #onRestart |
| 1596 | * @see #onResume |
| 1597 | * @see #onSaveInstanceState |
| 1598 | * @see #onDestroy |
| 1599 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1600 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1601 | protected void onStop() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1602 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 1603 | if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 1604 | mActivityTransitionState.onStop(); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1605 | getApplication().dispatchActivityStopped(this); |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 1606 | mTranslucentCallback = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | mCalled = true; |
| 1608 | } |
| 1609 | |
| 1610 | /** |
| 1611 | * Perform any final cleanup before an activity is destroyed. This can |
| 1612 | * happen either because the activity is finishing (someone called |
| 1613 | * {@link #finish} on it, or because the system is temporarily destroying |
| 1614 | * this instance of the activity to save space. You can distinguish |
| 1615 | * between these two scenarios with the {@link #isFinishing} method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1616 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1617 | * <p><em>Note: do not count on this method being called as a place for |
| 1618 | * saving data! For example, if an activity is editing data in a content |
| 1619 | * provider, those edits should be committed in either {@link #onPause} or |
| 1620 | * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to |
| 1621 | * free resources like threads that are associated with an activity, so |
| 1622 | * that a destroyed activity does not leave such things around while the |
| 1623 | * rest of its application is still running. There are situations where |
| 1624 | * the system will simply kill the activity's hosting process without |
| 1625 | * calling this method (or any others) in it, so it should not be used to |
| 1626 | * do things that are intended to remain around after the process goes |
| 1627 | * away. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1628 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1629 | * <p><em>Derived classes must call through to the super class's |
| 1630 | * implementation of this method. If they do not, an exception will be |
| 1631 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1632 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1633 | * @see #onPause |
| 1634 | * @see #onStop |
| 1635 | * @see #finish |
| 1636 | * @see #isFinishing |
| 1637 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1638 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1639 | protected void onDestroy() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1640 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | mCalled = true; |
| 1642 | |
| 1643 | // dismiss any dialogs we are managing. |
| 1644 | if (mManagedDialogs != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | final int numDialogs = mManagedDialogs.size(); |
| 1646 | for (int i = 0; i < numDialogs; i++) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1647 | final ManagedDialog md = mManagedDialogs.valueAt(i); |
| 1648 | if (md.mDialog.isShowing()) { |
| 1649 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | } |
| 1651 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1652 | mManagedDialogs = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1653 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | |
| 1655 | // close any cursors we are managing. |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 1656 | synchronized (mManagedCursors) { |
| 1657 | int numCursors = mManagedCursors.size(); |
| 1658 | for (int i = 0; i < numCursors; i++) { |
| 1659 | ManagedCursor c = mManagedCursors.get(i); |
| 1660 | if (c != null) { |
| 1661 | c.mCursor.close(); |
| 1662 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | } |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 1664 | mManagedCursors.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | } |
Amith Yamasani | 4986044 | 2010-03-17 20:54:10 -0700 | [diff] [blame] | 1666 | |
| 1667 | // Close any open search dialog |
| 1668 | if (mSearchManager != null) { |
| 1669 | mSearchManager.stopSearch(); |
| 1670 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1671 | |
| 1672 | getApplication().dispatchActivityDestroyed(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /** |
Dianne Hackborn | ebc15ef | 2013-10-09 17:36:57 -0700 | [diff] [blame] | 1676 | * Report to the system that your app is now fully drawn, purely for diagnostic |
| 1677 | * purposes (calling it does not impact the visible behavior of the activity). |
| 1678 | * This is only used to help instrument application launch times, so that the |
| 1679 | * app can report when it is fully in a usable state; without this, the only thing |
| 1680 | * the system itself can determine is the point at which the activity's window |
| 1681 | * is <em>first</em> drawn and displayed. To participate in app launch time |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1682 | * measurement, you should always call this method after first launch (when |
Dianne Hackborn | ebc15ef | 2013-10-09 17:36:57 -0700 | [diff] [blame] | 1683 | * {@link #onCreate(android.os.Bundle)} is called), at the point where you have |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1684 | * entirely drawn your UI and populated with all of the significant data. You |
| 1685 | * can safely call this method any time after first launch as well, in which case |
| 1686 | * it will simply be ignored. |
| 1687 | */ |
| 1688 | public void reportFullyDrawn() { |
| 1689 | if (mDoReportFullyDrawn) { |
| 1690 | mDoReportFullyDrawn = false; |
| 1691 | try { |
| 1692 | ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken); |
| 1693 | } catch (RemoteException e) { |
| 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1699 | * Called by the system when the device configuration changes while your |
| 1700 | * activity is running. Note that this will <em>only</em> be called if |
| 1701 | * you have selected configurations you would like to handle with the |
| 1702 | * {@link android.R.attr#configChanges} attribute in your manifest. If |
| 1703 | * any configuration change occurs that is not selected to be reported |
| 1704 | * by that attribute, then instead of reporting it the system will stop |
| 1705 | * and restart the activity (to have it launched with the new |
| 1706 | * configuration). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1707 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1708 | * <p>At the time that this function has been called, your Resources |
| 1709 | * object will have been updated to return resource values matching the |
| 1710 | * new configuration. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1711 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1712 | * @param newConfig The new device configuration. |
| 1713 | */ |
| 1714 | public void onConfigurationChanged(Configuration newConfig) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1715 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1716 | mCalled = true; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 1717 | |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1718 | mFragments.dispatchConfigurationChanged(newConfig); |
| 1719 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | if (mWindow != null) { |
| 1721 | // Pass the configuration changed event to the window |
| 1722 | mWindow.onConfigurationChanged(newConfig); |
| 1723 | } |
Adam Powell | 45c0b19 | 2011-07-28 15:11:57 -0700 | [diff] [blame] | 1724 | |
| 1725 | if (mActionBar != null) { |
| 1726 | // Do this last; the action bar will need to access |
| 1727 | // view changes from above. |
| 1728 | mActionBar.onConfigurationChanged(newConfig); |
| 1729 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1730 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1731 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1732 | /** |
| 1733 | * If this activity is being destroyed because it can not handle a |
| 1734 | * configuration parameter being changed (and thus its |
| 1735 | * {@link #onConfigurationChanged(Configuration)} method is |
| 1736 | * <em>not</em> being called), then you can use this method to discover |
| 1737 | * the set of changes that have occurred while in the process of being |
| 1738 | * destroyed. Note that there is no guarantee that these will be |
| 1739 | * accurate (other changes could have happened at any time), so you should |
| 1740 | * only use this as an optimization hint. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1741 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1742 | * @return Returns a bit field of the configuration parameters that are |
| 1743 | * changing, as defined by the {@link android.content.res.Configuration} |
| 1744 | * class. |
| 1745 | */ |
| 1746 | public int getChangingConfigurations() { |
| 1747 | return mConfigChangeFlags; |
| 1748 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1749 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1750 | /** |
| 1751 | * Retrieve the non-configuration instance data that was previously |
| 1752 | * returned by {@link #onRetainNonConfigurationInstance()}. This will |
| 1753 | * be available from the initial {@link #onCreate} and |
| 1754 | * {@link #onStart} calls to the new instance, allowing you to extract |
| 1755 | * any useful dynamic state from the previous instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1756 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1757 | * <p>Note that the data you retrieve here should <em>only</em> be used |
| 1758 | * as an optimization for handling configuration changes. You should always |
| 1759 | * be able to handle getting a null pointer back, and an activity must |
| 1760 | * still be able to restore itself to its previous state (through the |
| 1761 | * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this |
| 1762 | * function returns null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1763 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1764 | * @return Returns the object previously returned by |
| 1765 | * {@link #onRetainNonConfigurationInstance()}. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 1766 | * |
| 1767 | * @deprecated Use the new {@link Fragment} API |
| 1768 | * {@link Fragment#setRetainInstance(boolean)} instead; this is also |
| 1769 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1770 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1771 | @Nullable |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 1772 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1773 | public Object getLastNonConfigurationInstance() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1774 | return mLastNonConfigurationInstances != null |
| 1775 | ? mLastNonConfigurationInstances.activity : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1776 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1777 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1778 | /** |
| 1779 | * Called by the system, as part of destroying an |
| 1780 | * activity due to a configuration change, when it is known that a new |
| 1781 | * instance will immediately be created for the new configuration. You |
| 1782 | * can return any object you like here, including the activity instance |
| 1783 | * itself, which can later be retrieved by calling |
| 1784 | * {@link #getLastNonConfigurationInstance()} in the new activity |
| 1785 | * instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1786 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 1787 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 1788 | * or later, consider instead using a {@link Fragment} with |
| 1789 | * {@link Fragment#setRetainInstance(boolean) |
| 1790 | * Fragment.setRetainInstance(boolean}.</em> |
| 1791 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | * <p>This function is called purely as an optimization, and you must |
| 1793 | * not rely on it being called. When it is called, a number of guarantees |
| 1794 | * will be made to help optimize configuration switching: |
| 1795 | * <ul> |
| 1796 | * <li> The function will be called between {@link #onStop} and |
| 1797 | * {@link #onDestroy}. |
| 1798 | * <li> A new instance of the activity will <em>always</em> be immediately |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1799 | * created after this one's {@link #onDestroy()} is called. In particular, |
| 1800 | * <em>no</em> messages will be dispatched during this time (when the returned |
| 1801 | * object does not have an activity to be associated with). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1802 | * <li> The object you return here will <em>always</em> be available from |
| 1803 | * the {@link #getLastNonConfigurationInstance()} method of the following |
| 1804 | * activity instance as described there. |
| 1805 | * </ul> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1806 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1807 | * <p>These guarantees are designed so that an activity can use this API |
| 1808 | * to propagate extensive state from the old to new activity instance, from |
| 1809 | * loaded bitmaps, to network connections, to evenly actively running |
| 1810 | * threads. Note that you should <em>not</em> propagate any data that |
| 1811 | * may change based on the configuration, including any data loaded from |
| 1812 | * resources such as strings, layouts, or drawables. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1813 | * |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1814 | * <p>The guarantee of no message handling during the switch to the next |
| 1815 | * activity simplifies use with active objects. For example if your retained |
| 1816 | * state is an {@link android.os.AsyncTask} you are guaranteed that its |
| 1817 | * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will |
| 1818 | * not be called from the call here until you execute the next instance's |
| 1819 | * {@link #onCreate(Bundle)}. (Note however that there is of course no such |
| 1820 | * guarantee for {@link android.os.AsyncTask#doInBackground} since that is |
| 1821 | * running in a separate thread.) |
| 1822 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | * @return Return any Object holding the desired state to propagate to the |
| 1824 | * next activity instance. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 1825 | * |
| 1826 | * @deprecated Use the new {@link Fragment} API |
| 1827 | * {@link Fragment#setRetainInstance(boolean)} instead; this is also |
| 1828 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1829 | */ |
| 1830 | public Object onRetainNonConfigurationInstance() { |
| 1831 | return null; |
| 1832 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1833 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1834 | /** |
| 1835 | * Retrieve the non-configuration instance data that was previously |
| 1836 | * returned by {@link #onRetainNonConfigurationChildInstances()}. This will |
| 1837 | * be available from the initial {@link #onCreate} and |
| 1838 | * {@link #onStart} calls to the new instance, allowing you to extract |
| 1839 | * any useful dynamic state from the previous instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1840 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1841 | * <p>Note that the data you retrieve here should <em>only</em> be used |
| 1842 | * as an optimization for handling configuration changes. You should always |
| 1843 | * be able to handle getting a null pointer back, and an activity must |
| 1844 | * still be able to restore itself to its previous state (through the |
| 1845 | * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this |
| 1846 | * function returns null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1847 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1848 | * @return Returns the object previously returned by |
| 1849 | * {@link #onRetainNonConfigurationChildInstances()} |
| 1850 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1851 | @Nullable |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1852 | HashMap<String, Object> getLastNonConfigurationChildInstances() { |
| 1853 | return mLastNonConfigurationInstances != null |
| 1854 | ? mLastNonConfigurationInstances.children : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1855 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1856 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | /** |
| 1858 | * This method is similar to {@link #onRetainNonConfigurationInstance()} except that |
| 1859 | * it should return either a mapping from child activity id strings to arbitrary objects, |
| 1860 | * or null. This method is intended to be used by Activity framework subclasses that control a |
| 1861 | * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply |
| 1862 | * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null. |
| 1863 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1864 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1865 | HashMap<String,Object> onRetainNonConfigurationChildInstances() { |
| 1866 | return null; |
| 1867 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1868 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1869 | NonConfigurationInstances retainNonConfigurationInstances() { |
| 1870 | Object activity = onRetainNonConfigurationInstance(); |
| 1871 | HashMap<String, Object> children = onRetainNonConfigurationChildInstances(); |
| 1872 | ArrayList<Fragment> fragments = mFragments.retainNonConfig(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1873 | boolean retainLoaders = false; |
| 1874 | if (mAllLoaderManagers != null) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1875 | // prune out any loader managers that were already stopped and so |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1876 | // have nothing useful to retain. |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 1877 | final int N = mAllLoaderManagers.size(); |
| 1878 | LoaderManagerImpl loaders[] = new LoaderManagerImpl[N]; |
| 1879 | for (int i=N-1; i>=0; i--) { |
| 1880 | loaders[i] = mAllLoaderManagers.valueAt(i); |
| 1881 | } |
| 1882 | for (int i=0; i<N; i++) { |
| 1883 | LoaderManagerImpl lm = loaders[i]; |
| 1884 | if (lm.mRetaining) { |
| 1885 | retainLoaders = true; |
| 1886 | } else { |
| 1887 | lm.doDestroy(); |
| 1888 | mAllLoaderManagers.remove(lm.mWho); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1889 | } |
| 1890 | } |
| 1891 | } |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 1892 | if (activity == null && children == null && fragments == null && !retainLoaders |
| 1893 | && mVoiceInteractor == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1894 | return null; |
| 1895 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1896 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1897 | NonConfigurationInstances nci = new NonConfigurationInstances(); |
| 1898 | nci.activity = activity; |
| 1899 | nci.children = children; |
| 1900 | nci.fragments = fragments; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1901 | nci.loaders = mAllLoaderManagers; |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 1902 | nci.voiceInteractor = mVoiceInteractor; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1903 | return nci; |
| 1904 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1905 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1906 | public void onLowMemory() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1907 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1908 | mCalled = true; |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1909 | mFragments.dispatchLowMemory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1910 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1911 | |
| 1912 | public void onTrimMemory(int level) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1913 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1914 | mCalled = true; |
| 1915 | mFragments.dispatchTrimMemory(level); |
| 1916 | } |
| 1917 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1918 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1919 | * Return the FragmentManager for interacting with fragments associated |
| 1920 | * with this activity. |
| 1921 | */ |
| 1922 | public FragmentManager getFragmentManager() { |
| 1923 | return mFragments; |
| 1924 | } |
| 1925 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1926 | void invalidateFragment(String who) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1927 | //Log.v(TAG, "invalidateFragmentIndex: index=" + index); |
Dianne Hackborn | 9e14e9f3 | 2010-07-14 11:07:38 -0700 | [diff] [blame] | 1928 | if (mAllLoaderManagers != null) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1929 | LoaderManagerImpl lm = mAllLoaderManagers.get(who); |
Dianne Hackborn | d04ad54 | 2011-07-25 16:16:15 -0700 | [diff] [blame] | 1930 | if (lm != null && !lm.mRetaining) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1931 | lm.doDestroy(); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1932 | mAllLoaderManagers.remove(who); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1933 | } |
Dianne Hackborn | 9e14e9f3 | 2010-07-14 11:07:38 -0700 | [diff] [blame] | 1934 | } |
| 1935 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1936 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1937 | /** |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1938 | * Called when a Fragment is being attached to this activity, immediately |
| 1939 | * after the call to its {@link Fragment#onAttach Fragment.onAttach()} |
| 1940 | * method and before {@link Fragment#onCreate Fragment.onCreate()}. |
| 1941 | */ |
| 1942 | public void onAttachFragment(Fragment fragment) { |
| 1943 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1944 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1945 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1946 | * Wrapper around |
| 1947 | * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)} |
| 1948 | * that gives the resulting {@link Cursor} to call |
| 1949 | * {@link #startManagingCursor} so that the activity will manage its |
| 1950 | * lifecycle for you. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1951 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 1952 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 1953 | * or later, consider instead using {@link LoaderManager} instead, available |
| 1954 | * via {@link #getLoaderManager()}.</em> |
| 1955 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 1956 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using |
| 1957 | * this method, because the activity will do that for you at the appropriate time. However, if |
| 1958 | * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will |
| 1959 | * not</em> automatically close the cursor and, in that case, you must call |
| 1960 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1961 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1962 | * @param uri The URI of the content provider to query. |
| 1963 | * @param projection List of columns to return. |
| 1964 | * @param selection SQL WHERE clause. |
| 1965 | * @param sortOrder SQL ORDER BY clause. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1966 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1967 | * @return The Cursor that was returned by query(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1968 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | * @see ContentResolver#query(android.net.Uri , String[], String, String[], String) |
| 1970 | * @see #startManagingCursor |
| 1971 | * @hide |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 1972 | * |
| 1973 | * @deprecated Use {@link CursorLoader} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1974 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 1975 | @Deprecated |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 1976 | public final Cursor managedQuery(Uri uri, String[] projection, String selection, |
| 1977 | String sortOrder) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1978 | Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder); |
| 1979 | if (c != null) { |
| 1980 | startManagingCursor(c); |
| 1981 | } |
| 1982 | return c; |
| 1983 | } |
| 1984 | |
| 1985 | /** |
| 1986 | * Wrapper around |
| 1987 | * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)} |
| 1988 | * that gives the resulting {@link Cursor} to call |
| 1989 | * {@link #startManagingCursor} so that the activity will manage its |
| 1990 | * lifecycle for you. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1991 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 1992 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 1993 | * or later, consider instead using {@link LoaderManager} instead, available |
| 1994 | * via {@link #getLoaderManager()}.</em> |
| 1995 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 1996 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using |
| 1997 | * this method, because the activity will do that for you at the appropriate time. However, if |
| 1998 | * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will |
| 1999 | * not</em> automatically close the cursor and, in that case, you must call |
| 2000 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2001 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2002 | * @param uri The URI of the content provider to query. |
| 2003 | * @param projection List of columns to return. |
| 2004 | * @param selection SQL WHERE clause. |
| 2005 | * @param selectionArgs The arguments to selection, if any ?s are pesent |
| 2006 | * @param sortOrder SQL ORDER BY clause. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2007 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2008 | * @return The Cursor that was returned by query(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2009 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2010 | * @see ContentResolver#query(android.net.Uri , String[], String, String[], String) |
| 2011 | * @see #startManagingCursor |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2012 | * |
| 2013 | * @deprecated Use {@link CursorLoader} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2014 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2015 | @Deprecated |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2016 | public final Cursor managedQuery(Uri uri, String[] projection, String selection, |
| 2017 | String[] selectionArgs, String sortOrder) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2018 | Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder); |
| 2019 | if (c != null) { |
| 2020 | startManagingCursor(c); |
| 2021 | } |
| 2022 | return c; |
| 2023 | } |
| 2024 | |
| 2025 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2026 | * This method allows the activity to take care of managing the given |
| 2027 | * {@link Cursor}'s lifecycle for you based on the activity's lifecycle. |
| 2028 | * That is, when the activity is stopped it will automatically call |
| 2029 | * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted |
| 2030 | * it will call {@link Cursor#requery} for you. When the activity is |
| 2031 | * destroyed, all managed Cursors will be closed automatically. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2032 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2033 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2034 | * or later, consider instead using {@link LoaderManager} instead, available |
| 2035 | * via {@link #getLoaderManager()}.</em> |
| 2036 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2037 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from |
| 2038 | * {@link #managedQuery}, because the activity will do that for you at the appropriate time. |
| 2039 | * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system |
| 2040 | * <em>will not</em> automatically close the cursor and, in that case, you must call |
| 2041 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2042 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2043 | * @param c The Cursor to be managed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2044 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2045 | * @see #managedQuery(android.net.Uri , String[], String, String[], String) |
| 2046 | * @see #stopManagingCursor |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2047 | * |
| 2048 | * @deprecated Use the new {@link android.content.CursorLoader} class with |
| 2049 | * {@link LoaderManager} instead; this is also |
| 2050 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2051 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2052 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2053 | public void startManagingCursor(Cursor c) { |
| 2054 | synchronized (mManagedCursors) { |
| 2055 | mManagedCursors.add(new ManagedCursor(c)); |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | /** |
| 2060 | * Given a Cursor that was previously given to |
| 2061 | * {@link #startManagingCursor}, stop the activity's management of that |
| 2062 | * cursor. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2063 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2064 | * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query, |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2065 | * the system <em>will not</em> automatically close the cursor and you must call |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2066 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2067 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2068 | * @param c The Cursor that was being managed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2069 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2070 | * @see #startManagingCursor |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2071 | * |
| 2072 | * @deprecated Use the new {@link android.content.CursorLoader} class with |
| 2073 | * {@link LoaderManager} instead; this is also |
| 2074 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2075 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2076 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2077 | public void stopManagingCursor(Cursor c) { |
| 2078 | synchronized (mManagedCursors) { |
| 2079 | final int N = mManagedCursors.size(); |
| 2080 | for (int i=0; i<N; i++) { |
| 2081 | ManagedCursor mc = mManagedCursors.get(i); |
| 2082 | if (mc.mCursor == c) { |
| 2083 | mManagedCursors.remove(i); |
| 2084 | break; |
| 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | /** |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 2091 | * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD} |
| 2092 | * this is a no-op. |
Dianne Hackborn | 4f3867e | 2010-12-14 22:09:51 -0800 | [diff] [blame] | 2093 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2094 | */ |
Dianne Hackborn | d3efa39 | 2010-09-01 17:34:12 -0700 | [diff] [blame] | 2095 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2096 | public void setPersistent(boolean isPersistent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | /** |
| 2100 | * Finds a view that was identified by the id attribute from the XML that |
| 2101 | * was processed in {@link #onCreate}. |
| 2102 | * |
| 2103 | * @return The view if found or null otherwise. |
| 2104 | */ |
Scott Kennedy | c051955 | 2015-02-11 15:33:10 -0800 | [diff] [blame] | 2105 | @Nullable |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 2106 | public View findViewById(@IdRes int id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2107 | return getWindow().findViewById(id); |
| 2108 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2109 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2110 | /** |
| 2111 | * Retrieve a reference to this activity's ActionBar. |
Adam Powell | 42c0fe8 | 2010-08-10 16:36:56 -0700 | [diff] [blame] | 2112 | * |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2113 | * @return The Activity's ActionBar, or null if it does not have one. |
| 2114 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2115 | @Nullable |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2116 | public ActionBar getActionBar() { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2117 | initWindowDecorActionBar(); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2118 | return mActionBar; |
| 2119 | } |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2120 | |
| 2121 | /** |
| 2122 | * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this |
| 2123 | * Activity window. |
| 2124 | * |
| 2125 | * <p>When set to a non-null value the {@link #getActionBar()} method will return |
| 2126 | * an {@link ActionBar} object that can be used to control the given toolbar as if it were |
| 2127 | * a traditional window decor action bar. The toolbar's menu will be populated with the |
| 2128 | * Activity's options menu and the navigation button will be wired through the standard |
| 2129 | * {@link android.R.id#home home} menu select action.</p> |
| 2130 | * |
| 2131 | * <p>In order to use a Toolbar within the Activity's window content the application |
| 2132 | * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p> |
| 2133 | * |
Adam Powell | 3778014 | 2014-06-01 13:31:00 -0700 | [diff] [blame] | 2134 | * @param toolbar Toolbar to set as the Activity's action bar |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2135 | */ |
Adam Powell | 3778014 | 2014-06-01 13:31:00 -0700 | [diff] [blame] | 2136 | public void setActionBar(@Nullable Toolbar toolbar) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2137 | if (getActionBar() instanceof WindowDecorActionBar) { |
| 2138 | throw new IllegalStateException("This Activity already has an action bar supplied " + |
| 2139 | "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " + |
| 2140 | "android:windowActionBar to false in your theme to use a Toolbar instead."); |
| 2141 | } |
Adam Powell | 14e1afe | 2014-08-18 15:58:23 -0700 | [diff] [blame] | 2142 | ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this); |
| 2143 | mActionBar = tbab; |
| 2144 | mWindow.setCallback(tbab.getWrappedWindowCallback()); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2145 | mActionBar.invalidateOptionsMenu(); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2146 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2148 | /** |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2149 | * Creates a new ActionBar, locates the inflated ActionBarView, |
| 2150 | * initializes the ActionBar with the view, and sets mActionBar. |
| 2151 | */ |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2152 | private void initWindowDecorActionBar() { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 2153 | Window window = getWindow(); |
Adam Powell | a593d98 | 2011-05-13 14:09:54 -0700 | [diff] [blame] | 2154 | |
| 2155 | // Initializing the window decor can change window feature flags. |
| 2156 | // Make sure that we have the correct set before performing the test below. |
| 2157 | window.getDecorView(); |
| 2158 | |
Adam Powell | 9b4c804 | 2010-08-10 15:36:44 -0700 | [diff] [blame] | 2159 | if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2160 | return; |
| 2161 | } |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 2162 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2163 | mActionBar = new WindowDecorActionBar(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 2164 | mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp); |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 2165 | |
| 2166 | mWindow.setDefaultIcon(mActivityInfo.getIconResource()); |
| 2167 | mWindow.setDefaultLogo(mActivityInfo.getLogoResource()); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2168 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2169 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2170 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2171 | * Set the activity content from a layout resource. The resource will be |
| 2172 | * inflated, adding all top-level views to the activity. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2173 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2174 | * @param layoutResID Resource ID to be inflated. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2175 | * |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2176 | * @see #setContentView(android.view.View) |
| 2177 | * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2178 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 2179 | public void setContentView(@LayoutRes int layoutResID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2180 | getWindow().setContentView(layoutResID); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2181 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | /** |
| 2185 | * Set the activity content to an explicit view. This view is placed |
| 2186 | * directly into the activity's view hierarchy. It can itself be a complex |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2187 | * view hierarchy. When calling this method, the layout parameters of the |
| 2188 | * specified view are ignored. Both the width and the height of the view are |
| 2189 | * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use |
| 2190 | * your own layout parameters, invoke |
| 2191 | * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)} |
| 2192 | * instead. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2193 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2194 | * @param view The desired content to display. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2195 | * |
| 2196 | * @see #setContentView(int) |
| 2197 | * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2198 | */ |
| 2199 | public void setContentView(View view) { |
| 2200 | getWindow().setContentView(view); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2201 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | /** |
| 2205 | * Set the activity content to an explicit view. This view is placed |
| 2206 | * directly into the activity's view hierarchy. It can itself be a complex |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2207 | * view hierarchy. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2208 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2209 | * @param view The desired content to display. |
| 2210 | * @param params Layout parameters for the view. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2211 | * |
| 2212 | * @see #setContentView(android.view.View) |
| 2213 | * @see #setContentView(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2214 | */ |
| 2215 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 2216 | getWindow().setContentView(view, params); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2217 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | /** |
| 2221 | * Add an additional content view to the activity. Added after any existing |
| 2222 | * ones in the activity -- existing views are NOT removed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2223 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2224 | * @param view The desired content to display. |
| 2225 | * @param params Layout parameters for the view. |
| 2226 | */ |
| 2227 | public void addContentView(View view, ViewGroup.LayoutParams params) { |
| 2228 | getWindow().addContentView(view, params); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2229 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | /** |
Adam Powell | cfbe9be | 2013-11-06 14:58:58 -0800 | [diff] [blame] | 2233 | * Retrieve the {@link TransitionManager} responsible for default transitions in this window. |
| 2234 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2235 | * |
| 2236 | * <p>This method will return non-null after content has been initialized (e.g. by using |
| 2237 | * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p> |
| 2238 | * |
| 2239 | * @return This window's content TransitionManager or null if none is set. |
| 2240 | */ |
| 2241 | public TransitionManager getContentTransitionManager() { |
| 2242 | return getWindow().getTransitionManager(); |
| 2243 | } |
| 2244 | |
| 2245 | /** |
| 2246 | * Set the {@link TransitionManager} to use for default transitions in this window. |
| 2247 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2248 | * |
| 2249 | * @param tm The TransitionManager to use for scene changes. |
| 2250 | */ |
| 2251 | public void setContentTransitionManager(TransitionManager tm) { |
| 2252 | getWindow().setTransitionManager(tm); |
| 2253 | } |
| 2254 | |
| 2255 | /** |
| 2256 | * Retrieve the {@link Scene} representing this window's current content. |
| 2257 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2258 | * |
| 2259 | * <p>This method will return null if the current content is not represented by a Scene.</p> |
| 2260 | * |
| 2261 | * @return Current Scene being shown or null |
| 2262 | */ |
| 2263 | public Scene getContentScene() { |
| 2264 | return getWindow().getContentScene(); |
| 2265 | } |
| 2266 | |
| 2267 | /** |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2268 | * Sets whether this activity is finished when touched outside its window's |
| 2269 | * bounds. |
| 2270 | */ |
| 2271 | public void setFinishOnTouchOutside(boolean finish) { |
| 2272 | mWindow.setCloseOnTouchOutside(finish); |
| 2273 | } |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2274 | |
| 2275 | /** @hide */ |
| 2276 | @IntDef({ |
| 2277 | DEFAULT_KEYS_DISABLE, |
| 2278 | DEFAULT_KEYS_DIALER, |
| 2279 | DEFAULT_KEYS_SHORTCUT, |
| 2280 | DEFAULT_KEYS_SEARCH_LOCAL, |
| 2281 | DEFAULT_KEYS_SEARCH_GLOBAL}) |
| 2282 | @Retention(RetentionPolicy.SOURCE) |
| 2283 | @interface DefaultKeyMode {} |
| 2284 | |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2285 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2286 | * Use with {@link #setDefaultKeyMode} to turn off default handling of |
| 2287 | * keys. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2288 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2289 | * @see #setDefaultKeyMode |
| 2290 | */ |
| 2291 | static public final int DEFAULT_KEYS_DISABLE = 0; |
| 2292 | /** |
| 2293 | * Use with {@link #setDefaultKeyMode} to launch the dialer during default |
| 2294 | * key handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2295 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2296 | * @see #setDefaultKeyMode |
| 2297 | */ |
| 2298 | static public final int DEFAULT_KEYS_DIALER = 1; |
| 2299 | /** |
| 2300 | * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in |
| 2301 | * default key handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2302 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2303 | * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2304 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2305 | * @see #setDefaultKeyMode |
| 2306 | */ |
| 2307 | static public final int DEFAULT_KEYS_SHORTCUT = 2; |
| 2308 | /** |
| 2309 | * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes |
| 2310 | * will start an application-defined search. (If the application or activity does not |
| 2311 | * actually define a search, the the keys will be ignored.) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2312 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2313 | * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2314 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2315 | * @see #setDefaultKeyMode |
| 2316 | */ |
| 2317 | static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3; |
| 2318 | |
| 2319 | /** |
| 2320 | * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes |
| 2321 | * will start a global search (typically web search, but some platforms may define alternate |
| 2322 | * methods for global search) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2323 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2324 | * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2325 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2326 | * @see #setDefaultKeyMode |
| 2327 | */ |
| 2328 | static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4; |
| 2329 | |
| 2330 | /** |
| 2331 | * Select the default key handling for this activity. This controls what |
| 2332 | * will happen to key events that are not otherwise handled. The default |
| 2333 | * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the |
| 2334 | * floor. Other modes allow you to launch the dialer |
| 2335 | * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options |
| 2336 | * menu without requiring the menu key be held down |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2337 | * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2338 | * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2339 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2340 | * <p>Note that the mode selected here does not impact the default |
| 2341 | * handling of system keys, such as the "back" and "menu" keys, and your |
| 2342 | * activity and its views always get a first chance to receive and handle |
| 2343 | * all application keys. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2344 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2345 | * @param mode The desired default key mode constant. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2346 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2347 | * @see #DEFAULT_KEYS_DISABLE |
| 2348 | * @see #DEFAULT_KEYS_DIALER |
| 2349 | * @see #DEFAULT_KEYS_SHORTCUT |
| 2350 | * @see #DEFAULT_KEYS_SEARCH_LOCAL |
| 2351 | * @see #DEFAULT_KEYS_SEARCH_GLOBAL |
| 2352 | * @see #onKeyDown |
| 2353 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2354 | public final void setDefaultKeyMode(@DefaultKeyMode int mode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2355 | mDefaultKeyMode = mode; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2356 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2357 | // Some modes use a SpannableStringBuilder to track & dispatch input events |
| 2358 | // This list must remain in sync with the switch in onKeyDown() |
| 2359 | switch (mode) { |
| 2360 | case DEFAULT_KEYS_DISABLE: |
| 2361 | case DEFAULT_KEYS_SHORTCUT: |
| 2362 | mDefaultKeySsb = null; // not used in these modes |
| 2363 | break; |
| 2364 | case DEFAULT_KEYS_DIALER: |
| 2365 | case DEFAULT_KEYS_SEARCH_LOCAL: |
| 2366 | case DEFAULT_KEYS_SEARCH_GLOBAL: |
| 2367 | mDefaultKeySsb = new SpannableStringBuilder(); |
| 2368 | Selection.setSelection(mDefaultKeySsb,0); |
| 2369 | break; |
| 2370 | default: |
| 2371 | throw new IllegalArgumentException(); |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | /** |
| 2376 | * Called when a key was pressed down and not handled by any of the views |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2377 | * inside of the activity. So, for example, key presses while the cursor |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2378 | * is inside a TextView will not trigger the event (unless it is a navigation |
| 2379 | * to another object) because TextView handles its own key presses. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2380 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2381 | * <p>If the focused view didn't want this event, this method is called. |
| 2382 | * |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2383 | * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK} |
| 2384 | * by calling {@link #onBackPressed()}, though the behavior varies based |
| 2385 | * on the application compatibility mode: for |
| 2386 | * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications, |
| 2387 | * it will set up the dispatch to call {@link #onKeyUp} where the action |
| 2388 | * will be performed; for earlier applications, it will perform the |
| 2389 | * action immediately in on-down, as those versions of the platform |
| 2390 | * behaved. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2391 | * |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2392 | * <p>Other additional default key handling may be performed |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2393 | * if configured with {@link #setDefaultKeyMode}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2394 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2395 | * @return Return <code>true</code> to prevent this event from being propagated |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2396 | * further, or <code>false</code> to indicate that you have not handled |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2397 | * this event and it should continue to be propagated. |
| 2398 | * @see #onKeyUp |
| 2399 | * @see android.view.KeyEvent |
| 2400 | */ |
| 2401 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2402 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2403 | if (getApplicationInfo().targetSdkVersion |
| 2404 | >= Build.VERSION_CODES.ECLAIR) { |
| 2405 | event.startTracking(); |
| 2406 | } else { |
| 2407 | onBackPressed(); |
| 2408 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2409 | return true; |
| 2410 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2411 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2412 | if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) { |
| 2413 | return false; |
| 2414 | } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 2415 | Window w = getWindow(); |
| 2416 | if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 2417 | w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event, |
| 2418 | Menu.FLAG_ALWAYS_PERFORM_CLOSE)) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2419 | return true; |
| 2420 | } |
| 2421 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2422 | } else { |
| 2423 | // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_* |
| 2424 | boolean clearSpannable = false; |
| 2425 | boolean handled; |
| 2426 | if ((event.getRepeatCount() != 0) || event.isSystem()) { |
| 2427 | clearSpannable = true; |
| 2428 | handled = false; |
| 2429 | } else { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2430 | handled = TextKeyListener.getInstance().onKeyDown( |
| 2431 | null, mDefaultKeySsb, keyCode, event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2432 | if (handled && mDefaultKeySsb.length() > 0) { |
| 2433 | // something useable has been typed - dispatch it now. |
| 2434 | |
| 2435 | final String str = mDefaultKeySsb.toString(); |
| 2436 | clearSpannable = true; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2438 | switch (mDefaultKeyMode) { |
| 2439 | case DEFAULT_KEYS_DIALER: |
| 2440 | Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str)); |
| 2441 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2442 | startActivity(intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2443 | break; |
| 2444 | case DEFAULT_KEYS_SEARCH_LOCAL: |
| 2445 | startSearch(str, false, null, false); |
| 2446 | break; |
| 2447 | case DEFAULT_KEYS_SEARCH_GLOBAL: |
| 2448 | startSearch(str, false, null, true); |
| 2449 | break; |
| 2450 | } |
| 2451 | } |
| 2452 | } |
| 2453 | if (clearSpannable) { |
| 2454 | mDefaultKeySsb.clear(); |
| 2455 | mDefaultKeySsb.clearSpans(); |
| 2456 | Selection.setSelection(mDefaultKeySsb,0); |
| 2457 | } |
| 2458 | return handled; |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2463 | * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent) |
| 2464 | * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle |
| 2465 | * the event). |
| 2466 | */ |
| 2467 | public boolean onKeyLongPress(int keyCode, KeyEvent event) { |
| 2468 | return false; |
| 2469 | } |
| 2470 | |
| 2471 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2472 | * Called when a key was released and not handled by any of the views |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2473 | * inside of the activity. So, for example, key presses while the cursor |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2474 | * is inside a TextView will not trigger the event (unless it is a navigation |
| 2475 | * to another object) because TextView handles its own key presses. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2476 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2477 | * <p>The default implementation handles KEYCODE_BACK to stop the activity |
| 2478 | * and go back. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2479 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2480 | * @return Return <code>true</code> to prevent this event from being propagated |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2481 | * further, or <code>false</code> to indicate that you have not handled |
| 2482 | * this event and it should continue to be propagated. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2483 | * @see #onKeyDown |
| 2484 | * @see KeyEvent |
| 2485 | */ |
| 2486 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2487 | if (getApplicationInfo().targetSdkVersion |
| 2488 | >= Build.VERSION_CODES.ECLAIR) { |
| 2489 | if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking() |
| 2490 | && !event.isCanceled()) { |
| 2491 | onBackPressed(); |
| 2492 | return true; |
| 2493 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2494 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2495 | return false; |
| 2496 | } |
| 2497 | |
| 2498 | /** |
| 2499 | * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent) |
| 2500 | * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle |
| 2501 | * the event). |
| 2502 | */ |
| 2503 | public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
| 2504 | return false; |
| 2505 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2506 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2507 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2508 | * Called when the activity has detected the user's press of the back |
| 2509 | * key. The default implementation simply finishes the current activity, |
| 2510 | * but you can override this to do whatever you want. |
| 2511 | */ |
| 2512 | public void onBackPressed() { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2513 | if (mActionBar != null && mActionBar.collapseActionView()) { |
| 2514 | return; |
| 2515 | } |
| 2516 | |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 2517 | if (!mFragments.popBackStackImmediate()) { |
Craig Mautner | 73f843d | 2014-05-19 09:42:28 -0700 | [diff] [blame] | 2518 | finishAfterTransition(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 2519 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2520 | } |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 2521 | |
| 2522 | /** |
| 2523 | * Called when a key shortcut event is not handled by any of the views in the Activity. |
| 2524 | * Override this method to implement global key shortcuts for the Activity. |
| 2525 | * Key shortcuts can also be implemented by setting the |
| 2526 | * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items. |
| 2527 | * |
| 2528 | * @param keyCode The value in event.getKeyCode(). |
| 2529 | * @param event Description of the key event. |
| 2530 | * @return True if the key shortcut was handled. |
| 2531 | */ |
| 2532 | public boolean onKeyShortcut(int keyCode, KeyEvent event) { |
| 2533 | return false; |
| 2534 | } |
| 2535 | |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2536 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2537 | * Called when a touch screen event was not handled by any of the views |
| 2538 | * under it. This is most useful to process touch events that happen |
| 2539 | * outside of your window bounds, where there is no view to receive it. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2540 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2541 | * @param event The touch screen event being processed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2542 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2543 | * @return Return true if you have consumed the event, false if you haven't. |
| 2544 | * The default implementation always returns false. |
| 2545 | */ |
| 2546 | public boolean onTouchEvent(MotionEvent event) { |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2547 | if (mWindow.shouldCloseOnTouch(this, event)) { |
| 2548 | finish(); |
| 2549 | return true; |
| 2550 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2552 | return false; |
| 2553 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2554 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2555 | /** |
| 2556 | * Called when the trackball was moved and not handled by any of the |
| 2557 | * views inside of the activity. So, for example, if the trackball moves |
| 2558 | * while focus is on a button, you will receive a call here because |
| 2559 | * buttons do not normally do anything with trackball events. The call |
| 2560 | * here happens <em>before</em> trackball movements are converted to |
| 2561 | * DPAD key events, which then get sent back to the view hierarchy, and |
| 2562 | * will be processed at the point for things like focus navigation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2563 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2564 | * @param event The trackball event being processed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2565 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2566 | * @return Return true if you have consumed the event, false if you haven't. |
| 2567 | * The default implementation always returns false. |
| 2568 | */ |
| 2569 | public boolean onTrackballEvent(MotionEvent event) { |
| 2570 | return false; |
| 2571 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2572 | |
| 2573 | /** |
| 2574 | * Called when a generic motion event was not handled by any of the |
| 2575 | * views inside of the activity. |
| 2576 | * <p> |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2577 | * Generic motion events describe joystick movements, mouse hovers, track pad |
| 2578 | * touches, scroll wheel movements and other input events. The |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2579 | * {@link MotionEvent#getSource() source} of the motion event specifies |
| 2580 | * the class of input that was received. Implementations of this method |
| 2581 | * must examine the bits in the source before processing the event. |
| 2582 | * The following code example shows how this is done. |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2583 | * </p><p> |
| 2584 | * Generic motion events with source class |
| 2585 | * {@link android.view.InputDevice#SOURCE_CLASS_POINTER} |
| 2586 | * are delivered to the view under the pointer. All other generic motion events are |
| 2587 | * delivered to the focused view. |
| 2588 | * </p><p> |
| 2589 | * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to |
| 2590 | * handle this event. |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2591 | * </p> |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2592 | * |
| 2593 | * @param event The generic motion event being processed. |
| 2594 | * |
| 2595 | * @return Return true if you have consumed the event, false if you haven't. |
| 2596 | * The default implementation always returns false. |
| 2597 | */ |
| 2598 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 2599 | return false; |
| 2600 | } |
| 2601 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2602 | /** |
| 2603 | * Called whenever a key, touch, or trackball event is dispatched to the |
| 2604 | * activity. Implement this method if you wish to know that the user has |
| 2605 | * interacted with the device in some way while your activity is running. |
| 2606 | * This callback and {@link #onUserLeaveHint} are intended to help |
| 2607 | * activities manage status bar notifications intelligently; specifically, |
| 2608 | * for helping activities determine the proper time to cancel a notfication. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2609 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2610 | * <p>All calls to your activity's {@link #onUserLeaveHint} callback will |
| 2611 | * be accompanied by calls to {@link #onUserInteraction}. This |
| 2612 | * ensures that your activity will be told of relevant user activity such |
| 2613 | * as pulling down the notification pane and touching an item there. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2614 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2615 | * <p>Note that this callback will be invoked for the touch down action |
| 2616 | * that begins a touch gesture, but may not be invoked for the touch-moved |
| 2617 | * and touch-up actions that follow. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2618 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2619 | * @see #onUserLeaveHint() |
| 2620 | */ |
| 2621 | public void onUserInteraction() { |
| 2622 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2623 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2624 | public void onWindowAttributesChanged(WindowManager.LayoutParams params) { |
| 2625 | // Update window manager if: we have a view, that view is |
| 2626 | // attached to its parent (which will be a RootView), and |
| 2627 | // this activity is not embedded. |
| 2628 | if (mParent == null) { |
| 2629 | View decor = mDecor; |
| 2630 | if (decor != null && decor.getParent() != null) { |
| 2631 | getWindowManager().updateViewLayout(decor, params); |
| 2632 | } |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | public void onContentChanged() { |
| 2637 | } |
| 2638 | |
| 2639 | /** |
| 2640 | * Called when the current {@link Window} of the activity gains or loses |
| 2641 | * focus. This is the best indicator of whether this activity is visible |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2642 | * to the user. The default implementation clears the key tracking |
| 2643 | * state, so should always be called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2644 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2645 | * <p>Note that this provides information about global focus state, which |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2646 | * is managed independently of activity lifecycles. As such, while focus |
| 2647 | * changes will generally have some relation to lifecycle changes (an |
| 2648 | * activity that is stopped will not generally get window focus), you |
| 2649 | * should not rely on any particular order between the callbacks here and |
| 2650 | * those in the other lifecycle methods such as {@link #onResume}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2651 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2652 | * <p>As a general rule, however, a resumed activity will have window |
| 2653 | * focus... unless it has displayed other dialogs or popups that take |
| 2654 | * input focus, in which case the activity itself will not have focus |
| 2655 | * when the other windows have it. Likewise, the system may display |
| 2656 | * system-level windows (such as the status bar notification panel or |
| 2657 | * a system alert) which will temporarily take window input focus without |
| 2658 | * pausing the foreground activity. |
| 2659 | * |
| 2660 | * @param hasFocus Whether the window of this activity has focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2661 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2662 | * @see #hasWindowFocus() |
| 2663 | * @see #onResume |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2664 | * @see View#onWindowFocusChanged(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2665 | */ |
| 2666 | public void onWindowFocusChanged(boolean hasFocus) { |
| 2667 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2668 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2669 | /** |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2670 | * Called when the main window associated with the activity has been |
| 2671 | * attached to the window manager. |
| 2672 | * See {@link View#onAttachedToWindow() View.onAttachedToWindow()} |
| 2673 | * for more information. |
| 2674 | * @see View#onAttachedToWindow |
| 2675 | */ |
| 2676 | public void onAttachedToWindow() { |
| 2677 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2678 | |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2679 | /** |
| 2680 | * Called when the main window associated with the activity has been |
| 2681 | * detached from the window manager. |
| 2682 | * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()} |
| 2683 | * for more information. |
| 2684 | * @see View#onDetachedFromWindow |
| 2685 | */ |
| 2686 | public void onDetachedFromWindow() { |
| 2687 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2688 | |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2689 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2690 | * Returns true if this activity's <em>main</em> window currently has window focus. |
| 2691 | * Note that this is not the same as the view itself having focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2692 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2693 | * @return True if this activity's main window currently has window focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2694 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2695 | * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams) |
| 2696 | */ |
| 2697 | public boolean hasWindowFocus() { |
| 2698 | Window w = getWindow(); |
| 2699 | if (w != null) { |
| 2700 | View d = w.getDecorView(); |
| 2701 | if (d != null) { |
| 2702 | return d.hasWindowFocus(); |
| 2703 | } |
| 2704 | } |
| 2705 | return false; |
| 2706 | } |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2707 | |
| 2708 | /** |
| 2709 | * Called when the main window associated with the activity has been dismissed. |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 2710 | * @hide |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2711 | */ |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 2712 | @Override |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2713 | public void onWindowDismissed() { |
| 2714 | finish(); |
| 2715 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2716 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2717 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2718 | * Called to process key events. You can override this to intercept all |
| 2719 | * key events before they are dispatched to the window. Be sure to call |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | * this implementation for key events that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2721 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2722 | * @param event The key event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2723 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2724 | * @return boolean Return true if this event was consumed. |
| 2725 | */ |
| 2726 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 2727 | onUserInteraction(); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2728 | |
| 2729 | // Let action bars open menus in response to the menu key prioritized over |
| 2730 | // the window handling it |
| 2731 | if (event.getKeyCode() == KeyEvent.KEYCODE_MENU && |
| 2732 | mActionBar != null && mActionBar.onMenuKeyEvent(event)) { |
| 2733 | return true; |
| 2734 | } |
| 2735 | |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2736 | Window win = getWindow(); |
| 2737 | if (win.superDispatchKeyEvent(event)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2738 | return true; |
| 2739 | } |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2740 | View decor = mDecor; |
| 2741 | if (decor == null) decor = win.getDecorView(); |
| 2742 | return event.dispatch(this, decor != null |
| 2743 | ? decor.getKeyDispatcherState() : null, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | /** |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 2747 | * Called to process a key shortcut event. |
| 2748 | * You can override this to intercept all key shortcut events before they are |
| 2749 | * dispatched to the window. Be sure to call this implementation for key shortcut |
| 2750 | * events that should be handled normally. |
| 2751 | * |
| 2752 | * @param event The key shortcut event. |
| 2753 | * @return True if this event was consumed. |
| 2754 | */ |
| 2755 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
| 2756 | onUserInteraction(); |
| 2757 | if (getWindow().superDispatchKeyShortcutEvent(event)) { |
| 2758 | return true; |
| 2759 | } |
| 2760 | return onKeyShortcut(event.getKeyCode(), event); |
| 2761 | } |
| 2762 | |
| 2763 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2764 | * Called to process touch screen events. You can override this to |
| 2765 | * intercept all touch screen events before they are dispatched to the |
| 2766 | * window. Be sure to call this implementation for touch screen events |
| 2767 | * that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2768 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2769 | * @param ev The touch screen event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2770 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2771 | * @return boolean Return true if this event was consumed. |
| 2772 | */ |
| 2773 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 2774 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 2775 | onUserInteraction(); |
| 2776 | } |
| 2777 | if (getWindow().superDispatchTouchEvent(ev)) { |
| 2778 | return true; |
| 2779 | } |
| 2780 | return onTouchEvent(ev); |
| 2781 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2782 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2783 | /** |
| 2784 | * Called to process trackball events. You can override this to |
| 2785 | * intercept all trackball events before they are dispatched to the |
| 2786 | * window. Be sure to call this implementation for trackball events |
| 2787 | * that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2788 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2789 | * @param ev The trackball event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2790 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2791 | * @return boolean Return true if this event was consumed. |
| 2792 | */ |
| 2793 | public boolean dispatchTrackballEvent(MotionEvent ev) { |
| 2794 | onUserInteraction(); |
| 2795 | if (getWindow().superDispatchTrackballEvent(ev)) { |
| 2796 | return true; |
| 2797 | } |
| 2798 | return onTrackballEvent(ev); |
| 2799 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2800 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2801 | /** |
| 2802 | * Called to process generic motion events. You can override this to |
| 2803 | * intercept all generic motion events before they are dispatched to the |
| 2804 | * window. Be sure to call this implementation for generic motion events |
| 2805 | * that should be handled normally. |
| 2806 | * |
| 2807 | * @param ev The generic motion event. |
| 2808 | * |
| 2809 | * @return boolean Return true if this event was consumed. |
| 2810 | */ |
| 2811 | public boolean dispatchGenericMotionEvent(MotionEvent ev) { |
| 2812 | onUserInteraction(); |
| 2813 | if (getWindow().superDispatchGenericMotionEvent(ev)) { |
| 2814 | return true; |
| 2815 | } |
| 2816 | return onGenericMotionEvent(ev); |
| 2817 | } |
| 2818 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2819 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 2820 | event.setClassName(getClass().getName()); |
| 2821 | event.setPackageName(getPackageName()); |
| 2822 | |
| 2823 | LayoutParams params = getWindow().getAttributes(); |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 2824 | boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) && |
| 2825 | (params.height == LayoutParams.MATCH_PARENT); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2826 | event.setFullScreen(isFullScreen); |
| 2827 | |
| 2828 | CharSequence title = getTitle(); |
| 2829 | if (!TextUtils.isEmpty(title)) { |
| 2830 | event.getText().add(title); |
| 2831 | } |
| 2832 | |
| 2833 | return true; |
| 2834 | } |
| 2835 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2836 | /** |
| 2837 | * Default implementation of |
| 2838 | * {@link android.view.Window.Callback#onCreatePanelView} |
| 2839 | * for activities. This |
| 2840 | * simply returns null so that all panel sub-windows will have the default |
| 2841 | * menu behavior. |
| 2842 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2843 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2844 | public View onCreatePanelView(int featureId) { |
| 2845 | return null; |
| 2846 | } |
| 2847 | |
| 2848 | /** |
| 2849 | * Default implementation of |
| 2850 | * {@link android.view.Window.Callback#onCreatePanelMenu} |
| 2851 | * for activities. This calls through to the new |
| 2852 | * {@link #onCreateOptionsMenu} method for the |
| 2853 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel, |
| 2854 | * so that subclasses of Activity don't need to deal with feature codes. |
| 2855 | */ |
| 2856 | public boolean onCreatePanelMenu(int featureId, Menu menu) { |
| 2857 | if (featureId == Window.FEATURE_OPTIONS_PANEL) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2858 | boolean show = onCreateOptionsMenu(menu); |
| 2859 | show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater()); |
| 2860 | return show; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2861 | } |
| 2862 | return false; |
| 2863 | } |
| 2864 | |
| 2865 | /** |
| 2866 | * Default implementation of |
| 2867 | * {@link android.view.Window.Callback#onPreparePanel} |
| 2868 | * for activities. This |
| 2869 | * calls through to the new {@link #onPrepareOptionsMenu} method for the |
| 2870 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} |
| 2871 | * panel, so that subclasses of |
| 2872 | * Activity don't need to deal with feature codes. |
| 2873 | */ |
| 2874 | public boolean onPreparePanel(int featureId, View view, Menu menu) { |
| 2875 | if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { |
| 2876 | boolean goforit = onPrepareOptionsMenu(menu); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2877 | goforit |= mFragments.dispatchPrepareOptionsMenu(menu); |
Adam Powell | ef31e7c | 2012-05-08 09:59:40 -0700 | [diff] [blame] | 2878 | return goforit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2879 | } |
| 2880 | return true; |
| 2881 | } |
| 2882 | |
| 2883 | /** |
| 2884 | * {@inheritDoc} |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2885 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2886 | * @return The default implementation returns true. |
| 2887 | */ |
| 2888 | public boolean onMenuOpened(int featureId, Menu menu) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 2889 | if (featureId == Window.FEATURE_ACTION_BAR) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2890 | initWindowDecorActionBar(); |
Adam Powell | 049dd3d | 2010-12-02 13:43:59 -0800 | [diff] [blame] | 2891 | if (mActionBar != null) { |
| 2892 | mActionBar.dispatchMenuVisibilityChanged(true); |
| 2893 | } else { |
| 2894 | Log.e(TAG, "Tried to open action bar menu with no action bar"); |
| 2895 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 2896 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2897 | return true; |
| 2898 | } |
| 2899 | |
| 2900 | /** |
| 2901 | * Default implementation of |
| 2902 | * {@link android.view.Window.Callback#onMenuItemSelected} |
| 2903 | * for activities. This calls through to the new |
| 2904 | * {@link #onOptionsItemSelected} method for the |
| 2905 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} |
| 2906 | * panel, so that subclasses of |
| 2907 | * Activity don't need to deal with feature codes. |
| 2908 | */ |
| 2909 | public boolean onMenuItemSelected(int featureId, MenuItem item) { |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 2910 | CharSequence titleCondensed = item.getTitleCondensed(); |
| 2911 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2912 | switch (featureId) { |
| 2913 | case Window.FEATURE_OPTIONS_PANEL: |
| 2914 | // Put event logging here so it gets called even if subclass |
| 2915 | // doesn't call through to superclass's implmeentation of each |
| 2916 | // of these methods below |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 2917 | if(titleCondensed != null) { |
| 2918 | EventLog.writeEvent(50000, 0, titleCondensed.toString()); |
| 2919 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2920 | if (onOptionsItemSelected(item)) { |
| 2921 | return true; |
| 2922 | } |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 2923 | if (mFragments.dispatchOptionsItemSelected(item)) { |
| 2924 | return true; |
| 2925 | } |
| 2926 | if (item.getItemId() == android.R.id.home && mActionBar != null && |
| 2927 | (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) { |
| 2928 | if (mParent == null) { |
Adam Powell | 07304f529 | 2012-05-07 17:32:44 -0700 | [diff] [blame] | 2929 | return onNavigateUp(); |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 2930 | } else { |
Adam Powell | 07304f529 | 2012-05-07 17:32:44 -0700 | [diff] [blame] | 2931 | return mParent.onNavigateUpFromChild(this); |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 2932 | } |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 2933 | } |
| 2934 | return false; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2935 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2936 | case Window.FEATURE_CONTEXT_MENU: |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 2937 | if(titleCondensed != null) { |
| 2938 | EventLog.writeEvent(50000, 1, titleCondensed.toString()); |
| 2939 | } |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 2940 | if (onContextItemSelected(item)) { |
| 2941 | return true; |
| 2942 | } |
| 2943 | return mFragments.dispatchContextItemSelected(item); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 2944 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2945 | default: |
| 2946 | return false; |
| 2947 | } |
| 2948 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2949 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2950 | /** |
| 2951 | * Default implementation of |
| 2952 | * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for |
| 2953 | * activities. This calls through to {@link #onOptionsMenuClosed(Menu)} |
| 2954 | * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel, |
| 2955 | * so that subclasses of Activity don't need to deal with feature codes. |
| 2956 | * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the |
| 2957 | * {@link #onContextMenuClosed(Menu)} will be called. |
| 2958 | */ |
| 2959 | public void onPanelClosed(int featureId, Menu menu) { |
| 2960 | switch (featureId) { |
| 2961 | case Window.FEATURE_OPTIONS_PANEL: |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2962 | mFragments.dispatchOptionsMenuClosed(menu); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2963 | onOptionsMenuClosed(menu); |
| 2964 | break; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2965 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2966 | case Window.FEATURE_CONTEXT_MENU: |
| 2967 | onContextMenuClosed(menu); |
| 2968 | break; |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 2969 | |
| 2970 | case Window.FEATURE_ACTION_BAR: |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2971 | initWindowDecorActionBar(); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 2972 | mActionBar.dispatchMenuVisibilityChanged(false); |
| 2973 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2974 | } |
| 2975 | } |
| 2976 | |
| 2977 | /** |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2978 | * Declare that the options menu has changed, so should be recreated. |
| 2979 | * The {@link #onCreateOptionsMenu(Menu)} method will be called the next |
| 2980 | * time it needs to be displayed. |
| 2981 | */ |
| 2982 | public void invalidateOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 2983 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 2984 | (mActionBar == null || !mActionBar.invalidateOptionsMenu())) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2985 | mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); |
| 2986 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2987 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2988 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2989 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2990 | * Initialize the contents of the Activity's standard options menu. You |
| 2991 | * should place your menu items in to <var>menu</var>. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2992 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2993 | * <p>This is only called once, the first time the options menu is |
| 2994 | * displayed. To update the menu every time it is displayed, see |
| 2995 | * {@link #onPrepareOptionsMenu}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2996 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2997 | * <p>The default implementation populates the menu with standard system |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2998 | * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that |
| 2999 | * they will be correctly ordered with application-defined menu items. |
| 3000 | * Deriving classes should always call through to the base implementation. |
| 3001 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3002 | * <p>You can safely hold on to <var>menu</var> (and any items created |
| 3003 | * from it), making modifications to it as desired, until the next |
| 3004 | * time onCreateOptionsMenu() is called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3005 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3006 | * <p>When you add items to the menu, you can implement the Activity's |
| 3007 | * {@link #onOptionsItemSelected} method to handle them there. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3008 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3009 | * @param menu The options menu in which you place your items. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3010 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3011 | * @return You must return true for the menu to be displayed; |
| 3012 | * if you return false it will not be shown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3013 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3014 | * @see #onPrepareOptionsMenu |
| 3015 | * @see #onOptionsItemSelected |
| 3016 | */ |
| 3017 | public boolean onCreateOptionsMenu(Menu menu) { |
| 3018 | if (mParent != null) { |
| 3019 | return mParent.onCreateOptionsMenu(menu); |
| 3020 | } |
| 3021 | return true; |
| 3022 | } |
| 3023 | |
| 3024 | /** |
| 3025 | * Prepare the Screen's standard options menu to be displayed. This is |
| 3026 | * called right before the menu is shown, every time it is shown. You can |
| 3027 | * use this method to efficiently enable/disable items or otherwise |
| 3028 | * dynamically modify the contents. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3029 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3030 | * <p>The default implementation updates the system menu items based on the |
| 3031 | * activity's state. Deriving classes should always call through to the |
| 3032 | * base class implementation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3033 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3034 | * @param menu The options menu as last shown or first initialized by |
| 3035 | * onCreateOptionsMenu(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3036 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3037 | * @return You must return true for the menu to be displayed; |
| 3038 | * if you return false it will not be shown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3039 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3040 | * @see #onCreateOptionsMenu |
| 3041 | */ |
| 3042 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 3043 | if (mParent != null) { |
| 3044 | return mParent.onPrepareOptionsMenu(menu); |
| 3045 | } |
| 3046 | return true; |
| 3047 | } |
| 3048 | |
| 3049 | /** |
| 3050 | * This hook is called whenever an item in your options menu is selected. |
| 3051 | * The default implementation simply returns false to have the normal |
| 3052 | * processing happen (calling the item's Runnable or sending a message to |
| 3053 | * its Handler as appropriate). You can use this method for any items |
| 3054 | * for which you would like to do processing without those other |
| 3055 | * facilities. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3056 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3057 | * <p>Derived classes should call through to the base class for it to |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3058 | * perform the default menu handling.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3059 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3060 | * @param item The menu item that was selected. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3061 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3062 | * @return boolean Return false to allow normal menu processing to |
| 3063 | * proceed, true to consume it here. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3064 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3065 | * @see #onCreateOptionsMenu |
| 3066 | */ |
| 3067 | public boolean onOptionsItemSelected(MenuItem item) { |
| 3068 | if (mParent != null) { |
| 3069 | return mParent.onOptionsItemSelected(item); |
| 3070 | } |
| 3071 | return false; |
| 3072 | } |
| 3073 | |
| 3074 | /** |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3075 | * This method is called whenever the user chooses to navigate Up within your application's |
| 3076 | * activity hierarchy from the action bar. |
| 3077 | * |
| 3078 | * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName} |
| 3079 | * was specified in the manifest for this activity or an activity-alias to it, |
| 3080 | * default Up navigation will be handled automatically. If any activity |
| 3081 | * along the parent chain requires extra Intent arguments, the Activity subclass |
| 3082 | * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)} |
| 3083 | * to supply those arguments.</p> |
| 3084 | * |
| 3085 | * <p>See <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a> |
| 3086 | * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a> |
| 3087 | * from the design guide for more information about navigating within your app.</p> |
| 3088 | * |
| 3089 | * <p>See the {@link TaskStackBuilder} class and the Activity methods |
| 3090 | * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and |
| 3091 | * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation. |
| 3092 | * The AppNavigation sample application in the Android SDK is also available for reference.</p> |
| 3093 | * |
| 3094 | * @return true if Up navigation completed successfully and this Activity was finished, |
| 3095 | * false otherwise. |
| 3096 | */ |
| 3097 | public boolean onNavigateUp() { |
| 3098 | // Automatically handle hierarchical Up navigation if the proper |
| 3099 | // metadata is available. |
| 3100 | Intent upIntent = getParentActivityIntent(); |
| 3101 | if (upIntent != null) { |
Adam Powell | 0fc5b2b | 2012-07-18 18:20:29 -0700 | [diff] [blame] | 3102 | if (mActivityInfo.taskAffinity == null) { |
| 3103 | // Activities with a null affinity are special; they really shouldn't |
| 3104 | // specify a parent activity intent in the first place. Just finish |
| 3105 | // the current activity and call it a day. |
| 3106 | finish(); |
| 3107 | } else if (shouldUpRecreateTask(upIntent)) { |
Adam Powell | f78a844 | 2012-05-01 18:09:32 -0700 | [diff] [blame] | 3108 | TaskStackBuilder b = TaskStackBuilder.create(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3109 | onCreateNavigateUpTaskStack(b); |
| 3110 | onPrepareNavigateUpTaskStack(b); |
| 3111 | b.startActivities(); |
Adam Powell | 3d193d9 | 2012-05-15 16:51:55 -0700 | [diff] [blame] | 3112 | |
| 3113 | // We can't finishAffinity if we have a result. |
| 3114 | // Fall back and simply finish the current activity instead. |
| 3115 | if (mResultCode != RESULT_CANCELED || mResultData != null) { |
| 3116 | // Tell the developer what's going on to avoid hair-pulling. |
| 3117 | Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result"); |
| 3118 | finish(); |
| 3119 | } else { |
| 3120 | finishAffinity(); |
| 3121 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3122 | } else { |
| 3123 | navigateUpTo(upIntent); |
| 3124 | } |
| 3125 | return true; |
| 3126 | } |
| 3127 | return false; |
| 3128 | } |
| 3129 | |
| 3130 | /** |
| 3131 | * This is called when a child activity of this one attempts to navigate up. |
| 3132 | * The default implementation simply calls onNavigateUp() on this activity (the parent). |
| 3133 | * |
| 3134 | * @param child The activity making the call. |
| 3135 | */ |
| 3136 | public boolean onNavigateUpFromChild(Activity child) { |
| 3137 | return onNavigateUp(); |
| 3138 | } |
| 3139 | |
| 3140 | /** |
| 3141 | * Define the synthetic task stack that will be generated during Up navigation from |
| 3142 | * a different task. |
| 3143 | * |
| 3144 | * <p>The default implementation of this method adds the parent chain of this activity |
| 3145 | * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications |
| 3146 | * may choose to override this method to construct the desired task stack in a different |
| 3147 | * way.</p> |
| 3148 | * |
Adam Powell | f019595 | 2012-05-02 21:38:54 -0700 | [diff] [blame] | 3149 | * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()} |
| 3150 | * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent |
| 3151 | * returned by {@link #getParentActivityIntent()}.</p> |
| 3152 | * |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3153 | * <p>Applications that wish to supply extra Intent parameters to the parent stack defined |
| 3154 | * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p> |
| 3155 | * |
| 3156 | * @param builder An empty TaskStackBuilder - the application should add intents representing |
| 3157 | * the desired task stack |
| 3158 | */ |
| 3159 | public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) { |
| 3160 | builder.addParentStack(this); |
| 3161 | } |
| 3162 | |
| 3163 | /** |
| 3164 | * Prepare the synthetic task stack that will be generated during Up navigation |
| 3165 | * from a different task. |
| 3166 | * |
| 3167 | * <p>This method receives the {@link TaskStackBuilder} with the constructed series of |
| 3168 | * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}. |
| 3169 | * If any extra data should be added to these intents before launching the new task, |
| 3170 | * the application should override this method and add that data here.</p> |
| 3171 | * |
| 3172 | * @param builder A TaskStackBuilder that has been populated with Intents by |
| 3173 | * onCreateNavigateUpTaskStack. |
| 3174 | */ |
| 3175 | public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) { |
| 3176 | } |
| 3177 | |
| 3178 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3179 | * This hook is called whenever the options menu is being closed (either by the user canceling |
| 3180 | * the menu with the back/menu button, or when an item is selected). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3181 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3182 | * @param menu The options menu as last shown or first initialized by |
| 3183 | * onCreateOptionsMenu(). |
| 3184 | */ |
| 3185 | public void onOptionsMenuClosed(Menu menu) { |
| 3186 | if (mParent != null) { |
| 3187 | mParent.onOptionsMenuClosed(menu); |
| 3188 | } |
| 3189 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3190 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3191 | /** |
| 3192 | * Programmatically opens the options menu. If the options menu is already |
| 3193 | * open, this method does nothing. |
| 3194 | */ |
| 3195 | public void openOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3196 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 3197 | (mActionBar == null || !mActionBar.openOptionsMenu())) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3198 | mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null); |
| 3199 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3200 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3202 | /** |
| 3203 | * Progammatically closes the options menu. If the options menu is already |
| 3204 | * closed, this method does nothing. |
| 3205 | */ |
| 3206 | public void closeOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3207 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) { |
| 3208 | mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL); |
| 3209 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | /** |
| 3213 | * Called when a context menu for the {@code view} is about to be shown. |
| 3214 | * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every |
| 3215 | * time the context menu is about to be shown and should be populated for |
| 3216 | * the view (or item inside the view for {@link AdapterView} subclasses, |
| 3217 | * this can be found in the {@code menuInfo})). |
| 3218 | * <p> |
| 3219 | * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an |
| 3220 | * item has been selected. |
| 3221 | * <p> |
| 3222 | * It is not safe to hold onto the context menu after this method returns. |
Scott Main | cdf5106 | 2013-01-08 20:03:05 -0800 | [diff] [blame] | 3223 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3224 | */ |
| 3225 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 3226 | } |
| 3227 | |
| 3228 | /** |
| 3229 | * Registers a context menu to be shown for the given view (multiple views |
| 3230 | * can show the context menu). This method will set the |
| 3231 | * {@link OnCreateContextMenuListener} on the view to this activity, so |
| 3232 | * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be |
| 3233 | * called when it is time to show the context menu. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3234 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3235 | * @see #unregisterForContextMenu(View) |
| 3236 | * @param view The view that should show a context menu. |
| 3237 | */ |
| 3238 | public void registerForContextMenu(View view) { |
| 3239 | view.setOnCreateContextMenuListener(this); |
| 3240 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3242 | /** |
| 3243 | * Prevents a context menu to be shown for the given view. This method will remove the |
| 3244 | * {@link OnCreateContextMenuListener} on the view. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3245 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3246 | * @see #registerForContextMenu(View) |
| 3247 | * @param view The view that should stop showing a context menu. |
| 3248 | */ |
| 3249 | public void unregisterForContextMenu(View view) { |
| 3250 | view.setOnCreateContextMenuListener(null); |
| 3251 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3252 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3253 | /** |
| 3254 | * Programmatically opens the context menu for a particular {@code view}. |
| 3255 | * The {@code view} should have been added via |
| 3256 | * {@link #registerForContextMenu(View)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3257 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3258 | * @param view The view to show the context menu for. |
| 3259 | */ |
| 3260 | public void openContextMenu(View view) { |
| 3261 | view.showContextMenu(); |
| 3262 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3263 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3264 | /** |
| 3265 | * Programmatically closes the most recently opened context menu, if showing. |
| 3266 | */ |
| 3267 | public void closeContextMenu() { |
Jose Lima | bd769a1 | 2015-01-28 10:43:15 -0800 | [diff] [blame] | 3268 | if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) { |
| 3269 | mWindow.closePanel(Window.FEATURE_CONTEXT_MENU); |
| 3270 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3271 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3273 | /** |
| 3274 | * This hook is called whenever an item in a context menu is selected. The |
| 3275 | * default implementation simply returns false to have the normal processing |
| 3276 | * happen (calling the item's Runnable or sending a message to its Handler |
| 3277 | * as appropriate). You can use this method for any items for which you |
| 3278 | * would like to do processing without those other facilities. |
| 3279 | * <p> |
| 3280 | * Use {@link MenuItem#getMenuInfo()} to get extra information set by the |
| 3281 | * View that added this menu item. |
| 3282 | * <p> |
| 3283 | * Derived classes should call through to the base class for it to perform |
| 3284 | * the default menu handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3285 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3286 | * @param item The context menu item that was selected. |
| 3287 | * @return boolean Return false to allow normal context menu processing to |
| 3288 | * proceed, true to consume it here. |
| 3289 | */ |
| 3290 | public boolean onContextItemSelected(MenuItem item) { |
| 3291 | if (mParent != null) { |
| 3292 | return mParent.onContextItemSelected(item); |
| 3293 | } |
| 3294 | return false; |
| 3295 | } |
| 3296 | |
| 3297 | /** |
| 3298 | * This hook is called whenever the context menu is being closed (either by |
| 3299 | * the user canceling the menu with the back/menu button, or when an item is |
| 3300 | * selected). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3301 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3302 | * @param menu The context menu that is being closed. |
| 3303 | */ |
| 3304 | public void onContextMenuClosed(Menu menu) { |
| 3305 | if (mParent != null) { |
| 3306 | mParent.onContextMenuClosed(menu); |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3311 | * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3312 | */ |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3313 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3314 | protected Dialog onCreateDialog(int id) { |
| 3315 | return null; |
| 3316 | } |
| 3317 | |
| 3318 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3319 | * Callback for creating dialogs that are managed (saved and restored) for you |
| 3320 | * by the activity. The default implementation calls through to |
| 3321 | * {@link #onCreateDialog(int)} for compatibility. |
| 3322 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 3323 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 3324 | * or later, consider instead using a {@link DialogFragment} instead.</em> |
| 3325 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3326 | * <p>If you use {@link #showDialog(int)}, the activity will call through to |
| 3327 | * this method the first time, and hang onto it thereafter. Any dialog |
| 3328 | * that is created by this method will automatically be saved and restored |
| 3329 | * for you, including whether it is showing. |
| 3330 | * |
| 3331 | * <p>If you would like the activity to manage saving and restoring dialogs |
| 3332 | * for you, you should override this method and handle any ids that are |
| 3333 | * passed to {@link #showDialog}. |
| 3334 | * |
| 3335 | * <p>If you would like an opportunity to prepare your dialog before it is shown, |
| 3336 | * override {@link #onPrepareDialog(int, Dialog, Bundle)}. |
| 3337 | * |
| 3338 | * @param id The id of the dialog. |
| 3339 | * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}. |
| 3340 | * @return The dialog. If you return null, the dialog will not be created. |
| 3341 | * |
| 3342 | * @see #onPrepareDialog(int, Dialog, Bundle) |
| 3343 | * @see #showDialog(int, Bundle) |
| 3344 | * @see #dismissDialog(int) |
| 3345 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3346 | * |
| 3347 | * @deprecated Use the new {@link DialogFragment} class with |
| 3348 | * {@link FragmentManager} instead; this is also |
| 3349 | * available on older platforms through the Android compatibility package. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3350 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3351 | @Nullable |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3352 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3353 | protected Dialog onCreateDialog(int id, Bundle args) { |
| 3354 | return onCreateDialog(id); |
| 3355 | } |
| 3356 | |
| 3357 | /** |
| 3358 | * @deprecated Old no-arguments version of |
| 3359 | * {@link #onPrepareDialog(int, Dialog, Bundle)}. |
| 3360 | */ |
| 3361 | @Deprecated |
| 3362 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 3363 | dialog.setOwnerActivity(this); |
| 3364 | } |
| 3365 | |
| 3366 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3367 | * Provides an opportunity to prepare a managed dialog before it is being |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3368 | * shown. The default implementation calls through to |
| 3369 | * {@link #onPrepareDialog(int, Dialog)} for compatibility. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3370 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3371 | * <p> |
| 3372 | * Override this if you need to update a managed dialog based on the state |
| 3373 | * of the application each time it is shown. For example, a time picker |
| 3374 | * dialog might want to be updated with the current time. You should call |
| 3375 | * through to the superclass's implementation. The default implementation |
| 3376 | * will set this Activity as the owner activity on the Dialog. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3377 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3378 | * @param id The id of the managed dialog. |
| 3379 | * @param dialog The dialog. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3380 | * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}. |
| 3381 | * @see #onCreateDialog(int, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3382 | * @see #showDialog(int) |
| 3383 | * @see #dismissDialog(int) |
| 3384 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3385 | * |
| 3386 | * @deprecated Use the new {@link DialogFragment} class with |
| 3387 | * {@link FragmentManager} instead; this is also |
| 3388 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3389 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3390 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3391 | protected void onPrepareDialog(int id, Dialog dialog, Bundle args) { |
| 3392 | onPrepareDialog(id, dialog); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3396 | * Simple version of {@link #showDialog(int, Bundle)} that does not |
| 3397 | * take any arguments. Simply calls {@link #showDialog(int, Bundle)} |
| 3398 | * with null arguments. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3399 | * |
| 3400 | * @deprecated Use the new {@link DialogFragment} class with |
| 3401 | * {@link FragmentManager} instead; this is also |
| 3402 | * available on older platforms through the Android compatibility package. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3403 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3404 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3405 | public final void showDialog(int id) { |
| 3406 | showDialog(id, null); |
| 3407 | } |
| 3408 | |
| 3409 | /** |
| 3410 | * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3411 | * will be made with the same id the first time this is called for a given |
| 3412 | * id. From thereafter, the dialog will be automatically saved and restored. |
| 3413 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 3414 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 3415 | * or later, consider instead using a {@link DialogFragment} instead.</em> |
| 3416 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3417 | * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3418 | * be made to provide an opportunity to do any timely preparation. |
| 3419 | * |
| 3420 | * @param id The id of the managed dialog. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3421 | * @param args Arguments to pass through to the dialog. These will be saved |
| 3422 | * and restored for you. Note that if the dialog is already created, |
| 3423 | * {@link #onCreateDialog(int, Bundle)} will not be called with the new |
| 3424 | * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be. |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 3425 | * If you need to rebuild the dialog, call {@link #removeDialog(int)} first. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3426 | * @return Returns true if the Dialog was created; false is returned if |
| 3427 | * it is not created because {@link #onCreateDialog(int, Bundle)} returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3428 | * |
Joe Onorato | 37296dc | 2009-07-31 17:58:55 -0700 | [diff] [blame] | 3429 | * @see Dialog |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3430 | * @see #onCreateDialog(int, Bundle) |
| 3431 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3432 | * @see #dismissDialog(int) |
| 3433 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3434 | * |
| 3435 | * @deprecated Use the new {@link DialogFragment} class with |
| 3436 | * {@link FragmentManager} instead; this is also |
| 3437 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3438 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3439 | @Nullable |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3440 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3441 | public final boolean showDialog(int id, Bundle args) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3442 | if (mManagedDialogs == null) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3443 | mManagedDialogs = new SparseArray<ManagedDialog>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3444 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3445 | ManagedDialog md = mManagedDialogs.get(id); |
| 3446 | if (md == null) { |
| 3447 | md = new ManagedDialog(); |
| 3448 | md.mDialog = createDialog(id, null, args); |
| 3449 | if (md.mDialog == null) { |
| 3450 | return false; |
| 3451 | } |
| 3452 | mManagedDialogs.put(id, md); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3453 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3454 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3455 | md.mArgs = args; |
| 3456 | onPrepareDialog(id, md.mDialog, args); |
| 3457 | md.mDialog.show(); |
| 3458 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | /** |
| 3462 | * Dismiss a dialog that was previously shown via {@link #showDialog(int)}. |
| 3463 | * |
| 3464 | * @param id The id of the managed dialog. |
| 3465 | * |
| 3466 | * @throws IllegalArgumentException if the id was not previously shown via |
| 3467 | * {@link #showDialog(int)}. |
| 3468 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3469 | * @see #onCreateDialog(int, Bundle) |
| 3470 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3471 | * @see #showDialog(int) |
| 3472 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3473 | * |
| 3474 | * @deprecated Use the new {@link DialogFragment} class with |
| 3475 | * {@link FragmentManager} instead; this is also |
| 3476 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3477 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3478 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3479 | public final void dismissDialog(int id) { |
| 3480 | if (mManagedDialogs == null) { |
| 3481 | throw missingDialog(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3482 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3483 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3484 | final ManagedDialog md = mManagedDialogs.get(id); |
| 3485 | if (md == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3486 | throw missingDialog(id); |
| 3487 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3488 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | /** |
| 3492 | * Creates an exception to throw if a user passed in a dialog id that is |
| 3493 | * unexpected. |
| 3494 | */ |
| 3495 | private IllegalArgumentException missingDialog(int id) { |
| 3496 | return new IllegalArgumentException("no dialog with id " + id + " was ever " |
| 3497 | + "shown via Activity#showDialog"); |
| 3498 | } |
| 3499 | |
| 3500 | /** |
| 3501 | * Removes any internal references to a dialog managed by this Activity. |
| 3502 | * If the dialog is showing, it will dismiss it as part of the clean up. |
| 3503 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3504 | * <p>This can be useful if you know that you will never show a dialog again and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3505 | * want to avoid the overhead of saving and restoring it in the future. |
| 3506 | * |
Dianne Hackborn | d2ce8bbb | 2010-10-06 16:46:05 -0700 | [diff] [blame] | 3507 | * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function |
| 3508 | * will not throw an exception if you try to remove an ID that does not |
| 3509 | * currently have an associated dialog.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3510 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3511 | * @param id The id of the managed dialog. |
| 3512 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3513 | * @see #onCreateDialog(int, Bundle) |
| 3514 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3515 | * @see #showDialog(int) |
| 3516 | * @see #dismissDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3517 | * |
| 3518 | * @deprecated Use the new {@link DialogFragment} class with |
| 3519 | * {@link FragmentManager} instead; this is also |
| 3520 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3521 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3522 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3523 | public final void removeDialog(int id) { |
Dianne Hackborn | d2ce8bbb | 2010-10-06 16:46:05 -0700 | [diff] [blame] | 3524 | if (mManagedDialogs != null) { |
| 3525 | final ManagedDialog md = mManagedDialogs.get(id); |
| 3526 | if (md != null) { |
| 3527 | md.mDialog.dismiss(); |
| 3528 | mManagedDialogs.remove(id); |
| 3529 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3530 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | /** |
| 3534 | * This hook is called when the user signals the desire to start a search. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3535 | * |
Bjorn Bringert | 6266e40 | 2009-09-25 14:25:41 +0100 | [diff] [blame] | 3536 | * <p>You can use this function as a simple way to launch the search UI, in response to a |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3537 | * menu item, search button, or other widgets within your activity. Unless overidden, |
Bjorn Bringert | 6266e40 | 2009-09-25 14:25:41 +0100 | [diff] [blame] | 3538 | * calling this function is the same as calling |
| 3539 | * {@link #startSearch startSearch(null, false, null, false)}, which launches |
| 3540 | * search for the current activity as specified in its manifest, see {@link SearchManager}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3541 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3542 | * <p>You can override this function to force global search, e.g. in response to a dedicated |
| 3543 | * search key, or to block search entirely (by simply returning false). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3544 | * |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3545 | * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default |
| 3546 | * implementation changes to simply return false and you must supply your own custom |
| 3547 | * implementation if you want to support search.</p> |
| 3548 | * |
| 3549 | * @return Returns {@code true} if search launched, and {@code false} if the activity does |
| 3550 | * not respond to search. The default implementation always returns {@code true}, except |
| 3551 | * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3552 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3553 | * @see android.app.SearchManager |
| 3554 | */ |
| 3555 | public boolean onSearchRequested() { |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3556 | if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK) |
| 3557 | != Configuration.UI_MODE_TYPE_TELEVISION) { |
| 3558 | startSearch(null, false, null, false); |
| 3559 | return true; |
| 3560 | } else { |
| 3561 | return false; |
| 3562 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3563 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3564 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3565 | /** |
| 3566 | * This hook is called to launch the search UI. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3567 | * |
| 3568 | * <p>It is typically called from onSearchRequested(), either directly from |
| 3569 | * Activity.onSearchRequested() or from an overridden version in any given |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3570 | * Activity. If your goal is simply to activate search, it is preferred to call |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3571 | * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3572 | * is to inject specific data such as context data, it is preferred to <i>override</i> |
| 3573 | * onSearchRequested(), so that any callers to it will benefit from the override. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3574 | * |
| 3575 | * @param initialQuery Any non-null non-empty string will be inserted as |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3576 | * pre-entered text in the search query box. |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3577 | * @param selectInitialQuery If true, the initial query will be preselected, which means that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3578 | * any further typing will replace it. This is useful for cases where an entire pre-formed |
| 3579 | * query is being inserted. If false, the selection point will be placed at the end of the |
| 3580 | * inserted query. This is useful when the inserted query is text that the user entered, |
| 3581 | * and the user would expect to be able to keep typing. <i>This parameter is only meaningful |
| 3582 | * if initialQuery is a non-empty string.</i> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3583 | * @param appSearchData An application can insert application-specific |
| 3584 | * context here, in order to improve quality or specificity of its own |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3585 | * searches. This data will be returned with SEARCH intent(s). Null if |
| 3586 | * no extra data is required. |
| 3587 | * @param globalSearch If false, this will only launch the search that has been specifically |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3588 | * defined by the application (which is usually defined as a local search). If no default |
Mike LeBeau | cfa419b | 2009-08-17 10:56:02 -0700 | [diff] [blame] | 3589 | * search is defined in the current application or activity, global search will be launched. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3590 | * If true, this will always launch a platform-global (e.g. web-based) search instead. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3591 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3592 | * @see android.app.SearchManager |
| 3593 | * @see #onSearchRequested |
| 3594 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3595 | public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery, |
| 3596 | @Nullable Bundle appSearchData, boolean globalSearch) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3597 | ensureSearchManager(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 3598 | mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(), |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3599 | appSearchData, globalSearch); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | /** |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3603 | * Similar to {@link #startSearch}, but actually fires off the search query after invoking |
| 3604 | * the search dialog. Made available for testing purposes. |
| 3605 | * |
| 3606 | * @param query The query to trigger. If empty, the request will be ignored. |
| 3607 | * @param appSearchData An application can insert application-specific |
| 3608 | * context here, in order to improve quality or specificity of its own |
| 3609 | * searches. This data will be returned with SEARCH intent(s). Null if |
| 3610 | * no extra data is required. |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3611 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3612 | public void triggerSearch(String query, @Nullable Bundle appSearchData) { |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3613 | ensureSearchManager(); |
Bjorn Bringert | b782a2f | 2009-10-01 09:57:33 +0100 | [diff] [blame] | 3614 | mSearchManager.triggerSearch(query, getComponentName(), appSearchData); |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3618 | * Request that key events come to this activity. Use this if your |
| 3619 | * activity has no views with focus, but the activity still wants |
| 3620 | * a chance to process key events. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3621 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3622 | * @see android.view.Window#takeKeyEvents |
| 3623 | */ |
| 3624 | public void takeKeyEvents(boolean get) { |
| 3625 | getWindow().takeKeyEvents(get); |
| 3626 | } |
| 3627 | |
| 3628 | /** |
| 3629 | * Enable extended window features. This is a convenience for calling |
| 3630 | * {@link android.view.Window#requestFeature getWindow().requestFeature()}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3631 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3632 | * @param featureId The desired feature as defined in |
| 3633 | * {@link android.view.Window}. |
| 3634 | * @return Returns true if the requested feature is supported and now |
| 3635 | * enabled. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3636 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3637 | * @see android.view.Window#requestFeature |
| 3638 | */ |
| 3639 | public final boolean requestWindowFeature(int featureId) { |
| 3640 | return getWindow().requestFeature(featureId); |
| 3641 | } |
| 3642 | |
| 3643 | /** |
| 3644 | * Convenience for calling |
| 3645 | * {@link android.view.Window#setFeatureDrawableResource}. |
| 3646 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 3647 | public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3648 | getWindow().setFeatureDrawableResource(featureId, resId); |
| 3649 | } |
| 3650 | |
| 3651 | /** |
| 3652 | * Convenience for calling |
| 3653 | * {@link android.view.Window#setFeatureDrawableUri}. |
| 3654 | */ |
| 3655 | public final void setFeatureDrawableUri(int featureId, Uri uri) { |
| 3656 | getWindow().setFeatureDrawableUri(featureId, uri); |
| 3657 | } |
| 3658 | |
| 3659 | /** |
| 3660 | * Convenience for calling |
| 3661 | * {@link android.view.Window#setFeatureDrawable(int, Drawable)}. |
| 3662 | */ |
| 3663 | public final void setFeatureDrawable(int featureId, Drawable drawable) { |
| 3664 | getWindow().setFeatureDrawable(featureId, drawable); |
| 3665 | } |
| 3666 | |
| 3667 | /** |
| 3668 | * Convenience for calling |
| 3669 | * {@link android.view.Window#setFeatureDrawableAlpha}. |
| 3670 | */ |
| 3671 | public final void setFeatureDrawableAlpha(int featureId, int alpha) { |
| 3672 | getWindow().setFeatureDrawableAlpha(featureId, alpha); |
| 3673 | } |
| 3674 | |
| 3675 | /** |
| 3676 | * Convenience for calling |
| 3677 | * {@link android.view.Window#getLayoutInflater}. |
| 3678 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3679 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3680 | public LayoutInflater getLayoutInflater() { |
| 3681 | return getWindow().getLayoutInflater(); |
| 3682 | } |
| 3683 | |
| 3684 | /** |
| 3685 | * Returns a {@link MenuInflater} with this context. |
| 3686 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3687 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3688 | public MenuInflater getMenuInflater() { |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 3689 | // Make sure that action views can get an appropriate theme. |
| 3690 | if (mMenuInflater == null) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 3691 | initWindowDecorActionBar(); |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 3692 | if (mActionBar != null) { |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 3693 | mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this); |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 3694 | } else { |
| 3695 | mMenuInflater = new MenuInflater(this); |
| 3696 | } |
| 3697 | } |
| 3698 | return mMenuInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | @Override |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 3702 | protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3703 | boolean first) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3704 | if (mParent == null) { |
| 3705 | super.onApplyThemeResource(theme, resid, first); |
| 3706 | } else { |
| 3707 | try { |
| 3708 | theme.setTo(mParent.getTheme()); |
| 3709 | } catch (Exception e) { |
| 3710 | // Empty |
| 3711 | } |
| 3712 | theme.applyStyle(resid, false); |
| 3713 | } |
Winson Chung | 03a9bae | 2014-05-02 09:56:12 -0700 | [diff] [blame] | 3714 | |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 3715 | // Get the primary color and update the TaskDescription for this activity |
Winson Chung | 4d1a434 | 2014-05-09 16:42:00 -0700 | [diff] [blame] | 3716 | if (theme != null) { |
| 3717 | TypedArray a = theme.obtainStyledAttributes(com.android.internal.R.styleable.Theme); |
Winson Chung | aca9602 | 2014-05-09 15:04:08 -0700 | [diff] [blame] | 3718 | int colorPrimary = a.getColor(com.android.internal.R.styleable.Theme_colorPrimary, 0); |
| 3719 | a.recycle(); |
| 3720 | if (colorPrimary != 0) { |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 3721 | ActivityManager.TaskDescription v = new ActivityManager.TaskDescription(null, null, |
| 3722 | colorPrimary); |
| 3723 | setTaskDescription(v); |
Winson Chung | aca9602 | 2014-05-09 15:04:08 -0700 | [diff] [blame] | 3724 | } |
Winson Chung | 03a9bae | 2014-05-02 09:56:12 -0700 | [diff] [blame] | 3725 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3729 | * Same as calling {@link #startActivityForResult(Intent, int, Bundle)} |
| 3730 | * with no options. |
| 3731 | * |
| 3732 | * @param intent The intent to start. |
| 3733 | * @param requestCode If >= 0, this code will be returned in |
| 3734 | * onActivityResult() when the activity exits. |
| 3735 | * |
| 3736 | * @throws android.content.ActivityNotFoundException |
| 3737 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3738 | * @see #startActivity |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3739 | */ |
| 3740 | public void startActivityForResult(Intent intent, int requestCode) { |
George Mount | abb63cb | 2014-06-23 11:17:58 -0700 | [diff] [blame] | 3741 | startActivityForResult(intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3745 | * Launch an activity for which you would like a result when it finished. |
| 3746 | * When this activity exits, your |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3747 | * onActivityResult() method will be called with the given requestCode. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3748 | * Using a negative requestCode is the same as calling |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3749 | * {@link #startActivity} (the activity is not launched as a sub-activity). |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3750 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3751 | * <p>Note that this method should only be used with Intent protocols |
| 3752 | * that are defined to return a result. In other protocols (such as |
| 3753 | * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may |
| 3754 | * not get the result when you expect. For example, if the activity you |
| 3755 | * are launching uses the singleTask launch mode, it will not run in your |
| 3756 | * task and thus you will immediately receive a cancel result. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3757 | * |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3758 | * <p>As a special case, if you call startActivityForResult() with a requestCode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3759 | * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3760 | * activity, then your window will not be displayed until a result is |
| 3761 | * returned back from the started activity. This is to avoid visible |
| 3762 | * flickering when redirecting to another activity. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3763 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3764 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 3765 | * if there was no Activity found to run the given Intent. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3766 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3767 | * @param intent The intent to start. |
| 3768 | * @param requestCode If >= 0, this code will be returned in |
| 3769 | * onActivityResult() when the activity exits. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3770 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 3771 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 3772 | * Context.startActivity(Intent, Bundle)} for more details. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3773 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3774 | * @throws android.content.ActivityNotFoundException |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3775 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 3776 | * @see #startActivity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3777 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3778 | public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3779 | if (mParent == null) { |
| 3780 | Instrumentation.ActivityResult ar = |
| 3781 | mInstrumentation.execStartActivity( |
| 3782 | this, mMainThread.getApplicationThread(), mToken, this, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3783 | intent, requestCode, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3784 | if (ar != null) { |
| 3785 | mMainThread.sendActivityResult( |
| 3786 | mToken, mEmbeddedID, requestCode, ar.getResultCode(), |
| 3787 | ar.getResultData()); |
| 3788 | } |
| 3789 | if (requestCode >= 0) { |
| 3790 | // If this start is requesting a result, we can avoid making |
| 3791 | // the activity visible until the result is received. Setting |
| 3792 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 3793 | // activity hidden during this time, to avoid flickering. |
| 3794 | // This can only be done when a result is requested because |
| 3795 | // that guarantees we will get information back when the |
| 3796 | // activity is finished, no matter what happens to it. |
| 3797 | mStartedActivity = true; |
| 3798 | } |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 3799 | |
| 3800 | final View decor = mWindow != null ? mWindow.peekDecorView() : null; |
| 3801 | if (decor != null) { |
| 3802 | decor.cancelPendingInputEvents(); |
| 3803 | } |
| 3804 | // TODO Consider clearing/flushing other event sources and events for child windows. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3805 | } else { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3806 | if (options != null) { |
| 3807 | mParent.startActivityFromChild(this, intent, requestCode, options); |
| 3808 | } else { |
| 3809 | // Note we want to go through this method for compatibility with |
| 3810 | // existing applications that may have overridden it. |
| 3811 | mParent.startActivityFromChild(this, intent, requestCode); |
| 3812 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3813 | } |
George Mount | ff24328 | 2014-07-07 16:12:07 -0700 | [diff] [blame] | 3814 | if (options != null && !isTopOfTask()) { |
| 3815 | mActivityTransitionState.startExitOutTransition(this, options); |
| 3816 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | /** |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 3820 | * @hide Implement to provide correct calling token. |
| 3821 | */ |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 3822 | public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) { |
| 3823 | startActivityForResultAsUser(intent, requestCode, null, user); |
| 3824 | } |
| 3825 | |
| 3826 | /** |
| 3827 | * @hide Implement to provide correct calling token. |
| 3828 | */ |
| 3829 | public void startActivityForResultAsUser(Intent intent, int requestCode, |
| 3830 | @Nullable Bundle options, UserHandle user) { |
| 3831 | if (options != null) { |
| 3832 | mActivityTransitionState.startExitOutTransition(this, options); |
| 3833 | } |
| 3834 | if (mParent != null) { |
| 3835 | throw new RuntimeException("Can't be called from a child"); |
| 3836 | } |
| 3837 | Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( |
| 3838 | this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, |
| 3839 | options, user); |
| 3840 | if (ar != null) { |
| 3841 | mMainThread.sendActivityResult( |
| 3842 | mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); |
| 3843 | } |
| 3844 | if (requestCode >= 0) { |
| 3845 | // If this start is requesting a result, we can avoid making |
| 3846 | // the activity visible until the result is received. Setting |
| 3847 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 3848 | // activity hidden during this time, to avoid flickering. |
| 3849 | // This can only be done when a result is requested because |
| 3850 | // that guarantees we will get information back when the |
| 3851 | // activity is finished, no matter what happens to it. |
| 3852 | mStartedActivity = true; |
| 3853 | } |
| 3854 | |
| 3855 | final View decor = mWindow != null ? mWindow.peekDecorView() : null; |
| 3856 | if (decor != null) { |
| 3857 | decor.cancelPendingInputEvents(); |
| 3858 | } |
| 3859 | } |
| 3860 | |
| 3861 | /** |
| 3862 | * @hide Implement to provide correct calling token. |
| 3863 | */ |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 3864 | public void startActivityAsUser(Intent intent, UserHandle user) { |
| 3865 | startActivityAsUser(intent, null, user); |
| 3866 | } |
| 3867 | |
| 3868 | /** |
| 3869 | * @hide Implement to provide correct calling token. |
| 3870 | */ |
| 3871 | public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) { |
| 3872 | if (mParent != null) { |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 3873 | throw new RuntimeException("Can't be called from a child"); |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 3874 | } |
| 3875 | Instrumentation.ActivityResult ar = |
| 3876 | mInstrumentation.execStartActivity( |
| 3877 | this, mMainThread.getApplicationThread(), mToken, this, |
| 3878 | intent, -1, options, user); |
| 3879 | if (ar != null) { |
| 3880 | mMainThread.sendActivityResult( |
| 3881 | mToken, mEmbeddedID, -1, ar.getResultCode(), |
| 3882 | ar.getResultData()); |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | /** |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 3887 | * Start a new activity as if it was started by the activity that started our |
| 3888 | * current activity. This is for the resolver and chooser activities, which operate |
| 3889 | * as intermediaries that dispatch their intent to the target the user selects -- to |
| 3890 | * do this, they must perform all security checks including permission grants as if |
| 3891 | * their launch had come from the original activity. |
| 3892 | * @hide |
| 3893 | */ |
Jeff Sharkey | 9797880 | 2014-10-14 10:48:18 -0700 | [diff] [blame] | 3894 | public void startActivityAsCaller(Intent intent, @Nullable Bundle options, int userId) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 3895 | if (mParent != null) { |
| 3896 | throw new RuntimeException("Can't be called from a child"); |
| 3897 | } |
| 3898 | Instrumentation.ActivityResult ar = |
| 3899 | mInstrumentation.execStartActivityAsCaller( |
| 3900 | this, mMainThread.getApplicationThread(), mToken, this, |
Jeff Sharkey | 9797880 | 2014-10-14 10:48:18 -0700 | [diff] [blame] | 3901 | intent, -1, options, userId); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 3902 | if (ar != null) { |
| 3903 | mMainThread.sendActivityResult( |
| 3904 | mToken, mEmbeddedID, -1, ar.getResultCode(), |
| 3905 | ar.getResultData()); |
| 3906 | } |
| 3907 | } |
| 3908 | |
| 3909 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3910 | * Same as calling {@link #startIntentSenderForResult(IntentSender, int, |
| 3911 | * Intent, int, int, int, Bundle)} with no options. |
| 3912 | * |
| 3913 | * @param intent The IntentSender to launch. |
| 3914 | * @param requestCode If >= 0, this code will be returned in |
| 3915 | * onActivityResult() when the activity exits. |
| 3916 | * @param fillInIntent If non-null, this will be provided as the |
| 3917 | * intent parameter to {@link IntentSender#sendIntent}. |
| 3918 | * @param flagsMask Intent flags in the original IntentSender that you |
| 3919 | * would like to change. |
| 3920 | * @param flagsValues Desired values for any bits set in |
| 3921 | * <var>flagsMask</var> |
| 3922 | * @param extraFlags Always set to 0. |
| 3923 | */ |
| 3924 | public void startIntentSenderForResult(IntentSender intent, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3925 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3926 | throws IntentSender.SendIntentException { |
| 3927 | startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask, |
| 3928 | flagsValues, extraFlags, null); |
| 3929 | } |
| 3930 | |
| 3931 | /** |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3932 | * Like {@link #startActivityForResult(Intent, int)}, but allowing you |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3933 | * to use a IntentSender to describe the activity to be started. If |
| 3934 | * the IntentSender is for an activity, that activity will be started |
| 3935 | * as if you had called the regular {@link #startActivityForResult(Intent, int)} |
| 3936 | * here; otherwise, its associated action will be executed (such as |
| 3937 | * sending a broadcast) as if you had called |
| 3938 | * {@link IntentSender#sendIntent IntentSender.sendIntent} on it. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3939 | * |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3940 | * @param intent The IntentSender to launch. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3941 | * @param requestCode If >= 0, this code will be returned in |
| 3942 | * onActivityResult() when the activity exits. |
| 3943 | * @param fillInIntent If non-null, this will be provided as the |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3944 | * intent parameter to {@link IntentSender#sendIntent}. |
| 3945 | * @param flagsMask Intent flags in the original IntentSender that you |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3946 | * would like to change. |
| 3947 | * @param flagsValues Desired values for any bits set in |
| 3948 | * <var>flagsMask</var> |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3949 | * @param extraFlags Always set to 0. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3950 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 3951 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 3952 | * Context.startActivity(Intent, Bundle)} for more details. If options |
| 3953 | * have also been supplied by the IntentSender, options given here will |
| 3954 | * override any that conflict with those given by the IntentSender. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3955 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3956 | public void startIntentSenderForResult(IntentSender intent, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3957 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3958 | Bundle options) throws IntentSender.SendIntentException { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3959 | if (mParent == null) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3960 | startIntentSenderForResultInner(intent, requestCode, fillInIntent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3961 | flagsMask, flagsValues, this, options); |
| 3962 | } else if (options != null) { |
| 3963 | mParent.startIntentSenderFromChild(this, intent, requestCode, |
| 3964 | fillInIntent, flagsMask, flagsValues, extraFlags, options); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3965 | } else { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3966 | // Note we want to go through this call for compatibility with |
| 3967 | // existing applications that may have overridden the method. |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3968 | mParent.startIntentSenderFromChild(this, intent, requestCode, |
| 3969 | fillInIntent, flagsMask, flagsValues, extraFlags); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3970 | } |
| 3971 | } |
| 3972 | |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3973 | private void startIntentSenderForResultInner(IntentSender intent, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3974 | Intent fillInIntent, int flagsMask, int flagsValues, Activity activity, |
| 3975 | Bundle options) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3976 | throws IntentSender.SendIntentException { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3977 | try { |
| 3978 | String resolvedType = null; |
| 3979 | if (fillInIntent != null) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 3980 | fillInIntent.migrateExtraStreamToClipData(); |
| 3981 | fillInIntent.prepareToLeaveProcess(); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3982 | resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); |
| 3983 | } |
| 3984 | int result = ActivityManagerNative.getDefault() |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3985 | .startActivityIntentSender(mMainThread.getApplicationThread(), intent, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3986 | fillInIntent, resolvedType, mToken, activity.mEmbeddedID, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 3987 | requestCode, flagsMask, flagsValues, options); |
| 3988 | if (result == ActivityManager.START_CANCELED) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3989 | throw new IntentSender.SendIntentException(); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3990 | } |
| 3991 | Instrumentation.checkStartActivityResult(result, null); |
| 3992 | } catch (RemoteException e) { |
| 3993 | } |
| 3994 | if (requestCode >= 0) { |
| 3995 | // If this start is requesting a result, we can avoid making |
| 3996 | // the activity visible until the result is received. Setting |
| 3997 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 3998 | // activity hidden during this time, to avoid flickering. |
| 3999 | // This can only be done when a result is requested because |
| 4000 | // that guarantees we will get information back when the |
| 4001 | // activity is finished, no matter what happens to it. |
| 4002 | mStartedActivity = true; |
| 4003 | } |
| 4004 | } |
| 4005 | |
| 4006 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4007 | * Same as {@link #startActivity(Intent, Bundle)} with no options |
| 4008 | * specified. |
| 4009 | * |
| 4010 | * @param intent The intent to start. |
| 4011 | * |
| 4012 | * @throws android.content.ActivityNotFoundException |
| 4013 | * |
| 4014 | * @see {@link #startActivity(Intent, Bundle)} |
| 4015 | * @see #startActivityForResult |
| 4016 | */ |
| 4017 | @Override |
| 4018 | public void startActivity(Intent intent) { |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 4019 | this.startActivity(intent, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4020 | } |
| 4021 | |
| 4022 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4023 | * Launch a new activity. You will not receive any information about when |
| 4024 | * the activity exits. This implementation overrides the base version, |
| 4025 | * providing information about |
| 4026 | * the activity performing the launch. Because of this additional |
| 4027 | * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not |
| 4028 | * required; if not specified, the new activity will be added to the |
| 4029 | * task of the caller. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4030 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4031 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4032 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4033 | * |
| 4034 | * @param intent The intent to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4035 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4036 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4037 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4038 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4039 | * @throws android.content.ActivityNotFoundException |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4040 | * |
| 4041 | * @see {@link #startActivity(Intent)} |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4042 | * @see #startActivityForResult |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4043 | */ |
| 4044 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4045 | public void startActivity(Intent intent, @Nullable Bundle options) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4046 | if (options != null) { |
| 4047 | startActivityForResult(intent, -1, options); |
| 4048 | } else { |
| 4049 | // Note we want to go through this call for compatibility with |
| 4050 | // applications that may have overridden the method. |
| 4051 | startActivityForResult(intent, -1); |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | /** |
| 4056 | * Same as {@link #startActivities(Intent[], Bundle)} with no options |
| 4057 | * specified. |
| 4058 | * |
| 4059 | * @param intents The intents to start. |
| 4060 | * |
| 4061 | * @throws android.content.ActivityNotFoundException |
| 4062 | * |
| 4063 | * @see {@link #startActivities(Intent[], Bundle)} |
| 4064 | * @see #startActivityForResult |
| 4065 | */ |
| 4066 | @Override |
| 4067 | public void startActivities(Intent[] intents) { |
| 4068 | startActivities(intents, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | /** |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4072 | * Launch a new activity. You will not receive any information about when |
| 4073 | * the activity exits. This implementation overrides the base version, |
| 4074 | * providing information about |
| 4075 | * the activity performing the launch. Because of this additional |
| 4076 | * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not |
| 4077 | * required; if not specified, the new activity will be added to the |
| 4078 | * task of the caller. |
| 4079 | * |
| 4080 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4081 | * if there was no Activity found to run the given Intent. |
| 4082 | * |
| 4083 | * @param intents The intents to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4084 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4085 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4086 | * Context.startActivity(Intent, Bundle)} for more details. |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4087 | * |
| 4088 | * @throws android.content.ActivityNotFoundException |
| 4089 | * |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4090 | * @see {@link #startActivities(Intent[])} |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4091 | * @see #startActivityForResult |
| 4092 | */ |
| 4093 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4094 | public void startActivities(Intent[] intents, @Nullable Bundle options) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4095 | mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(), |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4096 | mToken, this, intents, options); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4097 | } |
| 4098 | |
| 4099 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4100 | * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)} |
| 4101 | * with no options. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4102 | * |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4103 | * @param intent The IntentSender to launch. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4104 | * @param fillInIntent If non-null, this will be provided as the |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4105 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4106 | * @param flagsMask Intent flags in the original IntentSender that you |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4107 | * would like to change. |
| 4108 | * @param flagsValues Desired values for any bits set in |
| 4109 | * <var>flagsMask</var> |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4110 | * @param extraFlags Always set to 0. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4111 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4112 | public void startIntentSender(IntentSender intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4113 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4114 | throws IntentSender.SendIntentException { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4115 | startIntentSender(intent, fillInIntent, flagsMask, flagsValues, |
| 4116 | extraFlags, null); |
| 4117 | } |
| 4118 | |
| 4119 | /** |
| 4120 | * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender |
| 4121 | * to start; see |
| 4122 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)} |
| 4123 | * for more information. |
| 4124 | * |
| 4125 | * @param intent The IntentSender to launch. |
| 4126 | * @param fillInIntent If non-null, this will be provided as the |
| 4127 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4128 | * @param flagsMask Intent flags in the original IntentSender that you |
| 4129 | * would like to change. |
| 4130 | * @param flagsValues Desired values for any bits set in |
| 4131 | * <var>flagsMask</var> |
| 4132 | * @param extraFlags Always set to 0. |
| 4133 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4134 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4135 | * Context.startActivity(Intent, Bundle)} for more details. If options |
| 4136 | * have also been supplied by the IntentSender, options given here will |
| 4137 | * override any that conflict with those given by the IntentSender. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4138 | */ |
| 4139 | public void startIntentSender(IntentSender intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4140 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4141 | Bundle options) throws IntentSender.SendIntentException { |
| 4142 | if (options != null) { |
| 4143 | startIntentSenderForResult(intent, -1, fillInIntent, flagsMask, |
| 4144 | flagsValues, extraFlags, options); |
| 4145 | } else { |
| 4146 | // Note we want to go through this call for compatibility with |
| 4147 | // applications that may have overridden the method. |
| 4148 | startIntentSenderForResult(intent, -1, fillInIntent, flagsMask, |
| 4149 | flagsValues, extraFlags); |
| 4150 | } |
| 4151 | } |
| 4152 | |
| 4153 | /** |
| 4154 | * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)} |
| 4155 | * with no options. |
| 4156 | * |
| 4157 | * @param intent The intent to start. |
| 4158 | * @param requestCode If >= 0, this code will be returned in |
| 4159 | * onActivityResult() when the activity exits, as described in |
| 4160 | * {@link #startActivityForResult}. |
| 4161 | * |
| 4162 | * @return If a new activity was launched then true is returned; otherwise |
| 4163 | * false is returned and you must handle the Intent yourself. |
| 4164 | * |
| 4165 | * @see #startActivity |
| 4166 | * @see #startActivityForResult |
| 4167 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4168 | public boolean startActivityIfNeeded(@NonNull Intent intent, int requestCode) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4169 | return startActivityIfNeeded(intent, requestCode, null); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4173 | * A special variation to launch an activity only if a new activity |
| 4174 | * instance is needed to handle the given Intent. In other words, this is |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4175 | * just like {@link #startActivityForResult(Intent, int)} except: if you are |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4176 | * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4177 | * singleTask or singleTop |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4178 | * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode}, |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4179 | * and the activity |
| 4180 | * that handles <var>intent</var> is the same as your currently running |
| 4181 | * activity, then a new instance is not needed. In this case, instead of |
| 4182 | * the normal behavior of calling {@link #onNewIntent} this function will |
| 4183 | * return and you can handle the Intent yourself. |
| 4184 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4185 | * <p>This function can only be called from a top-level activity; if it is |
| 4186 | * called from a child activity, a runtime exception will be thrown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4187 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4188 | * @param intent The intent to start. |
| 4189 | * @param requestCode If >= 0, this code will be returned in |
| 4190 | * onActivityResult() when the activity exits, as described in |
| 4191 | * {@link #startActivityForResult}. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4192 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4193 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4194 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4195 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4196 | * @return If a new activity was launched then true is returned; otherwise |
| 4197 | * false is returned and you must handle the Intent yourself. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4198 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4199 | * @see #startActivity |
| 4200 | * @see #startActivityForResult |
| 4201 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4202 | public boolean startActivityIfNeeded(@NonNull Intent intent, int requestCode, |
| 4203 | @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4204 | if (mParent == null) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4205 | int result = ActivityManager.START_RETURN_INTENT_TO_CALLER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4206 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4207 | intent.migrateExtraStreamToClipData(); |
| 4208 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4209 | result = ActivityManagerNative.getDefault() |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 4210 | .startActivity(mMainThread.getApplicationThread(), getBasePackageName(), |
Jeff Hao | 1b012d3 | 2014-08-20 10:35:34 -0700 | [diff] [blame] | 4211 | intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken, |
| 4212 | mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED, |
| 4213 | null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4214 | } catch (RemoteException e) { |
| 4215 | // Empty |
| 4216 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4218 | Instrumentation.checkStartActivityResult(result, intent); |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4219 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4220 | if (requestCode >= 0) { |
| 4221 | // If this start is requesting a result, we can avoid making |
| 4222 | // the activity visible until the result is received. Setting |
| 4223 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4224 | // activity hidden during this time, to avoid flickering. |
| 4225 | // This can only be done when a result is requested because |
| 4226 | // that guarantees we will get information back when the |
| 4227 | // activity is finished, no matter what happens to it. |
| 4228 | mStartedActivity = true; |
| 4229 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4230 | return result != ActivityManager.START_RETURN_INTENT_TO_CALLER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | throw new UnsupportedOperationException( |
| 4234 | "startActivityIfNeeded can only be called from a top-level activity"); |
| 4235 | } |
| 4236 | |
| 4237 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4238 | * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with |
| 4239 | * no options. |
| 4240 | * |
| 4241 | * @param intent The intent to dispatch to the next activity. For |
| 4242 | * correct behavior, this must be the same as the Intent that started |
| 4243 | * your own activity; the only changes you can make are to the extras |
| 4244 | * inside of it. |
| 4245 | * |
| 4246 | * @return Returns a boolean indicating whether there was another Activity |
| 4247 | * to start: true if there was a next activity to start, false if there |
| 4248 | * wasn't. In general, if true is returned you will then want to call |
| 4249 | * finish() on yourself. |
| 4250 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4251 | public boolean startNextMatchingActivity(@NonNull Intent intent) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4252 | return startNextMatchingActivity(intent, null); |
| 4253 | } |
| 4254 | |
| 4255 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4256 | * Special version of starting an activity, for use when you are replacing |
| 4257 | * other activity components. You can use this to hand the Intent off |
| 4258 | * to the next Activity that can handle it. You typically call this in |
| 4259 | * {@link #onCreate} with the Intent returned by {@link #getIntent}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4260 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4261 | * @param intent The intent to dispatch to the next activity. For |
| 4262 | * correct behavior, this must be the same as the Intent that started |
| 4263 | * your own activity; the only changes you can make are to the extras |
| 4264 | * inside of it. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4265 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4266 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4267 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4268 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4269 | * @return Returns a boolean indicating whether there was another Activity |
| 4270 | * to start: true if there was a next activity to start, false if there |
| 4271 | * wasn't. In general, if true is returned you will then want to call |
| 4272 | * finish() on yourself. |
| 4273 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4274 | public boolean startNextMatchingActivity(@NonNull Intent intent, @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4275 | if (mParent == null) { |
| 4276 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4277 | intent.migrateExtraStreamToClipData(); |
| 4278 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4279 | return ActivityManagerNative.getDefault() |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4280 | .startNextMatchingActivity(mToken, intent, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4281 | } catch (RemoteException e) { |
| 4282 | // Empty |
| 4283 | } |
| 4284 | return false; |
| 4285 | } |
| 4286 | |
| 4287 | throw new UnsupportedOperationException( |
| 4288 | "startNextMatchingActivity can only be called from a top-level activity"); |
| 4289 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4290 | |
| 4291 | /** |
| 4292 | * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)} |
| 4293 | * with no options. |
| 4294 | * |
| 4295 | * @param child The activity making the call. |
| 4296 | * @param intent The intent to start. |
| 4297 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4298 | * |
| 4299 | * @throws android.content.ActivityNotFoundException |
| 4300 | * |
| 4301 | * @see #startActivity |
| 4302 | * @see #startActivityForResult |
| 4303 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4304 | public void startActivityFromChild(@NonNull Activity child, Intent intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4305 | int requestCode) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 4306 | startActivityFromChild(child, intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4307 | } |
| 4308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4309 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4310 | * This is called when a child activity of this one calls its |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4311 | * {@link #startActivity} or {@link #startActivityForResult} method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4312 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4313 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4314 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4315 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4316 | * @param child The activity making the call. |
| 4317 | * @param intent The intent to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4318 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4319 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4320 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4321 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4322 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4323 | * @throws android.content.ActivityNotFoundException |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4324 | * |
| 4325 | * @see #startActivity |
| 4326 | * @see #startActivityForResult |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4327 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4328 | public void startActivityFromChild(@NonNull Activity child, Intent intent, |
| 4329 | int requestCode, @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4330 | Instrumentation.ActivityResult ar = |
| 4331 | mInstrumentation.execStartActivity( |
| 4332 | this, mMainThread.getApplicationThread(), mToken, child, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4333 | intent, requestCode, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4334 | if (ar != null) { |
| 4335 | mMainThread.sendActivityResult( |
| 4336 | mToken, child.mEmbeddedID, requestCode, |
| 4337 | ar.getResultCode(), ar.getResultData()); |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4342 | * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)} |
| 4343 | * with no options. |
| 4344 | * |
| 4345 | * @param fragment The fragment making the call. |
| 4346 | * @param intent The intent to start. |
| 4347 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4348 | * |
| 4349 | * @throws android.content.ActivityNotFoundException |
| 4350 | * |
| 4351 | * @see Fragment#startActivity |
| 4352 | * @see Fragment#startActivityForResult |
| 4353 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4354 | public void startActivityFromFragment(@NonNull Fragment fragment, Intent intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4355 | int requestCode) { |
George Mount | abb63cb | 2014-06-23 11:17:58 -0700 | [diff] [blame] | 4356 | startActivityFromFragment(fragment, intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4357 | } |
| 4358 | |
| 4359 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4360 | * This is called when a Fragment in this activity calls its |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4361 | * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult} |
| 4362 | * method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4363 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4364 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4365 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4366 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4367 | * @param fragment The fragment making the call. |
| 4368 | * @param intent The intent to start. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4369 | * @param requestCode Reply request code. < 0 if reply is not requested. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4370 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4371 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4372 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4373 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4374 | * @throws android.content.ActivityNotFoundException |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4375 | * |
| 4376 | * @see Fragment#startActivity |
| 4377 | * @see Fragment#startActivityForResult |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4378 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4379 | public void startActivityFromFragment(@NonNull Fragment fragment, Intent intent, |
| 4380 | int requestCode, @Nullable Bundle options) { |
George Mount | a8bd929 | 2014-05-21 10:54:17 -0700 | [diff] [blame] | 4381 | if (options != null) { |
| 4382 | mActivityTransitionState.startExitOutTransition(this, options); |
| 4383 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4384 | Instrumentation.ActivityResult ar = |
| 4385 | mInstrumentation.execStartActivity( |
| 4386 | this, mMainThread.getApplicationThread(), mToken, fragment, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4387 | intent, requestCode, options); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4388 | if (ar != null) { |
| 4389 | mMainThread.sendActivityResult( |
| 4390 | mToken, fragment.mWho, requestCode, |
| 4391 | ar.getResultCode(), ar.getResultData()); |
| 4392 | } |
| 4393 | } |
| 4394 | |
| 4395 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4396 | * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender, |
| 4397 | * int, Intent, int, int, int, Bundle)} with no options. |
| 4398 | */ |
| 4399 | public void startIntentSenderFromChild(Activity child, IntentSender intent, |
| 4400 | int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, |
| 4401 | int extraFlags) |
| 4402 | throws IntentSender.SendIntentException { |
| 4403 | startIntentSenderFromChild(child, intent, requestCode, fillInIntent, |
| 4404 | flagsMask, flagsValues, extraFlags, null); |
| 4405 | } |
| 4406 | |
| 4407 | /** |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4408 | * Like {@link #startActivityFromChild(Activity, Intent, int)}, but |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4409 | * taking a IntentSender; see |
Dianne Hackborn | ae22c05 | 2009-09-17 18:46:22 -0700 | [diff] [blame] | 4410 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)} |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4411 | * for more information. |
| 4412 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4413 | public void startIntentSenderFromChild(Activity child, IntentSender intent, |
| 4414 | int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4415 | int extraFlags, @Nullable Bundle options) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4416 | throws IntentSender.SendIntentException { |
| 4417 | startIntentSenderForResultInner(intent, requestCode, fillInIntent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4418 | flagsMask, flagsValues, child, options); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | /** |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4422 | * Call immediately after one of the flavors of {@link #startActivity(Intent)} |
| 4423 | * or {@link #finish} to specify an explicit transition animation to |
| 4424 | * perform next. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4425 | * |
| 4426 | * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative |
| 4427 | * to using this with starting activities is to supply the desired animation |
| 4428 | * information through a {@link ActivityOptions} bundle to |
| 4429 | * {@link #startActivity(Intent, Bundle) or a related function. This allows |
| 4430 | * you to specify a custom animation even when starting an activity from |
| 4431 | * outside the context of the current top activity. |
| 4432 | * |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4433 | * @param enterAnim A resource ID of the animation resource to use for |
Dianne Hackborn | 8b571a8 | 2009-09-25 16:09:43 -0700 | [diff] [blame] | 4434 | * the incoming activity. Use 0 for no animation. |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4435 | * @param exitAnim A resource ID of the animation resource to use for |
Dianne Hackborn | 8b571a8 | 2009-09-25 16:09:43 -0700 | [diff] [blame] | 4436 | * the outgoing activity. Use 0 for no animation. |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4437 | */ |
| 4438 | public void overridePendingTransition(int enterAnim, int exitAnim) { |
| 4439 | try { |
| 4440 | ActivityManagerNative.getDefault().overridePendingTransition( |
| 4441 | mToken, getPackageName(), enterAnim, exitAnim); |
| 4442 | } catch (RemoteException e) { |
| 4443 | } |
| 4444 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4445 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4446 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4447 | * Call this to set the result that your activity will return to its |
| 4448 | * caller. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4449 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4450 | * @param resultCode The result code to propagate back to the originating |
| 4451 | * activity, often RESULT_CANCELED or RESULT_OK |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4452 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4453 | * @see #RESULT_CANCELED |
| 4454 | * @see #RESULT_OK |
| 4455 | * @see #RESULT_FIRST_USER |
| 4456 | * @see #setResult(int, Intent) |
| 4457 | */ |
| 4458 | public final void setResult(int resultCode) { |
| 4459 | synchronized (this) { |
| 4460 | mResultCode = resultCode; |
| 4461 | mResultData = null; |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | /** |
| 4466 | * Call this to set the result that your activity will return to its |
| 4467 | * caller. |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4468 | * |
| 4469 | * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent |
| 4470 | * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION |
| 4471 | * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION |
| 4472 | * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the |
| 4473 | * Activity receiving the result access to the specific URIs in the Intent. |
| 4474 | * Access will remain until the Activity has finished (it will remain across the hosting |
| 4475 | * process being killed and other temporary destruction) and will be added |
| 4476 | * to any existing set of URI permissions it already holds. |
| 4477 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4478 | * @param resultCode The result code to propagate back to the originating |
| 4479 | * activity, often RESULT_CANCELED or RESULT_OK |
| 4480 | * @param data The data to propagate back to the originating activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4481 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4482 | * @see #RESULT_CANCELED |
| 4483 | * @see #RESULT_OK |
| 4484 | * @see #RESULT_FIRST_USER |
| 4485 | * @see #setResult(int) |
| 4486 | */ |
| 4487 | public final void setResult(int resultCode, Intent data) { |
| 4488 | synchronized (this) { |
| 4489 | mResultCode = resultCode; |
| 4490 | mResultData = data; |
| 4491 | } |
| 4492 | } |
| 4493 | |
| 4494 | /** |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 4495 | * Return information about who launched this activity. If the launching Intent |
| 4496 | * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER}, |
| 4497 | * that will be returned as-is; otherwise, if known, an |
| 4498 | * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the |
| 4499 | * package name that started the Intent will be returned. This may return null if no |
| 4500 | * referrer can be identified -- it is neither explicitly specified, nor is it known which |
| 4501 | * application package was involved. |
| 4502 | * |
| 4503 | * <p>If called while inside the handling of {@link #onNewIntent}, this function will |
| 4504 | * return the referrer that submitted that new intent to the activity. Otherwise, it |
| 4505 | * always returns the referrer of the original Intent.</p> |
| 4506 | * |
| 4507 | * <p>Note that this is <em>not</em> a security feature -- you can not trust the |
| 4508 | * referrer information, applications can spoof it.</p> |
| 4509 | */ |
| 4510 | @Nullable |
| 4511 | public Uri getReferrer() { |
| 4512 | Intent intent = getIntent(); |
| 4513 | Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER); |
| 4514 | if (referrer != null) { |
| 4515 | return referrer; |
| 4516 | } |
| 4517 | String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME); |
| 4518 | if (referrerName != null) { |
| 4519 | return Uri.parse(referrerName); |
| 4520 | } |
| 4521 | if (mReferrer != null) { |
| 4522 | return new Uri.Builder().scheme("android-app").authority(mReferrer).build(); |
| 4523 | } |
| 4524 | return null; |
| 4525 | } |
| 4526 | |
| 4527 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4528 | * Return the name of the package that invoked this activity. This is who |
| 4529 | * the data in {@link #setResult setResult()} will be sent to. You can |
| 4530 | * use this information to validate that the recipient is allowed to |
| 4531 | * receive the data. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4532 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 4533 | * <p class="note">Note: if the calling activity is not expecting a result (that is it |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4534 | * did not use the {@link #startActivityForResult} |
| 4535 | * form that includes a request code), then the calling package will be |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 4536 | * null.</p> |
| 4537 | * |
| 4538 | * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
| 4539 | * the result from this method was unstable. If the process hosting the calling |
| 4540 | * package was no longer running, it would return null instead of the proper package |
| 4541 | * name. You can use {@link #getCallingActivity()} and retrieve the package name |
| 4542 | * from that instead.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4543 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4544 | * @return The package of the activity that will receive your |
| 4545 | * reply, or null if none. |
| 4546 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4547 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4548 | public String getCallingPackage() { |
| 4549 | try { |
| 4550 | return ActivityManagerNative.getDefault().getCallingPackage(mToken); |
| 4551 | } catch (RemoteException e) { |
| 4552 | return null; |
| 4553 | } |
| 4554 | } |
| 4555 | |
| 4556 | /** |
| 4557 | * Return the name of the activity that invoked this activity. This is |
| 4558 | * who the data in {@link #setResult setResult()} will be sent to. You |
| 4559 | * can use this information to validate that the recipient is allowed to |
| 4560 | * receive the data. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4561 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 4562 | * <p class="note">Note: if the calling activity is not expecting a result (that is it |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4563 | * did not use the {@link #startActivityForResult} |
| 4564 | * form that includes a request code), then the calling package will be |
| 4565 | * null. |
| 4566 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 4567 | * @return The ComponentName of the activity that will receive your |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4568 | * reply, or null if none. |
| 4569 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4570 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4571 | public ComponentName getCallingActivity() { |
| 4572 | try { |
| 4573 | return ActivityManagerNative.getDefault().getCallingActivity(mToken); |
| 4574 | } catch (RemoteException e) { |
| 4575 | return null; |
| 4576 | } |
| 4577 | } |
| 4578 | |
| 4579 | /** |
| 4580 | * Control whether this activity's main window is visible. This is intended |
| 4581 | * only for the special case of an activity that is not going to show a |
| 4582 | * UI itself, but can't just finish prior to onResume() because it needs |
| 4583 | * to wait for a service binding or such. Setting this to false allows |
| 4584 | * you to prevent your UI from being shown during that time. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4585 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4586 | * <p>The default value for this is taken from the |
| 4587 | * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme. |
| 4588 | */ |
| 4589 | public void setVisible(boolean visible) { |
| 4590 | if (mVisibleFromClient != visible) { |
| 4591 | mVisibleFromClient = visible; |
| 4592 | if (mVisibleFromServer) { |
| 4593 | if (visible) makeVisible(); |
| 4594 | else mDecor.setVisibility(View.INVISIBLE); |
| 4595 | } |
| 4596 | } |
| 4597 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4598 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4599 | void makeVisible() { |
| 4600 | if (!mWindowAdded) { |
| 4601 | ViewManager wm = getWindowManager(); |
| 4602 | wm.addView(mDecor, getWindow().getAttributes()); |
| 4603 | mWindowAdded = true; |
| 4604 | } |
| 4605 | mDecor.setVisibility(View.VISIBLE); |
| 4606 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4608 | /** |
| 4609 | * Check to see whether this activity is in the process of finishing, |
| 4610 | * either because you called {@link #finish} on it or someone else |
| 4611 | * has requested that it finished. This is often used in |
| 4612 | * {@link #onPause} to determine whether the activity is simply pausing or |
| 4613 | * completely finishing. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4614 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4615 | * @return If the activity is finishing, returns true; else returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4616 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4617 | * @see #finish |
| 4618 | */ |
| 4619 | public boolean isFinishing() { |
| 4620 | return mFinished; |
| 4621 | } |
| 4622 | |
| 4623 | /** |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 4624 | * Returns true if the final {@link #onDestroy()} call has been made |
| 4625 | * on the Activity, so this instance is now dead. |
| 4626 | */ |
| 4627 | public boolean isDestroyed() { |
| 4628 | return mDestroyed; |
| 4629 | } |
| 4630 | |
| 4631 | /** |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 4632 | * Check to see whether this activity is in the process of being destroyed in order to be |
| 4633 | * recreated with a new configuration. This is often used in |
| 4634 | * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed |
| 4635 | * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4636 | * |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 4637 | * @return If the activity is being torn down in order to be recreated with a new configuration, |
| 4638 | * returns true; else returns false. |
| 4639 | */ |
| 4640 | public boolean isChangingConfigurations() { |
| 4641 | return mChangingConfigurations; |
| 4642 | } |
| 4643 | |
| 4644 | /** |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 4645 | * Cause this Activity to be recreated with a new instance. This results |
| 4646 | * in essentially the same flow as when the Activity is created due to |
| 4647 | * a configuration change -- the current instance will go through its |
| 4648 | * lifecycle to {@link #onDestroy} and a new instance then created after it. |
| 4649 | */ |
| 4650 | public void recreate() { |
| 4651 | if (mParent != null) { |
| 4652 | throw new IllegalStateException("Can only be called on top-level activity"); |
| 4653 | } |
| 4654 | if (Looper.myLooper() != mMainThread.getLooper()) { |
| 4655 | throw new IllegalStateException("Must be called from main thread"); |
| 4656 | } |
Wale Ogunwale | 60454db | 2015-01-23 16:05:07 -0800 | [diff] [blame] | 4657 | mMainThread.requestRelaunchActivity(mToken, null, null, 0, false, null, null, false); |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 4661 | * Finishes the current activity and specifies whether to remove the task associated with this |
| 4662 | * activity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4663 | */ |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 4664 | private void finish(boolean finishTask) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4665 | if (mParent == null) { |
| 4666 | int resultCode; |
| 4667 | Intent resultData; |
| 4668 | synchronized (this) { |
| 4669 | resultCode = mResultCode; |
| 4670 | resultData = mResultData; |
| 4671 | } |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 4672 | if (false) Log.v(TAG, "Finishing self: token=" + mToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4673 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 4674 | if (resultData != null) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4675 | resultData.prepareToLeaveProcess(); |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 4676 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4677 | if (ActivityManagerNative.getDefault() |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 4678 | .finishActivity(mToken, resultCode, resultData, finishTask)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4679 | mFinished = true; |
| 4680 | } |
| 4681 | } catch (RemoteException e) { |
| 4682 | // Empty |
| 4683 | } |
| 4684 | } else { |
| 4685 | mParent.finishFromChild(this); |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 4690 | * Call this when your activity is done and should be closed. The |
| 4691 | * ActivityResult is propagated back to whoever launched you via |
| 4692 | * onActivityResult(). |
| 4693 | */ |
| 4694 | public void finish() { |
| 4695 | finish(false); |
| 4696 | } |
| 4697 | |
| 4698 | /** |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 4699 | * Finish this activity as well as all activities immediately below it |
| 4700 | * in the current task that have the same affinity. This is typically |
| 4701 | * used when an application can be launched on to another task (such as |
| 4702 | * from an ACTION_VIEW of a content type it understands) and the user |
| 4703 | * has used the up navigation to switch out of the current task and in |
| 4704 | * to its own task. In this case, if the user has navigated down into |
| 4705 | * any other activities of the second application, all of those should |
| 4706 | * be removed from the original task as part of the task switch. |
| 4707 | * |
| 4708 | * <p>Note that this finish does <em>not</em> allow you to deliver results |
| 4709 | * to the previous activity, and an exception will be thrown if you are trying |
| 4710 | * to do so.</p> |
| 4711 | */ |
| 4712 | public void finishAffinity() { |
| 4713 | if (mParent != null) { |
| 4714 | throw new IllegalStateException("Can not be called from an embedded activity"); |
| 4715 | } |
| 4716 | if (mResultCode != RESULT_CANCELED || mResultData != null) { |
| 4717 | throw new IllegalStateException("Can not be called to deliver a result"); |
| 4718 | } |
| 4719 | try { |
| 4720 | if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) { |
| 4721 | mFinished = true; |
| 4722 | } |
| 4723 | } catch (RemoteException e) { |
| 4724 | // Empty |
| 4725 | } |
| 4726 | } |
| 4727 | |
| 4728 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4729 | * This is called when a child activity of this one calls its |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4730 | * {@link #finish} method. The default implementation simply calls |
| 4731 | * finish() on this activity (the parent), finishing the entire group. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4732 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4733 | * @param child The activity making the call. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4734 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4735 | * @see #finish |
| 4736 | */ |
| 4737 | public void finishFromChild(Activity child) { |
| 4738 | finish(); |
| 4739 | } |
| 4740 | |
| 4741 | /** |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 4742 | * Reverses the Activity Scene entry Transition and triggers the calling Activity |
| 4743 | * to reverse its exit Transition. When the exit Transition completes, |
| 4744 | * {@link #finish()} is called. If no entry Transition was used, finish() is called |
| 4745 | * immediately and the Activity exit Transition is run. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 4746 | * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[]) |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 4747 | */ |
Craig Mautner | 73f843d | 2014-05-19 09:42:28 -0700 | [diff] [blame] | 4748 | public void finishAfterTransition() { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 4749 | if (!mActivityTransitionState.startExitBackTransition(this)) { |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 4750 | finish(); |
| 4751 | } |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4755 | * Force finish another activity that you had previously started with |
| 4756 | * {@link #startActivityForResult}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4757 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4758 | * @param requestCode The request code of the activity that you had |
| 4759 | * given to startActivityForResult(). If there are multiple |
| 4760 | * activities started with this request code, they |
| 4761 | * will all be finished. |
| 4762 | */ |
| 4763 | public void finishActivity(int requestCode) { |
| 4764 | if (mParent == null) { |
| 4765 | try { |
| 4766 | ActivityManagerNative.getDefault() |
| 4767 | .finishSubActivity(mToken, mEmbeddedID, requestCode); |
| 4768 | } catch (RemoteException e) { |
| 4769 | // Empty |
| 4770 | } |
| 4771 | } else { |
| 4772 | mParent.finishActivityFromChild(this, requestCode); |
| 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | /** |
| 4777 | * This is called when a child activity of this one calls its |
| 4778 | * finishActivity(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4779 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4780 | * @param child The activity making the call. |
| 4781 | * @param requestCode Request code that had been used to start the |
| 4782 | * activity. |
| 4783 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4784 | public void finishActivityFromChild(@NonNull Activity child, int requestCode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4785 | try { |
| 4786 | ActivityManagerNative.getDefault() |
| 4787 | .finishSubActivity(mToken, child.mEmbeddedID, requestCode); |
| 4788 | } catch (RemoteException e) { |
| 4789 | // Empty |
| 4790 | } |
| 4791 | } |
| 4792 | |
| 4793 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 4794 | * Call this when your activity is done and should be closed and the task should be completely |
| 4795 | * removed as a part of finishing the Activity. |
| 4796 | */ |
| 4797 | public void finishAndRemoveTask() { |
| 4798 | finish(true); |
| 4799 | } |
| 4800 | |
| 4801 | /** |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 4802 | * Ask that the local app instance of this activity be released to free up its memory. |
| 4803 | * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity -- |
| 4804 | * a new instance of the activity will later be re-created if needed due to the user |
| 4805 | * navigating back to it. |
| 4806 | * |
| 4807 | * @return Returns true if the activity was in a state that it has started the process |
| 4808 | * of destroying its current instance; returns false if for any reason this could not |
| 4809 | * be done: it is currently visible to the user, it is already being destroyed, it is |
| 4810 | * being finished, it hasn't yet saved its state, etc. |
| 4811 | */ |
| 4812 | public boolean releaseInstance() { |
| 4813 | try { |
| 4814 | return ActivityManagerNative.getDefault().releaseActivityInstance(mToken); |
| 4815 | } catch (RemoteException e) { |
| 4816 | // Empty |
| 4817 | } |
| 4818 | return false; |
| 4819 | } |
| 4820 | |
| 4821 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4822 | * Called when an activity you launched exits, giving you the requestCode |
| 4823 | * you started it with, the resultCode it returned, and any additional |
| 4824 | * data from it. The <var>resultCode</var> will be |
| 4825 | * {@link #RESULT_CANCELED} if the activity explicitly returned that, |
| 4826 | * didn't return any result, or crashed during its operation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4827 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4828 | * <p>You will receive this call immediately before onResume() when your |
| 4829 | * activity is re-starting. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4830 | * |
Ricardo Cervera | 92f6a74 | 2014-04-04 11:17:06 -0700 | [diff] [blame] | 4831 | * <p>This method is never invoked if your activity sets |
| 4832 | * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to |
| 4833 | * <code>true</code>. |
Ricardo Cervera | 93f94c2 | 2015-01-16 09:54:30 -0800 | [diff] [blame] | 4834 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4835 | * @param requestCode The integer request code originally supplied to |
| 4836 | * startActivityForResult(), allowing you to identify who this |
| 4837 | * result came from. |
| 4838 | * @param resultCode The integer result code returned by the child activity |
| 4839 | * through its setResult(). |
| 4840 | * @param data An Intent, which can return result data to the caller |
| 4841 | * (various data can be attached to Intent "extras"). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4842 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4843 | * @see #startActivityForResult |
| 4844 | * @see #createPendingResult |
| 4845 | * @see #setResult(int) |
| 4846 | */ |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4847 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4848 | } |
| 4849 | |
| 4850 | /** |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 4851 | * Called when an activity you launched with an activity transition exposes this |
| 4852 | * Activity through a returning activity transition, giving you the resultCode |
| 4853 | * and any additional data from it. This method will only be called if the activity |
| 4854 | * set a result code other than {@link #RESULT_CANCELED} and it supports activity |
George Mount | 9826f63 | 2014-09-11 08:50:09 -0700 | [diff] [blame] | 4855 | * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 4856 | * |
| 4857 | * <p>The purpose of this function is to let the called Activity send a hint about |
| 4858 | * its state so that this underlying Activity can prepare to be exposed. A call to |
| 4859 | * this method does not guarantee that the called Activity has or will be exiting soon. |
| 4860 | * It only indicates that it will expose this Activity's Window and it has |
| 4861 | * some data to pass to prepare it.</p> |
| 4862 | * |
| 4863 | * @param resultCode The integer result code returned by the child activity |
| 4864 | * through its setResult(). |
| 4865 | * @param data An Intent, which can return result data to the caller |
| 4866 | * (various data can be attached to Intent "extras"). |
| 4867 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 4868 | public void onActivityReenter(int resultCode, Intent data) { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4872 | * Create a new PendingIntent object which you can hand to others |
| 4873 | * for them to use to send result data back to your |
| 4874 | * {@link #onActivityResult} callback. The created object will be either |
| 4875 | * one-shot (becoming invalid after a result is sent back) or multiple |
| 4876 | * (allowing any number of results to be sent through it). |
| 4877 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4878 | * @param requestCode Private request code for the sender that will be |
| 4879 | * associated with the result data when it is returned. The sender can not |
| 4880 | * modify this value, allowing you to identify incoming results. |
| 4881 | * @param data Default data to supply in the result, which may be modified |
| 4882 | * by the sender. |
| 4883 | * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT}, |
| 4884 | * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE}, |
| 4885 | * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT}, |
| 4886 | * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT}, |
| 4887 | * or any of the flags as supported by |
| 4888 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 4889 | * of the intent that can be supplied when the actual send happens. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4890 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4891 | * @return Returns an existing or new PendingIntent matching the given |
| 4892 | * parameters. May return null only if |
| 4893 | * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been |
| 4894 | * supplied. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4895 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4896 | * @see PendingIntent |
| 4897 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4898 | public PendingIntent createPendingResult(int requestCode, @NonNull Intent data, |
| 4899 | @PendingIntent.Flags int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4900 | String packageName = getPackageName(); |
| 4901 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4902 | data.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4903 | IIntentSender target = |
| 4904 | ActivityManagerNative.getDefault().getIntentSender( |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4905 | ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4906 | mParent == null ? mToken : mParent.mToken, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4907 | mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null, |
| 4908 | UserHandle.myUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4909 | return target != null ? new PendingIntent(target) : null; |
| 4910 | } catch (RemoteException e) { |
| 4911 | // Empty |
| 4912 | } |
| 4913 | return null; |
| 4914 | } |
| 4915 | |
| 4916 | /** |
| 4917 | * Change the desired orientation of this activity. If the activity |
| 4918 | * is currently in the foreground or otherwise impacting the screen |
| 4919 | * orientation, the screen will immediately be changed (possibly causing |
| 4920 | * the activity to be restarted). Otherwise, this will be used the next |
| 4921 | * time the activity is visible. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4922 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4923 | * @param requestedOrientation An orientation constant as used in |
| 4924 | * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}. |
| 4925 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4926 | public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4927 | if (mParent == null) { |
| 4928 | try { |
| 4929 | ActivityManagerNative.getDefault().setRequestedOrientation( |
| 4930 | mToken, requestedOrientation); |
| 4931 | } catch (RemoteException e) { |
| 4932 | // Empty |
| 4933 | } |
| 4934 | } else { |
| 4935 | mParent.setRequestedOrientation(requestedOrientation); |
| 4936 | } |
| 4937 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4938 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4939 | /** |
| 4940 | * Return the current requested orientation of the activity. This will |
| 4941 | * either be the orientation requested in its component's manifest, or |
| 4942 | * the last requested orientation given to |
| 4943 | * {@link #setRequestedOrientation(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4944 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4945 | * @return Returns an orientation constant as used in |
| 4946 | * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}. |
| 4947 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4948 | @ActivityInfo.ScreenOrientation |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4949 | public int getRequestedOrientation() { |
| 4950 | if (mParent == null) { |
| 4951 | try { |
| 4952 | return ActivityManagerNative.getDefault() |
| 4953 | .getRequestedOrientation(mToken); |
| 4954 | } catch (RemoteException e) { |
| 4955 | // Empty |
| 4956 | } |
| 4957 | } else { |
| 4958 | return mParent.getRequestedOrientation(); |
| 4959 | } |
| 4960 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 4961 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4962 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4963 | /** |
| 4964 | * Return the identifier of the task this activity is in. This identifier |
| 4965 | * will remain the same for the lifetime of the activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4966 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4967 | * @return Task identifier, an opaque integer. |
| 4968 | */ |
| 4969 | public int getTaskId() { |
| 4970 | try { |
| 4971 | return ActivityManagerNative.getDefault() |
| 4972 | .getTaskForActivity(mToken, false); |
| 4973 | } catch (RemoteException e) { |
| 4974 | return -1; |
| 4975 | } |
| 4976 | } |
| 4977 | |
| 4978 | /** |
| 4979 | * Return whether this activity is the root of a task. The root is the |
| 4980 | * first activity in a task. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4981 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4982 | * @return True if this is the root activity, else false. |
| 4983 | */ |
| 4984 | public boolean isTaskRoot() { |
| 4985 | try { |
| 4986 | return ActivityManagerNative.getDefault() |
| 4987 | .getTaskForActivity(mToken, true) >= 0; |
| 4988 | } catch (RemoteException e) { |
| 4989 | return false; |
| 4990 | } |
| 4991 | } |
| 4992 | |
| 4993 | /** |
| 4994 | * Move the task containing this activity to the back of the activity |
| 4995 | * stack. The activity's order within the task is unchanged. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4996 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4997 | * @param nonRoot If false then this only works if the activity is the root |
| 4998 | * of a task; if true it will work for any activity in |
| 4999 | * a task. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5000 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5001 | * @return If the task was moved (or it was already at the |
| 5002 | * back) true is returned, else false. |
| 5003 | */ |
| 5004 | public boolean moveTaskToBack(boolean nonRoot) { |
| 5005 | try { |
| 5006 | return ActivityManagerNative.getDefault().moveActivityTaskToBack( |
| 5007 | mToken, nonRoot); |
| 5008 | } catch (RemoteException e) { |
| 5009 | // Empty |
| 5010 | } |
| 5011 | return false; |
| 5012 | } |
| 5013 | |
| 5014 | /** |
| 5015 | * Returns class name for this activity with the package prefix removed. |
| 5016 | * This is the default name used to read and write settings. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5017 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5018 | * @return The local class name. |
| 5019 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5020 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5021 | public String getLocalClassName() { |
| 5022 | final String pkg = getPackageName(); |
| 5023 | final String cls = mComponent.getClassName(); |
| 5024 | int packageLen = pkg.length(); |
| 5025 | if (!cls.startsWith(pkg) || cls.length() <= packageLen |
| 5026 | || cls.charAt(packageLen) != '.') { |
| 5027 | return cls; |
| 5028 | } |
| 5029 | return cls.substring(packageLen+1); |
| 5030 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5031 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5032 | /** |
| 5033 | * Returns complete component name of this activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5034 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5035 | * @return Returns the complete component name for this activity |
| 5036 | */ |
| 5037 | public ComponentName getComponentName() |
| 5038 | { |
| 5039 | return mComponent; |
| 5040 | } |
| 5041 | |
| 5042 | /** |
| 5043 | * Retrieve a {@link SharedPreferences} object for accessing preferences |
| 5044 | * that are private to this activity. This simply calls the underlying |
| 5045 | * {@link #getSharedPreferences(String, int)} method by passing in this activity's |
| 5046 | * class name as the preferences name. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5047 | * |
| 5048 | * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default |
| 5049 | * operation, {@link #MODE_WORLD_READABLE} and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5050 | * {@link #MODE_WORLD_WRITEABLE} to control permissions. |
| 5051 | * |
| 5052 | * @return Returns the single SharedPreferences instance that can be used |
| 5053 | * to retrieve and modify the preference values. |
| 5054 | */ |
| 5055 | public SharedPreferences getPreferences(int mode) { |
| 5056 | return getSharedPreferences(getLocalClassName(), mode); |
| 5057 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5058 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5059 | private void ensureSearchManager() { |
| 5060 | if (mSearchManager != null) { |
| 5061 | return; |
| 5062 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5063 | |
Amith Yamasani | e9ce3f0 | 2010-01-25 09:15:50 -0800 | [diff] [blame] | 5064 | mSearchManager = new SearchManager(this, null); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5065 | } |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5066 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5067 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5068 | public Object getSystemService(@ServiceName @NonNull String name) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5069 | if (getBaseContext() == null) { |
| 5070 | throw new IllegalStateException( |
| 5071 | "System services not available to Activities before onCreate()"); |
| 5072 | } |
| 5073 | |
| 5074 | if (WINDOW_SERVICE.equals(name)) { |
| 5075 | return mWindowManager; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 5076 | } else if (SEARCH_SERVICE.equals(name)) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5077 | ensureSearchManager(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 5078 | return mSearchManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5079 | } |
| 5080 | return super.getSystemService(name); |
| 5081 | } |
| 5082 | |
| 5083 | /** |
| 5084 | * Change the title associated with this activity. If this is a |
| 5085 | * top-level activity, the title for its window will change. If it |
| 5086 | * is an embedded activity, the parent can do whatever it wants |
| 5087 | * with it. |
| 5088 | */ |
| 5089 | public void setTitle(CharSequence title) { |
| 5090 | mTitle = title; |
| 5091 | onTitleChanged(title, mTitleColor); |
| 5092 | |
| 5093 | if (mParent != null) { |
| 5094 | mParent.onChildTitleChanged(this, title); |
| 5095 | } |
| 5096 | } |
| 5097 | |
| 5098 | /** |
| 5099 | * Change the title associated with this activity. If this is a |
| 5100 | * top-level activity, the title for its window will change. If it |
| 5101 | * is an embedded activity, the parent can do whatever it wants |
| 5102 | * with it. |
| 5103 | */ |
| 5104 | public void setTitle(int titleId) { |
| 5105 | setTitle(getText(titleId)); |
| 5106 | } |
| 5107 | |
Alan Viverette | 2525d9c | 2013-11-15 14:42:19 -0800 | [diff] [blame] | 5108 | /** |
| 5109 | * Change the color of the title associated with this activity. |
| 5110 | * <p> |
| 5111 | * This method is deprecated starting in API Level 11 and replaced by action |
| 5112 | * bar styles. For information on styling the Action Bar, read the <a |
| 5113 | * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer |
| 5114 | * guide. |
| 5115 | * |
| 5116 | * @deprecated Use action bar styles instead. |
| 5117 | */ |
| 5118 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5119 | public void setTitleColor(int textColor) { |
| 5120 | mTitleColor = textColor; |
| 5121 | onTitleChanged(mTitle, textColor); |
| 5122 | } |
| 5123 | |
| 5124 | public final CharSequence getTitle() { |
| 5125 | return mTitle; |
| 5126 | } |
| 5127 | |
| 5128 | public final int getTitleColor() { |
| 5129 | return mTitleColor; |
| 5130 | } |
| 5131 | |
| 5132 | protected void onTitleChanged(CharSequence title, int color) { |
| 5133 | if (mTitleReady) { |
| 5134 | final Window win = getWindow(); |
| 5135 | if (win != null) { |
| 5136 | win.setTitle(title); |
| 5137 | if (color != 0) { |
| 5138 | win.setTitleColor(color); |
| 5139 | } |
| 5140 | } |
Adam Powell | af2d859 | 2014-08-26 18:06:40 -0700 | [diff] [blame] | 5141 | if (mActionBar != null) { |
| 5142 | mActionBar.setWindowTitle(title); |
| 5143 | } |
Adam Powell | a557fdc | 2014-08-21 18:05:53 -0700 | [diff] [blame] | 5144 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | protected void onChildTitleChanged(Activity childActivity, CharSequence title) { |
| 5148 | } |
| 5149 | |
| 5150 | /** |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5151 | * Sets information describing the task with this activity for presentation inside the Recents |
| 5152 | * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task |
| 5153 | * are traversed in order from the topmost activity to the bottommost. The traversal continues |
| 5154 | * for each property until a suitable value is found. For each task the taskDescription will be |
| 5155 | * returned in {@link android.app.ActivityManager.TaskDescription}. |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5156 | * |
| 5157 | * @see ActivityManager#getRecentTasks |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5158 | * @see android.app.ActivityManager.TaskDescription |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5159 | * |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5160 | * @param taskDescription The TaskDescription properties that describe the task with this activity |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5161 | */ |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5162 | public void setTaskDescription(ActivityManager.TaskDescription taskDescription) { |
| 5163 | ActivityManager.TaskDescription td; |
| 5164 | // Scale the icon down to something reasonable if it is provided |
Craig Mautner | 648f69b | 2014-09-18 14:16:26 -0700 | [diff] [blame] | 5165 | if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) { |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5166 | final int size = ActivityManager.getLauncherLargeIconSizeInner(this); |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5167 | final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size, true); |
| 5168 | td = new ActivityManager.TaskDescription(taskDescription.getLabel(), icon, |
| 5169 | taskDescription.getPrimaryColor()); |
| 5170 | } else { |
| 5171 | td = taskDescription; |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5172 | } |
| 5173 | try { |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5174 | ActivityManagerNative.getDefault().setTaskDescription(mToken, td); |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5175 | } catch (RemoteException e) { |
| 5176 | } |
| 5177 | } |
| 5178 | |
| 5179 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5180 | * Sets the visibility of the progress bar in the title. |
| 5181 | * <p> |
| 5182 | * In order for the progress bar to be shown, the feature must be requested |
| 5183 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5184 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5185 | * @param visible Whether to show the progress bars in the title. |
| 5186 | */ |
| 5187 | public final void setProgressBarVisibility(boolean visible) { |
| 5188 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON : |
| 5189 | Window.PROGRESS_VISIBILITY_OFF); |
| 5190 | } |
| 5191 | |
| 5192 | /** |
| 5193 | * Sets the visibility of the indeterminate progress bar in the title. |
| 5194 | * <p> |
| 5195 | * In order for the progress bar to be shown, the feature must be requested |
| 5196 | * via {@link #requestWindowFeature(int)}. |
| 5197 | * |
| 5198 | * @param visible Whether to show the progress bars in the title. |
| 5199 | */ |
| 5200 | public final void setProgressBarIndeterminateVisibility(boolean visible) { |
| 5201 | getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, |
| 5202 | visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF); |
| 5203 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5204 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5205 | /** |
| 5206 | * Sets whether the horizontal progress bar in the title should be indeterminate (the circular |
| 5207 | * is always indeterminate). |
| 5208 | * <p> |
| 5209 | * In order for the progress bar to be shown, the feature must be requested |
| 5210 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5211 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5212 | * @param indeterminate Whether the horizontal progress bar should be indeterminate. |
| 5213 | */ |
| 5214 | public final void setProgressBarIndeterminate(boolean indeterminate) { |
| 5215 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 5216 | indeterminate ? Window.PROGRESS_INDETERMINATE_ON |
| 5217 | : Window.PROGRESS_INDETERMINATE_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5218 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5219 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5220 | /** |
| 5221 | * Sets the progress for the progress bars in the title. |
| 5222 | * <p> |
| 5223 | * In order for the progress bar to be shown, the feature must be requested |
| 5224 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5225 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5226 | * @param progress The progress for the progress bar. Valid ranges are from |
| 5227 | * 0 to 10000 (both inclusive). If 10000 is given, the progress |
| 5228 | * bar will be completely filled and will fade out. |
| 5229 | */ |
| 5230 | public final void setProgress(int progress) { |
| 5231 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START); |
| 5232 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5233 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5234 | /** |
| 5235 | * Sets the secondary progress for the progress bar in the title. This |
| 5236 | * progress is drawn between the primary progress (set via |
| 5237 | * {@link #setProgress(int)} and the background. It can be ideal for media |
| 5238 | * scenarios such as showing the buffering progress while the default |
| 5239 | * progress shows the play progress. |
| 5240 | * <p> |
| 5241 | * In order for the progress bar to be shown, the feature must be requested |
| 5242 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5243 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5244 | * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from |
| 5245 | * 0 to 10000 (both inclusive). |
| 5246 | */ |
| 5247 | public final void setSecondaryProgress(int secondaryProgress) { |
| 5248 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, |
| 5249 | secondaryProgress + Window.PROGRESS_SECONDARY_START); |
| 5250 | } |
| 5251 | |
| 5252 | /** |
| 5253 | * Suggests an audio stream whose volume should be changed by the hardware |
| 5254 | * volume controls. |
| 5255 | * <p> |
| 5256 | * The suggested audio stream will be tied to the window of this Activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5257 | * Volume requests which are received while the Activity is in the |
| 5258 | * foreground will affect this stream. |
| 5259 | * <p> |
| 5260 | * It is not guaranteed that the hardware volume controls will always change |
| 5261 | * this stream's volume (for example, if a call is in progress, its stream's |
| 5262 | * volume may be changed instead). To reset back to the default, use |
| 5263 | * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}. |
| 5264 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5265 | * @param streamType The type of the audio stream whose volume should be |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5266 | * changed by the hardware volume controls. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5267 | */ |
| 5268 | public final void setVolumeControlStream(int streamType) { |
| 5269 | getWindow().setVolumeControlStream(streamType); |
| 5270 | } |
| 5271 | |
| 5272 | /** |
| 5273 | * Gets the suggested audio stream whose volume should be changed by the |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5274 | * hardware volume controls. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5275 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5276 | * @return The suggested audio stream type whose volume should be changed by |
| 5277 | * the hardware volume controls. |
| 5278 | * @see #setVolumeControlStream(int) |
| 5279 | */ |
| 5280 | public final int getVolumeControlStream() { |
| 5281 | return getWindow().getVolumeControlStream(); |
| 5282 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5283 | |
| 5284 | /** |
| 5285 | * Sets a {@link MediaController} to send media keys and volume changes to. |
| 5286 | * <p> |
| 5287 | * The controller will be tied to the window of this Activity. Media key and |
| 5288 | * volume events which are received while the Activity is in the foreground |
| 5289 | * will be forwarded to the controller and used to invoke transport controls |
| 5290 | * or adjust the volume. This may be used instead of or in addition to |
| 5291 | * {@link #setVolumeControlStream} to affect a specific session instead of a |
| 5292 | * specific stream. |
| 5293 | * <p> |
| 5294 | * It is not guaranteed that the hardware volume controls will always change |
| 5295 | * this session's volume (for example, if a call is in progress, its |
| 5296 | * stream's volume may be changed instead). To reset back to the default use |
| 5297 | * null as the controller. |
| 5298 | * |
| 5299 | * @param controller The controller for the session which should receive |
| 5300 | * media keys and volume changes. |
| 5301 | */ |
| 5302 | public final void setMediaController(MediaController controller) { |
| 5303 | getWindow().setMediaController(controller); |
| 5304 | } |
| 5305 | |
| 5306 | /** |
| 5307 | * Gets the controller which should be receiving media key and volume events |
| 5308 | * while this activity is in the foreground. |
| 5309 | * |
| 5310 | * @return The controller which should receive events. |
| 5311 | * @see #setMediaController(android.media.session.MediaController) |
| 5312 | */ |
| 5313 | public final MediaController getMediaController() { |
| 5314 | return getWindow().getMediaController(); |
| 5315 | } |
| 5316 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5317 | /** |
| 5318 | * Runs the specified action on the UI thread. If the current thread is the UI |
| 5319 | * thread, then the action is executed immediately. If the current thread is |
| 5320 | * not the UI thread, the action is posted to the event queue of the UI thread. |
| 5321 | * |
| 5322 | * @param action the action to run on the UI thread |
| 5323 | */ |
| 5324 | public final void runOnUiThread(Runnable action) { |
| 5325 | if (Thread.currentThread() != mUiThread) { |
| 5326 | mHandler.post(action); |
| 5327 | } else { |
| 5328 | action.run(); |
| 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | /** |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5333 | * Standard implementation of |
| 5334 | * {@link android.view.LayoutInflater.Factory#onCreateView} used when |
| 5335 | * inflating with the LayoutInflater returned by {@link #getSystemService}. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5336 | * This implementation does nothing and is for |
| 5337 | * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps |
| 5338 | * should use {@link #onCreateView(View, String, Context, AttributeSet)}. |
| 5339 | * |
| 5340 | * @see android.view.LayoutInflater#createView |
| 5341 | * @see android.view.Window#getLayoutInflater |
| 5342 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5343 | @Nullable |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5344 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
| 5345 | return null; |
| 5346 | } |
| 5347 | |
| 5348 | /** |
| 5349 | * Standard implementation of |
| 5350 | * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)} |
| 5351 | * used when inflating with the LayoutInflater returned by {@link #getSystemService}. |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5352 | * This implementation handles <fragment> tags to embed fragments inside |
| 5353 | * of the activity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5354 | * |
| 5355 | * @see android.view.LayoutInflater#createView |
| 5356 | * @see android.view.Window#getLayoutInflater |
| 5357 | */ |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5358 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5359 | if (!"fragment".equals(name)) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5360 | return onCreateView(name, context, attrs); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5361 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5362 | |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 5363 | return mFragments.onCreateView(parent, name, context, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5364 | } |
| 5365 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5366 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5367 | * Print the Activity's state into the given stream. This gets invoked if |
Jeff Sharkey | 5554b70 | 2012-04-11 18:30:51 -0700 | [diff] [blame] | 5368 | * you run "adb shell dumpsys activity <activity_component_name>". |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5369 | * |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5370 | * @param prefix Desired prefix to prepend at each line of output. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5371 | * @param fd The raw file descriptor that the dump is being sent to. |
| 5372 | * @param writer The PrintWriter to which you should dump your state. This will be |
| 5373 | * closed for you after you return. |
| 5374 | * @param args additional arguments to the dump request. |
| 5375 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5376 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 5377 | dumpInner(prefix, fd, writer, args); |
| 5378 | } |
| 5379 | |
| 5380 | void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5381 | writer.print(prefix); writer.print("Local Activity "); |
| 5382 | writer.print(Integer.toHexString(System.identityHashCode(this))); |
| 5383 | writer.println(" State:"); |
| 5384 | String innerPrefix = prefix + " "; |
| 5385 | writer.print(innerPrefix); writer.print("mResumed="); |
| 5386 | writer.print(mResumed); writer.print(" mStopped="); |
| 5387 | writer.print(mStopped); writer.print(" mFinished="); |
| 5388 | writer.println(mFinished); |
| 5389 | writer.print(innerPrefix); writer.print("mLoadersStarted="); |
| 5390 | writer.println(mLoadersStarted); |
| 5391 | writer.print(innerPrefix); writer.print("mChangingConfigurations="); |
| 5392 | writer.println(mChangingConfigurations); |
| 5393 | writer.print(innerPrefix); writer.print("mCurrentConfig="); |
| 5394 | writer.println(mCurrentConfig); |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5395 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5396 | if (mLoaderManager != null) { |
| 5397 | writer.print(prefix); writer.print("Loader Manager "); |
| 5398 | writer.print(Integer.toHexString(System.identityHashCode(mLoaderManager))); |
| 5399 | writer.println(":"); |
| 5400 | mLoaderManager.dump(prefix + " ", fd, writer, args); |
| 5401 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5402 | |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5403 | mFragments.dump(prefix, fd, writer, args); |
| 5404 | |
Michael Wright | 5f48dc7 | 2013-11-01 12:42:49 -0700 | [diff] [blame] | 5405 | if (getWindow() != null && |
| 5406 | getWindow().peekDecorView() != null && |
| 5407 | getWindow().peekDecorView().getViewRootImpl() != null) { |
| 5408 | getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args); |
| 5409 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5410 | |
| 5411 | mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | /** |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5415 | * Bit indicating that this activity is "immersive" and should not be |
| 5416 | * interrupted by notifications if possible. |
| 5417 | * |
| 5418 | * This value is initially set by the manifest property |
| 5419 | * <code>android:immersive</code> but may be changed at runtime by |
| 5420 | * {@link #setImmersive}. |
| 5421 | * |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 5422 | * @see #setImmersive(boolean) |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5423 | * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 5424 | */ |
| 5425 | public boolean isImmersive() { |
| 5426 | try { |
| 5427 | return ActivityManagerNative.getDefault().isImmersive(mToken); |
| 5428 | } catch (RemoteException e) { |
| 5429 | return false; |
| 5430 | } |
| 5431 | } |
| 5432 | |
| 5433 | /** |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5434 | * Indication of whether this is the highest level activity in this task. Can be used to |
| 5435 | * determine whether an activity launched by this activity was placed in the same task or |
| 5436 | * another task. |
| 5437 | * |
| 5438 | * @return true if this is the topmost, non-finishing activity in its task. |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5439 | */ |
George Mount | ff24328 | 2014-07-07 16:12:07 -0700 | [diff] [blame] | 5440 | private boolean isTopOfTask() { |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5441 | try { |
| 5442 | return ActivityManagerNative.getDefault().isTopOfTask(mToken); |
| 5443 | } catch (RemoteException e) { |
| 5444 | return false; |
| 5445 | } |
| 5446 | } |
| 5447 | |
| 5448 | /** |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5449 | * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a |
| 5450 | * fullscreen opaque Activity. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5451 | * <p> |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5452 | * Call this whenever the background of a translucent Activity has changed to become opaque. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5453 | * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released. |
| 5454 | * <p> |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5455 | * This call has no effect on non-translucent activities or on activities with the |
| 5456 | * {@link android.R.attr#windowIsFloating} attribute. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5457 | * |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5458 | * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener, |
| 5459 | * ActivityOptions) |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5460 | * @see TranslucentConversionListener |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 5461 | * |
| 5462 | * @hide |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5463 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5464 | @SystemApi |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5465 | public void convertFromTranslucent() { |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5466 | try { |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5467 | mTranslucentCallback = null; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 5468 | if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) { |
| 5469 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true); |
| 5470 | } |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5471 | } catch (RemoteException e) { |
| 5472 | // pass |
| 5473 | } |
| 5474 | } |
| 5475 | |
| 5476 | /** |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5477 | * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from |
| 5478 | * opaque to translucent following a call to {@link #convertFromTranslucent()}. |
| 5479 | * <p> |
| 5480 | * Calling this allows the Activity behind this one to be seen again. Once all such Activities |
| 5481 | * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will |
| 5482 | * be called indicating that it is safe to make this activity translucent again. Until |
| 5483 | * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image |
| 5484 | * behind the frontmost Activity will be indeterminate. |
| 5485 | * <p> |
| 5486 | * This call has no effect on non-translucent activities or on activities with the |
| 5487 | * {@link android.R.attr#windowIsFloating} attribute. |
| 5488 | * |
| 5489 | * @param callback the method to call when all visible Activities behind this one have been |
| 5490 | * drawn and it is safe to make this Activity translucent again. |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5491 | * @param options activity options delivered to the activity below this one. The options |
| 5492 | * are retrieved using {@link #getActivityOptions}. |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 5493 | * @return <code>true</code> if Window was opaque and will become translucent or |
| 5494 | * <code>false</code> if window was translucent and no change needed to be made. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5495 | * |
| 5496 | * @see #convertFromTranslucent() |
| 5497 | * @see TranslucentConversionListener |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 5498 | * |
| 5499 | * @hide |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5500 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5501 | @SystemApi |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 5502 | public boolean convertToTranslucent(TranslucentConversionListener callback, |
Jose Lima | 5517ea7 | 2014-06-10 12:31:43 -0700 | [diff] [blame] | 5503 | ActivityOptions options) { |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5504 | boolean drawComplete; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5505 | try { |
| 5506 | mTranslucentCallback = callback; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 5507 | mChangeCanvasToTranslucent = |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5508 | ActivityManagerNative.getDefault().convertToTranslucent(mToken, options); |
George Mount | 9e18397 | 2014-09-03 12:35:09 -0700 | [diff] [blame] | 5509 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false); |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5510 | drawComplete = true; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5511 | } catch (RemoteException e) { |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5512 | // Make callback return as though it timed out. |
| 5513 | mChangeCanvasToTranslucent = false; |
| 5514 | drawComplete = false; |
| 5515 | } |
| 5516 | if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) { |
| 5517 | // Window is already translucent. |
| 5518 | mTranslucentCallback.onTranslucentConversionComplete(drawComplete); |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5519 | } |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 5520 | return mChangeCanvasToTranslucent; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5521 | } |
| 5522 | |
| 5523 | /** @hide */ |
| 5524 | void onTranslucentConversionComplete(boolean drawComplete) { |
| 5525 | if (mTranslucentCallback != null) { |
| 5526 | mTranslucentCallback.onTranslucentConversionComplete(drawComplete); |
| 5527 | mTranslucentCallback = null; |
| 5528 | } |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 5529 | if (mChangeCanvasToTranslucent) { |
| 5530 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false); |
| 5531 | } |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5532 | } |
| 5533 | |
Craig Mautner | eb8abf7 | 2014-07-02 15:04:09 -0700 | [diff] [blame] | 5534 | /** @hide */ |
| 5535 | public void onNewActivityOptions(ActivityOptions options) { |
| 5536 | mActivityTransitionState.setEnterActivityOptions(this, options); |
| 5537 | if (!mStopped) { |
| 5538 | mActivityTransitionState.enterReady(this); |
| 5539 | } |
| 5540 | } |
| 5541 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5542 | /** |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5543 | * Retrieve the ActivityOptions passed in from the launching activity or passed back |
| 5544 | * from an activity launched by this activity in its call to {@link |
| 5545 | * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)} |
| 5546 | * |
| 5547 | * @return The ActivityOptions passed to {@link #convertToTranslucent}. |
| 5548 | * @hide |
| 5549 | */ |
| 5550 | ActivityOptions getActivityOptions() { |
| 5551 | try { |
| 5552 | return ActivityManagerNative.getDefault().getActivityOptions(mToken); |
| 5553 | } catch (RemoteException e) { |
| 5554 | } |
| 5555 | return null; |
| 5556 | } |
| 5557 | |
| 5558 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5559 | * Activities that want to remain visible behind a translucent activity above them must call |
Craig Mautner | 64ccb70 | 2014-10-01 09:38:40 -0700 | [diff] [blame] | 5560 | * this method anytime between the start of {@link #onResume()} and the return from |
| 5561 | * {@link #onPause()}. If this call is successful then the activity will remain visible after |
| 5562 | * {@link #onPause()} is called, and is allowed to continue playing media in the background. |
| 5563 | * |
| 5564 | * <p>The actions of this call are reset each time that this activity is brought to the |
| 5565 | * front. That is, every time {@link #onResume()} is called the activity will be assumed |
| 5566 | * to not have requested visible behind. Therefore, if you want this activity to continue to |
| 5567 | * be visible in the background you must call this method again. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5568 | * |
| 5569 | * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop |
| 5570 | * for dialog and translucent activities. |
| 5571 | * |
Craig Mautner | 64ccb70 | 2014-10-01 09:38:40 -0700 | [diff] [blame] | 5572 | * <p>Under all circumstances, the activity must stop playing and release resources prior to or |
| 5573 | * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false. |
| 5574 | * |
| 5575 | * <p>False will be returned any time this method is called between the return of onPause and |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5576 | * the next call to onResume. |
| 5577 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5578 | * @param visible true to notify the system that the activity wishes to be visible behind other |
| 5579 | * translucent activities, false to indicate otherwise. Resources must be |
| 5580 | * released when passing false to this method. |
Craig Mautner | 64ccb70 | 2014-10-01 09:38:40 -0700 | [diff] [blame] | 5581 | * @return the resulting visibiity state. If true the activity will remain visible beyond |
| 5582 | * {@link #onPause()} if the next activity is translucent or not fullscreen. If false |
| 5583 | * then the activity may not count on being visible behind other translucent activities, |
| 5584 | * and must stop any media playback and release resources. |
| 5585 | * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so |
| 5586 | * the return value must be checked. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5587 | * |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 5588 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5589 | * @see #onBackgroundVisibleBehindChanged(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5590 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5591 | public boolean requestVisibleBehind(boolean visible) { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5592 | if (!mResumed) { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5593 | // Do not permit paused or stopped activities to do this. |
| 5594 | visible = false; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5595 | } |
| 5596 | try { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5597 | mVisibleBehind = ActivityManagerNative.getDefault() |
| 5598 | .requestVisibleBehind(mToken, visible) && visible; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5599 | } catch (RemoteException e) { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5600 | mVisibleBehind = false; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5601 | } |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5602 | return mVisibleBehind; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5603 | } |
| 5604 | |
| 5605 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5606 | * Called when a translucent activity over this activity is becoming opaque or another |
| 5607 | * activity is being launched. Activities that override this method must call |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 5608 | * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5609 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5610 | * <p>When this method is called the activity has 500 msec to release any resources it may be |
| 5611 | * using while visible in the background. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5612 | * If the activity has not returned from this method in 500 msec the system will destroy |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5613 | * the activity and kill the process in order to recover the resources for another |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5614 | * process. Otherwise {@link #onStop()} will be called following return. |
| 5615 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5616 | * @see #requestVisibleBehind(boolean) |
| 5617 | * @see #onBackgroundVisibleBehindChanged(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5618 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 5619 | @CallSuper |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 5620 | public void onVisibleBehindCanceled() { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5621 | mCalled = true; |
| 5622 | } |
| 5623 | |
| 5624 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5625 | * Translucent activities may call this to determine if there is an activity below them that |
| 5626 | * is currently set to be visible in the background. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5627 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5628 | * @return true if an activity below is set to visible according to the most recent call to |
| 5629 | * {@link #requestVisibleBehind(boolean)}, false otherwise. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5630 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5631 | * @see #requestVisibleBehind(boolean) |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 5632 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5633 | * @see #onBackgroundVisibleBehindChanged(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5634 | * @hide |
| 5635 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5636 | @SystemApi |
| 5637 | public boolean isBackgroundVisibleBehind() { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5638 | try { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5639 | return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken); |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5640 | } catch (RemoteException e) { |
| 5641 | } |
| 5642 | return false; |
| 5643 | } |
| 5644 | |
| 5645 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5646 | * The topmost foreground activity will receive this call when the background visibility state |
| 5647 | * of the activity below it changes. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5648 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5649 | * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5650 | * due to a background activity finishing itself. |
| 5651 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5652 | * @param visible true if a background activity is visible, false otherwise. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5653 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5654 | * @see #requestVisibleBehind(boolean) |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 5655 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5656 | * @hide |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5657 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5658 | @SystemApi |
| 5659 | public void onBackgroundVisibleBehindChanged(boolean visible) { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | /** |
Craig Mautner | 8746a47 | 2014-07-24 15:12:54 -0700 | [diff] [blame] | 5663 | * Activities cannot draw during the period that their windows are animating in. In order |
| 5664 | * to know when it is safe to begin drawing they can override this method which will be |
| 5665 | * called when the entering animation has completed. |
| 5666 | */ |
| 5667 | public void onEnterAnimationComplete() { |
| 5668 | } |
| 5669 | |
| 5670 | /** |
Filip Gruszczynski | 6eafa90 | 2014-11-14 14:24:37 -0800 | [diff] [blame] | 5671 | * @hide |
| 5672 | */ |
| 5673 | public void dispatchEnterAnimationComplete() { |
| 5674 | onEnterAnimationComplete(); |
| 5675 | if (getWindow() != null && getWindow().getDecorView() != null) { |
| 5676 | getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete(); |
| 5677 | } |
| 5678 | } |
| 5679 | |
| 5680 | /** |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5681 | * Adjust the current immersive mode setting. |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 5682 | * |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5683 | * Note that changing this value will have no effect on the activity's |
| 5684 | * {@link android.content.pm.ActivityInfo} structure; that is, if |
| 5685 | * <code>android:immersive</code> is set to <code>true</code> |
| 5686 | * in the application's manifest entry for this activity, the {@link |
| 5687 | * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will |
| 5688 | * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 5689 | * FLAG_IMMERSIVE} bit set. |
| 5690 | * |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 5691 | * @see #isImmersive() |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5692 | * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 5693 | */ |
| 5694 | public void setImmersive(boolean i) { |
| 5695 | try { |
| 5696 | ActivityManagerNative.getDefault().setImmersive(mToken, i); |
| 5697 | } catch (RemoteException e) { |
| 5698 | // pass |
| 5699 | } |
| 5700 | } |
| 5701 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 5702 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 5703 | * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}. |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 5704 | * |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 5705 | * @param callback Callback that will manage lifecycle events for this action mode |
| 5706 | * @return The ActionMode that was started, or null if it was canceled |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 5707 | * |
| 5708 | * @see ActionMode |
| 5709 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5710 | @Nullable |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 5711 | public ActionMode startActionMode(ActionMode.Callback callback) { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 5712 | return mWindow.getDecorView().startActionMode(callback); |
| 5713 | } |
| 5714 | |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 5715 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 5716 | * Start an action mode of the given type. |
| 5717 | * |
| 5718 | * @param callback Callback that will manage lifecycle events for this action mode |
| 5719 | * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}. |
| 5720 | * @return The ActionMode that was started, or null if it was canceled |
| 5721 | * |
| 5722 | * @see ActionMode |
| 5723 | */ |
| 5724 | @Nullable |
| 5725 | public ActionMode startActionMode(ActionMode.Callback callback, int type) { |
| 5726 | return mWindow.getDecorView().startActionMode(callback, type); |
| 5727 | } |
| 5728 | |
| 5729 | /** |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 5730 | * Give the Activity a chance to control the UI for an action mode requested |
| 5731 | * by the system. |
| 5732 | * |
| 5733 | * <p>Note: If you are looking for a notification callback that an action mode |
| 5734 | * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p> |
| 5735 | * |
| 5736 | * @param callback The callback that should control the new action mode |
| 5737 | * @return The new action mode, or <code>null</code> if the activity does not want to |
| 5738 | * provide special handling for this action mode. (It will be handled by the system.) |
| 5739 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5740 | @Nullable |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5741 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 5742 | public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 5743 | // Only Primary ActionModes are represented in the ActionBar. |
| 5744 | if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) { |
| 5745 | initWindowDecorActionBar(); |
| 5746 | if (mActionBar != null) { |
| 5747 | return mActionBar.startActionMode(callback); |
| 5748 | } |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 5749 | } |
| 5750 | return null; |
| 5751 | } |
| 5752 | |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 5753 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 5754 | * {@inheritDoc} |
| 5755 | */ |
| 5756 | @Nullable |
| 5757 | @Override |
| 5758 | public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) { |
| 5759 | try { |
| 5760 | mActionModeTypeStarting = type; |
| 5761 | return onWindowStartingActionMode(callback); |
| 5762 | } finally { |
| 5763 | mActionModeTypeStarting = ActionMode.TYPE_PRIMARY; |
| 5764 | } |
| 5765 | } |
| 5766 | |
| 5767 | /** |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 5768 | * Notifies the Activity that an action mode has been started. |
| 5769 | * Activity subclasses overriding this method should call the superclass implementation. |
| 5770 | * |
| 5771 | * @param mode The new action mode. |
| 5772 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 5773 | @CallSuper |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5774 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 5775 | public void onActionModeStarted(ActionMode mode) { |
| 5776 | } |
| 5777 | |
| 5778 | /** |
| 5779 | * Notifies the activity that an action mode has finished. |
| 5780 | * Activity subclasses overriding this method should call the superclass implementation. |
| 5781 | * |
| 5782 | * @param mode The action mode that just finished. |
| 5783 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 5784 | @CallSuper |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5785 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 5786 | public void onActionModeFinished(ActionMode mode) { |
| 5787 | } |
| 5788 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 5789 | /** |
| 5790 | * Returns true if the app should recreate the task when navigating 'up' from this activity |
| 5791 | * by using targetIntent. |
| 5792 | * |
| 5793 | * <p>If this method returns false the app can trivially call |
| 5794 | * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform |
| 5795 | * up navigation. If this method returns false, the app should synthesize a new task stack |
| 5796 | * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p> |
| 5797 | * |
| 5798 | * @param targetIntent An intent representing the target destination for up navigation |
| 5799 | * @return true if navigating up should recreate a new task stack, false if the same task |
| 5800 | * should be used for the destination |
| 5801 | */ |
| 5802 | public boolean shouldUpRecreateTask(Intent targetIntent) { |
| 5803 | try { |
| 5804 | PackageManager pm = getPackageManager(); |
| 5805 | ComponentName cn = targetIntent.getComponent(); |
| 5806 | if (cn == null) { |
| 5807 | cn = targetIntent.resolveActivity(pm); |
| 5808 | } |
| 5809 | ActivityInfo info = pm.getActivityInfo(cn, 0); |
| 5810 | if (info.taskAffinity == null) { |
| 5811 | return false; |
| 5812 | } |
Dianne Hackborn | 6f4d61f | 2014-08-21 17:50:42 -0700 | [diff] [blame] | 5813 | return ActivityManagerNative.getDefault() |
| 5814 | .shouldUpRecreateTask(mToken, info.taskAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 5815 | } catch (RemoteException e) { |
| 5816 | return false; |
| 5817 | } catch (NameNotFoundException e) { |
| 5818 | return false; |
| 5819 | } |
| 5820 | } |
| 5821 | |
| 5822 | /** |
| 5823 | * Navigate from this activity to the activity specified by upIntent, finishing this activity |
| 5824 | * in the process. If the activity indicated by upIntent already exists in the task's history, |
| 5825 | * this activity and all others before the indicated activity in the history stack will be |
Adam Powell | 35c064b | 2012-05-02 11:37:15 -0700 | [diff] [blame] | 5826 | * finished. |
| 5827 | * |
| 5828 | * <p>If the indicated activity does not appear in the history stack, this will finish |
| 5829 | * each activity in this task until the root activity of the task is reached, resulting in |
| 5830 | * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy |
| 5831 | * when an activity may be reached by a path not passing through a canonical parent |
| 5832 | * activity.</p> |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 5833 | * |
| 5834 | * <p>This method should be used when performing up navigation from within the same task |
| 5835 | * as the destination. If up navigation should cross tasks in some cases, see |
| 5836 | * {@link #shouldUpRecreateTask(Intent)}.</p> |
| 5837 | * |
| 5838 | * @param upIntent An intent representing the target destination for up navigation |
| 5839 | * |
| 5840 | * @return true if up navigation successfully reached the activity indicated by upIntent and |
| 5841 | * upIntent was delivered to it. false if an instance of the indicated activity could |
| 5842 | * not be found and this activity was simply finished normally. |
| 5843 | */ |
| 5844 | public boolean navigateUpTo(Intent upIntent) { |
| 5845 | if (mParent == null) { |
| 5846 | ComponentName destInfo = upIntent.getComponent(); |
| 5847 | if (destInfo == null) { |
| 5848 | destInfo = upIntent.resolveActivity(getPackageManager()); |
| 5849 | if (destInfo == null) { |
| 5850 | return false; |
| 5851 | } |
| 5852 | upIntent = new Intent(upIntent); |
| 5853 | upIntent.setComponent(destInfo); |
| 5854 | } |
| 5855 | int resultCode; |
| 5856 | Intent resultData; |
| 5857 | synchronized (this) { |
| 5858 | resultCode = mResultCode; |
| 5859 | resultData = mResultData; |
| 5860 | } |
| 5861 | if (resultData != null) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 5862 | resultData.prepareToLeaveProcess(); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 5863 | } |
| 5864 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 5865 | upIntent.prepareToLeaveProcess(); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 5866 | return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent, |
| 5867 | resultCode, resultData); |
| 5868 | } catch (RemoteException e) { |
| 5869 | return false; |
| 5870 | } |
| 5871 | } else { |
| 5872 | return mParent.navigateUpToFromChild(this, upIntent); |
| 5873 | } |
| 5874 | } |
| 5875 | |
| 5876 | /** |
| 5877 | * This is called when a child activity of this one calls its |
| 5878 | * {@link #navigateUpTo} method. The default implementation simply calls |
| 5879 | * navigateUpTo(upIntent) on this activity (the parent). |
| 5880 | * |
| 5881 | * @param child The activity making the call. |
| 5882 | * @param upIntent An intent representing the target destination for up navigation |
| 5883 | * |
| 5884 | * @return true if up navigation successfully reached the activity indicated by upIntent and |
| 5885 | * upIntent was delivered to it. false if an instance of the indicated activity could |
| 5886 | * not be found and this activity was simply finished normally. |
| 5887 | */ |
| 5888 | public boolean navigateUpToFromChild(Activity child, Intent upIntent) { |
| 5889 | return navigateUpTo(upIntent); |
| 5890 | } |
| 5891 | |
| 5892 | /** |
| 5893 | * Obtain an {@link Intent} that will launch an explicit target activity specified by |
| 5894 | * this activity's logical parent. The logical parent is named in the application's manifest |
| 5895 | * by the {@link android.R.attr#parentActivityName parentActivityName} attribute. |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 5896 | * Activity subclasses may override this method to modify the Intent returned by |
| 5897 | * super.getParentActivityIntent() or to implement a different mechanism of retrieving |
| 5898 | * the parent intent entirely. |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 5899 | * |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 5900 | * @return a new Intent targeting the defined parent of this activity or null if |
| 5901 | * there is no valid parent. |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 5902 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5903 | @Nullable |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 5904 | public Intent getParentActivityIntent() { |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 5905 | final String parentName = mActivityInfo.parentActivityName; |
| 5906 | if (TextUtils.isEmpty(parentName)) { |
| 5907 | return null; |
| 5908 | } |
Adam Powell | 5a4010c | 2012-09-16 15:14:05 -0700 | [diff] [blame] | 5909 | |
| 5910 | // If the parent itself has no parent, generate a main activity intent. |
| 5911 | final ComponentName target = new ComponentName(this, parentName); |
| 5912 | try { |
| 5913 | final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0); |
| 5914 | final String parentActivity = parentInfo.parentActivityName; |
| 5915 | final Intent parentIntent = parentActivity == null |
| 5916 | ? Intent.makeMainActivity(target) |
| 5917 | : new Intent().setComponent(target); |
| 5918 | return parentIntent; |
| 5919 | } catch (NameNotFoundException e) { |
| 5920 | Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName + |
| 5921 | "' in manifest"); |
| 5922 | return null; |
| 5923 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 5924 | } |
| 5925 | |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 5926 | /** |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5927 | * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 5928 | * android.view.View, String)} was used to start an Activity, <var>callback</var> |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 5929 | * will be called to handle shared elements on the <i>launched</i> Activity. This requires |
George Mount | 9826f63 | 2014-09-11 08:50:09 -0700 | [diff] [blame] | 5930 | * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 5931 | * |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 5932 | * @param callback Used to manipulate shared element transitions on the launched Activity. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 5933 | */ |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 5934 | public void setEnterSharedElementCallback(SharedElementCallback callback) { |
| 5935 | if (callback == null) { |
| 5936 | callback = SharedElementCallback.NULL_CALLBACK; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 5937 | } |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 5938 | mEnterTransitionListener = callback; |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 5939 | } |
| 5940 | |
| 5941 | /** |
| 5942 | * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 5943 | * android.view.View, String)} was used to start an Activity, <var>callback</var> |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 5944 | * will be called to handle shared elements on the <i>launching</i> Activity. Most |
| 5945 | * calls will only come when returning from the started Activity. |
George Mount | 9826f63 | 2014-09-11 08:50:09 -0700 | [diff] [blame] | 5946 | * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 5947 | * |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 5948 | * @param callback Used to manipulate shared element transitions on the launching Activity. |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 5949 | */ |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 5950 | public void setExitSharedElementCallback(SharedElementCallback callback) { |
| 5951 | if (callback == null) { |
| 5952 | callback = SharedElementCallback.NULL_CALLBACK; |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 5953 | } |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 5954 | mExitTransitionListener = callback; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 5955 | } |
| 5956 | |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 5957 | /** |
| 5958 | * Postpone the entering activity transition when Activity was started with |
| 5959 | * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
| 5960 | * android.util.Pair[])}. |
| 5961 | * <p>This method gives the Activity the ability to delay starting the entering and |
| 5962 | * shared element transitions until all data is loaded. Until then, the Activity won't |
| 5963 | * draw into its window, leaving the window transparent. This may also cause the |
| 5964 | * returning animation to be delayed until data is ready. This method should be |
| 5965 | * called in {@link #onCreate(android.os.Bundle)} or in |
| 5966 | * {@link #onActivityReenter(int, android.content.Intent)}. |
| 5967 | * {@link #startPostponedEnterTransition()} must be called to allow the Activity to |
| 5968 | * start the transitions. If the Activity did not use |
| 5969 | * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
| 5970 | * android.util.Pair[])}, then this method does nothing.</p> |
| 5971 | */ |
| 5972 | public void postponeEnterTransition() { |
| 5973 | mActivityTransitionState.postponeEnterTransition(); |
| 5974 | } |
| 5975 | |
| 5976 | /** |
| 5977 | * Begin postponed transitions after {@link #postponeEnterTransition()} was called. |
| 5978 | * If postponeEnterTransition() was called, you must call startPostponedEnterTransition() |
| 5979 | * to have your Activity start drawing. |
| 5980 | */ |
| 5981 | public void startPostponedEnterTransition() { |
| 5982 | mActivityTransitionState.startPostponedEnterTransition(); |
| 5983 | } |
| 5984 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5985 | // ------------------ Internal API ------------------ |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5986 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5987 | final void setParent(Activity parent) { |
| 5988 | mParent = parent; |
| 5989 | } |
| 5990 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5991 | final void attach(Context context, ActivityThread aThread, |
| 5992 | Instrumentation instr, IBinder token, int ident, |
| 5993 | Application application, Intent intent, ActivityInfo info, |
| 5994 | CharSequence title, Activity parent, String id, |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 5995 | NonConfigurationInstances lastNonConfigurationInstances, |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 5996 | Configuration config, String referrer, IVoiceInteractor voiceInteractor) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5997 | attachBaseContext(context); |
| 5998 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 5999 | mFragments.attachActivity(this, mContainer, null); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6000 | |
Jorim Jaggi | b10e33f | 2015-02-04 21:57:40 +0100 | [diff] [blame] | 6001 | mWindow = new PhoneWindow(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6002 | mWindow.setCallback(this); |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 6003 | mWindow.setOnWindowDismissedCallback(this); |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 6004 | mWindow.getLayoutInflater().setPrivateFactory(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6005 | if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) { |
| 6006 | mWindow.setSoftInputMode(info.softInputMode); |
| 6007 | } |
Adam Powell | 269248d | 2011-08-02 10:26:54 -0700 | [diff] [blame] | 6008 | if (info.uiOptions != 0) { |
| 6009 | mWindow.setUiOptions(info.uiOptions); |
| 6010 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6011 | mUiThread = Thread.currentThread(); |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6012 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6013 | mMainThread = aThread; |
| 6014 | mInstrumentation = instr; |
| 6015 | mToken = token; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6016 | mIdent = ident; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6017 | mApplication = application; |
| 6018 | mIntent = intent; |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 6019 | mReferrer = referrer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6020 | mComponent = intent.getComponent(); |
| 6021 | mActivityInfo = info; |
| 6022 | mTitle = title; |
| 6023 | mParent = parent; |
| 6024 | mEmbeddedID = id; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6025 | mLastNonConfigurationInstances = lastNonConfigurationInstances; |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 6026 | if (voiceInteractor != null) { |
| 6027 | if (lastNonConfigurationInstances != null) { |
| 6028 | mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor; |
| 6029 | } else { |
| 6030 | mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this, |
| 6031 | Looper.myLooper()); |
| 6032 | } |
| 6033 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6034 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 6035 | mWindow.setWindowManager( |
| 6036 | (WindowManager)context.getSystemService(Context.WINDOW_SERVICE), |
| 6037 | mToken, mComponent.flattenToString(), |
Romain Guy | 529b60a | 2010-08-03 18:05:47 -0700 | [diff] [blame] | 6038 | (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6039 | if (mParent != null) { |
| 6040 | mWindow.setContainer(mParent.getWindow()); |
| 6041 | } |
| 6042 | mWindowManager = mWindow.getWindowManager(); |
| 6043 | mCurrentConfig = config; |
| 6044 | } |
| 6045 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 6046 | /** @hide */ |
| 6047 | public final IBinder getActivityToken() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6048 | return mParent != null ? mParent.getActivityToken() : mToken; |
| 6049 | } |
| 6050 | |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6051 | final void performCreateCommon() { |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 6052 | mVisibleFromClient = !mWindow.getWindowStyle().getBoolean( |
| 6053 | com.android.internal.R.styleable.Window_windowNoDisplay, false); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 6054 | mFragments.dispatchActivityCreated(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6055 | mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions()); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6056 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6057 | |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6058 | final void performCreate(Bundle icicle) { |
| 6059 | onCreate(icicle); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6060 | mActivityTransitionState.readState(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6061 | performCreateCommon(); |
| 6062 | } |
| 6063 | |
| 6064 | final void performCreate(Bundle icicle, PersistableBundle persistentState) { |
| 6065 | onCreate(icicle, persistentState); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6066 | mActivityTransitionState.readState(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6067 | performCreateCommon(); |
| 6068 | } |
| 6069 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6070 | final void performStart() { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6071 | mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions()); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6072 | mFragments.noteStateNotSaved(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6073 | mCalled = false; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6074 | mFragments.execPendingActions(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6075 | mInstrumentation.callActivityOnStart(this); |
| 6076 | if (!mCalled) { |
| 6077 | throw new SuperNotCalledException( |
| 6078 | "Activity " + mComponent.toShortString() + |
| 6079 | " did not call through to super.onStart()"); |
| 6080 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6081 | mFragments.dispatchStart(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 6082 | if (mAllLoaderManagers != null) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 6083 | final int N = mAllLoaderManagers.size(); |
| 6084 | LoaderManagerImpl loaders[] = new LoaderManagerImpl[N]; |
| 6085 | for (int i=N-1; i>=0; i--) { |
| 6086 | loaders[i] = mAllLoaderManagers.valueAt(i); |
| 6087 | } |
| 6088 | for (int i=0; i<N; i++) { |
| 6089 | LoaderManagerImpl lm = loaders[i]; |
| 6090 | lm.finishRetain(); |
| 6091 | lm.doReportStart(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 6092 | } |
| 6093 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6094 | mActivityTransitionState.enterReady(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6095 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6096 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6097 | final void performRestart() { |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6098 | mFragments.noteStateNotSaved(); |
Dianne Hackborn | a21e3da | 2010-09-12 19:27:46 -0700 | [diff] [blame] | 6099 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6100 | if (mStopped) { |
| 6101 | mStopped = false; |
Dianne Hackborn | ce418e6 | 2011-03-01 14:31:38 -0800 | [diff] [blame] | 6102 | if (mToken != null && mParent == null) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 6103 | WindowManagerGlobal.getInstance().setStoppedState(mToken, false); |
Dianne Hackborn | ce418e6 | 2011-03-01 14:31:38 -0800 | [diff] [blame] | 6104 | } |
Dianne Hackborn | 185e3e2 | 2011-06-03 15:26:01 -0700 | [diff] [blame] | 6105 | |
| 6106 | synchronized (mManagedCursors) { |
| 6107 | final int N = mManagedCursors.size(); |
| 6108 | for (int i=0; i<N; i++) { |
| 6109 | ManagedCursor mc = mManagedCursors.get(i); |
| 6110 | if (mc.mReleased || mc.mUpdated) { |
| 6111 | if (!mc.mCursor.requery()) { |
Dianne Hackborn | a5445d3 | 2011-09-01 14:38:24 -0700 | [diff] [blame] | 6112 | if (getApplicationInfo().targetSdkVersion |
| 6113 | >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 6114 | throw new IllegalStateException( |
| 6115 | "trying to requery an already closed cursor " |
| 6116 | + mc.mCursor); |
| 6117 | } |
Dianne Hackborn | 185e3e2 | 2011-06-03 15:26:01 -0700 | [diff] [blame] | 6118 | } |
| 6119 | mc.mReleased = false; |
| 6120 | mc.mUpdated = false; |
| 6121 | } |
| 6122 | } |
| 6123 | } |
| 6124 | |
| 6125 | mCalled = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6126 | mInstrumentation.callActivityOnRestart(this); |
| 6127 | if (!mCalled) { |
| 6128 | throw new SuperNotCalledException( |
| 6129 | "Activity " + mComponent.toShortString() + |
| 6130 | " did not call through to super.onRestart()"); |
| 6131 | } |
| 6132 | performStart(); |
| 6133 | } |
| 6134 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6136 | final void performResume() { |
| 6137 | performRestart(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6138 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6139 | mFragments.execPendingActions(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6140 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6141 | mLastNonConfigurationInstances = null; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6143 | mCalled = false; |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6144 | // mResumed is set by the instrumentation |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6145 | mInstrumentation.callActivityOnResume(this); |
| 6146 | if (!mCalled) { |
| 6147 | throw new SuperNotCalledException( |
| 6148 | "Activity " + mComponent.toShortString() + |
| 6149 | " did not call through to super.onResume()"); |
| 6150 | } |
| 6151 | |
Todd Kennedy | ee8c9c6 | 2014-12-10 14:22:59 -0800 | [diff] [blame] | 6152 | // invisible activities must be finished before onResume() completes |
| 6153 | if (!mVisibleFromClient && !mFinished) { |
| 6154 | Log.w(TAG, "An activity without a UI must call finish() before onResume() completes"); |
| 6155 | if (getApplicationInfo().targetSdkVersion |
| 6156 | > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 6157 | throw new IllegalStateException( |
| 6158 | "Activity " + mComponent.toShortString() + |
| 6159 | " did not call finish() prior to onResume() completing"); |
| 6160 | } |
| 6161 | } |
| 6162 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6163 | // Now really resume, and install the current status bar and menu. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6164 | mCalled = false; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6165 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6166 | mFragments.dispatchResume(); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6167 | mFragments.execPendingActions(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6168 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6169 | onPostResume(); |
| 6170 | if (!mCalled) { |
| 6171 | throw new SuperNotCalledException( |
| 6172 | "Activity " + mComponent.toShortString() + |
| 6173 | " did not call through to super.onPostResume()"); |
| 6174 | } |
| 6175 | } |
| 6176 | |
| 6177 | final void performPause() { |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 6178 | mDoReportFullyDrawn = false; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6179 | mFragments.dispatchPause(); |
Dianne Hackborn | e794e9f | 2010-08-24 12:32:10 -0700 | [diff] [blame] | 6180 | mCalled = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6181 | onPause(); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6182 | mResumed = false; |
Dianne Hackborn | e794e9f | 2010-08-24 12:32:10 -0700 | [diff] [blame] | 6183 | if (!mCalled && getApplicationInfo().targetSdkVersion |
| 6184 | >= android.os.Build.VERSION_CODES.GINGERBREAD) { |
| 6185 | throw new SuperNotCalledException( |
| 6186 | "Activity " + mComponent.toShortString() + |
| 6187 | " did not call through to super.onPause()"); |
| 6188 | } |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6189 | mResumed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6190 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6191 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6192 | final void performUserLeaving() { |
| 6193 | onUserInteraction(); |
| 6194 | onUserLeaveHint(); |
| 6195 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6197 | final void performStop() { |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 6198 | mDoReportFullyDrawn = false; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6199 | if (mLoadersStarted) { |
| 6200 | mLoadersStarted = false; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 6201 | if (mLoaderManager != null) { |
| 6202 | if (!mChangingConfigurations) { |
| 6203 | mLoaderManager.doStop(); |
| 6204 | } else { |
| 6205 | mLoaderManager.doRetain(); |
| 6206 | } |
| 6207 | } |
| 6208 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6210 | if (!mStopped) { |
| 6211 | if (mWindow != null) { |
| 6212 | mWindow.closeAllPanels(); |
| 6213 | } |
| 6214 | |
Dianne Hackborn | ce418e6 | 2011-03-01 14:31:38 -0800 | [diff] [blame] | 6215 | if (mToken != null && mParent == null) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 6216 | WindowManagerGlobal.getInstance().setStoppedState(mToken, true); |
Dianne Hackborn | ce418e6 | 2011-03-01 14:31:38 -0800 | [diff] [blame] | 6217 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6218 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6219 | mFragments.dispatchStop(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6221 | mCalled = false; |
| 6222 | mInstrumentation.callActivityOnStop(this); |
| 6223 | if (!mCalled) { |
| 6224 | throw new SuperNotCalledException( |
| 6225 | "Activity " + mComponent.toShortString() + |
| 6226 | " did not call through to super.onStop()"); |
| 6227 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6228 | |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 6229 | synchronized (mManagedCursors) { |
| 6230 | final int N = mManagedCursors.size(); |
| 6231 | for (int i=0; i<N; i++) { |
| 6232 | ManagedCursor mc = mManagedCursors.get(i); |
| 6233 | if (!mc.mReleased) { |
| 6234 | mc.mCursor.deactivate(); |
| 6235 | mc.mReleased = true; |
| 6236 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6237 | } |
| 6238 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6240 | mStopped = true; |
| 6241 | } |
| 6242 | mResumed = false; |
| 6243 | } |
| 6244 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6245 | final void performDestroy() { |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 6246 | mDestroyed = true; |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 6247 | mWindow.destroy(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6248 | mFragments.dispatchDestroy(); |
| 6249 | onDestroy(); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 6250 | if (mLoaderManager != null) { |
| 6251 | mLoaderManager.doDestroy(); |
| 6252 | } |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 6253 | if (mVoiceInteractor != null) { |
| 6254 | mVoiceInteractor.detachActivity(); |
| 6255 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6256 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6257 | |
| 6258 | /** |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6259 | * @hide |
| 6260 | */ |
| 6261 | public final boolean isResumed() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6262 | return mResumed; |
| 6263 | } |
| 6264 | |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6265 | void dispatchActivityResult(String who, int requestCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6266 | int resultCode, Intent data) { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 6267 | if (false) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6268 | TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode |
| 6269 | + ", resCode=" + resultCode + ", data=" + data); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6270 | mFragments.noteStateNotSaved(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6271 | if (who == null) { |
| 6272 | onActivityResult(requestCode, resultCode, data); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 6273 | } else { |
| 6274 | Fragment frag = mFragments.findFragmentByWho(who); |
| 6275 | if (frag != null) { |
| 6276 | frag.onActivityResult(requestCode, resultCode, data); |
| 6277 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6278 | } |
| 6279 | } |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6280 | |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6281 | /** |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 6282 | * Request to put this Activity in a mode where the user is locked to the |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6283 | * current task. |
| 6284 | * |
| 6285 | * This will prevent the user from launching other apps, going to settings, |
| 6286 | * or reaching the home screen. |
| 6287 | * |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 6288 | * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true |
| 6289 | * for this component then the app will go directly into Lock Task mode. The user |
| 6290 | * will not be able to exit this mode until {@link Activity#stopLockTask()} is called. |
| 6291 | * |
| 6292 | * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false |
| 6293 | * then the system will prompt the user with a dialog requesting permission to enter |
Jason Monk | 8863b57 | 2014-07-29 13:49:32 -0400 | [diff] [blame] | 6294 | * this mode. When entered through this method the user can exit at any time through |
| 6295 | * an action described by the request dialog. Calling stopLockTask will also exit the |
| 6296 | * mode. |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6297 | */ |
Craig Mautner | aea74a5 | 2014-03-08 14:23:10 -0800 | [diff] [blame] | 6298 | public void startLockTask() { |
| 6299 | try { |
| 6300 | ActivityManagerNative.getDefault().startLockTaskMode(mToken); |
| 6301 | } catch (RemoteException e) { |
| 6302 | } |
| 6303 | } |
| 6304 | |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6305 | /** |
| 6306 | * Allow the user to switch away from the current task. |
| 6307 | * |
| 6308 | * Called to end the mode started by {@link Activity#startLockTask}. This |
| 6309 | * can only be called by activities that have successfully called |
| 6310 | * startLockTask previously. |
| 6311 | * |
| 6312 | * This will allow the user to exit this app and move onto other activities. |
| 6313 | */ |
Craig Mautner | aea74a5 | 2014-03-08 14:23:10 -0800 | [diff] [blame] | 6314 | public void stopLockTask() { |
| 6315 | try { |
| 6316 | ActivityManagerNative.getDefault().stopLockTaskMode(); |
| 6317 | } catch (RemoteException e) { |
| 6318 | } |
| 6319 | } |
| 6320 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6321 | /** |
| 6322 | * Interface for informing a translucent {@link Activity} once all visible activities below it |
| 6323 | * have completed drawing. This is necessary only after an {@link Activity} has been made |
| 6324 | * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn |
| 6325 | * translucent again following a call to {@link |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6326 | * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener, |
| 6327 | * ActivityOptions)} |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 6328 | * |
| 6329 | * @hide |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6330 | */ |
Jose Lima | 1491485 | 2014-08-14 09:14:12 -0700 | [diff] [blame] | 6331 | @SystemApi |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6332 | public interface TranslucentConversionListener { |
| 6333 | /** |
| 6334 | * Callback made following {@link Activity#convertToTranslucent} once all visible Activities |
| 6335 | * below the top one have been redrawn. Following this callback it is safe to make the top |
| 6336 | * Activity translucent because the underlying Activity has been drawn. |
| 6337 | * |
| 6338 | * @param drawComplete True if the background Activity has drawn itself. False if a timeout |
| 6339 | * occurred waiting for the Activity to complete drawing. |
| 6340 | * |
| 6341 | * @see Activity#convertFromTranslucent() |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6342 | * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions) |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6343 | */ |
| 6344 | public void onTranslucentConversionComplete(boolean drawComplete); |
| 6345 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6346 | } |