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 | |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 19 | import static java.lang.Character.MIN_VALUE; |
| 20 | |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 21 | import android.annotation.CallSuper; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 22 | import android.annotation.DrawableRes; |
| 23 | import android.annotation.IdRes; |
| 24 | import android.annotation.IntDef; |
| 25 | import android.annotation.LayoutRes; |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 26 | import android.annotation.MainThread; |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 27 | import android.annotation.NonNull; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 28 | import android.annotation.Nullable; |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 29 | import android.annotation.RequiresPermission; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 30 | import android.annotation.StyleRes; |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 31 | import android.annotation.SystemApi; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 32 | import android.app.admin.DevicePolicyManager; |
Dianne Hackborn | 69c6adc | 2015-06-02 10:52:59 -0700 | [diff] [blame] | 33 | import android.app.assist.AssistContent; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 34 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.content.ComponentName; |
| 36 | import android.content.ContentResolver; |
| 37 | import android.content.Context; |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 38 | import android.content.CursorLoader; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 39 | import android.content.IIntentSender; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 40 | import android.content.Intent; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 41 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.content.SharedPreferences; |
| 43 | import android.content.pm.ActivityInfo; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 44 | import android.content.pm.PackageManager; |
| 45 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.content.res.Configuration; |
| 47 | import android.content.res.Resources; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 48 | import android.content.res.TypedArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.database.Cursor; |
| 50 | import android.graphics.Bitmap; |
| 51 | import android.graphics.Canvas; |
Winson | 2d47683 | 2016-02-17 14:53:46 -0800 | [diff] [blame] | 52 | import android.graphics.Color; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 53 | import android.graphics.Paint; |
| 54 | import android.graphics.drawable.ColorDrawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.graphics.drawable.Drawable; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 56 | import android.graphics.drawable.InsetDrawable; |
| 57 | import android.graphics.drawable.LayerDrawable; |
| 58 | import android.graphics.drawable.ShapeDrawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 68 | import android.os.PersistableBundle; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 69 | import android.os.RemoteException; |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 70 | import android.os.StrictMode; |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 71 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.text.Selection; |
| 73 | import android.text.SpannableStringBuilder; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 74 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.text.method.TextKeyListener; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 76 | import android.transition.Scene; |
| 77 | import android.transition.TransitionManager; |
| 78 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.util.EventLog; |
| 81 | import android.util.Log; |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 82 | import android.util.PrintWriterPrinter; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 83 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.util.SparseArray; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 85 | import android.util.SuperNotCalledException; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 86 | import android.view.ActionMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import android.view.ContextMenu; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 88 | import android.view.ContextMenu.ContextMenuInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | import android.view.ContextThemeWrapper; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 90 | import android.view.DragEvent; |
| 91 | import android.view.DropPermissions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.view.KeyEvent; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 93 | import android.view.KeyboardShortcutGroup; |
| 94 | import android.view.KeyboardShortcutInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.view.LayoutInflater; |
| 96 | import android.view.Menu; |
| 97 | import android.view.MenuInflater; |
| 98 | import android.view.MenuItem; |
| 99 | import android.view.MotionEvent; |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 100 | import android.view.SearchEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.view.View; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 102 | import android.view.View.OnCreateContextMenuListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | import android.view.ViewGroup; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 104 | import android.view.ViewGroup.LayoutParams; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.view.ViewManager; |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 106 | import android.view.ViewRootImpl; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.view.Window; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 108 | import android.view.Window.WindowControllerCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | import android.view.WindowManager; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 110 | import android.view.WindowManagerGlobal; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 111 | import android.view.accessibility.AccessibilityEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.widget.AdapterView; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 113 | import android.widget.Toolbar; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 114 | |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 115 | import com.android.internal.app.IVoiceInteractor; |
| 116 | import com.android.internal.app.ToolbarActionBar; |
| 117 | import com.android.internal.app.WindowDecorActionBar; |
| 118 | import com.android.internal.policy.DecorView; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 119 | import com.android.internal.policy.PhoneWindow; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 121 | import java.io.FileDescriptor; |
| 122 | import java.io.PrintWriter; |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 123 | import java.lang.annotation.Retention; |
| 124 | import java.lang.annotation.RetentionPolicy; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 125 | import java.util.ArrayList; |
| 126 | import java.util.HashMap; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 127 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * An activity is a single, focused thing that the user can do. Almost all |
| 131 | * activities interact with the user, so the Activity class takes care of |
| 132 | * creating a window for you in which you can place your UI with |
| 133 | * {@link #setContentView}. While activities are often presented to the user |
| 134 | * as full-screen windows, they can also be used in other ways: as floating |
| 135 | * windows (via a theme with {@link android.R.attr#windowIsFloating} set) |
| 136 | * or embedded inside of another activity (using {@link ActivityGroup}). |
| 137 | * |
| 138 | * There are two methods almost all subclasses of Activity will implement: |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 139 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | * <ul> |
| 141 | * <li> {@link #onCreate} is where you initialize your activity. Most |
| 142 | * importantly, here you will usually call {@link #setContentView(int)} |
| 143 | * with a layout resource defining your UI, and using {@link #findViewById} |
| 144 | * to retrieve the widgets in that UI that you need to interact with |
| 145 | * programmatically. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 146 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | * <li> {@link #onPause} is where you deal with the user leaving your |
| 148 | * activity. Most importantly, any changes made by the user should at this |
| 149 | * point be committed (usually to the |
| 150 | * {@link android.content.ContentProvider} holding the data). |
| 151 | * </ul> |
| 152 | * |
| 153 | * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all |
| 154 | * activity classes must have a corresponding |
| 155 | * {@link android.R.styleable#AndroidManifestActivity <activity>} |
| 156 | * declaration in their package's <code>AndroidManifest.xml</code>.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 157 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | * <p>Topics covered here: |
| 159 | * <ol> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 160 | * <li><a href="#Fragments">Fragments</a> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | * <li><a href="#ActivityLifecycle">Activity Lifecycle</a> |
| 162 | * <li><a href="#ConfigurationChanges">Configuration Changes</a> |
| 163 | * <li><a href="#StartingActivities">Starting Activities and Getting Results</a> |
| 164 | * <li><a href="#SavingPersistentState">Saving Persistent State</a> |
| 165 | * <li><a href="#Permissions">Permissions</a> |
| 166 | * <li><a href="#ProcessLifecycle">Process Lifecycle</a> |
| 167 | * </ol> |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 168 | * |
| 169 | * <div class="special reference"> |
| 170 | * <h3>Developer Guides</h3> |
| 171 | * <p>The Activity class is an important part of an application's overall lifecycle, |
| 172 | * and the way activities are launched and put together is a fundamental |
| 173 | * part of the platform's application model. For a detailed perspective on the structure of an |
| 174 | * Android application and how activities behave, please read the |
| 175 | * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and |
| 176 | * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a> |
| 177 | * developer guides.</p> |
| 178 | * |
| 179 | * <p>You can also find a detailed discussion about how to create activities in the |
| 180 | * <a href="{@docRoot}guide/topics/fundamentals/activities.html">Activities</a> |
| 181 | * developer guide.</p> |
| 182 | * </div> |
| 183 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 184 | * <a name="Fragments"></a> |
| 185 | * <h3>Fragments</h3> |
| 186 | * |
| 187 | * <p>Starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}, Activity |
| 188 | * implementations can make use of the {@link Fragment} class to better |
| 189 | * modularize their code, build more sophisticated user interfaces for larger |
| 190 | * screens, and help scale their application between small and large screens. |
| 191 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | * <a name="ActivityLifecycle"></a> |
| 193 | * <h3>Activity Lifecycle</h3> |
| 194 | * |
| 195 | * <p>Activities in the system are managed as an <em>activity stack</em>. |
| 196 | * When a new activity is started, it is placed on the top of the stack |
| 197 | * and becomes the running activity -- the previous activity always remains |
| 198 | * below it in the stack, and will not come to the foreground again until |
| 199 | * the new activity exits.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 200 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | * <p>An activity has essentially four states:</p> |
| 202 | * <ul> |
| 203 | * <li> If an activity in the foreground of the screen (at the top of |
| 204 | * the stack), |
| 205 | * it is <em>active</em> or <em>running</em>. </li> |
| 206 | * <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] | 207 | * 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] | 208 | * is <em>paused</em>. A paused activity is completely alive (it |
| 209 | * maintains all state and member information and remains attached to |
| 210 | * the window manager), but can be killed by the system in extreme |
| 211 | * low memory situations. |
| 212 | * <li>If an activity is completely obscured by another activity, |
| 213 | * it is <em>stopped</em>. It still retains all state and member information, |
| 214 | * however, it is no longer visible to the user so its window is hidden |
| 215 | * and it will often be killed by the system when memory is needed |
| 216 | * elsewhere.</li> |
| 217 | * <li>If an activity is paused or stopped, the system can drop the activity |
| 218 | * from memory by either asking it to finish, or simply killing its |
| 219 | * process. When it is displayed again to the user, it must be |
| 220 | * completely restarted and restored to its previous state.</li> |
| 221 | * </ul> |
| 222 | * |
| 223 | * <p>The following diagram shows the important state paths of an Activity. |
| 224 | * The square rectangles represent callback methods you can implement to |
| 225 | * perform operations when the Activity moves between states. The colored |
| 226 | * ovals are major states the Activity can be in.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 227 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | * <p><img src="../../../images/activity_lifecycle.png" |
| 229 | * alt="State diagram for an Android Activity Lifecycle." border="0" /></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 230 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | * <p>There are three key loops you may be interested in monitoring within your |
| 232 | * activity: |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 233 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | * <ul> |
| 235 | * <li>The <b>entire lifetime</b> of an activity happens between the first call |
| 236 | * to {@link android.app.Activity#onCreate} through to a single final call |
| 237 | * to {@link android.app.Activity#onDestroy}. An activity will do all setup |
| 238 | * of "global" state in onCreate(), and release all remaining resources in |
| 239 | * onDestroy(). For example, if it has a thread running in the background |
| 240 | * to download data from the network, it may create that thread in onCreate() |
| 241 | * and then stop the thread in onDestroy(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 242 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | * <li>The <b>visible lifetime</b> of an activity happens between a call to |
| 244 | * {@link android.app.Activity#onStart} until a corresponding call to |
| 245 | * {@link android.app.Activity#onStop}. During this time the user can see the |
| 246 | * activity on-screen, though it may not be in the foreground and interacting |
| 247 | * with the user. Between these two methods you can maintain resources that |
| 248 | * are needed to show the activity to the user. For example, you can register |
| 249 | * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 250 | * that impact your UI, and unregister it in onStop() when the user no |
| 251 | * 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] | 252 | * can be called multiple times, as the activity becomes visible and hidden |
| 253 | * to the user. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 254 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | * <li>The <b>foreground lifetime</b> of an activity happens between a call to |
| 256 | * {@link android.app.Activity#onResume} until a corresponding call to |
| 257 | * {@link android.app.Activity#onPause}. During this time the activity is |
| 258 | * in front of all other activities and interacting with the user. An activity |
| 259 | * can frequently go between the resumed and paused states -- for example when |
| 260 | * the device goes to sleep, when an activity result is delivered, when a new |
| 261 | * intent is delivered -- so the code in these methods should be fairly |
| 262 | * lightweight. |
| 263 | * </ul> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 264 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | * <p>The entire lifecycle of an activity is defined by the following |
| 266 | * Activity methods. All of these are hooks that you can override |
| 267 | * to do appropriate work when the activity changes state. All |
| 268 | * activities will implement {@link android.app.Activity#onCreate} |
| 269 | * to do their initial setup; many will also implement |
| 270 | * {@link android.app.Activity#onPause} to commit changes to data and |
| 271 | * otherwise prepare to stop interacting with the user. You should always |
| 272 | * call up to your superclass when implementing these methods.</p> |
| 273 | * |
| 274 | * </p> |
| 275 | * <pre class="prettyprint"> |
| 276 | * public class Activity extends ApplicationContext { |
| 277 | * protected void onCreate(Bundle savedInstanceState); |
| 278 | * |
| 279 | * protected void onStart(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 280 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | * protected void onRestart(); |
| 282 | * |
| 283 | * protected void onResume(); |
| 284 | * |
| 285 | * protected void onPause(); |
| 286 | * |
| 287 | * protected void onStop(); |
| 288 | * |
| 289 | * protected void onDestroy(); |
| 290 | * } |
| 291 | * </pre> |
| 292 | * |
| 293 | * <p>In general the movement through an activity's lifecycle looks like |
| 294 | * this:</p> |
| 295 | * |
| 296 | * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> |
| 297 | * <colgroup align="left" span="3" /> |
| 298 | * <colgroup align="left" /> |
| 299 | * <colgroup align="center" /> |
| 300 | * <colgroup align="center" /> |
| 301 | * |
| 302 | * <thead> |
| 303 | * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr> |
| 304 | * </thead> |
| 305 | * |
| 306 | * <tbody> |
| 307 | * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onCreate onCreate()}</th> |
| 308 | * <td>Called when the activity is first created. |
| 309 | * This is where you should do all of your normal static set up: |
| 310 | * create views, bind data to lists, etc. This method also |
| 311 | * provides you with a Bundle containing the activity's previously |
| 312 | * frozen state, if there was one. |
| 313 | * <p>Always followed by <code>onStart()</code>.</td> |
| 314 | * <td align="center">No</td> |
| 315 | * <td align="center"><code>onStart()</code></td> |
| 316 | * </tr> |
| 317 | * |
| 318 | * <tr><td rowspan="5" style="border-left: none; border-right: none;"> </td> |
| 319 | * <th colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</th> |
| 320 | * <td>Called after your activity has been stopped, prior to it being |
| 321 | * started again. |
| 322 | * <p>Always followed by <code>onStart()</code></td> |
| 323 | * <td align="center">No</td> |
| 324 | * <td align="center"><code>onStart()</code></td> |
| 325 | * </tr> |
| 326 | * |
| 327 | * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStart onStart()}</th> |
| 328 | * <td>Called when the activity is becoming visible to the user. |
| 329 | * <p>Followed by <code>onResume()</code> if the activity comes |
| 330 | * to the foreground, or <code>onStop()</code> if it becomes hidden.</td> |
| 331 | * <td align="center">No</td> |
| 332 | * <td align="center"><code>onResume()</code> or <code>onStop()</code></td> |
| 333 | * </tr> |
| 334 | * |
| 335 | * <tr><td rowspan="2" style="border-left: none;"> </td> |
| 336 | * <th align="left" border="0">{@link android.app.Activity#onResume onResume()}</th> |
| 337 | * <td>Called when the activity will start |
| 338 | * interacting with the user. At this point your activity is at |
| 339 | * the top of the activity stack, with user input going to it. |
| 340 | * <p>Always followed by <code>onPause()</code>.</td> |
| 341 | * <td align="center">No</td> |
| 342 | * <td align="center"><code>onPause()</code></td> |
| 343 | * </tr> |
| 344 | * |
| 345 | * <tr><th align="left" border="0">{@link android.app.Activity#onPause onPause()}</th> |
| 346 | * <td>Called when the system is about to start resuming a previous |
| 347 | * activity. This is typically used to commit unsaved changes to |
| 348 | * persistent data, stop animations and other things that may be consuming |
| 349 | * CPU, etc. Implementations of this method must be very quick because |
| 350 | * the next activity will not be resumed until this method returns. |
| 351 | * <p>Followed by either <code>onResume()</code> if the activity |
| 352 | * returns back to the front, or <code>onStop()</code> if it becomes |
| 353 | * invisible to the user.</td> |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 354 | * <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] | 355 | * <td align="center"><code>onResume()</code> or<br> |
| 356 | * <code>onStop()</code></td> |
| 357 | * </tr> |
| 358 | * |
| 359 | * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</th> |
| 360 | * <td>Called when the activity is no longer visible to the user, because |
| 361 | * another activity has been resumed and is covering this one. This |
| 362 | * may happen either because a new activity is being started, an existing |
| 363 | * one is being brought in front of this one, or this one is being |
| 364 | * destroyed. |
| 365 | * <p>Followed by either <code>onRestart()</code> if |
| 366 | * this activity is coming back to interact with the user, or |
| 367 | * <code>onDestroy()</code> if this activity is going away.</td> |
| 368 | * <td align="center"><font color="#800000"><strong>Yes</strong></font></td> |
| 369 | * <td align="center"><code>onRestart()</code> or<br> |
| 370 | * <code>onDestroy()</code></td> |
| 371 | * </tr> |
| 372 | * |
| 373 | * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onDestroy onDestroy()}</th> |
| 374 | * <td>The final call you receive before your |
| 375 | * activity is destroyed. This can happen either because the |
| 376 | * activity is finishing (someone called {@link Activity#finish} on |
| 377 | * it, or because the system is temporarily destroying this |
| 378 | * instance of the activity to save space. You can distinguish |
| 379 | * between these two scenarios with the {@link |
| 380 | * Activity#isFinishing} method.</td> |
| 381 | * <td align="center"><font color="#800000"><strong>Yes</strong></font></td> |
| 382 | * <td align="center"><em>nothing</em></td> |
| 383 | * </tr> |
| 384 | * </tbody> |
| 385 | * </table> |
| 386 | * |
| 387 | * <p>Note the "Killable" column in the above table -- for those methods that |
| 388 | * 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] | 389 | * 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] | 390 | * of its code being executed. Because of this, you should use the |
| 391 | * {@link #onPause} method to write any persistent data (such as user edits) |
| 392 | * to storage. In addition, the method |
| 393 | * {@link #onSaveInstanceState(Bundle)} is called before placing the activity |
| 394 | * in such a background state, allowing you to save away any dynamic instance |
| 395 | * state in your activity into the given Bundle, to be later received in |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 396 | * {@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] | 397 | * See the <a href="#ProcessLifecycle">Process Lifecycle</a> |
| 398 | * section for more information on how the lifecycle of a process is tied |
| 399 | * to the activities it is hosting. Note that it is important to save |
| 400 | * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState} |
Daisuke Miyakawa | 5c40f3f | 2011-02-15 13:24:36 -0800 | [diff] [blame] | 401 | * 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] | 402 | * be called in every situation as described in its documentation.</p> |
| 403 | * |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 404 | * <p class="note">Be aware that these semantics will change slightly between |
| 405 | * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 406 | * vs. those targeting prior platforms. Starting with Honeycomb, an application |
| 407 | * is not in the killable state until its {@link #onStop} has returned. This |
| 408 | * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be |
| 409 | * safely called after {@link #onPause()} and allows and application to safely |
| 410 | * wait until {@link #onStop()} to save persistent state.</p> |
| 411 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | * <p>For those methods that are not marked as being killable, the activity's |
| 413 | * process will not be killed by the system starting from the time the method |
| 414 | * is called and continuing after it returns. Thus an activity is in the killable |
| 415 | * state, for example, between after <code>onPause()</code> to the start of |
| 416 | * <code>onResume()</code>.</p> |
| 417 | * |
| 418 | * <a name="ConfigurationChanges"></a> |
| 419 | * <h3>Configuration Changes</h3> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 420 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | * <p>If the configuration of the device (as defined by the |
| 422 | * {@link Configuration Resources.Configuration} class) changes, |
| 423 | * then anything displaying a user interface will need to update to match that |
| 424 | * configuration. Because Activity is the primary mechanism for interacting |
| 425 | * with the user, it includes special support for handling configuration |
| 426 | * changes.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 427 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | * <p>Unless you specify otherwise, a configuration change (such as a change |
| 429 | * in screen orientation, language, input devices, etc) will cause your |
| 430 | * current activity to be <em>destroyed</em>, going through the normal activity |
| 431 | * lifecycle process of {@link #onPause}, |
| 432 | * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity |
| 433 | * had been in the foreground or visible to the user, once {@link #onDestroy} is |
| 434 | * called in that instance then a new instance of the activity will be |
| 435 | * created, with whatever savedInstanceState the previous instance had generated |
| 436 | * from {@link #onSaveInstanceState}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 437 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | * <p>This is done because any application resource, |
| 439 | * including layout files, can change based on any configuration value. Thus |
| 440 | * the only safe way to handle a configuration change is to re-retrieve all |
| 441 | * resources, including layouts, drawables, and strings. Because activities |
| 442 | * must already know how to save their state and re-create themselves from |
| 443 | * that state, this is a convenient way to have an activity restart itself |
| 444 | * with a new configuration.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 445 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | * <p>In some special cases, you may want to bypass restarting of your |
| 447 | * activity based on one or more types of configuration changes. This is |
| 448 | * done with the {@link android.R.attr#configChanges android:configChanges} |
| 449 | * attribute in its manifest. For any types of configuration changes you say |
| 450 | * that you handle there, you will receive a call to your current activity's |
| 451 | * {@link #onConfigurationChanged} method instead of being restarted. If |
| 452 | * a configuration change involves any that you do not handle, however, the |
| 453 | * activity will still be restarted and {@link #onConfigurationChanged} |
| 454 | * will not be called.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 455 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | * <a name="StartingActivities"></a> |
| 457 | * <h3>Starting Activities and Getting Results</h3> |
| 458 | * |
| 459 | * <p>The {@link android.app.Activity#startActivity} |
| 460 | * method is used to start a |
| 461 | * new activity, which will be placed at the top of the activity stack. It |
| 462 | * takes a single argument, an {@link android.content.Intent Intent}, |
| 463 | * which describes the activity |
| 464 | * to be executed.</p> |
| 465 | * |
| 466 | * <p>Sometimes you want to get a result back from an activity when it |
| 467 | * ends. For example, you may start an activity that lets the user pick |
| 468 | * a person in a list of contacts; when it ends, it returns the person |
| 469 | * that was selected. To do this, you call the |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 470 | * {@link android.app.Activity#startActivityForResult(Intent, int)} |
| 471 | * 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] | 472 | * will come back through your {@link android.app.Activity#onActivityResult} |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 473 | * method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | * |
| 475 | * <p>When an activity exits, it can call |
| 476 | * {@link android.app.Activity#setResult(int)} |
| 477 | * to return data back to its parent. It must always supply a result code, |
| 478 | * which can be the standard results RESULT_CANCELED, RESULT_OK, or any |
| 479 | * custom values starting at RESULT_FIRST_USER. In addition, it can optionally |
| 480 | * return back an Intent containing any additional data it wants. All of this |
| 481 | * information appears back on the |
| 482 | * parent's <code>Activity.onActivityResult()</code>, along with the integer |
| 483 | * identifier it originally supplied.</p> |
| 484 | * |
| 485 | * <p>If a child activity fails for any reason (such as crashing), the parent |
| 486 | * activity will receive a result with the code RESULT_CANCELED.</p> |
| 487 | * |
| 488 | * <pre class="prettyprint"> |
| 489 | * public class MyActivity extends Activity { |
| 490 | * ... |
| 491 | * |
| 492 | * static final int PICK_CONTACT_REQUEST = 0; |
| 493 | * |
Michael Wright | 5438e4e | 2014-07-25 14:05:42 -0700 | [diff] [blame] | 494 | * public boolean onKeyDown(int keyCode, KeyEvent event) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { |
| 496 | * // When the user center presses, let them pick a contact. |
| 497 | * startActivityForResult( |
| 498 | * new Intent(Intent.ACTION_PICK, |
| 499 | * new Uri("content://contacts")), |
| 500 | * PICK_CONTACT_REQUEST); |
| 501 | * return true; |
| 502 | * } |
| 503 | * return false; |
| 504 | * } |
| 505 | * |
| 506 | * protected void onActivityResult(int requestCode, int resultCode, |
| 507 | * Intent data) { |
| 508 | * if (requestCode == PICK_CONTACT_REQUEST) { |
| 509 | * if (resultCode == RESULT_OK) { |
| 510 | * // A contact was picked. Here we will just display it |
| 511 | * // to the user. |
| 512 | * startActivity(new Intent(Intent.ACTION_VIEW, data)); |
| 513 | * } |
| 514 | * } |
| 515 | * } |
| 516 | * } |
| 517 | * </pre> |
| 518 | * |
| 519 | * <a name="SavingPersistentState"></a> |
| 520 | * <h3>Saving Persistent State</h3> |
| 521 | * |
| 522 | * <p>There are generally two kinds of persistent state than an activity |
| 523 | * will deal with: shared document-like data (typically stored in a SQLite |
| 524 | * database using a {@linkplain android.content.ContentProvider content provider}) |
| 525 | * and internal state such as user preferences.</p> |
| 526 | * |
| 527 | * <p>For content provider data, we suggest that activities use a |
| 528 | * "edit in place" user model. That is, any edits a user makes are effectively |
| 529 | * made immediately without requiring an additional confirmation step. |
| 530 | * Supporting this model is generally a simple matter of following two rules:</p> |
| 531 | * |
| 532 | * <ul> |
| 533 | * <li> <p>When creating a new document, the backing database entry or file for |
| 534 | * it is created immediately. For example, if the user chooses to write |
| 535 | * a new e-mail, a new entry for that e-mail is created as soon as they |
| 536 | * start entering data, so that if they go to any other activity after |
| 537 | * that point this e-mail will now appear in the list of drafts.</p> |
| 538 | * <li> <p>When an activity's <code>onPause()</code> method is called, it should |
| 539 | * commit to the backing content provider or file any changes the user |
| 540 | * has made. This ensures that those changes will be seen by any other |
| 541 | * activity that is about to run. You will probably want to commit |
| 542 | * your data even more aggressively at key times during your |
| 543 | * activity's lifecycle: for example before starting a new |
| 544 | * activity, before finishing your own activity, when the user |
| 545 | * switches between input fields, etc.</p> |
| 546 | * </ul> |
| 547 | * |
| 548 | * <p>This model is designed to prevent data loss when a user is navigating |
| 549 | * between activities, and allows the system to safely kill an activity (because |
| 550 | * system resources are needed somewhere else) at any time after it has been |
| 551 | * paused. Note this implies |
| 552 | * that the user pressing BACK from your activity does <em>not</em> |
| 553 | * mean "cancel" -- it means to leave the activity with its current contents |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 554 | * 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] | 555 | * some other mechanism, such as an explicit "revert" or "undo" option.</p> |
| 556 | * |
| 557 | * <p>See the {@linkplain android.content.ContentProvider content package} for |
| 558 | * more information about content providers. These are a key aspect of how |
| 559 | * different activities invoke and propagate data between themselves.</p> |
| 560 | * |
| 561 | * <p>The Activity class also provides an API for managing internal persistent state |
| 562 | * associated with an activity. This can be used, for example, to remember |
| 563 | * the user's preferred initial display in a calendar (day view or week view) |
| 564 | * or the user's default home page in a web browser.</p> |
| 565 | * |
| 566 | * <p>Activity persistent state is managed |
| 567 | * with the method {@link #getPreferences}, |
| 568 | * allowing you to retrieve and |
| 569 | * modify a set of name/value pairs associated with the activity. To use |
| 570 | * preferences that are shared across multiple application components |
| 571 | * (activities, receivers, services, providers), you can use the underlying |
| 572 | * {@link Context#getSharedPreferences Context.getSharedPreferences()} method |
| 573 | * to retrieve a preferences |
| 574 | * object stored under a specific name. |
| 575 | * (Note that it is not possible to share settings data across application |
| 576 | * packages -- for that you will need a content provider.)</p> |
| 577 | * |
| 578 | * <p>Here is an excerpt from a calendar activity that stores the user's |
| 579 | * preferred view mode in its persistent settings:</p> |
| 580 | * |
| 581 | * <pre class="prettyprint"> |
| 582 | * public class CalendarActivity extends Activity { |
| 583 | * ... |
| 584 | * |
| 585 | * static final int DAY_VIEW_MODE = 0; |
| 586 | * static final int WEEK_VIEW_MODE = 1; |
| 587 | * |
| 588 | * private SharedPreferences mPrefs; |
| 589 | * private int mCurViewMode; |
| 590 | * |
| 591 | * protected void onCreate(Bundle savedInstanceState) { |
| 592 | * super.onCreate(savedInstanceState); |
| 593 | * |
| 594 | * SharedPreferences mPrefs = getSharedPreferences(); |
Quddus Chong | 3fa98cb | 2012-04-13 11:17:24 -0700 | [diff] [blame] | 595 | * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | * } |
| 597 | * |
| 598 | * protected void onPause() { |
| 599 | * super.onPause(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 600 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | * SharedPreferences.Editor ed = mPrefs.edit(); |
| 602 | * ed.putInt("view_mode", mCurViewMode); |
| 603 | * ed.commit(); |
| 604 | * } |
| 605 | * } |
| 606 | * </pre> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 607 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | * <a name="Permissions"></a> |
| 609 | * <h3>Permissions</h3> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 610 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | * <p>The ability to start a particular Activity can be enforced when it is |
| 612 | * declared in its |
| 613 | * manifest's {@link android.R.styleable#AndroidManifestActivity <activity>} |
| 614 | * tag. By doing so, other applications will need to declare a corresponding |
| 615 | * {@link android.R.styleable#AndroidManifestUsesPermission <uses-permission>} |
| 616 | * element in their own manifest to be able to start that activity. |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 617 | * |
| 618 | * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION |
| 619 | * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION |
| 620 | * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the |
| 621 | * Activity access to the specific URIs in the Intent. Access will remain |
| 622 | * until the Activity has finished (it will remain across the hosting |
| 623 | * process being killed and other temporary destruction). As of |
| 624 | * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity |
| 625 | * was already created and a new Intent is being delivered to |
| 626 | * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added |
| 627 | * to the existing ones it holds. |
| 628 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a> |
| 630 | * document for more information on permissions and security in general. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 631 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | * <a name="ProcessLifecycle"></a> |
| 633 | * <h3>Process Lifecycle</h3> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 634 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | * <p>The Android system attempts to keep application process around for as |
| 636 | * long as possible, but eventually will need to remove old processes when |
| 637 | * memory runs low. As described in <a href="#ActivityLifecycle">Activity |
| 638 | * Lifecycle</a>, the decision about which process to remove is intimately |
| 639 | * tied to the state of the user's interaction with it. In general, there |
| 640 | * are four states a process can be in based on the activities running in it, |
| 641 | * listed here in order of importance. The system will kill less important |
| 642 | * processes (the last ones) before it resorts to killing more important |
| 643 | * processes (the first ones). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 644 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | * <ol> |
| 646 | * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen |
| 647 | * that the user is currently interacting with) is considered the most important. |
| 648 | * Its process will only be killed as a last resort, if it uses more memory |
| 649 | * than is available on the device. Generally at this point the device has |
| 650 | * reached a memory paging state, so this is required in order to keep the user |
| 651 | * interface responsive. |
| 652 | * <li> <p>A <b>visible activity</b> (an activity that is visible to the user |
| 653 | * but not in the foreground, such as one sitting behind a foreground dialog) |
| 654 | * is considered extremely important and will not be killed unless that is |
| 655 | * required to keep the foreground activity running. |
| 656 | * <li> <p>A <b>background activity</b> (an activity that is not visible to |
| 657 | * the user and has been paused) is no longer critical, so the system may |
| 658 | * safely kill its process to reclaim memory for other foreground or |
| 659 | * visible processes. If its process needs to be killed, when the user navigates |
| 660 | * back to the activity (making it visible on the screen again), its |
| 661 | * {@link #onCreate} method will be called with the savedInstanceState it had previously |
| 662 | * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same |
| 663 | * state as the user last left it. |
| 664 | * <li> <p>An <b>empty process</b> is one hosting no activities or other |
| 665 | * application components (such as {@link Service} or |
| 666 | * {@link android.content.BroadcastReceiver} classes). These are killed very |
| 667 | * quickly by the system as memory becomes low. For this reason, any |
| 668 | * background operation you do outside of an activity must be executed in the |
| 669 | * context of an activity BroadcastReceiver or Service to ensure that the system |
| 670 | * knows it needs to keep your process around. |
| 671 | * </ol> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 672 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | * <p>Sometimes an Activity may need to do a long-running operation that exists |
| 674 | * independently of the activity lifecycle itself. An example may be a camera |
| 675 | * application that allows you to upload a picture to a web site. The upload |
| 676 | * may take a long time, and the application should allow the user to leave |
| 677 | * the application will it is executing. To accomplish this, your Activity |
| 678 | * should start a {@link Service} in which the upload takes place. This allows |
| 679 | * the system to properly prioritize your process (considering it to be more |
| 680 | * important than other non-visible applications) for the duration of the |
| 681 | * upload, independent of whether the original activity is paused, stopped, |
| 682 | * or finished. |
| 683 | */ |
| 684 | public class Activity extends ContextThemeWrapper |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 685 | implements LayoutInflater.Factory2, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | Window.Callback, KeyEvent.Callback, |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 687 | OnCreateContextMenuListener, ComponentCallbacks2, |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 688 | Window.OnWindowDismissedCallback, WindowControllerCallback { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | private static final String TAG = "Activity"; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 690 | private static final boolean DEBUG_LIFECYCLE = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | |
| 692 | /** Standard activity result: operation canceled. */ |
| 693 | public static final int RESULT_CANCELED = 0; |
| 694 | /** Standard activity result: operation succeeded. */ |
| 695 | public static final int RESULT_OK = -1; |
| 696 | /** Start of user-defined activity results. */ |
| 697 | public static final int RESULT_FIRST_USER = 1; |
| 698 | |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 699 | /** @hide Task isn't finished when activity is finished */ |
| 700 | public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0; |
Filip Gruszczynski | c29ecc8 | 2015-10-08 11:08:44 -0700 | [diff] [blame] | 701 | /** |
| 702 | * @hide Task is finished if the finishing activity is the root of the task. To preserve the |
| 703 | * past behavior the task is also removed from recents. |
| 704 | */ |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 705 | public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1; |
Filip Gruszczynski | c29ecc8 | 2015-10-08 11:08:44 -0700 | [diff] [blame] | 706 | /** |
| 707 | * @hide Task is finished along with the finishing activity, but it is not removed from |
| 708 | * recents. |
| 709 | */ |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 710 | public static final int FINISH_TASK_WITH_ACTIVITY = 2; |
| 711 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 712 | static final String FRAGMENTS_TAG = "android:fragments"; |
| 713 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState"; |
| 715 | private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds"; |
| 716 | private static final String SAVED_DIALOGS_TAG = "android:savedDialogs"; |
| 717 | private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_"; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 718 | private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_"; |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 719 | private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY = |
| 720 | "android:hasCurrentPermissionsRequest"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 722 | private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:"; |
| 723 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 724 | private static class ManagedDialog { |
| 725 | Dialog mDialog; |
| 726 | Bundle mArgs; |
| 727 | } |
| 728 | private SparseArray<ManagedDialog> mManagedDialogs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | |
| 730 | // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called. |
| 731 | private Instrumentation mInstrumentation; |
| 732 | private IBinder mToken; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 733 | private int mIdent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | /*package*/ String mEmbeddedID; |
| 735 | private Application mApplication; |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 736 | /*package*/ Intent mIntent; |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 737 | /*package*/ String mReferrer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | private ComponentName mComponent; |
| 739 | /*package*/ ActivityInfo mActivityInfo; |
| 740 | /*package*/ ActivityThread mMainThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | Activity mParent; |
| 742 | boolean mCalled; |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 743 | /*package*/ boolean mResumed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | private boolean mStopped; |
| 745 | boolean mFinished; |
| 746 | boolean mStartedActivity; |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 747 | private boolean mDestroyed; |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 748 | private boolean mDoReportFullyDrawn = true; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 749 | /** true if the activity is going through a transient pause */ |
| 750 | /*package*/ boolean mTemporaryPause = false; |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 751 | /** true if the activity is being destroyed in order to recreate it with a new configuration */ |
| 752 | /*package*/ boolean mChangingConfigurations = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | /*package*/ int mConfigChangeFlags; |
| 754 | /*package*/ Configuration mCurrentConfig; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 755 | private SearchManager mSearchManager; |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 756 | private MenuInflater mMenuInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 758 | static final class NonConfigurationInstances { |
| 759 | Object activity; |
| 760 | HashMap<String, Object> children; |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 761 | FragmentManagerNonConfig fragments; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 762 | ArrayMap<String, LoaderManager> loaders; |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 763 | VoiceInteractor voiceInteractor; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 764 | } |
| 765 | /* package */ NonConfigurationInstances mLastNonConfigurationInstances; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 766 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | private Window mWindow; |
| 768 | |
| 769 | private WindowManager mWindowManager; |
| 770 | /*package*/ View mDecor = null; |
| 771 | /*package*/ boolean mWindowAdded = false; |
| 772 | /*package*/ boolean mVisibleFromServer = false; |
| 773 | /*package*/ boolean mVisibleFromClient = true; |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 774 | /*package*/ ActionBar mActionBar = null; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 775 | private boolean mEnableDefaultActionBarUp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 777 | private VoiceInteractor mVoiceInteractor; |
| 778 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | private CharSequence mTitle; |
| 780 | private int mTitleColor = 0; |
| 781 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 782 | // we must have a handler before the FragmentController is constructed |
| 783 | final Handler mHandler = new Handler(); |
| 784 | final FragmentController mFragments = FragmentController.createController(new HostCallbacks()); |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 785 | |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 786 | // Most recent call to requestVisibleBehind(). |
| 787 | boolean mVisibleBehind; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | private static final class ManagedCursor { |
| 790 | ManagedCursor(Cursor cursor) { |
| 791 | mCursor = cursor; |
| 792 | mReleased = false; |
| 793 | mUpdated = false; |
| 794 | } |
| 795 | |
| 796 | private final Cursor mCursor; |
| 797 | private boolean mReleased; |
| 798 | private boolean mUpdated; |
| 799 | } |
| 800 | private final ArrayList<ManagedCursor> mManagedCursors = |
| 801 | new ArrayList<ManagedCursor>(); |
| 802 | |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 803 | // protected by synchronized (this) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | int mResultCode = RESULT_CANCELED; |
| 805 | Intent mResultData = null; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 806 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 807 | private TranslucentConversionListener mTranslucentCallback; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 808 | private boolean mChangeCanvasToTranslucent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 810 | private SearchEvent mSearchEvent; |
| 811 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | private boolean mTitleReady = false; |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 813 | private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | |
| 815 | private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE; |
| 816 | private SpannableStringBuilder mDefaultKeySsb = null; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 817 | |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 818 | private ActivityManager.TaskDescription mTaskDescription = |
| 819 | new ActivityManager.TaskDescription(); |
| 820 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused}; |
| 822 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 823 | @SuppressWarnings("unused") |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 824 | private final Object mInstanceTracker = StrictMode.trackActivity(this); |
| 825 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | private Thread mUiThread; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 827 | |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 828 | ActivityTransitionState mActivityTransitionState = new ActivityTransitionState(); |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 829 | SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK; |
| 830 | SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 832 | private boolean mHasCurrentPermissionsRequest; |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 833 | private boolean mEatKeyUpEvent; |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 834 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | /** Return the intent that started this activity. */ |
| 836 | public Intent getIntent() { |
| 837 | return mIntent; |
| 838 | } |
| 839 | |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 840 | /** |
| 841 | * Change the intent returned by {@link #getIntent}. This holds a |
| 842 | * reference to the given intent; it does not copy it. Often used in |
| 843 | * conjunction with {@link #onNewIntent}. |
| 844 | * |
| 845 | * @param newIntent The new Intent object to return from getIntent |
| 846 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | * @see #getIntent |
| 848 | * @see #onNewIntent |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 849 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | public void setIntent(Intent newIntent) { |
| 851 | mIntent = newIntent; |
| 852 | } |
| 853 | |
| 854 | /** Return the application that owns this activity. */ |
| 855 | public final Application getApplication() { |
| 856 | return mApplication; |
| 857 | } |
| 858 | |
| 859 | /** Is this activity embedded inside of another activity? */ |
| 860 | public final boolean isChild() { |
| 861 | return mParent != null; |
| 862 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 863 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | /** Return the parent activity if this view is an embedded child. */ |
| 865 | public final Activity getParent() { |
| 866 | return mParent; |
| 867 | } |
| 868 | |
| 869 | /** Retrieve the window manager for showing custom windows. */ |
| 870 | public WindowManager getWindowManager() { |
| 871 | return mWindowManager; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Retrieve the current {@link android.view.Window} for the activity. |
| 876 | * This can be used to directly access parts of the Window API that |
| 877 | * are not available through Activity/Screen. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 878 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | * @return Window The current window, or null if the activity is not |
| 880 | * visual. |
| 881 | */ |
| 882 | public Window getWindow() { |
| 883 | return mWindow; |
| 884 | } |
| 885 | |
| 886 | /** |
Mark Doliner | 9525f2a | 2014-01-02 11:17:47 -0800 | [diff] [blame] | 887 | * Return the LoaderManager for this activity, creating it if needed. |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 888 | */ |
| 889 | public LoaderManager getLoaderManager() { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 890 | return mFragments.getLoaderManager(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 891 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 892 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 893 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | * Calls {@link android.view.Window#getCurrentFocus} on the |
| 895 | * Window of this Activity to return the currently focused view. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 896 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | * @return View The current View with focus or null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 898 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | * @see #getWindow |
| 900 | * @see android.view.Window#getCurrentFocus |
| 901 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 902 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | public View getCurrentFocus() { |
| 904 | return mWindow != null ? mWindow.getCurrentFocus() : null; |
| 905 | } |
| 906 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | /** |
| 908 | * Called when the activity is starting. This is where most initialization |
| 909 | * should go: calling {@link #setContentView(int)} to inflate the |
| 910 | * activity's UI, using {@link #findViewById} to programmatically interact |
| 911 | * with widgets in the UI, calling |
| 912 | * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve |
| 913 | * cursors for data being displayed, etc. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 914 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | * <p>You can call {@link #finish} from within this function, in |
| 916 | * which case onDestroy() will be immediately called without any of the rest |
| 917 | * of the activity lifecycle ({@link #onStart}, {@link #onResume}, |
| 918 | * {@link #onPause}, etc) executing. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 919 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | * <p><em>Derived classes must call through to the super class's |
| 921 | * implementation of this method. If they do not, an exception will be |
| 922 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 923 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | * @param savedInstanceState If the activity is being re-initialized after |
| 925 | * previously being shut down then this Bundle contains the data it most |
| 926 | * 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] | 927 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | * @see #onStart |
| 929 | * @see #onSaveInstanceState |
| 930 | * @see #onRestoreInstanceState |
| 931 | * @see #onPostCreate |
| 932 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 933 | @MainThread |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 934 | @CallSuper |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 935 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 936 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 937 | if (mLastNonConfigurationInstances != null) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 938 | mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 939 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 940 | if (mActivityInfo.parentActivityName != null) { |
| 941 | if (mActionBar == null) { |
| 942 | mEnableDefaultActionBarUp = true; |
| 943 | } else { |
| 944 | mActionBar.setDefaultDisplayHomeAsUpEnabled(true); |
| 945 | } |
| 946 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 947 | if (savedInstanceState != null) { |
| 948 | Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG); |
| 949 | mFragments.restoreAllState(p, mLastNonConfigurationInstances != null |
| 950 | ? mLastNonConfigurationInstances.fragments : null); |
| 951 | } |
| 952 | mFragments.dispatchCreate(); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 953 | getApplication().dispatchActivityCreated(this, savedInstanceState); |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 954 | if (mVoiceInteractor != null) { |
| 955 | mVoiceInteractor.attachActivity(this); |
| 956 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | mCalled = true; |
| 958 | } |
| 959 | |
| 960 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 961 | * 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] | 962 | * the attribute {@link android.R.attr#persistableMode} set to |
| 963 | * <code>persistAcrossReboots</code>. |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 964 | * |
| 965 | * @param savedInstanceState if the activity is being re-initialized after |
| 966 | * previously being shut down then this Bundle contains the data it most |
| 967 | * recently supplied in {@link #onSaveInstanceState}. |
| 968 | * <b><i>Note: Otherwise it is null.</i></b> |
| 969 | * @param persistentState if the activity is being re-initialized after |
| 970 | * previously being shut down or powered off then this Bundle contains the data it most |
| 971 | * recently supplied to outPersistentState in {@link #onSaveInstanceState}. |
| 972 | * <b><i>Note: Otherwise it is null.</i></b> |
| 973 | * |
| 974 | * @see #onCreate(android.os.Bundle) |
| 975 | * @see #onStart |
| 976 | * @see #onSaveInstanceState |
| 977 | * @see #onRestoreInstanceState |
| 978 | * @see #onPostCreate |
| 979 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 980 | public void onCreate(@Nullable Bundle savedInstanceState, |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 981 | @Nullable PersistableBundle persistentState) { |
| 982 | onCreate(savedInstanceState); |
| 983 | } |
| 984 | |
| 985 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | * The hook for {@link ActivityThread} to restore the state of this activity. |
| 987 | * |
| 988 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} and |
| 989 | * {@link #restoreManagedDialogs(android.os.Bundle)}. |
| 990 | * |
| 991 | * @param savedInstanceState contains the saved state |
| 992 | */ |
| 993 | final void performRestoreInstanceState(Bundle savedInstanceState) { |
| 994 | onRestoreInstanceState(savedInstanceState); |
| 995 | restoreManagedDialogs(savedInstanceState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 999 | * The hook for {@link ActivityThread} to restore the state of this activity. |
| 1000 | * |
| 1001 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} and |
| 1002 | * {@link #restoreManagedDialogs(android.os.Bundle)}. |
| 1003 | * |
| 1004 | * @param savedInstanceState contains the saved state |
| 1005 | * @param persistentState contains the persistable saved state |
| 1006 | */ |
| 1007 | final void performRestoreInstanceState(Bundle savedInstanceState, |
| 1008 | PersistableBundle persistentState) { |
| 1009 | onRestoreInstanceState(savedInstanceState, persistentState); |
| 1010 | if (savedInstanceState != null) { |
| 1011 | restoreManagedDialogs(savedInstanceState); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | * This method is called after {@link #onStart} when the activity is |
| 1017 | * being re-initialized from a previously saved state, given here in |
Mike LeBeau | 305de9d | 2010-03-11 09:21:08 -0800 | [diff] [blame] | 1018 | * <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] | 1019 | * to restore their state, but it is sometimes convenient to do it here |
| 1020 | * after all of the initialization has been done or to allow subclasses to |
| 1021 | * decide whether to use your default implementation. The default |
| 1022 | * implementation of this method performs a restore of any view state that |
| 1023 | * had previously been frozen by {@link #onSaveInstanceState}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1024 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | * <p>This method is called between {@link #onStart} and |
| 1026 | * {@link #onPostCreate}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1027 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1029 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | * @see #onCreate |
| 1031 | * @see #onPostCreate |
| 1032 | * @see #onResume |
| 1033 | * @see #onSaveInstanceState |
| 1034 | */ |
| 1035 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 1036 | if (mWindow != null) { |
| 1037 | Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG); |
| 1038 | if (windowState != null) { |
| 1039 | mWindow.restoreHierarchyState(windowState); |
| 1040 | } |
| 1041 | } |
| 1042 | } |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1043 | |
| 1044 | /** |
| 1045 | * 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] | 1046 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1047 | * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed |
| 1048 | * came from the restored PersistableBundle first |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1049 | * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}. |
| 1050 | * |
| 1051 | * <p>This method is called between {@link #onStart} and |
| 1052 | * {@link #onPostCreate}. |
| 1053 | * |
| 1054 | * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called. |
| 1055 | * |
| 1056 | * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}. |
| 1057 | * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}. |
| 1058 | * |
| 1059 | * @see #onRestoreInstanceState(Bundle) |
| 1060 | * @see #onCreate |
| 1061 | * @see #onPostCreate |
| 1062 | * @see #onResume |
| 1063 | * @see #onSaveInstanceState |
| 1064 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1065 | public void onRestoreInstanceState(Bundle savedInstanceState, |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1066 | PersistableBundle persistentState) { |
| 1067 | if (savedInstanceState != null) { |
| 1068 | onRestoreInstanceState(savedInstanceState); |
| 1069 | } |
| 1070 | } |
| 1071 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | /** |
| 1073 | * Restore the state of any saved managed dialogs. |
| 1074 | * |
| 1075 | * @param savedInstanceState The bundle to restore from. |
| 1076 | */ |
| 1077 | private void restoreManagedDialogs(Bundle savedInstanceState) { |
| 1078 | final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG); |
| 1079 | if (b == null) { |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY); |
| 1084 | final int numDialogs = ids.length; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1085 | mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | for (int i = 0; i < numDialogs; i++) { |
| 1087 | final Integer dialogId = ids[i]; |
| 1088 | Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId)); |
| 1089 | if (dialogState != null) { |
Romain Guy | e35c235 | 2009-06-19 13:18:12 -0700 | [diff] [blame] | 1090 | // Calling onRestoreInstanceState() below will invoke dispatchOnCreate |
| 1091 | // so tell createDialog() not to do it, otherwise we get an exception |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1092 | final ManagedDialog md = new ManagedDialog(); |
| 1093 | md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId)); |
| 1094 | md.mDialog = createDialog(dialogId, dialogState, md.mArgs); |
| 1095 | if (md.mDialog != null) { |
| 1096 | mManagedDialogs.put(dialogId, md); |
| 1097 | onPrepareDialog(dialogId, md.mDialog, md.mArgs); |
| 1098 | md.mDialog.onRestoreInstanceState(dialogState); |
| 1099 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1104 | private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) { |
| 1105 | final Dialog dialog = onCreateDialog(dialogId, args); |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1106 | if (dialog == null) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1107 | return null; |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1108 | } |
Romain Guy | 6de4aed | 2009-07-08 10:54:45 -0700 | [diff] [blame] | 1109 | dialog.dispatchOnCreate(state); |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1110 | return dialog; |
| 1111 | } |
| 1112 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1113 | private static String savedDialogKeyFor(int key) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | return SAVED_DIALOG_KEY_PREFIX + key; |
| 1115 | } |
| 1116 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1117 | private static String savedDialogArgsKeyFor(int key) { |
| 1118 | return SAVED_DIALOG_ARGS_KEY_PREFIX + key; |
| 1119 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | |
| 1121 | /** |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 1122 | * Attempts to extract the color from a given drawable. |
| 1123 | * |
| 1124 | * @return the extracted color or 0 if no color could be extracted. |
| 1125 | */ |
| 1126 | private int tryExtractColorFromDrawable(Drawable drawable) { |
| 1127 | if (drawable instanceof ColorDrawable) { |
| 1128 | return ((ColorDrawable) drawable).getColor(); |
| 1129 | } else if (drawable instanceof InsetDrawable) { |
| 1130 | return tryExtractColorFromDrawable(((InsetDrawable) drawable).getDrawable()); |
| 1131 | } else if (drawable instanceof ShapeDrawable) { |
| 1132 | Paint p = ((ShapeDrawable) drawable).getPaint(); |
| 1133 | if (p != null) { |
| 1134 | return p.getColor(); |
| 1135 | } |
| 1136 | } else if (drawable instanceof LayerDrawable) { |
| 1137 | LayerDrawable ld = (LayerDrawable) drawable; |
| 1138 | int numLayers = ld.getNumberOfLayers(); |
| 1139 | for (int i = 0; i < numLayers; i++) { |
| 1140 | int color = tryExtractColorFromDrawable(ld.getDrawable(i)); |
| 1141 | if (color != 0) { |
| 1142 | return color; |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1150 | * Called when activity start-up is complete (after {@link #onStart} |
| 1151 | * and {@link #onRestoreInstanceState} have been called). Applications will |
| 1152 | * generally not implement this method; it is intended for system |
| 1153 | * classes to do final initialization after application code has run. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1154 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | * <p><em>Derived classes must call through to the super class's |
| 1156 | * implementation of this method. If they do not, an exception will be |
| 1157 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1158 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | * @param savedInstanceState If the activity is being re-initialized after |
| 1160 | * previously being shut down then this Bundle contains the data it most |
| 1161 | * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b> |
| 1162 | * @see #onCreate |
| 1163 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1164 | @CallSuper |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1165 | protected void onPostCreate(@Nullable Bundle savedInstanceState) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | if (!isChild()) { |
| 1167 | mTitleReady = true; |
| 1168 | onTitleChanged(getTitle(), getTitleColor()); |
| 1169 | } |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 1170 | |
| 1171 | Resources.Theme theme = getTheme(); |
| 1172 | if (theme != null) { |
| 1173 | // Get the primary color and update the TaskDescription for this activity |
| 1174 | TypedArray a = theme.obtainStyledAttributes( |
| 1175 | com.android.internal.R.styleable.ActivityTaskDescription); |
| 1176 | if (mTaskDescription.getPrimaryColor() == 0) { |
| 1177 | int colorPrimary = a.getColor( |
| 1178 | com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0); |
| 1179 | if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) { |
| 1180 | mTaskDescription.setPrimaryColor(colorPrimary); |
| 1181 | } |
| 1182 | } |
| 1183 | if (mTaskDescription.getBackgroundColor() == 0) { |
| 1184 | int windowBgResourceId = a.getResourceId( |
| 1185 | com.android.internal.R.styleable.ActivityTaskDescription_windowBackground, |
| 1186 | 0); |
| 1187 | int windowBgFallbackResourceId = a.getResourceId( |
| 1188 | com.android.internal.R.styleable.ActivityTaskDescription_windowBackgroundFallback, |
| 1189 | 0); |
| 1190 | int colorBg = tryExtractColorFromDrawable(DecorView.getResizingBackgroundDrawable( |
| 1191 | this, windowBgResourceId, windowBgFallbackResourceId)); |
| 1192 | if (colorBg != 0 && Color.alpha(colorBg) == 0xFF) { |
| 1193 | mTaskDescription.setBackgroundColor(colorBg); |
| 1194 | } |
| 1195 | } |
| 1196 | a.recycle(); |
| 1197 | setTaskDescription(mTaskDescription); |
| 1198 | } |
| 1199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | mCalled = true; |
| 1201 | } |
| 1202 | |
| 1203 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1204 | * 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] | 1205 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1206 | * <code>persistAcrossReboots</code>. |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1207 | * |
| 1208 | * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState} |
| 1209 | * @param persistentState The data caming from the PersistableBundle first |
| 1210 | * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}. |
| 1211 | * |
| 1212 | * @see #onCreate |
| 1213 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1214 | public void onPostCreate(@Nullable Bundle savedInstanceState, |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1215 | @Nullable PersistableBundle persistentState) { |
| 1216 | onPostCreate(savedInstanceState); |
| 1217 | } |
| 1218 | |
| 1219 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1220 | * Called after {@link #onCreate} — or after {@link #onRestart} when |
| 1221 | * 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] | 1222 | * user. It will be followed by {@link #onResume}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1223 | * |
| 1224 | * <p><em>Derived classes must call through to the super class's |
| 1225 | * implementation of this method. If they do not, an exception will be |
| 1226 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1227 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1228 | * @see #onCreate |
| 1229 | * @see #onStop |
| 1230 | * @see #onResume |
| 1231 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1232 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1233 | protected void onStart() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1234 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1235 | mCalled = true; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1236 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1237 | mFragments.doLoaderStart(); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1238 | |
| 1239 | getApplication().dispatchActivityStarted(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Called after {@link #onStop} when the current activity is being |
| 1244 | * re-displayed to the user (the user has navigated back to it). It will |
| 1245 | * be followed by {@link #onStart} and then {@link #onResume}. |
| 1246 | * |
| 1247 | * <p>For activities that are using raw {@link Cursor} objects (instead of |
| 1248 | * creating them through |
| 1249 | * {@link #managedQuery(android.net.Uri , String[], String, String[], String)}, |
| 1250 | * this is usually the place |
| 1251 | * where the cursor should be requeried (because you had deactivated it in |
| 1252 | * {@link #onStop}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1253 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | * <p><em>Derived classes must call through to the super class's |
| 1255 | * implementation of this method. If they do not, an exception will be |
| 1256 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1257 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | * @see #onStop |
| 1259 | * @see #onStart |
| 1260 | * @see #onResume |
| 1261 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1262 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | protected void onRestart() { |
| 1264 | mCalled = true; |
| 1265 | } |
| 1266 | |
| 1267 | /** |
Dianne Hackborn | 6bdd3a1 | 2015-08-05 15:01:28 -0700 | [diff] [blame] | 1268 | * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks |
| 1269 | * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended |
| 1270 | * to give the activity a hint that its state is no longer saved -- it will generally |
| 1271 | * be called after {@link #onSaveInstanceState} and prior to the activity being |
| 1272 | * resumed/started again. |
| 1273 | */ |
| 1274 | public void onStateNotSaved() { |
| 1275 | } |
| 1276 | |
| 1277 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or |
| 1279 | * {@link #onPause}, for your activity to start interacting with the user. |
| 1280 | * This is a good place to begin animations, open exclusive-access devices |
| 1281 | * (such as the camera), etc. |
| 1282 | * |
| 1283 | * <p>Keep in mind that onResume is not the best indicator that your activity |
| 1284 | * is visible to the user; a system window such as the keyguard may be in |
| 1285 | * front. Use {@link #onWindowFocusChanged} to know for certain that your |
| 1286 | * activity is visible to the user (for example, to resume a game). |
| 1287 | * |
| 1288 | * <p><em>Derived classes must call through to the super class's |
| 1289 | * implementation of this method. If they do not, an exception will be |
| 1290 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1291 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | * @see #onRestoreInstanceState |
| 1293 | * @see #onRestart |
| 1294 | * @see #onPostResume |
| 1295 | * @see #onPause |
| 1296 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1297 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | protected void onResume() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1299 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1300 | getApplication().dispatchActivityResumed(this); |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 1301 | mActivityTransitionState.onResume(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1302 | mCalled = true; |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * Called when activity resume is complete (after {@link #onResume} has |
| 1307 | * been called). Applications will generally not implement this method; |
| 1308 | * it is intended for system classes to do final setup after application |
| 1309 | * resume code has run. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1310 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | * <p><em>Derived classes must call through to the super class's |
| 1312 | * implementation of this method. If they do not, an exception will be |
| 1313 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1314 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | * @see #onResume |
| 1316 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1317 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1318 | protected void onPostResume() { |
| 1319 | final Window win = getWindow(); |
| 1320 | if (win != null) win.makeActive(); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 1321 | if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1322 | mCalled = true; |
| 1323 | } |
| 1324 | |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1325 | void setVoiceInteractor(IVoiceInteractor voiceInteractor) { |
| 1326 | if (voiceInteractor == null) { |
| 1327 | mVoiceInteractor = null; |
| 1328 | } else { |
| 1329 | mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this, |
| 1330 | Looper.myLooper()); |
| 1331 | } |
| 1332 | } |
| 1333 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1334 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 1335 | * Check whether this activity is running as part of a voice interaction with the user. |
| 1336 | * If true, it should perform its interaction with the user through the |
| 1337 | * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}. |
| 1338 | */ |
| 1339 | public boolean isVoiceInteraction() { |
| 1340 | return mVoiceInteractor != null; |
| 1341 | } |
| 1342 | |
| 1343 | /** |
Dianne Hackborn | fb81d09 | 2015-08-03 17:14:46 -0700 | [diff] [blame] | 1344 | * Like {@link #isVoiceInteraction}, but only returns true if this is also the root |
| 1345 | * of a voice interaction. That is, returns true if this activity was directly |
| 1346 | * started by the voice interaction service as the initiation of a voice interaction. |
| 1347 | * Otherwise, for example if it was started by another activity while under voice |
| 1348 | * interaction, returns false. |
| 1349 | */ |
| 1350 | public boolean isVoiceInteractionRoot() { |
| 1351 | try { |
| 1352 | return mVoiceInteractor != null |
| 1353 | && ActivityManagerNative.getDefault().isRootVoiceInteraction(mToken); |
| 1354 | } catch (RemoteException e) { |
| 1355 | } |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
| 1359 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 1360 | * Retrieve the active {@link VoiceInteractor} that the user is going through to |
| 1361 | * interact with this activity. |
| 1362 | */ |
| 1363 | public VoiceInteractor getVoiceInteractor() { |
| 1364 | return mVoiceInteractor; |
| 1365 | } |
| 1366 | |
| 1367 | /** |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1368 | * Queries whether the currently enabled voice interaction service supports returning |
| 1369 | * a voice interactor for use by the activity. This is valid only for the duration of the |
| 1370 | * activity. |
| 1371 | * |
| 1372 | * @return whether the current voice interaction service supports local voice interaction |
| 1373 | */ |
| 1374 | public boolean isLocalVoiceInteractionSupported() { |
| 1375 | try { |
| 1376 | return ActivityManagerNative.getDefault().supportsLocalVoiceInteraction(); |
| 1377 | } catch (RemoteException re) { |
| 1378 | } |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
| 1382 | /** |
| 1383 | * Starts a local voice interaction session. When ready, |
| 1384 | * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options |
| 1385 | * to the registered voice interaction service. |
| 1386 | * @param privateOptions a Bundle of private arguments to the current voice interaction service |
| 1387 | */ |
| 1388 | public void startLocalVoiceInteraction(Bundle privateOptions) { |
| 1389 | try { |
| 1390 | ActivityManagerNative.getDefault().startLocalVoiceInteraction(mToken, privateOptions); |
| 1391 | } catch (RemoteException re) { |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | /** |
| 1396 | * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a |
| 1397 | * voice interaction session being started. You can now retrieve a voice interactor using |
| 1398 | * {@link #getVoiceInteractor()}. |
| 1399 | */ |
| 1400 | public void onLocalVoiceInteractionStarted() { |
| 1401 | Log.i(TAG, "onLocalVoiceInteractionStarted! " + getVoiceInteractor()); |
| 1402 | } |
| 1403 | |
| 1404 | /** |
| 1405 | * Callback to indicate that the local voice interaction has stopped for some |
| 1406 | * reason. |
| 1407 | */ |
| 1408 | public void onLocalVoiceInteractionStopped() { |
| 1409 | Log.i(TAG, "onLocalVoiceInteractionStopped :( " + getVoiceInteractor()); |
| 1410 | } |
| 1411 | |
| 1412 | /** |
| 1413 | * Request to terminate the current voice interaction that was previously started |
| 1414 | * using {@link #startLocalVoiceInteraction(Bundle)}. |
| 1415 | */ |
| 1416 | public void stopLocalVoiceInteraction() { |
| 1417 | try { |
| 1418 | ActivityManagerNative.getDefault().stopLocalVoiceInteraction(mToken); |
| 1419 | } catch (RemoteException re) { |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | * This is called for activities that set launchMode to "singleTop" in |
| 1425 | * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} |
| 1426 | * flag when calling {@link #startActivity}. In either case, when the |
| 1427 | * activity is re-launched while at the top of the activity stack instead |
| 1428 | * of a new instance of the activity being started, onNewIntent() will be |
| 1429 | * called on the existing instance with the Intent that was used to |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1430 | * re-launch it. |
| 1431 | * |
| 1432 | * <p>An activity will always be paused before receiving a new intent, so |
| 1433 | * you can count on {@link #onResume} being called after this method. |
| 1434 | * |
| 1435 | * <p>Note that {@link #getIntent} still returns the original Intent. You |
| 1436 | * can use {@link #setIntent} to update it to this new Intent. |
| 1437 | * |
| 1438 | * @param intent The new intent that was started for the activity. |
| 1439 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1440 | * @see #getIntent |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1441 | * @see #setIntent |
| 1442 | * @see #onResume |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1443 | */ |
| 1444 | protected void onNewIntent(Intent intent) { |
| 1445 | } |
| 1446 | |
| 1447 | /** |
| 1448 | * The hook for {@link ActivityThread} to save the state of this activity. |
| 1449 | * |
| 1450 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} |
| 1451 | * and {@link #saveManagedDialogs(android.os.Bundle)}. |
| 1452 | * |
| 1453 | * @param outState The bundle to save the state to. |
| 1454 | */ |
| 1455 | final void performSaveInstanceState(Bundle outState) { |
| 1456 | onSaveInstanceState(outState); |
| 1457 | saveManagedDialogs(outState); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 1458 | mActivityTransitionState.saveState(outState); |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 1459 | storeHasCurrentPermissionRequest(outState); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1460 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1464 | * The hook for {@link ActivityThread} to save the state of this activity. |
| 1465 | * |
| 1466 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} |
| 1467 | * and {@link #saveManagedDialogs(android.os.Bundle)}. |
| 1468 | * |
| 1469 | * @param outState The bundle to save the state to. |
| 1470 | * @param outPersistentState The bundle to save persistent state to. |
| 1471 | */ |
| 1472 | final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { |
| 1473 | onSaveInstanceState(outState, outPersistentState); |
| 1474 | saveManagedDialogs(outState); |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 1475 | storeHasCurrentPermissionRequest(outState); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1476 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState + |
| 1477 | ", " + outPersistentState); |
| 1478 | } |
| 1479 | |
| 1480 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | * Called to retrieve per-instance state from an activity before being killed |
| 1482 | * so that the state can be restored in {@link #onCreate} or |
| 1483 | * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method |
| 1484 | * will be passed to both). |
| 1485 | * |
| 1486 | * <p>This method is called before an activity may be killed so that when it |
| 1487 | * comes back some time in the future it can restore its state. For example, |
| 1488 | * if activity B is launched in front of activity A, and at some point activity |
| 1489 | * A is killed to reclaim resources, activity A will have a chance to save the |
| 1490 | * current state of its user interface via this method so that when the user |
| 1491 | * returns to activity A, the state of the user interface can be restored |
| 1492 | * via {@link #onCreate} or {@link #onRestoreInstanceState}. |
| 1493 | * |
| 1494 | * <p>Do not confuse this method with activity lifecycle callbacks such as |
| 1495 | * {@link #onPause}, which is always called when an activity is being placed |
| 1496 | * in the background or on its way to destruction, or {@link #onStop} which |
| 1497 | * is called before destruction. One example of when {@link #onPause} and |
| 1498 | * {@link #onStop} is called and not this method is when a user navigates back |
| 1499 | * from activity B to activity A: there is no need to call {@link #onSaveInstanceState} |
| 1500 | * on B because that particular instance will never be restored, so the |
| 1501 | * system avoids calling it. An example when {@link #onPause} is called and |
| 1502 | * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A: |
| 1503 | * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't |
| 1504 | * killed during the lifetime of B since the state of the user interface of |
| 1505 | * A will stay intact. |
| 1506 | * |
| 1507 | * <p>The default implementation takes care of most of the UI per-instance |
| 1508 | * state for you by calling {@link android.view.View#onSaveInstanceState()} on each |
| 1509 | * view in the hierarchy that has an id, and by saving the id of the currently |
| 1510 | * focused view (all of which is restored by the default implementation of |
| 1511 | * {@link #onRestoreInstanceState}). If you override this method to save additional |
| 1512 | * information not captured by each individual view, you will likely want to |
| 1513 | * call through to the default implementation, otherwise be prepared to save |
| 1514 | * all of the state of each view yourself. |
| 1515 | * |
| 1516 | * <p>If called, this method will occur before {@link #onStop}. There are |
| 1517 | * no guarantees about whether it will occur before or after {@link #onPause}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1518 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | * @param outState Bundle in which to place your saved state. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1520 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | * @see #onCreate |
| 1522 | * @see #onRestoreInstanceState |
| 1523 | * @see #onPause |
| 1524 | */ |
| 1525 | protected void onSaveInstanceState(Bundle outState) { |
| 1526 | outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState()); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1527 | Parcelable p = mFragments.saveAllState(); |
| 1528 | if (p != null) { |
| 1529 | outState.putParcelable(FRAGMENTS_TAG, p); |
| 1530 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1531 | getApplication().dispatchActivitySaveInstanceState(this, outState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1535 | * This is the same as {@link #onSaveInstanceState} but is called for activities |
Craig Mautner | 43e52ed | 2014-06-16 17:18:52 -0700 | [diff] [blame] | 1536 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1537 | * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed |
| 1538 | * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)} |
| 1539 | * 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] | 1540 | * |
| 1541 | * @param outState Bundle in which to place your saved state. |
| 1542 | * @param outPersistentState State which will be saved across reboots. |
| 1543 | * |
| 1544 | * @see #onSaveInstanceState(Bundle) |
| 1545 | * @see #onCreate |
| 1546 | * @see #onRestoreInstanceState(Bundle, PersistableBundle) |
| 1547 | * @see #onPause |
| 1548 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1549 | public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1550 | onSaveInstanceState(outState); |
| 1551 | } |
| 1552 | |
| 1553 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1554 | * Save the state of any managed dialogs. |
| 1555 | * |
| 1556 | * @param outState place to store the saved state. |
| 1557 | */ |
| 1558 | private void saveManagedDialogs(Bundle outState) { |
| 1559 | if (mManagedDialogs == null) { |
| 1560 | return; |
| 1561 | } |
| 1562 | |
| 1563 | final int numDialogs = mManagedDialogs.size(); |
| 1564 | if (numDialogs == 0) { |
| 1565 | return; |
| 1566 | } |
| 1567 | |
| 1568 | Bundle dialogState = new Bundle(); |
| 1569 | |
| 1570 | int[] ids = new int[mManagedDialogs.size()]; |
| 1571 | |
| 1572 | // save each dialog's bundle, gather the ids |
| 1573 | for (int i = 0; i < numDialogs; i++) { |
| 1574 | final int key = mManagedDialogs.keyAt(i); |
| 1575 | ids[i] = key; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1576 | final ManagedDialog md = mManagedDialogs.valueAt(i); |
| 1577 | dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState()); |
| 1578 | if (md.mArgs != null) { |
| 1579 | dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs); |
| 1580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids); |
| 1584 | outState.putBundle(SAVED_DIALOGS_TAG, dialogState); |
| 1585 | } |
| 1586 | |
| 1587 | |
| 1588 | /** |
| 1589 | * Called as part of the activity lifecycle when an activity is going into |
| 1590 | * the background, but has not (yet) been killed. The counterpart to |
| 1591 | * {@link #onResume}. |
| 1592 | * |
| 1593 | * <p>When activity B is launched in front of activity A, this callback will |
| 1594 | * be invoked on A. B will not be created until A's {@link #onPause} returns, |
| 1595 | * so be sure to not do anything lengthy here. |
| 1596 | * |
| 1597 | * <p>This callback is mostly used for saving any persistent state the |
| 1598 | * activity is editing, to present a "edit in place" model to the user and |
| 1599 | * making sure nothing is lost if there are not enough resources to start |
| 1600 | * the new activity without first killing this one. This is also a good |
| 1601 | * 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] | 1602 | * 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] | 1603 | * as fast as possible, or to close resources that are exclusive access |
| 1604 | * such as the camera. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1605 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | * <p>In situations where the system needs more memory it may kill paused |
| 1607 | * processes to reclaim resources. Because of this, you should be sure |
| 1608 | * that all of your state is saved by the time you return from |
| 1609 | * this function. In general {@link #onSaveInstanceState} is used to save |
| 1610 | * per-instance state in the activity and this method is used to store |
| 1611 | * global persistent data (in content providers, files, etc.) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1612 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1613 | * <p>After receiving this call you will usually receive a following call |
| 1614 | * to {@link #onStop} (after the next activity has been resumed and |
| 1615 | * displayed), however in some cases there will be a direct call back to |
| 1616 | * {@link #onResume} without going through the stopped state. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1617 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | * <p><em>Derived classes must call through to the super class's |
| 1619 | * implementation of this method. If they do not, an exception will be |
| 1620 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1621 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | * @see #onResume |
| 1623 | * @see #onSaveInstanceState |
| 1624 | * @see #onStop |
| 1625 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1626 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1627 | protected void onPause() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1628 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1629 | getApplication().dispatchActivityPaused(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1630 | mCalled = true; |
| 1631 | } |
| 1632 | |
| 1633 | /** |
| 1634 | * Called as part of the activity lifecycle when an activity is about to go |
| 1635 | * into the background as the result of user choice. For example, when the |
| 1636 | * user presses the Home key, {@link #onUserLeaveHint} will be called, but |
| 1637 | * when an incoming phone call causes the in-call Activity to be automatically |
| 1638 | * brought to the foreground, {@link #onUserLeaveHint} will not be called on |
| 1639 | * the activity being interrupted. In cases when it is invoked, this method |
| 1640 | * is called right before the activity's {@link #onPause} callback. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1641 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1642 | * <p>This callback and {@link #onUserInteraction} are intended to help |
| 1643 | * activities manage status bar notifications intelligently; specifically, |
| 1644 | * for helping activities determine the proper time to cancel a notfication. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1645 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | * @see #onUserInteraction() |
| 1647 | */ |
| 1648 | protected void onUserLeaveHint() { |
| 1649 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1650 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1651 | /** |
| 1652 | * Generate a new thumbnail for this activity. This method is called before |
| 1653 | * pausing the activity, and should draw into <var>outBitmap</var> the |
| 1654 | * imagery for the desired thumbnail in the dimensions of that bitmap. It |
| 1655 | * can use the given <var>canvas</var>, which is configured to draw into the |
| 1656 | * bitmap, for rendering if desired. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1657 | * |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 1658 | * <p>The default implementation returns fails and does not draw a thumbnail; |
| 1659 | * this will result in the platform creating its own thumbnail if needed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1660 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1661 | * @param outBitmap The bitmap to contain the thumbnail. |
| 1662 | * @param canvas Can be used to render into the bitmap. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1663 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | * @return Return true if you have drawn into the bitmap; otherwise after |
| 1665 | * you return it will be filled with a default thumbnail. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1666 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | * @see #onCreateDescription |
| 1668 | * @see #onSaveInstanceState |
| 1669 | * @see #onPause |
| 1670 | */ |
| 1671 | public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) { |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 1672 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /** |
| 1676 | * Generate a new description for this activity. This method is called |
| 1677 | * before pausing the activity and can, if desired, return some textual |
| 1678 | * description of its current state to be displayed to the user. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1679 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1680 | * <p>The default implementation returns null, which will cause you to |
| 1681 | * inherit the description from the previous activity. If all activities |
| 1682 | * return null, generally the label of the top activity will be used as the |
| 1683 | * description. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1684 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1685 | * @return A description of what the user is doing. It should be short and |
| 1686 | * sweet (only a few words). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1687 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1688 | * @see #onCreateThumbnail |
| 1689 | * @see #onSaveInstanceState |
| 1690 | * @see #onPause |
| 1691 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1692 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1693 | public CharSequence onCreateDescription() { |
| 1694 | return null; |
| 1695 | } |
| 1696 | |
| 1697 | /** |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1698 | * This is called when the user is requesting an assist, to build a full |
| 1699 | * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current |
| 1700 | * application. You can override this method to place into the bundle anything |
| 1701 | * 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] | 1702 | * of the assist Intent. |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1703 | * |
| 1704 | * <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] | 1705 | * been registered with {@link Application#registerOnProvideAssistDataListener |
| 1706 | * Application.registerOnProvideAssistDataListener}. |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1707 | */ |
| 1708 | public void onProvideAssistData(Bundle data) { |
Dianne Hackborn | a83ce1d | 2015-03-11 15:16:13 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | /** |
| 1712 | * This is called when the user is requesting an assist, to provide references |
| 1713 | * to content related to the current activity. Before being called, the |
| 1714 | * {@code outContent} Intent is filled with the base Intent of the activity (the Intent |
| 1715 | * returned by {@link #getIntent()}). The Intent's extras are stripped of any types |
| 1716 | * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and |
| 1717 | * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and |
| 1718 | * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent. |
| 1719 | * |
| 1720 | * <p>Custom implementation may adjust the content intent to better reflect the top-level |
| 1721 | * context of the activity, and fill in its ClipData with additional content of |
| 1722 | * interest that the user is currently viewing. For example, an image gallery application |
| 1723 | * that has launched in to an activity allowing the user to swipe through pictures should |
| 1724 | * modify the intent to reference the current image they are looking it; such an |
| 1725 | * application when showing a list of pictures should add a ClipData that has |
| 1726 | * references to all of the pictures currently visible on screen.</p> |
| 1727 | * |
| 1728 | * @param outContent The assist content to return. |
| 1729 | */ |
| 1730 | public void onProvideAssistContent(AssistContent outContent) { |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 1733 | @Override |
| 1734 | public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, Menu menu) { |
| 1735 | if (menu == null) { |
| 1736 | return; |
| 1737 | } |
| 1738 | KeyboardShortcutGroup group = null; |
| 1739 | int menuSize = menu.size(); |
| 1740 | for (int i = 0; i < menuSize; ++i) { |
| 1741 | final MenuItem item = menu.getItem(i); |
| 1742 | final CharSequence title = item.getTitle(); |
| 1743 | final char alphaShortcut = item.getAlphabeticShortcut(); |
| 1744 | if (title != null && alphaShortcut != MIN_VALUE) { |
| 1745 | if (group == null) { |
Clara Bayarri | ada88b3 | 2016-02-02 18:08:01 +0000 | [diff] [blame] | 1746 | final int resource = mApplication.getApplicationInfo().labelRes; |
| 1747 | group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 1748 | } |
| 1749 | group.addItem(new KeyboardShortcutInfo( |
| 1750 | title, alphaShortcut, KeyEvent.META_CTRL_ON)); |
| 1751 | } |
| 1752 | } |
| 1753 | if (group != null) { |
| 1754 | data.add(group); |
| 1755 | } |
| 1756 | } |
| 1757 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1758 | /** |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 1759 | * Ask to have the current assistant shown to the user. This only works if the calling |
| 1760 | * activity is the current foreground activity. It is the same as calling |
| 1761 | * {@link android.service.voice.VoiceInteractionService#showSession |
| 1762 | * VoiceInteractionService.showSession} and requesting all of the possible context. |
| 1763 | * The receiver will always see |
| 1764 | * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set. |
| 1765 | * @return Returns true if the assistant was successfully invoked, else false. For example |
| 1766 | * false will be returned if the caller is not the current top activity. |
| 1767 | */ |
| 1768 | public boolean showAssist(Bundle args) { |
| 1769 | try { |
| 1770 | return ActivityManagerNative.getDefault().showAssistFromActivity(mToken, args); |
| 1771 | } catch (RemoteException e) { |
| 1772 | } |
| 1773 | return false; |
| 1774 | } |
| 1775 | |
| 1776 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1777 | * Called when you are no longer visible to the user. You will next |
| 1778 | * receive either {@link #onRestart}, {@link #onDestroy}, or nothing, |
| 1779 | * depending on later user activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1780 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | * <p>Note that this method may never be called, in low memory situations |
| 1782 | * where the system does not have enough memory to keep your activity's |
| 1783 | * process running after its {@link #onPause} method is called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1784 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1785 | * <p><em>Derived classes must call through to the super class's |
| 1786 | * implementation of this method. If they do not, an exception will be |
| 1787 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1788 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1789 | * @see #onRestart |
| 1790 | * @see #onResume |
| 1791 | * @see #onSaveInstanceState |
| 1792 | * @see #onDestroy |
| 1793 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1794 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | protected void onStop() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1796 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 1797 | if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 1798 | mActivityTransitionState.onStop(); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1799 | getApplication().dispatchActivityStopped(this); |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 1800 | mTranslucentCallback = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | mCalled = true; |
| 1802 | } |
| 1803 | |
| 1804 | /** |
| 1805 | * Perform any final cleanup before an activity is destroyed. This can |
| 1806 | * happen either because the activity is finishing (someone called |
| 1807 | * {@link #finish} on it, or because the system is temporarily destroying |
| 1808 | * this instance of the activity to save space. You can distinguish |
| 1809 | * between these two scenarios with the {@link #isFinishing} method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1810 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1811 | * <p><em>Note: do not count on this method being called as a place for |
| 1812 | * saving data! For example, if an activity is editing data in a content |
| 1813 | * provider, those edits should be committed in either {@link #onPause} or |
| 1814 | * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to |
| 1815 | * free resources like threads that are associated with an activity, so |
| 1816 | * that a destroyed activity does not leave such things around while the |
| 1817 | * rest of its application is still running. There are situations where |
| 1818 | * the system will simply kill the activity's hosting process without |
| 1819 | * calling this method (or any others) in it, so it should not be used to |
| 1820 | * do things that are intended to remain around after the process goes |
| 1821 | * away. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1822 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | * <p><em>Derived classes must call through to the super class's |
| 1824 | * implementation of this method. If they do not, an exception will be |
| 1825 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1826 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1827 | * @see #onPause |
| 1828 | * @see #onStop |
| 1829 | * @see #finish |
| 1830 | * @see #isFinishing |
| 1831 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1832 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1833 | protected void onDestroy() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1834 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1835 | mCalled = true; |
| 1836 | |
| 1837 | // dismiss any dialogs we are managing. |
| 1838 | if (mManagedDialogs != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | final int numDialogs = mManagedDialogs.size(); |
| 1840 | for (int i = 0; i < numDialogs; i++) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1841 | final ManagedDialog md = mManagedDialogs.valueAt(i); |
| 1842 | if (md.mDialog.isShowing()) { |
| 1843 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1844 | } |
| 1845 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1846 | mManagedDialogs = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1847 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1848 | |
| 1849 | // close any cursors we are managing. |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 1850 | synchronized (mManagedCursors) { |
| 1851 | int numCursors = mManagedCursors.size(); |
| 1852 | for (int i = 0; i < numCursors; i++) { |
| 1853 | ManagedCursor c = mManagedCursors.get(i); |
| 1854 | if (c != null) { |
| 1855 | c.mCursor.close(); |
| 1856 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | } |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 1858 | mManagedCursors.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1859 | } |
Amith Yamasani | 4986044 | 2010-03-17 20:54:10 -0700 | [diff] [blame] | 1860 | |
| 1861 | // Close any open search dialog |
| 1862 | if (mSearchManager != null) { |
| 1863 | mSearchManager.stopSearch(); |
| 1864 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1865 | |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 1866 | if (mActionBar != null) { |
| 1867 | mActionBar.onDestroy(); |
| 1868 | } |
| 1869 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1870 | getApplication().dispatchActivityDestroyed(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | /** |
Dianne Hackborn | ebc15ef | 2013-10-09 17:36:57 -0700 | [diff] [blame] | 1874 | * Report to the system that your app is now fully drawn, purely for diagnostic |
| 1875 | * purposes (calling it does not impact the visible behavior of the activity). |
| 1876 | * This is only used to help instrument application launch times, so that the |
| 1877 | * app can report when it is fully in a usable state; without this, the only thing |
| 1878 | * the system itself can determine is the point at which the activity's window |
| 1879 | * 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] | 1880 | * measurement, you should always call this method after first launch (when |
Dianne Hackborn | ebc15ef | 2013-10-09 17:36:57 -0700 | [diff] [blame] | 1881 | * {@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] | 1882 | * entirely drawn your UI and populated with all of the significant data. You |
| 1883 | * can safely call this method any time after first launch as well, in which case |
| 1884 | * it will simply be ignored. |
| 1885 | */ |
| 1886 | public void reportFullyDrawn() { |
| 1887 | if (mDoReportFullyDrawn) { |
| 1888 | mDoReportFullyDrawn = false; |
| 1889 | try { |
| 1890 | ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken); |
| 1891 | } catch (RemoteException e) { |
| 1892 | } |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | /** |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1897 | * Called by the system when the activity changes from fullscreen mode to multi-window mode and |
| 1898 | * visa-versa. |
| 1899 | * @see android.R.attr#resizeableActivity |
| 1900 | * |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1901 | * @param inMultiWindow True if the activity is in multi-window mode. |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1902 | */ |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1903 | @CallSuper |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1904 | public void onMultiWindowChanged(boolean inMultiWindow) { |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1905 | if (DEBUG_LIFECYCLE) Slog.v(TAG, |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1906 | "onMultiWindowChanged " + this + ": " + inMultiWindow); |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 1907 | mFragments.dispatchMultiWindowChanged(inMultiWindow); |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1908 | if (mWindow != null) { |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1909 | mWindow.onMultiWindowChanged(); |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1910 | } |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | /** |
| 1914 | * Returns true if the activity is currently in multi-window mode. |
| 1915 | * @see android.R.attr#resizeableActivity |
| 1916 | * |
| 1917 | * @return True if the activity is in multi-window mode. |
| 1918 | */ |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1919 | public boolean inMultiWindow() { |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1920 | try { |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1921 | return ActivityManagerNative.getDefault().inMultiWindow(mToken); |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1922 | } catch (RemoteException e) { |
| 1923 | } |
| 1924 | return false; |
| 1925 | } |
| 1926 | |
| 1927 | /** |
| 1928 | * Called by the system when the activity changes to and from picture-in-picture mode. |
| 1929 | * @see android.R.attr#supportsPictureInPicture |
| 1930 | * |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1931 | * @param inPictureInPicture True if the activity is in picture-in-picture mode. |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1932 | */ |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 1933 | @CallSuper |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1934 | public void onPictureInPictureChanged(boolean inPictureInPicture) { |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1935 | if (DEBUG_LIFECYCLE) Slog.v(TAG, |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1936 | "onPictureInPictureChanged " + this + ": " + inPictureInPicture); |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 1937 | mFragments.dispatchPictureInPictureChanged(inPictureInPicture); |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | /** |
| 1941 | * Returns true if the activity is currently in picture-in-picture mode. |
| 1942 | * @see android.R.attr#supportsPictureInPicture |
| 1943 | * |
| 1944 | * @return True if the activity is in picture-in-picture mode. |
| 1945 | */ |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1946 | public boolean inPictureInPicture() { |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1947 | try { |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1948 | return ActivityManagerNative.getDefault().inPictureInPicture(mToken); |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1949 | } catch (RemoteException e) { |
| 1950 | } |
| 1951 | return false; |
| 1952 | } |
| 1953 | |
| 1954 | /** |
Wale Ogunwale | 9c604c7 | 2015-12-06 18:42:57 -0800 | [diff] [blame] | 1955 | * Puts the activity in picture-in-picture mode. |
| 1956 | * @see android.R.attr#supportsPictureInPicture |
| 1957 | */ |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1958 | public void enterPictureInPicture() { |
Wale Ogunwale | 9c604c7 | 2015-12-06 18:42:57 -0800 | [diff] [blame] | 1959 | try { |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1960 | ActivityManagerNative.getDefault().enterPictureInPicture(mToken); |
Wale Ogunwale | 9c604c7 | 2015-12-06 18:42:57 -0800 | [diff] [blame] | 1961 | } catch (RemoteException e) { |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1966 | * Called by the system when the device configuration changes while your |
| 1967 | * activity is running. Note that this will <em>only</em> be called if |
| 1968 | * you have selected configurations you would like to handle with the |
| 1969 | * {@link android.R.attr#configChanges} attribute in your manifest. If |
| 1970 | * any configuration change occurs that is not selected to be reported |
| 1971 | * by that attribute, then instead of reporting it the system will stop |
| 1972 | * and restart the activity (to have it launched with the new |
| 1973 | * configuration). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1974 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1975 | * <p>At the time that this function has been called, your Resources |
| 1976 | * object will have been updated to return resource values matching the |
| 1977 | * new configuration. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1978 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1979 | * @param newConfig The new device configuration. |
| 1980 | */ |
| 1981 | public void onConfigurationChanged(Configuration newConfig) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1982 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1983 | mCalled = true; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 1984 | |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1985 | mFragments.dispatchConfigurationChanged(newConfig); |
| 1986 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1987 | if (mWindow != null) { |
| 1988 | // Pass the configuration changed event to the window |
| 1989 | mWindow.onConfigurationChanged(newConfig); |
| 1990 | } |
Adam Powell | 45c0b19 | 2011-07-28 15:11:57 -0700 | [diff] [blame] | 1991 | |
| 1992 | if (mActionBar != null) { |
| 1993 | // Do this last; the action bar will need to access |
| 1994 | // view changes from above. |
| 1995 | mActionBar.onConfigurationChanged(newConfig); |
| 1996 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1997 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1998 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1999 | /** |
| 2000 | * If this activity is being destroyed because it can not handle a |
| 2001 | * configuration parameter being changed (and thus its |
| 2002 | * {@link #onConfigurationChanged(Configuration)} method is |
| 2003 | * <em>not</em> being called), then you can use this method to discover |
| 2004 | * the set of changes that have occurred while in the process of being |
| 2005 | * destroyed. Note that there is no guarantee that these will be |
| 2006 | * accurate (other changes could have happened at any time), so you should |
| 2007 | * only use this as an optimization hint. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2008 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2009 | * @return Returns a bit field of the configuration parameters that are |
| 2010 | * changing, as defined by the {@link android.content.res.Configuration} |
| 2011 | * class. |
| 2012 | */ |
| 2013 | public int getChangingConfigurations() { |
| 2014 | return mConfigChangeFlags; |
| 2015 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2016 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2017 | /** |
| 2018 | * Retrieve the non-configuration instance data that was previously |
| 2019 | * returned by {@link #onRetainNonConfigurationInstance()}. This will |
| 2020 | * be available from the initial {@link #onCreate} and |
| 2021 | * {@link #onStart} calls to the new instance, allowing you to extract |
| 2022 | * any useful dynamic state from the previous instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2023 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2024 | * <p>Note that the data you retrieve here should <em>only</em> be used |
| 2025 | * as an optimization for handling configuration changes. You should always |
| 2026 | * be able to handle getting a null pointer back, and an activity must |
| 2027 | * still be able to restore itself to its previous state (through the |
| 2028 | * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this |
| 2029 | * function returns null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2030 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2031 | * @return Returns the object previously returned by |
| 2032 | * {@link #onRetainNonConfigurationInstance()}. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2033 | * |
| 2034 | * @deprecated Use the new {@link Fragment} API |
| 2035 | * {@link Fragment#setRetainInstance(boolean)} instead; this is also |
| 2036 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2037 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2038 | @Nullable |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 2039 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2040 | public Object getLastNonConfigurationInstance() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2041 | return mLastNonConfigurationInstances != null |
| 2042 | ? mLastNonConfigurationInstances.activity : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2043 | } |
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 | /** |
| 2046 | * Called by the system, as part of destroying an |
| 2047 | * activity due to a configuration change, when it is known that a new |
| 2048 | * instance will immediately be created for the new configuration. You |
| 2049 | * can return any object you like here, including the activity instance |
| 2050 | * itself, which can later be retrieved by calling |
| 2051 | * {@link #getLastNonConfigurationInstance()} in the new activity |
| 2052 | * instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2053 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2054 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2055 | * or later, consider instead using a {@link Fragment} with |
| 2056 | * {@link Fragment#setRetainInstance(boolean) |
| 2057 | * Fragment.setRetainInstance(boolean}.</em> |
| 2058 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2059 | * <p>This function is called purely as an optimization, and you must |
| 2060 | * not rely on it being called. When it is called, a number of guarantees |
| 2061 | * will be made to help optimize configuration switching: |
| 2062 | * <ul> |
| 2063 | * <li> The function will be called between {@link #onStop} and |
| 2064 | * {@link #onDestroy}. |
| 2065 | * <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] | 2066 | * created after this one's {@link #onDestroy()} is called. In particular, |
| 2067 | * <em>no</em> messages will be dispatched during this time (when the returned |
| 2068 | * 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] | 2069 | * <li> The object you return here will <em>always</em> be available from |
| 2070 | * the {@link #getLastNonConfigurationInstance()} method of the following |
| 2071 | * activity instance as described there. |
| 2072 | * </ul> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2073 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2074 | * <p>These guarantees are designed so that an activity can use this API |
| 2075 | * to propagate extensive state from the old to new activity instance, from |
| 2076 | * loaded bitmaps, to network connections, to evenly actively running |
| 2077 | * threads. Note that you should <em>not</em> propagate any data that |
| 2078 | * may change based on the configuration, including any data loaded from |
| 2079 | * resources such as strings, layouts, or drawables. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2080 | * |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 2081 | * <p>The guarantee of no message handling during the switch to the next |
| 2082 | * activity simplifies use with active objects. For example if your retained |
| 2083 | * state is an {@link android.os.AsyncTask} you are guaranteed that its |
| 2084 | * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will |
| 2085 | * not be called from the call here until you execute the next instance's |
| 2086 | * {@link #onCreate(Bundle)}. (Note however that there is of course no such |
| 2087 | * guarantee for {@link android.os.AsyncTask#doInBackground} since that is |
| 2088 | * running in a separate thread.) |
| 2089 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2090 | * @return Return any Object holding the desired state to propagate to the |
| 2091 | * next activity instance. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2092 | * |
| 2093 | * @deprecated Use the new {@link Fragment} API |
| 2094 | * {@link Fragment#setRetainInstance(boolean)} instead; this is also |
| 2095 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2096 | */ |
| 2097 | public Object onRetainNonConfigurationInstance() { |
| 2098 | return null; |
| 2099 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2101 | /** |
| 2102 | * Retrieve the non-configuration instance data that was previously |
| 2103 | * returned by {@link #onRetainNonConfigurationChildInstances()}. This will |
| 2104 | * be available from the initial {@link #onCreate} and |
| 2105 | * {@link #onStart} calls to the new instance, allowing you to extract |
| 2106 | * any useful dynamic state from the previous instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2107 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2108 | * <p>Note that the data you retrieve here should <em>only</em> be used |
| 2109 | * as an optimization for handling configuration changes. You should always |
| 2110 | * be able to handle getting a null pointer back, and an activity must |
| 2111 | * still be able to restore itself to its previous state (through the |
| 2112 | * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this |
| 2113 | * function returns null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2114 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2115 | * @return Returns the object previously returned by |
| 2116 | * {@link #onRetainNonConfigurationChildInstances()} |
| 2117 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2118 | @Nullable |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2119 | HashMap<String, Object> getLastNonConfigurationChildInstances() { |
| 2120 | return mLastNonConfigurationInstances != null |
| 2121 | ? mLastNonConfigurationInstances.children : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2122 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2124 | /** |
| 2125 | * This method is similar to {@link #onRetainNonConfigurationInstance()} except that |
| 2126 | * it should return either a mapping from child activity id strings to arbitrary objects, |
| 2127 | * or null. This method is intended to be used by Activity framework subclasses that control a |
| 2128 | * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply |
| 2129 | * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null. |
| 2130 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2131 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2132 | HashMap<String,Object> onRetainNonConfigurationChildInstances() { |
| 2133 | return null; |
| 2134 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2135 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2136 | NonConfigurationInstances retainNonConfigurationInstances() { |
| 2137 | Object activity = onRetainNonConfigurationInstance(); |
| 2138 | HashMap<String, Object> children = onRetainNonConfigurationChildInstances(); |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2139 | FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig(); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2140 | ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig(); |
| 2141 | if (activity == null && children == null && fragments == null && loaders == null |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 2142 | && mVoiceInteractor == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2143 | return null; |
| 2144 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2145 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2146 | NonConfigurationInstances nci = new NonConfigurationInstances(); |
| 2147 | nci.activity = activity; |
| 2148 | nci.children = children; |
| 2149 | nci.fragments = fragments; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2150 | nci.loaders = loaders; |
Dianne Hackborn | 57dd737 | 2015-07-27 18:11:14 -0700 | [diff] [blame] | 2151 | if (mVoiceInteractor != null) { |
| 2152 | mVoiceInteractor.retainInstance(); |
| 2153 | nci.voiceInteractor = mVoiceInteractor; |
| 2154 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2155 | return nci; |
| 2156 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2157 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2158 | public void onLowMemory() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 2159 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2160 | mCalled = true; |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 2161 | mFragments.dispatchLowMemory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2162 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2163 | |
| 2164 | public void onTrimMemory(int level) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 2165 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2166 | mCalled = true; |
| 2167 | mFragments.dispatchTrimMemory(level); |
| 2168 | } |
| 2169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2170 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 2171 | * Return the FragmentManager for interacting with fragments associated |
| 2172 | * with this activity. |
| 2173 | */ |
| 2174 | public FragmentManager getFragmentManager() { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2175 | return mFragments.getFragmentManager(); |
Dianne Hackborn | 9e14e9f3 | 2010-07-14 11:07:38 -0700 | [diff] [blame] | 2176 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2177 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2178 | /** |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 2179 | * Called when a Fragment is being attached to this activity, immediately |
| 2180 | * after the call to its {@link Fragment#onAttach Fragment.onAttach()} |
| 2181 | * method and before {@link Fragment#onCreate Fragment.onCreate()}. |
| 2182 | */ |
| 2183 | public void onAttachFragment(Fragment fragment) { |
| 2184 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2185 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 2186 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2187 | * Wrapper around |
| 2188 | * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)} |
| 2189 | * that gives the resulting {@link Cursor} to call |
| 2190 | * {@link #startManagingCursor} so that the activity will manage its |
| 2191 | * lifecycle for you. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2192 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2193 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2194 | * or later, consider instead using {@link LoaderManager} instead, available |
| 2195 | * via {@link #getLoaderManager()}.</em> |
| 2196 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2197 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using |
| 2198 | * this method, because the activity will do that for you at the appropriate time. However, if |
| 2199 | * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will |
| 2200 | * not</em> automatically close the cursor and, in that case, you must call |
| 2201 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2202 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2203 | * @param uri The URI of the content provider to query. |
| 2204 | * @param projection List of columns to return. |
| 2205 | * @param selection SQL WHERE clause. |
| 2206 | * @param sortOrder SQL ORDER BY clause. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2207 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2208 | * @return The Cursor that was returned by query(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2209 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2210 | * @see ContentResolver#query(android.net.Uri , String[], String, String[], String) |
| 2211 | * @see #startManagingCursor |
| 2212 | * @hide |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2213 | * |
| 2214 | * @deprecated Use {@link CursorLoader} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2215 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2216 | @Deprecated |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2217 | public final Cursor managedQuery(Uri uri, String[] projection, String selection, |
| 2218 | String sortOrder) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2219 | Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder); |
| 2220 | if (c != null) { |
| 2221 | startManagingCursor(c); |
| 2222 | } |
| 2223 | return c; |
| 2224 | } |
| 2225 | |
| 2226 | /** |
| 2227 | * Wrapper around |
| 2228 | * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)} |
| 2229 | * that gives the resulting {@link Cursor} to call |
| 2230 | * {@link #startManagingCursor} so that the activity will manage its |
| 2231 | * lifecycle for you. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2232 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2233 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2234 | * or later, consider instead using {@link LoaderManager} instead, available |
| 2235 | * via {@link #getLoaderManager()}.</em> |
| 2236 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2237 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using |
| 2238 | * this method, because the activity will do that for you at the appropriate time. However, if |
| 2239 | * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will |
| 2240 | * not</em> automatically close the cursor and, in that case, you must call |
| 2241 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2242 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2243 | * @param uri The URI of the content provider to query. |
| 2244 | * @param projection List of columns to return. |
| 2245 | * @param selection SQL WHERE clause. |
| 2246 | * @param selectionArgs The arguments to selection, if any ?s are pesent |
| 2247 | * @param sortOrder SQL ORDER BY clause. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2248 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2249 | * @return The Cursor that was returned by query(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2250 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2251 | * @see ContentResolver#query(android.net.Uri , String[], String, String[], String) |
| 2252 | * @see #startManagingCursor |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2253 | * |
| 2254 | * @deprecated Use {@link CursorLoader} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2255 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2256 | @Deprecated |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2257 | public final Cursor managedQuery(Uri uri, String[] projection, String selection, |
| 2258 | String[] selectionArgs, String sortOrder) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2259 | Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder); |
| 2260 | if (c != null) { |
| 2261 | startManagingCursor(c); |
| 2262 | } |
| 2263 | return c; |
| 2264 | } |
| 2265 | |
| 2266 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2267 | * This method allows the activity to take care of managing the given |
| 2268 | * {@link Cursor}'s lifecycle for you based on the activity's lifecycle. |
| 2269 | * That is, when the activity is stopped it will automatically call |
| 2270 | * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted |
| 2271 | * it will call {@link Cursor#requery} for you. When the activity is |
| 2272 | * destroyed, all managed Cursors will be closed automatically. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2273 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2274 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2275 | * or later, consider instead using {@link LoaderManager} instead, available |
| 2276 | * via {@link #getLoaderManager()}.</em> |
| 2277 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2278 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from |
| 2279 | * {@link #managedQuery}, because the activity will do that for you at the appropriate time. |
| 2280 | * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system |
| 2281 | * <em>will not</em> automatically close the cursor and, in that case, you must call |
| 2282 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2283 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2284 | * @param c The Cursor to be managed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2285 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2286 | * @see #managedQuery(android.net.Uri , String[], String, String[], String) |
| 2287 | * @see #stopManagingCursor |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2288 | * |
| 2289 | * @deprecated Use the new {@link android.content.CursorLoader} class with |
| 2290 | * {@link LoaderManager} instead; this is also |
| 2291 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2292 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2293 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2294 | public void startManagingCursor(Cursor c) { |
| 2295 | synchronized (mManagedCursors) { |
| 2296 | mManagedCursors.add(new ManagedCursor(c)); |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | /** |
| 2301 | * Given a Cursor that was previously given to |
| 2302 | * {@link #startManagingCursor}, stop the activity's management of that |
| 2303 | * cursor. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2304 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2305 | * <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] | 2306 | * 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] | 2307 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2308 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2309 | * @param c The Cursor that was being managed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2310 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2311 | * @see #startManagingCursor |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2312 | * |
| 2313 | * @deprecated Use the new {@link android.content.CursorLoader} class with |
| 2314 | * {@link LoaderManager} instead; this is also |
| 2315 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2316 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2317 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2318 | public void stopManagingCursor(Cursor c) { |
| 2319 | synchronized (mManagedCursors) { |
| 2320 | final int N = mManagedCursors.size(); |
| 2321 | for (int i=0; i<N; i++) { |
| 2322 | ManagedCursor mc = mManagedCursors.get(i); |
| 2323 | if (mc.mCursor == c) { |
| 2324 | mManagedCursors.remove(i); |
| 2325 | break; |
| 2326 | } |
| 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | /** |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 2332 | * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD} |
| 2333 | * this is a no-op. |
Dianne Hackborn | 4f3867e | 2010-12-14 22:09:51 -0800 | [diff] [blame] | 2334 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2335 | */ |
Dianne Hackborn | d3efa39 | 2010-09-01 17:34:12 -0700 | [diff] [blame] | 2336 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2337 | public void setPersistent(boolean isPersistent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | /** |
| 2341 | * Finds a view that was identified by the id attribute from the XML that |
| 2342 | * was processed in {@link #onCreate}. |
| 2343 | * |
| 2344 | * @return The view if found or null otherwise. |
| 2345 | */ |
Scott Kennedy | c051955 | 2015-02-11 15:33:10 -0800 | [diff] [blame] | 2346 | @Nullable |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 2347 | public View findViewById(@IdRes int id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2348 | return getWindow().findViewById(id); |
| 2349 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2350 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2351 | /** |
| 2352 | * Retrieve a reference to this activity's ActionBar. |
Adam Powell | 42c0fe8 | 2010-08-10 16:36:56 -0700 | [diff] [blame] | 2353 | * |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2354 | * @return The Activity's ActionBar, or null if it does not have one. |
| 2355 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2356 | @Nullable |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2357 | public ActionBar getActionBar() { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2358 | initWindowDecorActionBar(); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2359 | return mActionBar; |
| 2360 | } |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2361 | |
| 2362 | /** |
| 2363 | * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this |
| 2364 | * Activity window. |
| 2365 | * |
| 2366 | * <p>When set to a non-null value the {@link #getActionBar()} method will return |
| 2367 | * an {@link ActionBar} object that can be used to control the given toolbar as if it were |
| 2368 | * a traditional window decor action bar. The toolbar's menu will be populated with the |
| 2369 | * Activity's options menu and the navigation button will be wired through the standard |
| 2370 | * {@link android.R.id#home home} menu select action.</p> |
| 2371 | * |
| 2372 | * <p>In order to use a Toolbar within the Activity's window content the application |
| 2373 | * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p> |
| 2374 | * |
Chris Banes | c7d6c32 | 2016-01-27 14:09:16 +0000 | [diff] [blame] | 2375 | * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2376 | */ |
Adam Powell | 3778014 | 2014-06-01 13:31:00 -0700 | [diff] [blame] | 2377 | public void setActionBar(@Nullable Toolbar toolbar) { |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 2378 | final ActionBar ab = getActionBar(); |
| 2379 | if (ab instanceof WindowDecorActionBar) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2380 | throw new IllegalStateException("This Activity already has an action bar supplied " + |
| 2381 | "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " + |
| 2382 | "android:windowActionBar to false in your theme to use a Toolbar instead."); |
| 2383 | } |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 2384 | |
| 2385 | // If we reach here then we're setting a new action bar |
| 2386 | // First clear out the MenuInflater to make sure that it is valid for the new Action Bar |
Chris Banes | 601bb99 | 2015-06-22 10:05:38 +0100 | [diff] [blame] | 2387 | mMenuInflater = null; |
| 2388 | |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 2389 | // If we have an action bar currently, destroy it |
| 2390 | if (ab != null) { |
| 2391 | ab.onDestroy(); |
| 2392 | } |
| 2393 | |
Chris Banes | c7d6c32 | 2016-01-27 14:09:16 +0000 | [diff] [blame] | 2394 | if (toolbar != null) { |
| 2395 | final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this); |
| 2396 | mActionBar = tbab; |
| 2397 | mWindow.setCallback(tbab.getWrappedWindowCallback()); |
| 2398 | } else { |
| 2399 | mActionBar = null; |
| 2400 | // Re-set the original window callback since we may have already set a Toolbar wrapper |
| 2401 | mWindow.setCallback(this); |
| 2402 | } |
| 2403 | |
| 2404 | invalidateOptionsMenu(); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2405 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2406 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2407 | /** |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2408 | * Creates a new ActionBar, locates the inflated ActionBarView, |
| 2409 | * initializes the ActionBar with the view, and sets mActionBar. |
| 2410 | */ |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2411 | private void initWindowDecorActionBar() { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 2412 | Window window = getWindow(); |
Adam Powell | a593d98 | 2011-05-13 14:09:54 -0700 | [diff] [blame] | 2413 | |
| 2414 | // Initializing the window decor can change window feature flags. |
| 2415 | // Make sure that we have the correct set before performing the test below. |
| 2416 | window.getDecorView(); |
| 2417 | |
Adam Powell | 9b4c804 | 2010-08-10 15:36:44 -0700 | [diff] [blame] | 2418 | if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2419 | return; |
| 2420 | } |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 2421 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2422 | mActionBar = new WindowDecorActionBar(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 2423 | mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp); |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 2424 | |
| 2425 | mWindow.setDefaultIcon(mActivityInfo.getIconResource()); |
| 2426 | mWindow.setDefaultLogo(mActivityInfo.getLogoResource()); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2427 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2428 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2429 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2430 | * Set the activity content from a layout resource. The resource will be |
| 2431 | * inflated, adding all top-level views to the activity. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2432 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2433 | * @param layoutResID Resource ID to be inflated. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2434 | * |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2435 | * @see #setContentView(android.view.View) |
| 2436 | * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2437 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 2438 | public void setContentView(@LayoutRes int layoutResID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2439 | getWindow().setContentView(layoutResID); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2440 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | /** |
| 2444 | * Set the activity content to an explicit view. This view is placed |
| 2445 | * 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] | 2446 | * view hierarchy. When calling this method, the layout parameters of the |
| 2447 | * specified view are ignored. Both the width and the height of the view are |
| 2448 | * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use |
| 2449 | * your own layout parameters, invoke |
| 2450 | * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)} |
| 2451 | * instead. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2452 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2453 | * @param view The desired content to display. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2454 | * |
| 2455 | * @see #setContentView(int) |
| 2456 | * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2457 | */ |
| 2458 | public void setContentView(View view) { |
| 2459 | getWindow().setContentView(view); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2460 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * Set the activity content to an explicit view. This view is placed |
| 2465 | * 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] | 2466 | * view hierarchy. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2467 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2468 | * @param view The desired content to display. |
| 2469 | * @param params Layout parameters for the view. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2470 | * |
| 2471 | * @see #setContentView(android.view.View) |
| 2472 | * @see #setContentView(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2473 | */ |
| 2474 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 2475 | getWindow().setContentView(view, params); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2476 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | /** |
| 2480 | * Add an additional content view to the activity. Added after any existing |
| 2481 | * ones in the activity -- existing views are NOT removed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2482 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2483 | * @param view The desired content to display. |
| 2484 | * @param params Layout parameters for the view. |
| 2485 | */ |
| 2486 | public void addContentView(View view, ViewGroup.LayoutParams params) { |
| 2487 | getWindow().addContentView(view, params); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2488 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | /** |
Adam Powell | cfbe9be | 2013-11-06 14:58:58 -0800 | [diff] [blame] | 2492 | * Retrieve the {@link TransitionManager} responsible for default transitions in this window. |
| 2493 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2494 | * |
| 2495 | * <p>This method will return non-null after content has been initialized (e.g. by using |
| 2496 | * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p> |
| 2497 | * |
| 2498 | * @return This window's content TransitionManager or null if none is set. |
| 2499 | */ |
| 2500 | public TransitionManager getContentTransitionManager() { |
| 2501 | return getWindow().getTransitionManager(); |
| 2502 | } |
| 2503 | |
| 2504 | /** |
| 2505 | * Set the {@link TransitionManager} to use for default transitions in this window. |
| 2506 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2507 | * |
| 2508 | * @param tm The TransitionManager to use for scene changes. |
| 2509 | */ |
| 2510 | public void setContentTransitionManager(TransitionManager tm) { |
| 2511 | getWindow().setTransitionManager(tm); |
| 2512 | } |
| 2513 | |
| 2514 | /** |
| 2515 | * Retrieve the {@link Scene} representing this window's current content. |
| 2516 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2517 | * |
| 2518 | * <p>This method will return null if the current content is not represented by a Scene.</p> |
| 2519 | * |
| 2520 | * @return Current Scene being shown or null |
| 2521 | */ |
| 2522 | public Scene getContentScene() { |
| 2523 | return getWindow().getContentScene(); |
| 2524 | } |
| 2525 | |
| 2526 | /** |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2527 | * Sets whether this activity is finished when touched outside its window's |
| 2528 | * bounds. |
| 2529 | */ |
| 2530 | public void setFinishOnTouchOutside(boolean finish) { |
| 2531 | mWindow.setCloseOnTouchOutside(finish); |
| 2532 | } |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2533 | |
| 2534 | /** @hide */ |
| 2535 | @IntDef({ |
| 2536 | DEFAULT_KEYS_DISABLE, |
| 2537 | DEFAULT_KEYS_DIALER, |
| 2538 | DEFAULT_KEYS_SHORTCUT, |
| 2539 | DEFAULT_KEYS_SEARCH_LOCAL, |
| 2540 | DEFAULT_KEYS_SEARCH_GLOBAL}) |
| 2541 | @Retention(RetentionPolicy.SOURCE) |
| 2542 | @interface DefaultKeyMode {} |
| 2543 | |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2544 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2545 | * Use with {@link #setDefaultKeyMode} to turn off default handling of |
| 2546 | * keys. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2547 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2548 | * @see #setDefaultKeyMode |
| 2549 | */ |
| 2550 | static public final int DEFAULT_KEYS_DISABLE = 0; |
| 2551 | /** |
| 2552 | * Use with {@link #setDefaultKeyMode} to launch the dialer during default |
| 2553 | * key handling. |
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 | * @see #setDefaultKeyMode |
| 2556 | */ |
| 2557 | static public final int DEFAULT_KEYS_DIALER = 1; |
| 2558 | /** |
| 2559 | * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in |
| 2560 | * default key handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2561 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2562 | * <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] | 2563 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2564 | * @see #setDefaultKeyMode |
| 2565 | */ |
| 2566 | static public final int DEFAULT_KEYS_SHORTCUT = 2; |
| 2567 | /** |
| 2568 | * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes |
| 2569 | * will start an application-defined search. (If the application or activity does not |
| 2570 | * actually define a search, the the keys will be ignored.) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2571 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2572 | * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2573 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2574 | * @see #setDefaultKeyMode |
| 2575 | */ |
| 2576 | static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3; |
| 2577 | |
| 2578 | /** |
| 2579 | * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes |
| 2580 | * will start a global search (typically web search, but some platforms may define alternate |
| 2581 | * methods for global search) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2582 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2583 | * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2584 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2585 | * @see #setDefaultKeyMode |
| 2586 | */ |
| 2587 | static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4; |
| 2588 | |
| 2589 | /** |
| 2590 | * Select the default key handling for this activity. This controls what |
| 2591 | * will happen to key events that are not otherwise handled. The default |
| 2592 | * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the |
| 2593 | * floor. Other modes allow you to launch the dialer |
| 2594 | * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options |
| 2595 | * menu without requiring the menu key be held down |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2596 | * ({@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] | 2597 | * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2598 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2599 | * <p>Note that the mode selected here does not impact the default |
| 2600 | * handling of system keys, such as the "back" and "menu" keys, and your |
| 2601 | * activity and its views always get a first chance to receive and handle |
| 2602 | * all application keys. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2603 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2604 | * @param mode The desired default key mode constant. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2605 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2606 | * @see #DEFAULT_KEYS_DISABLE |
| 2607 | * @see #DEFAULT_KEYS_DIALER |
| 2608 | * @see #DEFAULT_KEYS_SHORTCUT |
| 2609 | * @see #DEFAULT_KEYS_SEARCH_LOCAL |
| 2610 | * @see #DEFAULT_KEYS_SEARCH_GLOBAL |
| 2611 | * @see #onKeyDown |
| 2612 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2613 | public final void setDefaultKeyMode(@DefaultKeyMode int mode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2614 | mDefaultKeyMode = mode; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2615 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2616 | // Some modes use a SpannableStringBuilder to track & dispatch input events |
| 2617 | // This list must remain in sync with the switch in onKeyDown() |
| 2618 | switch (mode) { |
| 2619 | case DEFAULT_KEYS_DISABLE: |
| 2620 | case DEFAULT_KEYS_SHORTCUT: |
| 2621 | mDefaultKeySsb = null; // not used in these modes |
| 2622 | break; |
| 2623 | case DEFAULT_KEYS_DIALER: |
| 2624 | case DEFAULT_KEYS_SEARCH_LOCAL: |
| 2625 | case DEFAULT_KEYS_SEARCH_GLOBAL: |
| 2626 | mDefaultKeySsb = new SpannableStringBuilder(); |
| 2627 | Selection.setSelection(mDefaultKeySsb,0); |
| 2628 | break; |
| 2629 | default: |
| 2630 | throw new IllegalArgumentException(); |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | /** |
| 2635 | * 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] | 2636 | * 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] | 2637 | * is inside a TextView will not trigger the event (unless it is a navigation |
| 2638 | * to another object) because TextView handles its own key presses. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2639 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2640 | * <p>If the focused view didn't want this event, this method is called. |
| 2641 | * |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2642 | * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK} |
| 2643 | * by calling {@link #onBackPressed()}, though the behavior varies based |
| 2644 | * on the application compatibility mode: for |
| 2645 | * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications, |
| 2646 | * it will set up the dispatch to call {@link #onKeyUp} where the action |
| 2647 | * will be performed; for earlier applications, it will perform the |
| 2648 | * action immediately in on-down, as those versions of the platform |
| 2649 | * behaved. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2650 | * |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2651 | * <p>Other additional default key handling may be performed |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2652 | * if configured with {@link #setDefaultKeyMode}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2653 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2654 | * @return Return <code>true</code> to prevent this event from being propagated |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2655 | * 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] | 2656 | * this event and it should continue to be propagated. |
| 2657 | * @see #onKeyUp |
| 2658 | * @see android.view.KeyEvent |
| 2659 | */ |
| 2660 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2661 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2662 | if (getApplicationInfo().targetSdkVersion |
| 2663 | >= Build.VERSION_CODES.ECLAIR) { |
| 2664 | event.startTracking(); |
| 2665 | } else { |
| 2666 | onBackPressed(); |
| 2667 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2668 | return true; |
| 2669 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2670 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2671 | if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) { |
| 2672 | return false; |
| 2673 | } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 2674 | Window w = getWindow(); |
| 2675 | if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 2676 | w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event, |
| 2677 | Menu.FLAG_ALWAYS_PERFORM_CLOSE)) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2678 | return true; |
| 2679 | } |
| 2680 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2681 | } else { |
| 2682 | // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_* |
| 2683 | boolean clearSpannable = false; |
| 2684 | boolean handled; |
| 2685 | if ((event.getRepeatCount() != 0) || event.isSystem()) { |
| 2686 | clearSpannable = true; |
| 2687 | handled = false; |
| 2688 | } else { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2689 | handled = TextKeyListener.getInstance().onKeyDown( |
| 2690 | null, mDefaultKeySsb, keyCode, event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2691 | if (handled && mDefaultKeySsb.length() > 0) { |
| 2692 | // something useable has been typed - dispatch it now. |
| 2693 | |
| 2694 | final String str = mDefaultKeySsb.toString(); |
| 2695 | clearSpannable = true; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2696 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2697 | switch (mDefaultKeyMode) { |
| 2698 | case DEFAULT_KEYS_DIALER: |
| 2699 | Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str)); |
| 2700 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2701 | startActivity(intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2702 | break; |
| 2703 | case DEFAULT_KEYS_SEARCH_LOCAL: |
| 2704 | startSearch(str, false, null, false); |
| 2705 | break; |
| 2706 | case DEFAULT_KEYS_SEARCH_GLOBAL: |
| 2707 | startSearch(str, false, null, true); |
| 2708 | break; |
| 2709 | } |
| 2710 | } |
| 2711 | } |
| 2712 | if (clearSpannable) { |
| 2713 | mDefaultKeySsb.clear(); |
| 2714 | mDefaultKeySsb.clearSpans(); |
| 2715 | Selection.setSelection(mDefaultKeySsb,0); |
| 2716 | } |
| 2717 | return handled; |
| 2718 | } |
| 2719 | } |
| 2720 | |
| 2721 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2722 | * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent) |
| 2723 | * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle |
| 2724 | * the event). |
| 2725 | */ |
| 2726 | public boolean onKeyLongPress(int keyCode, KeyEvent event) { |
| 2727 | return false; |
| 2728 | } |
| 2729 | |
| 2730 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2731 | * 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] | 2732 | * 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] | 2733 | * is inside a TextView will not trigger the event (unless it is a navigation |
| 2734 | * to another object) because TextView handles its own key presses. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2735 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2736 | * <p>The default implementation handles KEYCODE_BACK to stop the activity |
| 2737 | * and go back. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2738 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2739 | * @return Return <code>true</code> to prevent this event from being propagated |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2740 | * further, or <code>false</code> to indicate that you have not handled |
| 2741 | * this event and it should continue to be propagated. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2742 | * @see #onKeyDown |
| 2743 | * @see KeyEvent |
| 2744 | */ |
| 2745 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2746 | if (getApplicationInfo().targetSdkVersion |
| 2747 | >= Build.VERSION_CODES.ECLAIR) { |
| 2748 | if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking() |
| 2749 | && !event.isCanceled()) { |
| 2750 | onBackPressed(); |
| 2751 | return true; |
| 2752 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2753 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2754 | return false; |
| 2755 | } |
| 2756 | |
| 2757 | /** |
| 2758 | * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent) |
| 2759 | * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle |
| 2760 | * the event). |
| 2761 | */ |
| 2762 | public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
| 2763 | return false; |
| 2764 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2765 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2766 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2767 | * Called when the activity has detected the user's press of the back |
| 2768 | * key. The default implementation simply finishes the current activity, |
| 2769 | * but you can override this to do whatever you want. |
| 2770 | */ |
| 2771 | public void onBackPressed() { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2772 | if (mActionBar != null && mActionBar.collapseActionView()) { |
| 2773 | return; |
| 2774 | } |
| 2775 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2776 | if (!mFragments.getFragmentManager().popBackStackImmediate()) { |
Craig Mautner | 73f843d | 2014-05-19 09:42:28 -0700 | [diff] [blame] | 2777 | finishAfterTransition(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 2778 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2779 | } |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 2780 | |
| 2781 | /** |
| 2782 | * Called when a key shortcut event is not handled by any of the views in the Activity. |
| 2783 | * Override this method to implement global key shortcuts for the Activity. |
| 2784 | * Key shortcuts can also be implemented by setting the |
| 2785 | * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items. |
| 2786 | * |
| 2787 | * @param keyCode The value in event.getKeyCode(). |
| 2788 | * @param event Description of the key event. |
| 2789 | * @return True if the key shortcut was handled. |
| 2790 | */ |
| 2791 | public boolean onKeyShortcut(int keyCode, KeyEvent event) { |
Abodunrinwa Toki | a04b7ad | 2015-06-30 17:44:04 -0700 | [diff] [blame] | 2792 | // Let the Action Bar have a chance at handling the shortcut. |
| 2793 | ActionBar actionBar = getActionBar(); |
| 2794 | return (actionBar != null && actionBar.onKeyShortcut(keyCode, event)); |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 2795 | } |
| 2796 | |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2797 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2798 | * Called when a touch screen event was not handled by any of the views |
| 2799 | * under it. This is most useful to process touch events that happen |
| 2800 | * outside of your window bounds, where there is no view to receive it. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2801 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2802 | * @param event The touch screen event being processed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2803 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2804 | * @return Return true if you have consumed the event, false if you haven't. |
| 2805 | * The default implementation always returns false. |
| 2806 | */ |
| 2807 | public boolean onTouchEvent(MotionEvent event) { |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2808 | if (mWindow.shouldCloseOnTouch(this, event)) { |
| 2809 | finish(); |
| 2810 | return true; |
| 2811 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2812 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2813 | return false; |
| 2814 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2815 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2816 | /** |
| 2817 | * Called when the trackball was moved and not handled by any of the |
| 2818 | * views inside of the activity. So, for example, if the trackball moves |
| 2819 | * while focus is on a button, you will receive a call here because |
| 2820 | * buttons do not normally do anything with trackball events. The call |
| 2821 | * here happens <em>before</em> trackball movements are converted to |
| 2822 | * DPAD key events, which then get sent back to the view hierarchy, and |
| 2823 | * will be processed at the point for things like focus navigation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2824 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2825 | * @param event The trackball event being processed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2826 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2827 | * @return Return true if you have consumed the event, false if you haven't. |
| 2828 | * The default implementation always returns false. |
| 2829 | */ |
| 2830 | public boolean onTrackballEvent(MotionEvent event) { |
| 2831 | return false; |
| 2832 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2833 | |
| 2834 | /** |
| 2835 | * Called when a generic motion event was not handled by any of the |
| 2836 | * views inside of the activity. |
| 2837 | * <p> |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2838 | * Generic motion events describe joystick movements, mouse hovers, track pad |
| 2839 | * touches, scroll wheel movements and other input events. The |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2840 | * {@link MotionEvent#getSource() source} of the motion event specifies |
| 2841 | * the class of input that was received. Implementations of this method |
| 2842 | * must examine the bits in the source before processing the event. |
| 2843 | * The following code example shows how this is done. |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2844 | * </p><p> |
| 2845 | * Generic motion events with source class |
| 2846 | * {@link android.view.InputDevice#SOURCE_CLASS_POINTER} |
| 2847 | * are delivered to the view under the pointer. All other generic motion events are |
| 2848 | * delivered to the focused view. |
| 2849 | * </p><p> |
| 2850 | * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to |
| 2851 | * handle this event. |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2852 | * </p> |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2853 | * |
| 2854 | * @param event The generic motion event being processed. |
| 2855 | * |
| 2856 | * @return Return true if you have consumed the event, false if you haven't. |
| 2857 | * The default implementation always returns false. |
| 2858 | */ |
| 2859 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 2860 | return false; |
| 2861 | } |
| 2862 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2863 | /** |
| 2864 | * Called whenever a key, touch, or trackball event is dispatched to the |
| 2865 | * activity. Implement this method if you wish to know that the user has |
| 2866 | * interacted with the device in some way while your activity is running. |
| 2867 | * This callback and {@link #onUserLeaveHint} are intended to help |
| 2868 | * activities manage status bar notifications intelligently; specifically, |
| 2869 | * for helping activities determine the proper time to cancel a notfication. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2870 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2871 | * <p>All calls to your activity's {@link #onUserLeaveHint} callback will |
| 2872 | * be accompanied by calls to {@link #onUserInteraction}. This |
| 2873 | * ensures that your activity will be told of relevant user activity such |
| 2874 | * as pulling down the notification pane and touching an item there. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2875 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2876 | * <p>Note that this callback will be invoked for the touch down action |
| 2877 | * that begins a touch gesture, but may not be invoked for the touch-moved |
| 2878 | * and touch-up actions that follow. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2879 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2880 | * @see #onUserLeaveHint() |
| 2881 | */ |
| 2882 | public void onUserInteraction() { |
| 2883 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2884 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2885 | public void onWindowAttributesChanged(WindowManager.LayoutParams params) { |
| 2886 | // Update window manager if: we have a view, that view is |
| 2887 | // attached to its parent (which will be a RootView), and |
| 2888 | // this activity is not embedded. |
| 2889 | if (mParent == null) { |
| 2890 | View decor = mDecor; |
| 2891 | if (decor != null && decor.getParent() != null) { |
| 2892 | getWindowManager().updateViewLayout(decor, params); |
| 2893 | } |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | public void onContentChanged() { |
| 2898 | } |
| 2899 | |
| 2900 | /** |
| 2901 | * Called when the current {@link Window} of the activity gains or loses |
| 2902 | * focus. This is the best indicator of whether this activity is visible |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2903 | * to the user. The default implementation clears the key tracking |
| 2904 | * state, so should always be called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2905 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2906 | * <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] | 2907 | * is managed independently of activity lifecycles. As such, while focus |
| 2908 | * changes will generally have some relation to lifecycle changes (an |
| 2909 | * activity that is stopped will not generally get window focus), you |
| 2910 | * should not rely on any particular order between the callbacks here and |
| 2911 | * those in the other lifecycle methods such as {@link #onResume}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2912 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2913 | * <p>As a general rule, however, a resumed activity will have window |
| 2914 | * focus... unless it has displayed other dialogs or popups that take |
| 2915 | * input focus, in which case the activity itself will not have focus |
| 2916 | * when the other windows have it. Likewise, the system may display |
| 2917 | * system-level windows (such as the status bar notification panel or |
| 2918 | * a system alert) which will temporarily take window input focus without |
| 2919 | * pausing the foreground activity. |
| 2920 | * |
| 2921 | * @param hasFocus Whether the window of this activity has focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2922 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2923 | * @see #hasWindowFocus() |
| 2924 | * @see #onResume |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2925 | * @see View#onWindowFocusChanged(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2926 | */ |
| 2927 | public void onWindowFocusChanged(boolean hasFocus) { |
| 2928 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2929 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2930 | /** |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2931 | * Called when the main window associated with the activity has been |
| 2932 | * attached to the window manager. |
| 2933 | * See {@link View#onAttachedToWindow() View.onAttachedToWindow()} |
| 2934 | * for more information. |
| 2935 | * @see View#onAttachedToWindow |
| 2936 | */ |
| 2937 | public void onAttachedToWindow() { |
| 2938 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2939 | |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2940 | /** |
| 2941 | * Called when the main window associated with the activity has been |
| 2942 | * detached from the window manager. |
| 2943 | * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()} |
| 2944 | * for more information. |
| 2945 | * @see View#onDetachedFromWindow |
| 2946 | */ |
| 2947 | public void onDetachedFromWindow() { |
| 2948 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2949 | |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2950 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2951 | * Returns true if this activity's <em>main</em> window currently has window focus. |
| 2952 | * Note that this is not the same as the view itself having focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2953 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2954 | * @return True if this activity's main window currently has window focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2955 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2956 | * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams) |
| 2957 | */ |
| 2958 | public boolean hasWindowFocus() { |
| 2959 | Window w = getWindow(); |
| 2960 | if (w != null) { |
| 2961 | View d = w.getDecorView(); |
| 2962 | if (d != null) { |
| 2963 | return d.hasWindowFocus(); |
| 2964 | } |
| 2965 | } |
| 2966 | return false; |
| 2967 | } |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2968 | |
| 2969 | /** |
| 2970 | * Called when the main window associated with the activity has been dismissed. |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 2971 | * @hide |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2972 | */ |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 2973 | @Override |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 2974 | public void onWindowDismissed(boolean finishTask) { |
| 2975 | finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY); |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2976 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2977 | |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2978 | |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 2979 | /** |
Filip Gruszczynski | 411c06f | 2016-01-07 09:44:44 -0800 | [diff] [blame] | 2980 | * Moves the activity from |
| 2981 | * {@link android.app.ActivityManager.StackId#FREEFORM_WORKSPACE_STACK_ID} to |
| 2982 | * {@link android.app.ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} stack. |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2983 | * |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2984 | * @hide |
| 2985 | */ |
| 2986 | @Override |
Filip Gruszczynski | 411c06f | 2016-01-07 09:44:44 -0800 | [diff] [blame] | 2987 | public void exitFreeformMode() throws RemoteException { |
| 2988 | ActivityManagerNative.getDefault().exitFreeformMode(mToken); |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | /** Returns the current stack Id for the window. |
| 2992 | * @hide |
| 2993 | */ |
| 2994 | @Override |
| 2995 | public int getWindowStackId() throws RemoteException { |
| 2996 | return ActivityManagerNative.getDefault().getActivityStackId(mToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3000 | * Called to process key events. You can override this to intercept all |
| 3001 | * 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] | 3002 | * this implementation for key events that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3003 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3004 | * @param event The key event. |
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 | * @return boolean Return true if this event was consumed. |
| 3007 | */ |
| 3008 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 3009 | onUserInteraction(); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3010 | |
| 3011 | // Let action bars open menus in response to the menu key prioritized over |
| 3012 | // the window handling it |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 3013 | final int keyCode = event.getKeyCode(); |
| 3014 | if (keyCode == KeyEvent.KEYCODE_MENU && |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3015 | mActionBar != null && mActionBar.onMenuKeyEvent(event)) { |
| 3016 | return true; |
George Mount | cbe2835 | 2016-01-06 16:24:26 -0800 | [diff] [blame] | 3017 | } else if (event.isCtrlPressed() && |
| 3018 | event.getUnicodeChar(event.getMetaState() & ~KeyEvent.META_CTRL_MASK) == '<') { |
| 3019 | // Capture the Control-< and send focus to the ActionBar |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 3020 | final int action = event.getAction(); |
| 3021 | if (action == KeyEvent.ACTION_DOWN) { |
George Mount | cbe2835 | 2016-01-06 16:24:26 -0800 | [diff] [blame] | 3022 | final ActionBar actionBar = getActionBar(); |
| 3023 | if (actionBar != null && actionBar.isShowing() && actionBar.requestFocus()) { |
| 3024 | mEatKeyUpEvent = true; |
| 3025 | return true; |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 3026 | } |
| 3027 | } else if (action == KeyEvent.ACTION_UP && mEatKeyUpEvent) { |
| 3028 | mEatKeyUpEvent = false; |
| 3029 | return true; |
| 3030 | } |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3031 | } |
| 3032 | |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 3033 | Window win = getWindow(); |
| 3034 | if (win.superDispatchKeyEvent(event)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3035 | return true; |
| 3036 | } |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 3037 | View decor = mDecor; |
| 3038 | if (decor == null) decor = win.getDecorView(); |
| 3039 | return event.dispatch(this, decor != null |
| 3040 | ? decor.getKeyDispatcherState() : null, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | /** |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 3044 | * Called to process a key shortcut event. |
| 3045 | * You can override this to intercept all key shortcut events before they are |
| 3046 | * dispatched to the window. Be sure to call this implementation for key shortcut |
| 3047 | * events that should be handled normally. |
| 3048 | * |
| 3049 | * @param event The key shortcut event. |
| 3050 | * @return True if this event was consumed. |
| 3051 | */ |
| 3052 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
| 3053 | onUserInteraction(); |
| 3054 | if (getWindow().superDispatchKeyShortcutEvent(event)) { |
| 3055 | return true; |
| 3056 | } |
| 3057 | return onKeyShortcut(event.getKeyCode(), event); |
| 3058 | } |
| 3059 | |
| 3060 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3061 | * Called to process touch screen events. You can override this to |
| 3062 | * intercept all touch screen events before they are dispatched to the |
| 3063 | * window. Be sure to call this implementation for touch screen events |
| 3064 | * that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3065 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3066 | * @param ev The touch screen event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3067 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3068 | * @return boolean Return true if this event was consumed. |
| 3069 | */ |
| 3070 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 3071 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 3072 | onUserInteraction(); |
| 3073 | } |
| 3074 | if (getWindow().superDispatchTouchEvent(ev)) { |
| 3075 | return true; |
| 3076 | } |
| 3077 | return onTouchEvent(ev); |
| 3078 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3079 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3080 | /** |
| 3081 | * Called to process trackball events. You can override this to |
| 3082 | * intercept all trackball events before they are dispatched to the |
| 3083 | * window. Be sure to call this implementation for trackball events |
| 3084 | * that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3085 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3086 | * @param ev The trackball event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3087 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3088 | * @return boolean Return true if this event was consumed. |
| 3089 | */ |
| 3090 | public boolean dispatchTrackballEvent(MotionEvent ev) { |
| 3091 | onUserInteraction(); |
| 3092 | if (getWindow().superDispatchTrackballEvent(ev)) { |
| 3093 | return true; |
| 3094 | } |
| 3095 | return onTrackballEvent(ev); |
| 3096 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3097 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 3098 | /** |
| 3099 | * Called to process generic motion events. You can override this to |
| 3100 | * intercept all generic motion events before they are dispatched to the |
| 3101 | * window. Be sure to call this implementation for generic motion events |
| 3102 | * that should be handled normally. |
| 3103 | * |
| 3104 | * @param ev The generic motion event. |
| 3105 | * |
| 3106 | * @return boolean Return true if this event was consumed. |
| 3107 | */ |
| 3108 | public boolean dispatchGenericMotionEvent(MotionEvent ev) { |
| 3109 | onUserInteraction(); |
| 3110 | if (getWindow().superDispatchGenericMotionEvent(ev)) { |
| 3111 | return true; |
| 3112 | } |
| 3113 | return onGenericMotionEvent(ev); |
| 3114 | } |
| 3115 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3116 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 3117 | event.setClassName(getClass().getName()); |
| 3118 | event.setPackageName(getPackageName()); |
| 3119 | |
| 3120 | LayoutParams params = getWindow().getAttributes(); |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 3121 | boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) && |
| 3122 | (params.height == LayoutParams.MATCH_PARENT); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3123 | event.setFullScreen(isFullScreen); |
| 3124 | |
| 3125 | CharSequence title = getTitle(); |
| 3126 | if (!TextUtils.isEmpty(title)) { |
| 3127 | event.getText().add(title); |
| 3128 | } |
| 3129 | |
| 3130 | return true; |
| 3131 | } |
| 3132 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3133 | /** |
| 3134 | * Default implementation of |
| 3135 | * {@link android.view.Window.Callback#onCreatePanelView} |
| 3136 | * for activities. This |
| 3137 | * simply returns null so that all panel sub-windows will have the default |
| 3138 | * menu behavior. |
| 3139 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3140 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3141 | public View onCreatePanelView(int featureId) { |
| 3142 | return null; |
| 3143 | } |
| 3144 | |
| 3145 | /** |
| 3146 | * Default implementation of |
| 3147 | * {@link android.view.Window.Callback#onCreatePanelMenu} |
| 3148 | * for activities. This calls through to the new |
| 3149 | * {@link #onCreateOptionsMenu} method for the |
| 3150 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel, |
| 3151 | * so that subclasses of Activity don't need to deal with feature codes. |
| 3152 | */ |
| 3153 | public boolean onCreatePanelMenu(int featureId, Menu menu) { |
| 3154 | if (featureId == Window.FEATURE_OPTIONS_PANEL) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3155 | boolean show = onCreateOptionsMenu(menu); |
| 3156 | show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater()); |
| 3157 | return show; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3158 | } |
| 3159 | return false; |
| 3160 | } |
| 3161 | |
| 3162 | /** |
| 3163 | * Default implementation of |
| 3164 | * {@link android.view.Window.Callback#onPreparePanel} |
| 3165 | * for activities. This |
| 3166 | * calls through to the new {@link #onPrepareOptionsMenu} method for the |
| 3167 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} |
| 3168 | * panel, so that subclasses of |
| 3169 | * Activity don't need to deal with feature codes. |
| 3170 | */ |
| 3171 | public boolean onPreparePanel(int featureId, View view, Menu menu) { |
| 3172 | if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { |
| 3173 | boolean goforit = onPrepareOptionsMenu(menu); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3174 | goforit |= mFragments.dispatchPrepareOptionsMenu(menu); |
Adam Powell | ef31e7c | 2012-05-08 09:59:40 -0700 | [diff] [blame] | 3175 | return goforit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3176 | } |
| 3177 | return true; |
| 3178 | } |
| 3179 | |
| 3180 | /** |
| 3181 | * {@inheritDoc} |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3182 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3183 | * @return The default implementation returns true. |
| 3184 | */ |
| 3185 | public boolean onMenuOpened(int featureId, Menu menu) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3186 | if (featureId == Window.FEATURE_ACTION_BAR) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 3187 | initWindowDecorActionBar(); |
Adam Powell | 049dd3d | 2010-12-02 13:43:59 -0800 | [diff] [blame] | 3188 | if (mActionBar != null) { |
| 3189 | mActionBar.dispatchMenuVisibilityChanged(true); |
| 3190 | } else { |
| 3191 | Log.e(TAG, "Tried to open action bar menu with no action bar"); |
| 3192 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3193 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3194 | return true; |
| 3195 | } |
| 3196 | |
| 3197 | /** |
| 3198 | * Default implementation of |
| 3199 | * {@link android.view.Window.Callback#onMenuItemSelected} |
| 3200 | * for activities. This calls through to the new |
| 3201 | * {@link #onOptionsItemSelected} method for the |
| 3202 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} |
| 3203 | * panel, so that subclasses of |
| 3204 | * Activity don't need to deal with feature codes. |
| 3205 | */ |
| 3206 | public boolean onMenuItemSelected(int featureId, MenuItem item) { |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 3207 | CharSequence titleCondensed = item.getTitleCondensed(); |
| 3208 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3209 | switch (featureId) { |
| 3210 | case Window.FEATURE_OPTIONS_PANEL: |
| 3211 | // Put event logging here so it gets called even if subclass |
| 3212 | // doesn't call through to superclass's implmeentation of each |
| 3213 | // of these methods below |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 3214 | if(titleCondensed != null) { |
| 3215 | EventLog.writeEvent(50000, 0, titleCondensed.toString()); |
| 3216 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3217 | if (onOptionsItemSelected(item)) { |
| 3218 | return true; |
| 3219 | } |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3220 | if (mFragments.dispatchOptionsItemSelected(item)) { |
| 3221 | return true; |
| 3222 | } |
| 3223 | if (item.getItemId() == android.R.id.home && mActionBar != null && |
| 3224 | (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) { |
| 3225 | if (mParent == null) { |
Adam Powell | 07304f529 | 2012-05-07 17:32:44 -0700 | [diff] [blame] | 3226 | return onNavigateUp(); |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3227 | } else { |
Adam Powell | 07304f529 | 2012-05-07 17:32:44 -0700 | [diff] [blame] | 3228 | return mParent.onNavigateUpFromChild(this); |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3229 | } |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3230 | } |
| 3231 | return false; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3232 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3233 | case Window.FEATURE_CONTEXT_MENU: |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 3234 | if(titleCondensed != null) { |
| 3235 | EventLog.writeEvent(50000, 1, titleCondensed.toString()); |
| 3236 | } |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 3237 | if (onContextItemSelected(item)) { |
| 3238 | return true; |
| 3239 | } |
| 3240 | return mFragments.dispatchContextItemSelected(item); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3242 | default: |
| 3243 | return false; |
| 3244 | } |
| 3245 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3247 | /** |
| 3248 | * Default implementation of |
| 3249 | * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for |
| 3250 | * activities. This calls through to {@link #onOptionsMenuClosed(Menu)} |
| 3251 | * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel, |
| 3252 | * so that subclasses of Activity don't need to deal with feature codes. |
| 3253 | * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the |
| 3254 | * {@link #onContextMenuClosed(Menu)} will be called. |
| 3255 | */ |
| 3256 | public void onPanelClosed(int featureId, Menu menu) { |
| 3257 | switch (featureId) { |
| 3258 | case Window.FEATURE_OPTIONS_PANEL: |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3259 | mFragments.dispatchOptionsMenuClosed(menu); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3260 | onOptionsMenuClosed(menu); |
| 3261 | break; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3263 | case Window.FEATURE_CONTEXT_MENU: |
| 3264 | onContextMenuClosed(menu); |
| 3265 | break; |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3266 | |
| 3267 | case Window.FEATURE_ACTION_BAR: |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 3268 | initWindowDecorActionBar(); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3269 | mActionBar.dispatchMenuVisibilityChanged(false); |
| 3270 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3271 | } |
| 3272 | } |
| 3273 | |
| 3274 | /** |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3275 | * Declare that the options menu has changed, so should be recreated. |
| 3276 | * The {@link #onCreateOptionsMenu(Menu)} method will be called the next |
| 3277 | * time it needs to be displayed. |
| 3278 | */ |
| 3279 | public void invalidateOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3280 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 3281 | (mActionBar == null || !mActionBar.invalidateOptionsMenu())) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3282 | mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); |
| 3283 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3284 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3285 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3286 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3287 | * Initialize the contents of the Activity's standard options menu. You |
| 3288 | * should place your menu items in to <var>menu</var>. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3289 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3290 | * <p>This is only called once, the first time the options menu is |
| 3291 | * displayed. To update the menu every time it is displayed, see |
| 3292 | * {@link #onPrepareOptionsMenu}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3293 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3294 | * <p>The default implementation populates the menu with standard system |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3295 | * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that |
| 3296 | * they will be correctly ordered with application-defined menu items. |
| 3297 | * Deriving classes should always call through to the base implementation. |
| 3298 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3299 | * <p>You can safely hold on to <var>menu</var> (and any items created |
| 3300 | * from it), making modifications to it as desired, until the next |
| 3301 | * time onCreateOptionsMenu() is called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3302 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3303 | * <p>When you add items to the menu, you can implement the Activity's |
| 3304 | * {@link #onOptionsItemSelected} method to handle them there. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3305 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3306 | * @param menu The options menu in which you place your items. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3307 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3308 | * @return You must return true for the menu to be displayed; |
| 3309 | * if you return false it will not be shown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3310 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3311 | * @see #onPrepareOptionsMenu |
| 3312 | * @see #onOptionsItemSelected |
| 3313 | */ |
| 3314 | public boolean onCreateOptionsMenu(Menu menu) { |
| 3315 | if (mParent != null) { |
| 3316 | return mParent.onCreateOptionsMenu(menu); |
| 3317 | } |
| 3318 | return true; |
| 3319 | } |
| 3320 | |
| 3321 | /** |
| 3322 | * Prepare the Screen's standard options menu to be displayed. This is |
| 3323 | * called right before the menu is shown, every time it is shown. You can |
| 3324 | * use this method to efficiently enable/disable items or otherwise |
| 3325 | * dynamically modify the contents. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3326 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3327 | * <p>The default implementation updates the system menu items based on the |
| 3328 | * activity's state. Deriving classes should always call through to the |
| 3329 | * base class implementation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3330 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3331 | * @param menu The options menu as last shown or first initialized by |
| 3332 | * onCreateOptionsMenu(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3333 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3334 | * @return You must return true for the menu to be displayed; |
| 3335 | * if you return false it will not be shown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3336 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3337 | * @see #onCreateOptionsMenu |
| 3338 | */ |
| 3339 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 3340 | if (mParent != null) { |
| 3341 | return mParent.onPrepareOptionsMenu(menu); |
| 3342 | } |
| 3343 | return true; |
| 3344 | } |
| 3345 | |
| 3346 | /** |
| 3347 | * This hook is called whenever an item in your options menu is selected. |
| 3348 | * The default implementation simply returns false to have the normal |
| 3349 | * processing happen (calling the item's Runnable or sending a message to |
| 3350 | * its Handler as appropriate). You can use this method for any items |
| 3351 | * for which you would like to do processing without those other |
| 3352 | * facilities. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3353 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3354 | * <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] | 3355 | * perform the default menu handling.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3356 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3357 | * @param item The menu item that was selected. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3358 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3359 | * @return boolean Return false to allow normal menu processing to |
| 3360 | * proceed, true to consume it here. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3361 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3362 | * @see #onCreateOptionsMenu |
| 3363 | */ |
| 3364 | public boolean onOptionsItemSelected(MenuItem item) { |
| 3365 | if (mParent != null) { |
| 3366 | return mParent.onOptionsItemSelected(item); |
| 3367 | } |
| 3368 | return false; |
| 3369 | } |
| 3370 | |
| 3371 | /** |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3372 | * This method is called whenever the user chooses to navigate Up within your application's |
| 3373 | * activity hierarchy from the action bar. |
| 3374 | * |
| 3375 | * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName} |
| 3376 | * was specified in the manifest for this activity or an activity-alias to it, |
| 3377 | * default Up navigation will be handled automatically. If any activity |
| 3378 | * along the parent chain requires extra Intent arguments, the Activity subclass |
| 3379 | * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)} |
| 3380 | * to supply those arguments.</p> |
| 3381 | * |
| 3382 | * <p>See <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a> |
| 3383 | * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a> |
| 3384 | * from the design guide for more information about navigating within your app.</p> |
| 3385 | * |
| 3386 | * <p>See the {@link TaskStackBuilder} class and the Activity methods |
| 3387 | * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and |
| 3388 | * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation. |
| 3389 | * The AppNavigation sample application in the Android SDK is also available for reference.</p> |
| 3390 | * |
| 3391 | * @return true if Up navigation completed successfully and this Activity was finished, |
| 3392 | * false otherwise. |
| 3393 | */ |
| 3394 | public boolean onNavigateUp() { |
| 3395 | // Automatically handle hierarchical Up navigation if the proper |
| 3396 | // metadata is available. |
| 3397 | Intent upIntent = getParentActivityIntent(); |
| 3398 | if (upIntent != null) { |
Adam Powell | 0fc5b2b | 2012-07-18 18:20:29 -0700 | [diff] [blame] | 3399 | if (mActivityInfo.taskAffinity == null) { |
| 3400 | // Activities with a null affinity are special; they really shouldn't |
| 3401 | // specify a parent activity intent in the first place. Just finish |
| 3402 | // the current activity and call it a day. |
| 3403 | finish(); |
| 3404 | } else if (shouldUpRecreateTask(upIntent)) { |
Adam Powell | f78a844 | 2012-05-01 18:09:32 -0700 | [diff] [blame] | 3405 | TaskStackBuilder b = TaskStackBuilder.create(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3406 | onCreateNavigateUpTaskStack(b); |
| 3407 | onPrepareNavigateUpTaskStack(b); |
| 3408 | b.startActivities(); |
Adam Powell | 3d193d9 | 2012-05-15 16:51:55 -0700 | [diff] [blame] | 3409 | |
| 3410 | // We can't finishAffinity if we have a result. |
| 3411 | // Fall back and simply finish the current activity instead. |
| 3412 | if (mResultCode != RESULT_CANCELED || mResultData != null) { |
| 3413 | // Tell the developer what's going on to avoid hair-pulling. |
| 3414 | Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result"); |
| 3415 | finish(); |
| 3416 | } else { |
| 3417 | finishAffinity(); |
| 3418 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3419 | } else { |
| 3420 | navigateUpTo(upIntent); |
| 3421 | } |
| 3422 | return true; |
| 3423 | } |
| 3424 | return false; |
| 3425 | } |
| 3426 | |
| 3427 | /** |
| 3428 | * This is called when a child activity of this one attempts to navigate up. |
| 3429 | * The default implementation simply calls onNavigateUp() on this activity (the parent). |
| 3430 | * |
| 3431 | * @param child The activity making the call. |
| 3432 | */ |
| 3433 | public boolean onNavigateUpFromChild(Activity child) { |
| 3434 | return onNavigateUp(); |
| 3435 | } |
| 3436 | |
| 3437 | /** |
| 3438 | * Define the synthetic task stack that will be generated during Up navigation from |
| 3439 | * a different task. |
| 3440 | * |
| 3441 | * <p>The default implementation of this method adds the parent chain of this activity |
| 3442 | * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications |
| 3443 | * may choose to override this method to construct the desired task stack in a different |
| 3444 | * way.</p> |
| 3445 | * |
Adam Powell | f019595 | 2012-05-02 21:38:54 -0700 | [diff] [blame] | 3446 | * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()} |
| 3447 | * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent |
| 3448 | * returned by {@link #getParentActivityIntent()}.</p> |
| 3449 | * |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3450 | * <p>Applications that wish to supply extra Intent parameters to the parent stack defined |
| 3451 | * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p> |
| 3452 | * |
| 3453 | * @param builder An empty TaskStackBuilder - the application should add intents representing |
| 3454 | * the desired task stack |
| 3455 | */ |
| 3456 | public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) { |
| 3457 | builder.addParentStack(this); |
| 3458 | } |
| 3459 | |
| 3460 | /** |
| 3461 | * Prepare the synthetic task stack that will be generated during Up navigation |
| 3462 | * from a different task. |
| 3463 | * |
| 3464 | * <p>This method receives the {@link TaskStackBuilder} with the constructed series of |
| 3465 | * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}. |
| 3466 | * If any extra data should be added to these intents before launching the new task, |
| 3467 | * the application should override this method and add that data here.</p> |
| 3468 | * |
| 3469 | * @param builder A TaskStackBuilder that has been populated with Intents by |
| 3470 | * onCreateNavigateUpTaskStack. |
| 3471 | */ |
| 3472 | public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) { |
| 3473 | } |
| 3474 | |
| 3475 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3476 | * This hook is called whenever the options menu is being closed (either by the user canceling |
| 3477 | * the menu with the back/menu button, or when an item is selected). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3478 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3479 | * @param menu The options menu as last shown or first initialized by |
| 3480 | * onCreateOptionsMenu(). |
| 3481 | */ |
| 3482 | public void onOptionsMenuClosed(Menu menu) { |
| 3483 | if (mParent != null) { |
| 3484 | mParent.onOptionsMenuClosed(menu); |
| 3485 | } |
| 3486 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3487 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3488 | /** |
| 3489 | * Programmatically opens the options menu. If the options menu is already |
| 3490 | * open, this method does nothing. |
| 3491 | */ |
| 3492 | public void openOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3493 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 3494 | (mActionBar == null || !mActionBar.openOptionsMenu())) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3495 | mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null); |
| 3496 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3497 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3498 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3499 | /** |
| 3500 | * Progammatically closes the options menu. If the options menu is already |
| 3501 | * closed, this method does nothing. |
| 3502 | */ |
| 3503 | public void closeOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3504 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) { |
| 3505 | mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL); |
| 3506 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | /** |
| 3510 | * Called when a context menu for the {@code view} is about to be shown. |
| 3511 | * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every |
| 3512 | * time the context menu is about to be shown and should be populated for |
| 3513 | * the view (or item inside the view for {@link AdapterView} subclasses, |
| 3514 | * this can be found in the {@code menuInfo})). |
| 3515 | * <p> |
| 3516 | * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an |
| 3517 | * item has been selected. |
| 3518 | * <p> |
| 3519 | * 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] | 3520 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3521 | */ |
| 3522 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 3523 | } |
| 3524 | |
| 3525 | /** |
| 3526 | * Registers a context menu to be shown for the given view (multiple views |
| 3527 | * can show the context menu). This method will set the |
| 3528 | * {@link OnCreateContextMenuListener} on the view to this activity, so |
| 3529 | * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be |
| 3530 | * called when it is time to show the context menu. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3531 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3532 | * @see #unregisterForContextMenu(View) |
| 3533 | * @param view The view that should show a context menu. |
| 3534 | */ |
| 3535 | public void registerForContextMenu(View view) { |
| 3536 | view.setOnCreateContextMenuListener(this); |
| 3537 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3538 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3539 | /** |
| 3540 | * Prevents a context menu to be shown for the given view. This method will remove the |
| 3541 | * {@link OnCreateContextMenuListener} on the view. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3542 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3543 | * @see #registerForContextMenu(View) |
| 3544 | * @param view The view that should stop showing a context menu. |
| 3545 | */ |
| 3546 | public void unregisterForContextMenu(View view) { |
| 3547 | view.setOnCreateContextMenuListener(null); |
| 3548 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3549 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3550 | /** |
| 3551 | * Programmatically opens the context menu for a particular {@code view}. |
| 3552 | * The {@code view} should have been added via |
| 3553 | * {@link #registerForContextMenu(View)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3554 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3555 | * @param view The view to show the context menu for. |
| 3556 | */ |
| 3557 | public void openContextMenu(View view) { |
| 3558 | view.showContextMenu(); |
| 3559 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3560 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3561 | /** |
| 3562 | * Programmatically closes the most recently opened context menu, if showing. |
| 3563 | */ |
| 3564 | public void closeContextMenu() { |
Jose Lima | bd769a1 | 2015-01-28 10:43:15 -0800 | [diff] [blame] | 3565 | if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) { |
| 3566 | mWindow.closePanel(Window.FEATURE_CONTEXT_MENU); |
| 3567 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3568 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3569 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3570 | /** |
| 3571 | * This hook is called whenever an item in a context menu is selected. The |
| 3572 | * default implementation simply returns false to have the normal processing |
| 3573 | * happen (calling the item's Runnable or sending a message to its Handler |
| 3574 | * as appropriate). You can use this method for any items for which you |
| 3575 | * would like to do processing without those other facilities. |
| 3576 | * <p> |
| 3577 | * Use {@link MenuItem#getMenuInfo()} to get extra information set by the |
| 3578 | * View that added this menu item. |
| 3579 | * <p> |
| 3580 | * Derived classes should call through to the base class for it to perform |
| 3581 | * the default menu handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3582 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3583 | * @param item The context menu item that was selected. |
| 3584 | * @return boolean Return false to allow normal context menu processing to |
| 3585 | * proceed, true to consume it here. |
| 3586 | */ |
| 3587 | public boolean onContextItemSelected(MenuItem item) { |
| 3588 | if (mParent != null) { |
| 3589 | return mParent.onContextItemSelected(item); |
| 3590 | } |
| 3591 | return false; |
| 3592 | } |
| 3593 | |
| 3594 | /** |
| 3595 | * This hook is called whenever the context menu is being closed (either by |
| 3596 | * the user canceling the menu with the back/menu button, or when an item is |
| 3597 | * selected). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3598 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3599 | * @param menu The context menu that is being closed. |
| 3600 | */ |
| 3601 | public void onContextMenuClosed(Menu menu) { |
| 3602 | if (mParent != null) { |
| 3603 | mParent.onContextMenuClosed(menu); |
| 3604 | } |
| 3605 | } |
| 3606 | |
| 3607 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3608 | * @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] | 3609 | */ |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3610 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3611 | protected Dialog onCreateDialog(int id) { |
| 3612 | return null; |
| 3613 | } |
| 3614 | |
| 3615 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3616 | * Callback for creating dialogs that are managed (saved and restored) for you |
| 3617 | * by the activity. The default implementation calls through to |
| 3618 | * {@link #onCreateDialog(int)} for compatibility. |
| 3619 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 3620 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 3621 | * or later, consider instead using a {@link DialogFragment} instead.</em> |
| 3622 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3623 | * <p>If you use {@link #showDialog(int)}, the activity will call through to |
| 3624 | * this method the first time, and hang onto it thereafter. Any dialog |
| 3625 | * that is created by this method will automatically be saved and restored |
| 3626 | * for you, including whether it is showing. |
| 3627 | * |
| 3628 | * <p>If you would like the activity to manage saving and restoring dialogs |
| 3629 | * for you, you should override this method and handle any ids that are |
| 3630 | * passed to {@link #showDialog}. |
| 3631 | * |
| 3632 | * <p>If you would like an opportunity to prepare your dialog before it is shown, |
| 3633 | * override {@link #onPrepareDialog(int, Dialog, Bundle)}. |
| 3634 | * |
| 3635 | * @param id The id of the dialog. |
| 3636 | * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}. |
| 3637 | * @return The dialog. If you return null, the dialog will not be created. |
| 3638 | * |
| 3639 | * @see #onPrepareDialog(int, Dialog, Bundle) |
| 3640 | * @see #showDialog(int, Bundle) |
| 3641 | * @see #dismissDialog(int) |
| 3642 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3643 | * |
| 3644 | * @deprecated Use the new {@link DialogFragment} class with |
| 3645 | * {@link FragmentManager} instead; this is also |
| 3646 | * available on older platforms through the Android compatibility package. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3647 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3648 | @Nullable |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3649 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3650 | protected Dialog onCreateDialog(int id, Bundle args) { |
| 3651 | return onCreateDialog(id); |
| 3652 | } |
| 3653 | |
| 3654 | /** |
| 3655 | * @deprecated Old no-arguments version of |
| 3656 | * {@link #onPrepareDialog(int, Dialog, Bundle)}. |
| 3657 | */ |
| 3658 | @Deprecated |
| 3659 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 3660 | dialog.setOwnerActivity(this); |
| 3661 | } |
| 3662 | |
| 3663 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3664 | * Provides an opportunity to prepare a managed dialog before it is being |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3665 | * shown. The default implementation calls through to |
| 3666 | * {@link #onPrepareDialog(int, Dialog)} for compatibility. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3667 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3668 | * <p> |
| 3669 | * Override this if you need to update a managed dialog based on the state |
| 3670 | * of the application each time it is shown. For example, a time picker |
| 3671 | * dialog might want to be updated with the current time. You should call |
| 3672 | * through to the superclass's implementation. The default implementation |
| 3673 | * will set this Activity as the owner activity on the Dialog. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3674 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3675 | * @param id The id of the managed dialog. |
| 3676 | * @param dialog The dialog. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3677 | * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}. |
| 3678 | * @see #onCreateDialog(int, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3679 | * @see #showDialog(int) |
| 3680 | * @see #dismissDialog(int) |
| 3681 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3682 | * |
| 3683 | * @deprecated Use the new {@link DialogFragment} class with |
| 3684 | * {@link FragmentManager} instead; this is also |
| 3685 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3686 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3687 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3688 | protected void onPrepareDialog(int id, Dialog dialog, Bundle args) { |
| 3689 | onPrepareDialog(id, dialog); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3693 | * Simple version of {@link #showDialog(int, Bundle)} that does not |
| 3694 | * take any arguments. Simply calls {@link #showDialog(int, Bundle)} |
| 3695 | * with null arguments. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3696 | * |
| 3697 | * @deprecated Use the new {@link DialogFragment} class with |
| 3698 | * {@link FragmentManager} instead; this is also |
| 3699 | * available on older platforms through the Android compatibility package. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3700 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3701 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3702 | public final void showDialog(int id) { |
| 3703 | showDialog(id, null); |
| 3704 | } |
| 3705 | |
| 3706 | /** |
| 3707 | * 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] | 3708 | * will be made with the same id the first time this is called for a given |
| 3709 | * id. From thereafter, the dialog will be automatically saved and restored. |
| 3710 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 3711 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 3712 | * or later, consider instead using a {@link DialogFragment} instead.</em> |
| 3713 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3714 | * <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] | 3715 | * be made to provide an opportunity to do any timely preparation. |
| 3716 | * |
| 3717 | * @param id The id of the managed dialog. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3718 | * @param args Arguments to pass through to the dialog. These will be saved |
| 3719 | * and restored for you. Note that if the dialog is already created, |
| 3720 | * {@link #onCreateDialog(int, Bundle)} will not be called with the new |
| 3721 | * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be. |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 3722 | * If you need to rebuild the dialog, call {@link #removeDialog(int)} first. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3723 | * @return Returns true if the Dialog was created; false is returned if |
| 3724 | * it is not created because {@link #onCreateDialog(int, Bundle)} returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3725 | * |
Joe Onorato | 37296dc | 2009-07-31 17:58:55 -0700 | [diff] [blame] | 3726 | * @see Dialog |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3727 | * @see #onCreateDialog(int, Bundle) |
| 3728 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3729 | * @see #dismissDialog(int) |
| 3730 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3731 | * |
| 3732 | * @deprecated Use the new {@link DialogFragment} class with |
| 3733 | * {@link FragmentManager} instead; this is also |
| 3734 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3735 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3736 | @Nullable |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3737 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3738 | public final boolean showDialog(int id, Bundle args) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3739 | if (mManagedDialogs == null) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3740 | mManagedDialogs = new SparseArray<ManagedDialog>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3741 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3742 | ManagedDialog md = mManagedDialogs.get(id); |
| 3743 | if (md == null) { |
| 3744 | md = new ManagedDialog(); |
| 3745 | md.mDialog = createDialog(id, null, args); |
| 3746 | if (md.mDialog == null) { |
| 3747 | return false; |
| 3748 | } |
| 3749 | mManagedDialogs.put(id, md); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3750 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3751 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3752 | md.mArgs = args; |
| 3753 | onPrepareDialog(id, md.mDialog, args); |
| 3754 | md.mDialog.show(); |
| 3755 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | /** |
| 3759 | * Dismiss a dialog that was previously shown via {@link #showDialog(int)}. |
| 3760 | * |
| 3761 | * @param id The id of the managed dialog. |
| 3762 | * |
| 3763 | * @throws IllegalArgumentException if the id was not previously shown via |
| 3764 | * {@link #showDialog(int)}. |
| 3765 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3766 | * @see #onCreateDialog(int, Bundle) |
| 3767 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3768 | * @see #showDialog(int) |
| 3769 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3770 | * |
| 3771 | * @deprecated Use the new {@link DialogFragment} class with |
| 3772 | * {@link FragmentManager} instead; this is also |
| 3773 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3774 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3775 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3776 | public final void dismissDialog(int id) { |
| 3777 | if (mManagedDialogs == null) { |
| 3778 | throw missingDialog(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3779 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3780 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3781 | final ManagedDialog md = mManagedDialogs.get(id); |
| 3782 | if (md == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3783 | throw missingDialog(id); |
| 3784 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3785 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | /** |
| 3789 | * Creates an exception to throw if a user passed in a dialog id that is |
| 3790 | * unexpected. |
| 3791 | */ |
| 3792 | private IllegalArgumentException missingDialog(int id) { |
| 3793 | return new IllegalArgumentException("no dialog with id " + id + " was ever " |
| 3794 | + "shown via Activity#showDialog"); |
| 3795 | } |
| 3796 | |
| 3797 | /** |
| 3798 | * Removes any internal references to a dialog managed by this Activity. |
| 3799 | * If the dialog is showing, it will dismiss it as part of the clean up. |
| 3800 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3801 | * <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] | 3802 | * want to avoid the overhead of saving and restoring it in the future. |
| 3803 | * |
Dianne Hackborn | d2ce8bbb | 2010-10-06 16:46:05 -0700 | [diff] [blame] | 3804 | * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function |
| 3805 | * will not throw an exception if you try to remove an ID that does not |
| 3806 | * currently have an associated dialog.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3807 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3808 | * @param id The id of the managed dialog. |
| 3809 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3810 | * @see #onCreateDialog(int, Bundle) |
| 3811 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3812 | * @see #showDialog(int) |
| 3813 | * @see #dismissDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3814 | * |
| 3815 | * @deprecated Use the new {@link DialogFragment} class with |
| 3816 | * {@link FragmentManager} instead; this is also |
| 3817 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3818 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3819 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3820 | public final void removeDialog(int id) { |
Dianne Hackborn | d2ce8bbb | 2010-10-06 16:46:05 -0700 | [diff] [blame] | 3821 | if (mManagedDialogs != null) { |
| 3822 | final ManagedDialog md = mManagedDialogs.get(id); |
| 3823 | if (md != null) { |
| 3824 | md.mDialog.dismiss(); |
| 3825 | mManagedDialogs.remove(id); |
| 3826 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3827 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | /** |
| 3831 | * 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] | 3832 | * |
Bjorn Bringert | 6266e40 | 2009-09-25 14:25:41 +0100 | [diff] [blame] | 3833 | * <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] | 3834 | * menu item, search button, or other widgets within your activity. Unless overidden, |
Bjorn Bringert | 6266e40 | 2009-09-25 14:25:41 +0100 | [diff] [blame] | 3835 | * calling this function is the same as calling |
| 3836 | * {@link #startSearch startSearch(null, false, null, false)}, which launches |
| 3837 | * search for the current activity as specified in its manifest, see {@link SearchManager}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3838 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3839 | * <p>You can override this function to force global search, e.g. in response to a dedicated |
| 3840 | * search key, or to block search entirely (by simply returning false). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3841 | * |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3842 | * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default |
| 3843 | * implementation changes to simply return false and you must supply your own custom |
| 3844 | * implementation if you want to support search.</p> |
| 3845 | * |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 3846 | * @param searchEvent The {@link SearchEvent} that signaled this search. |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3847 | * @return Returns {@code true} if search launched, and {@code false} if the activity does |
| 3848 | * not respond to search. The default implementation always returns {@code true}, except |
| 3849 | * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3850 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3851 | * @see android.app.SearchManager |
| 3852 | */ |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 3853 | public boolean onSearchRequested(@Nullable SearchEvent searchEvent) { |
| 3854 | mSearchEvent = searchEvent; |
| 3855 | boolean result = onSearchRequested(); |
| 3856 | mSearchEvent = null; |
| 3857 | return result; |
| 3858 | } |
| 3859 | |
| 3860 | /** |
| 3861 | * @see #onSearchRequested(SearchEvent) |
| 3862 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3863 | public boolean onSearchRequested() { |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3864 | if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK) |
| 3865 | != Configuration.UI_MODE_TYPE_TELEVISION) { |
| 3866 | startSearch(null, false, null, false); |
| 3867 | return true; |
| 3868 | } else { |
| 3869 | return false; |
| 3870 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3871 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3872 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3873 | /** |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 3874 | * During the onSearchRequested() callbacks, this function will return the |
| 3875 | * {@link SearchEvent} that triggered the callback, if it exists. |
| 3876 | * |
| 3877 | * @return SearchEvent The SearchEvent that triggered the {@link |
| 3878 | * #onSearchRequested} callback. |
| 3879 | */ |
| 3880 | public final SearchEvent getSearchEvent() { |
| 3881 | return mSearchEvent; |
| 3882 | } |
| 3883 | |
| 3884 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3885 | * This hook is called to launch the search UI. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3886 | * |
| 3887 | * <p>It is typically called from onSearchRequested(), either directly from |
| 3888 | * 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] | 3889 | * 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] | 3890 | * 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] | 3891 | * is to inject specific data such as context data, it is preferred to <i>override</i> |
| 3892 | * onSearchRequested(), so that any callers to it will benefit from the override. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3893 | * |
| 3894 | * @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] | 3895 | * pre-entered text in the search query box. |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3896 | * @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] | 3897 | * any further typing will replace it. This is useful for cases where an entire pre-formed |
| 3898 | * query is being inserted. If false, the selection point will be placed at the end of the |
| 3899 | * inserted query. This is useful when the inserted query is text that the user entered, |
| 3900 | * and the user would expect to be able to keep typing. <i>This parameter is only meaningful |
| 3901 | * if initialQuery is a non-empty string.</i> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3902 | * @param appSearchData An application can insert application-specific |
| 3903 | * 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] | 3904 | * searches. This data will be returned with SEARCH intent(s). Null if |
| 3905 | * no extra data is required. |
| 3906 | * @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] | 3907 | * 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] | 3908 | * 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] | 3909 | * 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] | 3910 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3911 | * @see android.app.SearchManager |
| 3912 | * @see #onSearchRequested |
| 3913 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3914 | public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery, |
| 3915 | @Nullable Bundle appSearchData, boolean globalSearch) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3916 | ensureSearchManager(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 3917 | mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(), |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3918 | appSearchData, globalSearch); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3919 | } |
| 3920 | |
| 3921 | /** |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3922 | * Similar to {@link #startSearch}, but actually fires off the search query after invoking |
| 3923 | * the search dialog. Made available for testing purposes. |
| 3924 | * |
| 3925 | * @param query The query to trigger. If empty, the request will be ignored. |
| 3926 | * @param appSearchData An application can insert application-specific |
| 3927 | * context here, in order to improve quality or specificity of its own |
| 3928 | * searches. This data will be returned with SEARCH intent(s). Null if |
| 3929 | * no extra data is required. |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3930 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3931 | public void triggerSearch(String query, @Nullable Bundle appSearchData) { |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3932 | ensureSearchManager(); |
Bjorn Bringert | b782a2f | 2009-10-01 09:57:33 +0100 | [diff] [blame] | 3933 | mSearchManager.triggerSearch(query, getComponentName(), appSearchData); |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3937 | * Request that key events come to this activity. Use this if your |
| 3938 | * activity has no views with focus, but the activity still wants |
| 3939 | * a chance to process key events. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3940 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3941 | * @see android.view.Window#takeKeyEvents |
| 3942 | */ |
| 3943 | public void takeKeyEvents(boolean get) { |
| 3944 | getWindow().takeKeyEvents(get); |
| 3945 | } |
| 3946 | |
| 3947 | /** |
| 3948 | * Enable extended window features. This is a convenience for calling |
| 3949 | * {@link android.view.Window#requestFeature getWindow().requestFeature()}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3950 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3951 | * @param featureId The desired feature as defined in |
| 3952 | * {@link android.view.Window}. |
| 3953 | * @return Returns true if the requested feature is supported and now |
| 3954 | * enabled. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3955 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3956 | * @see android.view.Window#requestFeature |
| 3957 | */ |
| 3958 | public final boolean requestWindowFeature(int featureId) { |
| 3959 | return getWindow().requestFeature(featureId); |
| 3960 | } |
| 3961 | |
| 3962 | /** |
| 3963 | * Convenience for calling |
| 3964 | * {@link android.view.Window#setFeatureDrawableResource}. |
| 3965 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 3966 | public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3967 | getWindow().setFeatureDrawableResource(featureId, resId); |
| 3968 | } |
| 3969 | |
| 3970 | /** |
| 3971 | * Convenience for calling |
| 3972 | * {@link android.view.Window#setFeatureDrawableUri}. |
| 3973 | */ |
| 3974 | public final void setFeatureDrawableUri(int featureId, Uri uri) { |
| 3975 | getWindow().setFeatureDrawableUri(featureId, uri); |
| 3976 | } |
| 3977 | |
| 3978 | /** |
| 3979 | * Convenience for calling |
| 3980 | * {@link android.view.Window#setFeatureDrawable(int, Drawable)}. |
| 3981 | */ |
| 3982 | public final void setFeatureDrawable(int featureId, Drawable drawable) { |
| 3983 | getWindow().setFeatureDrawable(featureId, drawable); |
| 3984 | } |
| 3985 | |
| 3986 | /** |
| 3987 | * Convenience for calling |
| 3988 | * {@link android.view.Window#setFeatureDrawableAlpha}. |
| 3989 | */ |
| 3990 | public final void setFeatureDrawableAlpha(int featureId, int alpha) { |
| 3991 | getWindow().setFeatureDrawableAlpha(featureId, alpha); |
| 3992 | } |
| 3993 | |
| 3994 | /** |
| 3995 | * Convenience for calling |
| 3996 | * {@link android.view.Window#getLayoutInflater}. |
| 3997 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3998 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3999 | public LayoutInflater getLayoutInflater() { |
| 4000 | return getWindow().getLayoutInflater(); |
| 4001 | } |
| 4002 | |
| 4003 | /** |
| 4004 | * Returns a {@link MenuInflater} with this context. |
| 4005 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4006 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4007 | public MenuInflater getMenuInflater() { |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 4008 | // Make sure that action views can get an appropriate theme. |
| 4009 | if (mMenuInflater == null) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 4010 | initWindowDecorActionBar(); |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 4011 | if (mActionBar != null) { |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 4012 | mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this); |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 4013 | } else { |
| 4014 | mMenuInflater = new MenuInflater(this); |
| 4015 | } |
| 4016 | } |
| 4017 | return mMenuInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | @Override |
Filip Gruszczynski | 0daf210 | 2015-09-29 08:39:07 -0700 | [diff] [blame] | 4021 | public void setTheme(int resid) { |
| 4022 | super.setTheme(resid); |
| 4023 | mWindow.setTheme(resid); |
| 4024 | } |
| 4025 | |
| 4026 | @Override |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 4027 | protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4028 | boolean first) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4029 | if (mParent == null) { |
| 4030 | super.onApplyThemeResource(theme, resid, first); |
| 4031 | } else { |
| 4032 | try { |
| 4033 | theme.setTo(mParent.getTheme()); |
| 4034 | } catch (Exception e) { |
| 4035 | // Empty |
| 4036 | } |
| 4037 | theme.applyStyle(resid, false); |
| 4038 | } |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | /** |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4042 | * Requests permissions to be granted to this application. These permissions |
| 4043 | * must be requested in your manifest, they should not be granted to your app, |
| 4044 | * and they should have protection level {@link android.content.pm.PermissionInfo |
| 4045 | * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by |
| 4046 | * the platform or a third-party app. |
| 4047 | * <p> |
| 4048 | * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL} |
| 4049 | * are granted at install time if requested in the manifest. Signature permissions |
| 4050 | * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at |
| 4051 | * install time if requested in the manifest and the signature of your app matches |
| 4052 | * the signature of the app declaring the permissions. |
| 4053 | * </p> |
| 4054 | * <p> |
| 4055 | * If your app does not have the requested permissions the user will be presented |
| 4056 | * with UI for accepting them. After the user has accepted or rejected the |
| 4057 | * requested permissions you will receive a callback on {@link |
| 4058 | * #onRequestPermissionsResult(int, String[], int[])} reporting whether the |
| 4059 | * permissions were granted or not. |
| 4060 | * </p> |
| 4061 | * <p> |
| 4062 | * Note that requesting a permission does not guarantee it will be granted and |
| 4063 | * your app should be able to run without having this permission. |
| 4064 | * </p> |
| 4065 | * <p> |
| 4066 | * This method may start an activity allowing the user to choose which permissions |
| 4067 | * to grant and which to reject. Hence, you should be prepared that your activity |
| 4068 | * may be paused and resumed. Further, granting some permissions may require |
| 4069 | * a restart of you application. In such a case, the system will recreate the |
| 4070 | * activity stack before delivering the result to {@link |
| 4071 | * #onRequestPermissionsResult(int, String[], int[])}. |
| 4072 | * </p> |
| 4073 | * <p> |
| 4074 | * When checking whether you have a permission you should use {@link |
| 4075 | * #checkSelfPermission(String)}. |
| 4076 | * </p> |
| 4077 | * <p> |
Svet Ganov | d7ad54e | 2015-08-21 06:18:09 -0700 | [diff] [blame] | 4078 | * Calling this API for permissions already granted to your app would show UI |
| 4079 | * to the user to decide whether the app can still hold these permissions. This |
| 4080 | * can be useful if the way your app uses data guarded by the permissions |
| 4081 | * changes significantly. |
| 4082 | * </p> |
| 4083 | * <p> |
Svet Ganov | 255aece | 2015-07-03 16:03:53 -0700 | [diff] [blame] | 4084 | * You cannot request a permission if your activity sets {@link |
| 4085 | * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to |
| 4086 | * <code>true</code> because in this case the activity would not receive |
| 4087 | * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}. |
| 4088 | * </p> |
| 4089 | * <p> |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4090 | * A sample permissions request looks like this: |
| 4091 | * </p> |
| 4092 | * <code><pre><p> |
| 4093 | * private void showContacts() { |
| 4094 | * if (checkSelfPermission(Manifest.permission.READ_CONTACTS) |
| 4095 | * != PackageManager.PERMISSION_GRANTED) { |
| 4096 | * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, |
| 4097 | * PERMISSIONS_REQUEST_READ_CONTACTS); |
| 4098 | * } else { |
| 4099 | * doShowContacts(); |
| 4100 | * } |
| 4101 | * } |
| 4102 | * |
| 4103 | * {@literal @}Override |
| 4104 | * public void onRequestPermissionsResult(int requestCode, String[] permissions, |
| 4105 | * int[] grantResults) { |
| 4106 | * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS |
| 4107 | * && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 4108 | * showContacts(); |
| 4109 | * } |
| 4110 | * } |
| 4111 | * </code></pre></p> |
| 4112 | * |
Svet Ganov | f66381c | 2016-02-18 20:02:36 -0800 | [diff] [blame] | 4113 | * @param permissions The requested permissions. Must me non-null and not empty. |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4114 | * @param requestCode Application specific request code to match with a result |
| 4115 | * reported to {@link #onRequestPermissionsResult(int, String[], int[])}. |
Svet Ganov | 255aece | 2015-07-03 16:03:53 -0700 | [diff] [blame] | 4116 | * Should be >= 0. |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4117 | * |
| 4118 | * @see #onRequestPermissionsResult(int, String[], int[]) |
| 4119 | * @see #checkSelfPermission(String) |
Svetoslav | aa048b6 | 2015-06-02 15:08:48 -0700 | [diff] [blame] | 4120 | * @see #shouldShowRequestPermissionRationale(String) |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4121 | */ |
| 4122 | public final void requestPermissions(@NonNull String[] permissions, int requestCode) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 4123 | if (mHasCurrentPermissionsRequest) { |
| 4124 | Log.w(TAG, "Can reqeust only one set of permissions at a time"); |
| 4125 | // Dispatch the callback with empty arrays which means a cancellation. |
| 4126 | onRequestPermissionsResult(requestCode, new String[0], new int[0]); |
| 4127 | return; |
| 4128 | } |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4129 | Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions); |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 4130 | startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null); |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 4131 | mHasCurrentPermissionsRequest = true; |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | /** |
| 4135 | * Callback for the result from requesting permissions. This method |
| 4136 | * is invoked for every call on {@link #requestPermissions(String[], int)}. |
Svet Ganov | 0f45c7c | 2015-07-16 16:28:51 -0700 | [diff] [blame] | 4137 | * <p> |
| 4138 | * <strong>Note:</strong> It is possible that the permissions request interaction |
| 4139 | * with the user is interrupted. In this case you will receive empty permissions |
| 4140 | * and results arrays which should be treated as a cancellation. |
| 4141 | * </p> |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4142 | * |
| 4143 | * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}. |
| 4144 | * @param permissions The requested permissions. Never null. |
| 4145 | * @param grantResults The grant results for the corresponding permissions |
| 4146 | * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED} |
| 4147 | * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null. |
| 4148 | * |
| 4149 | * @see #requestPermissions(String[], int) |
| 4150 | */ |
| 4151 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
| 4152 | @NonNull int[] grantResults) { |
| 4153 | /* callback - no nothing */ |
| 4154 | } |
| 4155 | |
| 4156 | /** |
Svetoslav | 20770dd | 2015-05-29 15:43:04 -0700 | [diff] [blame] | 4157 | * Gets whether you should show UI with rationale for requesting a permission. |
| 4158 | * You should do this only if you do not have the permission and the context in |
| 4159 | * which the permission is requested does not clearly communicate to the user |
| 4160 | * what would be the benefit from granting this permission. |
| 4161 | * <p> |
| 4162 | * For example, if you write a camera app, requesting the camera permission |
| 4163 | * would be expected by the user and no rationale for why it is requested is |
| 4164 | * needed. If however, the app needs location for tagging photos then a non-tech |
| 4165 | * savvy user may wonder how location is related to taking photos. In this case |
| 4166 | * you may choose to show UI with rationale of requesting this permission. |
| 4167 | * </p> |
| 4168 | * |
| 4169 | * @param permission A permission your app wants to request. |
| 4170 | * @return Whether you can show permission rationale UI. |
| 4171 | * |
| 4172 | * @see #checkSelfPermission(String) |
| 4173 | * @see #requestPermissions(String[], int) |
| 4174 | * @see #onRequestPermissionsResult(int, String[], int[]) |
| 4175 | */ |
Svetoslav | 9ce9a15 | 2015-06-02 17:59:31 -0700 | [diff] [blame] | 4176 | public boolean shouldShowRequestPermissionRationale(@NonNull String permission) { |
Svetoslav | 20770dd | 2015-05-29 15:43:04 -0700 | [diff] [blame] | 4177 | return getPackageManager().shouldShowRequestPermissionRationale(permission); |
| 4178 | } |
| 4179 | |
| 4180 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4181 | * Same as calling {@link #startActivityForResult(Intent, int, Bundle)} |
| 4182 | * with no options. |
| 4183 | * |
| 4184 | * @param intent The intent to start. |
| 4185 | * @param requestCode If >= 0, this code will be returned in |
| 4186 | * onActivityResult() when the activity exits. |
| 4187 | * |
| 4188 | * @throws android.content.ActivityNotFoundException |
| 4189 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4190 | * @see #startActivity |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4191 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4192 | public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) { |
George Mount | abb63cb | 2014-06-23 11:17:58 -0700 | [diff] [blame] | 4193 | startActivityForResult(intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4197 | * Launch an activity for which you would like a result when it finished. |
| 4198 | * When this activity exits, your |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4199 | * onActivityResult() method will be called with the given requestCode. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4200 | * Using a negative requestCode is the same as calling |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4201 | * {@link #startActivity} (the activity is not launched as a sub-activity). |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4202 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4203 | * <p>Note that this method should only be used with Intent protocols |
| 4204 | * that are defined to return a result. In other protocols (such as |
| 4205 | * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may |
| 4206 | * not get the result when you expect. For example, if the activity you |
| 4207 | * are launching uses the singleTask launch mode, it will not run in your |
| 4208 | * task and thus you will immediately receive a cancel result. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4209 | * |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4210 | * <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] | 4211 | * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4212 | * activity, then your window will not be displayed until a result is |
| 4213 | * returned back from the started activity. This is to avoid visible |
| 4214 | * flickering when redirecting to another activity. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4215 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4216 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4217 | * if there was no Activity found to run the given Intent. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4218 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4219 | * @param intent The intent to start. |
| 4220 | * @param requestCode If >= 0, this code will be returned in |
| 4221 | * onActivityResult() when the activity exits. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4222 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4223 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4224 | * Context.startActivity(Intent, Bundle)} for more details. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4225 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4226 | * @throws android.content.ActivityNotFoundException |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4227 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4228 | * @see #startActivity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4229 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4230 | public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, |
| 4231 | @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4232 | if (mParent == null) { |
| 4233 | Instrumentation.ActivityResult ar = |
| 4234 | mInstrumentation.execStartActivity( |
| 4235 | this, mMainThread.getApplicationThread(), mToken, this, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4236 | intent, requestCode, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4237 | if (ar != null) { |
| 4238 | mMainThread.sendActivityResult( |
| 4239 | mToken, mEmbeddedID, requestCode, ar.getResultCode(), |
| 4240 | ar.getResultData()); |
| 4241 | } |
| 4242 | if (requestCode >= 0) { |
| 4243 | // If this start is requesting a result, we can avoid making |
| 4244 | // the activity visible until the result is received. Setting |
| 4245 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4246 | // activity hidden during this time, to avoid flickering. |
| 4247 | // This can only be done when a result is requested because |
| 4248 | // that guarantees we will get information back when the |
| 4249 | // activity is finished, no matter what happens to it. |
| 4250 | mStartedActivity = true; |
| 4251 | } |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 4252 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4253 | cancelInputsAndStartExitTransition(options); |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 4254 | // 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] | 4255 | } else { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4256 | if (options != null) { |
| 4257 | mParent.startActivityFromChild(this, intent, requestCode, options); |
| 4258 | } else { |
| 4259 | // Note we want to go through this method for compatibility with |
| 4260 | // existing applications that may have overridden it. |
| 4261 | mParent.startActivityFromChild(this, intent, requestCode); |
| 4262 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4263 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | /** |
| 4267 | * Cancels pending inputs and if an Activity Transition is to be run, starts the transition. |
| 4268 | * |
| 4269 | * @param options The ActivityOptions bundle used to start an Activity. |
| 4270 | */ |
| 4271 | private void cancelInputsAndStartExitTransition(Bundle options) { |
| 4272 | final View decor = mWindow != null ? mWindow.peekDecorView() : null; |
| 4273 | if (decor != null) { |
| 4274 | decor.cancelPendingInputEvents(); |
| 4275 | } |
George Mount | ff24328 | 2014-07-07 16:12:07 -0700 | [diff] [blame] | 4276 | if (options != null && !isTopOfTask()) { |
| 4277 | mActivityTransitionState.startExitOutTransition(this, options); |
| 4278 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | /** |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4282 | * @hide Implement to provide correct calling token. |
| 4283 | */ |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4284 | public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) { |
| 4285 | startActivityForResultAsUser(intent, requestCode, null, user); |
| 4286 | } |
| 4287 | |
| 4288 | /** |
| 4289 | * @hide Implement to provide correct calling token. |
| 4290 | */ |
| 4291 | public void startActivityForResultAsUser(Intent intent, int requestCode, |
| 4292 | @Nullable Bundle options, UserHandle user) { |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4293 | if (mParent != null) { |
| 4294 | throw new RuntimeException("Can't be called from a child"); |
| 4295 | } |
| 4296 | Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( |
| 4297 | this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, |
| 4298 | options, user); |
| 4299 | if (ar != null) { |
| 4300 | mMainThread.sendActivityResult( |
| 4301 | mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); |
| 4302 | } |
| 4303 | if (requestCode >= 0) { |
| 4304 | // If this start is requesting a result, we can avoid making |
| 4305 | // the activity visible until the result is received. Setting |
| 4306 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4307 | // activity hidden during this time, to avoid flickering. |
| 4308 | // This can only be done when a result is requested because |
| 4309 | // that guarantees we will get information back when the |
| 4310 | // activity is finished, no matter what happens to it. |
| 4311 | mStartedActivity = true; |
| 4312 | } |
| 4313 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4314 | cancelInputsAndStartExitTransition(options); |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | /** |
| 4318 | * @hide Implement to provide correct calling token. |
| 4319 | */ |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4320 | public void startActivityAsUser(Intent intent, UserHandle user) { |
| 4321 | startActivityAsUser(intent, null, user); |
| 4322 | } |
| 4323 | |
| 4324 | /** |
| 4325 | * @hide Implement to provide correct calling token. |
| 4326 | */ |
| 4327 | public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) { |
| 4328 | if (mParent != null) { |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4329 | throw new RuntimeException("Can't be called from a child"); |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4330 | } |
| 4331 | Instrumentation.ActivityResult ar = |
| 4332 | mInstrumentation.execStartActivity( |
| 4333 | this, mMainThread.getApplicationThread(), mToken, this, |
| 4334 | intent, -1, options, user); |
| 4335 | if (ar != null) { |
| 4336 | mMainThread.sendActivityResult( |
| 4337 | mToken, mEmbeddedID, -1, ar.getResultCode(), |
| 4338 | ar.getResultData()); |
| 4339 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4340 | cancelInputsAndStartExitTransition(options); |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | /** |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4344 | * Start a new activity as if it was started by the activity that started our |
| 4345 | * current activity. This is for the resolver and chooser activities, which operate |
| 4346 | * as intermediaries that dispatch their intent to the target the user selects -- to |
| 4347 | * do this, they must perform all security checks including permission grants as if |
| 4348 | * their launch had come from the original activity. |
Dianne Hackborn | a7cfbe0 | 2015-07-16 10:52:52 -0700 | [diff] [blame] | 4349 | * @param intent The Intent to start. |
| 4350 | * @param options ActivityOptions or null. |
| 4351 | * @param ignoreTargetSecurity If true, the activity manager will not check whether the |
| 4352 | * caller it is doing the start is, is actually allowed to start the target activity. |
| 4353 | * If you set this to true, you must set an explicit component in the Intent and do any |
| 4354 | * appropriate security checks yourself. |
| 4355 | * @param userId The user the new activity should run as. |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4356 | * @hide |
| 4357 | */ |
Dianne Hackborn | a7cfbe0 | 2015-07-16 10:52:52 -0700 | [diff] [blame] | 4358 | public void startActivityAsCaller(Intent intent, @Nullable Bundle options, |
| 4359 | boolean ignoreTargetSecurity, int userId) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4360 | if (mParent != null) { |
| 4361 | throw new RuntimeException("Can't be called from a child"); |
| 4362 | } |
| 4363 | Instrumentation.ActivityResult ar = |
| 4364 | mInstrumentation.execStartActivityAsCaller( |
| 4365 | this, mMainThread.getApplicationThread(), mToken, this, |
Dianne Hackborn | a7cfbe0 | 2015-07-16 10:52:52 -0700 | [diff] [blame] | 4366 | intent, -1, options, ignoreTargetSecurity, userId); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4367 | if (ar != null) { |
| 4368 | mMainThread.sendActivityResult( |
| 4369 | mToken, mEmbeddedID, -1, ar.getResultCode(), |
| 4370 | ar.getResultData()); |
| 4371 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4372 | cancelInputsAndStartExitTransition(options); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4376 | * Same as calling {@link #startIntentSenderForResult(IntentSender, int, |
| 4377 | * Intent, int, int, int, Bundle)} with no options. |
| 4378 | * |
| 4379 | * @param intent The IntentSender to launch. |
| 4380 | * @param requestCode If >= 0, this code will be returned in |
| 4381 | * onActivityResult() when the activity exits. |
| 4382 | * @param fillInIntent If non-null, this will be provided as the |
| 4383 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4384 | * @param flagsMask Intent flags in the original IntentSender that you |
| 4385 | * would like to change. |
| 4386 | * @param flagsValues Desired values for any bits set in |
| 4387 | * <var>flagsMask</var> |
| 4388 | * @param extraFlags Always set to 0. |
| 4389 | */ |
| 4390 | public void startIntentSenderForResult(IntentSender intent, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4391 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4392 | throws IntentSender.SendIntentException { |
| 4393 | startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask, |
| 4394 | flagsValues, extraFlags, null); |
| 4395 | } |
| 4396 | |
| 4397 | /** |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4398 | * Like {@link #startActivityForResult(Intent, int)}, but allowing you |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4399 | * to use a IntentSender to describe the activity to be started. If |
| 4400 | * the IntentSender is for an activity, that activity will be started |
| 4401 | * as if you had called the regular {@link #startActivityForResult(Intent, int)} |
| 4402 | * here; otherwise, its associated action will be executed (such as |
| 4403 | * sending a broadcast) as if you had called |
| 4404 | * {@link IntentSender#sendIntent IntentSender.sendIntent} on it. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4405 | * |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4406 | * @param intent The IntentSender to launch. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4407 | * @param requestCode If >= 0, this code will be returned in |
| 4408 | * onActivityResult() when the activity exits. |
| 4409 | * @param fillInIntent If non-null, this will be provided as the |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4410 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4411 | * @param flagsMask Intent flags in the original IntentSender that you |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4412 | * would like to change. |
| 4413 | * @param flagsValues Desired values for any bits set in |
| 4414 | * <var>flagsMask</var> |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4415 | * @param extraFlags Always set to 0. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4416 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4417 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4418 | * Context.startActivity(Intent, Bundle)} for more details. If options |
| 4419 | * have also been supplied by the IntentSender, options given here will |
| 4420 | * override any that conflict with those given by the IntentSender. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4421 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4422 | public void startIntentSenderForResult(IntentSender intent, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4423 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4424 | Bundle options) throws IntentSender.SendIntentException { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4425 | if (mParent == null) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4426 | startIntentSenderForResultInner(intent, requestCode, fillInIntent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4427 | flagsMask, flagsValues, this, options); |
| 4428 | } else if (options != null) { |
| 4429 | mParent.startIntentSenderFromChild(this, intent, requestCode, |
| 4430 | fillInIntent, flagsMask, flagsValues, extraFlags, options); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4431 | } else { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4432 | // Note we want to go through this call for compatibility with |
| 4433 | // existing applications that may have overridden the method. |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4434 | mParent.startIntentSenderFromChild(this, intent, requestCode, |
| 4435 | fillInIntent, flagsMask, flagsValues, extraFlags); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4436 | } |
| 4437 | } |
| 4438 | |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4439 | private void startIntentSenderForResultInner(IntentSender intent, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4440 | Intent fillInIntent, int flagsMask, int flagsValues, Activity activity, |
| 4441 | Bundle options) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4442 | throws IntentSender.SendIntentException { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4443 | try { |
| 4444 | String resolvedType = null; |
| 4445 | if (fillInIntent != null) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4446 | fillInIntent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 4447 | fillInIntent.prepareToLeaveProcess(this); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4448 | resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); |
| 4449 | } |
| 4450 | int result = ActivityManagerNative.getDefault() |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4451 | .startActivityIntentSender(mMainThread.getApplicationThread(), intent, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4452 | fillInIntent, resolvedType, mToken, activity.mEmbeddedID, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4453 | requestCode, flagsMask, flagsValues, options); |
| 4454 | if (result == ActivityManager.START_CANCELED) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4455 | throw new IntentSender.SendIntentException(); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4456 | } |
| 4457 | Instrumentation.checkStartActivityResult(result, null); |
| 4458 | } catch (RemoteException e) { |
| 4459 | } |
| 4460 | if (requestCode >= 0) { |
| 4461 | // If this start is requesting a result, we can avoid making |
| 4462 | // the activity visible until the result is received. Setting |
| 4463 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4464 | // activity hidden during this time, to avoid flickering. |
| 4465 | // This can only be done when a result is requested because |
| 4466 | // that guarantees we will get information back when the |
| 4467 | // activity is finished, no matter what happens to it. |
| 4468 | mStartedActivity = true; |
| 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4473 | * Same as {@link #startActivity(Intent, Bundle)} with no options |
| 4474 | * specified. |
| 4475 | * |
| 4476 | * @param intent The intent to start. |
| 4477 | * |
| 4478 | * @throws android.content.ActivityNotFoundException |
| 4479 | * |
| 4480 | * @see {@link #startActivity(Intent, Bundle)} |
| 4481 | * @see #startActivityForResult |
| 4482 | */ |
| 4483 | @Override |
| 4484 | public void startActivity(Intent intent) { |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 4485 | this.startActivity(intent, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4489 | * Launch a new activity. You will not receive any information about when |
| 4490 | * the activity exits. This implementation overrides the base version, |
| 4491 | * providing information about |
| 4492 | * the activity performing the launch. Because of this additional |
| 4493 | * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not |
| 4494 | * required; if not specified, the new activity will be added to the |
| 4495 | * task of the caller. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4496 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4497 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4498 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4499 | * |
| 4500 | * @param intent The intent to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4501 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4502 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4503 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4504 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4505 | * @throws android.content.ActivityNotFoundException |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4506 | * |
| 4507 | * @see {@link #startActivity(Intent)} |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4508 | * @see #startActivityForResult |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4509 | */ |
| 4510 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4511 | public void startActivity(Intent intent, @Nullable Bundle options) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4512 | if (options != null) { |
| 4513 | startActivityForResult(intent, -1, options); |
| 4514 | } else { |
| 4515 | // Note we want to go through this call for compatibility with |
| 4516 | // applications that may have overridden the method. |
| 4517 | startActivityForResult(intent, -1); |
| 4518 | } |
| 4519 | } |
| 4520 | |
| 4521 | /** |
| 4522 | * Same as {@link #startActivities(Intent[], Bundle)} with no options |
| 4523 | * specified. |
| 4524 | * |
| 4525 | * @param intents The intents to start. |
| 4526 | * |
| 4527 | * @throws android.content.ActivityNotFoundException |
| 4528 | * |
| 4529 | * @see {@link #startActivities(Intent[], Bundle)} |
| 4530 | * @see #startActivityForResult |
| 4531 | */ |
| 4532 | @Override |
| 4533 | public void startActivities(Intent[] intents) { |
| 4534 | startActivities(intents, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | /** |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4538 | * Launch a new activity. You will not receive any information about when |
| 4539 | * the activity exits. This implementation overrides the base version, |
| 4540 | * providing information about |
| 4541 | * the activity performing the launch. Because of this additional |
| 4542 | * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not |
| 4543 | * required; if not specified, the new activity will be added to the |
| 4544 | * task of the caller. |
| 4545 | * |
| 4546 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4547 | * if there was no Activity found to run the given Intent. |
| 4548 | * |
| 4549 | * @param intents The intents to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4550 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4551 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4552 | * Context.startActivity(Intent, Bundle)} for more details. |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4553 | * |
| 4554 | * @throws android.content.ActivityNotFoundException |
| 4555 | * |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4556 | * @see {@link #startActivities(Intent[])} |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4557 | * @see #startActivityForResult |
| 4558 | */ |
| 4559 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4560 | public void startActivities(Intent[] intents, @Nullable Bundle options) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4561 | mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(), |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4562 | mToken, this, intents, options); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4566 | * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)} |
| 4567 | * with no options. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4568 | * |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4569 | * @param intent The IntentSender to launch. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4570 | * @param fillInIntent If non-null, this will be provided as the |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4571 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4572 | * @param flagsMask Intent flags in the original IntentSender that you |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4573 | * would like to change. |
| 4574 | * @param flagsValues Desired values for any bits set in |
| 4575 | * <var>flagsMask</var> |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4576 | * @param extraFlags Always set to 0. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4577 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4578 | public void startIntentSender(IntentSender intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4579 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4580 | throws IntentSender.SendIntentException { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4581 | startIntentSender(intent, fillInIntent, flagsMask, flagsValues, |
| 4582 | extraFlags, null); |
| 4583 | } |
| 4584 | |
| 4585 | /** |
| 4586 | * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender |
| 4587 | * to start; see |
| 4588 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)} |
| 4589 | * for more information. |
| 4590 | * |
| 4591 | * @param intent The IntentSender to launch. |
| 4592 | * @param fillInIntent If non-null, this will be provided as the |
| 4593 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4594 | * @param flagsMask Intent flags in the original IntentSender that you |
| 4595 | * would like to change. |
| 4596 | * @param flagsValues Desired values for any bits set in |
| 4597 | * <var>flagsMask</var> |
| 4598 | * @param extraFlags Always set to 0. |
| 4599 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4600 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4601 | * Context.startActivity(Intent, Bundle)} for more details. If options |
| 4602 | * have also been supplied by the IntentSender, options given here will |
| 4603 | * override any that conflict with those given by the IntentSender. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4604 | */ |
| 4605 | public void startIntentSender(IntentSender intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4606 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4607 | Bundle options) throws IntentSender.SendIntentException { |
| 4608 | if (options != null) { |
| 4609 | startIntentSenderForResult(intent, -1, fillInIntent, flagsMask, |
| 4610 | flagsValues, extraFlags, options); |
| 4611 | } else { |
| 4612 | // Note we want to go through this call for compatibility with |
| 4613 | // applications that may have overridden the method. |
| 4614 | startIntentSenderForResult(intent, -1, fillInIntent, flagsMask, |
| 4615 | flagsValues, extraFlags); |
| 4616 | } |
| 4617 | } |
| 4618 | |
| 4619 | /** |
| 4620 | * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)} |
| 4621 | * with no options. |
| 4622 | * |
| 4623 | * @param intent The intent to start. |
| 4624 | * @param requestCode If >= 0, this code will be returned in |
| 4625 | * onActivityResult() when the activity exits, as described in |
| 4626 | * {@link #startActivityForResult}. |
| 4627 | * |
| 4628 | * @return If a new activity was launched then true is returned; otherwise |
| 4629 | * false is returned and you must handle the Intent yourself. |
| 4630 | * |
| 4631 | * @see #startActivity |
| 4632 | * @see #startActivityForResult |
| 4633 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4634 | public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent, |
| 4635 | int requestCode) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4636 | return startActivityIfNeeded(intent, requestCode, null); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4640 | * A special variation to launch an activity only if a new activity |
| 4641 | * instance is needed to handle the given Intent. In other words, this is |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4642 | * 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] | 4643 | * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4644 | * singleTask or singleTop |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4645 | * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode}, |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4646 | * and the activity |
| 4647 | * that handles <var>intent</var> is the same as your currently running |
| 4648 | * activity, then a new instance is not needed. In this case, instead of |
| 4649 | * the normal behavior of calling {@link #onNewIntent} this function will |
| 4650 | * return and you can handle the Intent yourself. |
| 4651 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4652 | * <p>This function can only be called from a top-level activity; if it is |
| 4653 | * called from a child activity, a runtime exception will be thrown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4654 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4655 | * @param intent The intent to start. |
| 4656 | * @param requestCode If >= 0, this code will be returned in |
| 4657 | * onActivityResult() when the activity exits, as described in |
| 4658 | * {@link #startActivityForResult}. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4659 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4660 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4661 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4662 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4663 | * @return If a new activity was launched then true is returned; otherwise |
| 4664 | * false is returned and you must handle the Intent yourself. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4665 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4666 | * @see #startActivity |
| 4667 | * @see #startActivityForResult |
| 4668 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4669 | public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent, |
| 4670 | int requestCode, @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4671 | if (mParent == null) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4672 | int result = ActivityManager.START_RETURN_INTENT_TO_CALLER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4673 | try { |
Dianne Hackborn | a3acdb3 | 2015-06-08 17:07:40 -0700 | [diff] [blame] | 4674 | Uri referrer = onProvideReferrer(); |
| 4675 | if (referrer != null) { |
| 4676 | intent.putExtra(Intent.EXTRA_REFERRER, referrer); |
| 4677 | } |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4678 | intent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 4679 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4680 | result = ActivityManagerNative.getDefault() |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 4681 | .startActivity(mMainThread.getApplicationThread(), getBasePackageName(), |
Jeff Hao | 1b012d3 | 2014-08-20 10:35:34 -0700 | [diff] [blame] | 4682 | intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken, |
| 4683 | mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED, |
| 4684 | null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4685 | } catch (RemoteException e) { |
| 4686 | // Empty |
| 4687 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4688 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4689 | Instrumentation.checkStartActivityResult(result, intent); |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4690 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4691 | if (requestCode >= 0) { |
| 4692 | // If this start is requesting a result, we can avoid making |
| 4693 | // the activity visible until the result is received. Setting |
| 4694 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4695 | // activity hidden during this time, to avoid flickering. |
| 4696 | // This can only be done when a result is requested because |
| 4697 | // that guarantees we will get information back when the |
| 4698 | // activity is finished, no matter what happens to it. |
| 4699 | mStartedActivity = true; |
| 4700 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4701 | return result != ActivityManager.START_RETURN_INTENT_TO_CALLER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | throw new UnsupportedOperationException( |
| 4705 | "startActivityIfNeeded can only be called from a top-level activity"); |
| 4706 | } |
| 4707 | |
| 4708 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4709 | * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with |
| 4710 | * no options. |
| 4711 | * |
| 4712 | * @param intent The intent to dispatch to the next activity. For |
| 4713 | * correct behavior, this must be the same as the Intent that started |
| 4714 | * your own activity; the only changes you can make are to the extras |
| 4715 | * inside of it. |
| 4716 | * |
| 4717 | * @return Returns a boolean indicating whether there was another Activity |
| 4718 | * to start: true if there was a next activity to start, false if there |
| 4719 | * wasn't. In general, if true is returned you will then want to call |
| 4720 | * finish() on yourself. |
| 4721 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4722 | public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4723 | return startNextMatchingActivity(intent, null); |
| 4724 | } |
| 4725 | |
| 4726 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4727 | * Special version of starting an activity, for use when you are replacing |
| 4728 | * other activity components. You can use this to hand the Intent off |
| 4729 | * to the next Activity that can handle it. You typically call this in |
| 4730 | * {@link #onCreate} with the Intent returned by {@link #getIntent}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4731 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4732 | * @param intent The intent to dispatch to the next activity. For |
| 4733 | * correct behavior, this must be the same as the Intent that started |
| 4734 | * your own activity; the only changes you can make are to the extras |
| 4735 | * inside of it. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4736 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4737 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4738 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4739 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4740 | * @return Returns a boolean indicating whether there was another Activity |
| 4741 | * to start: true if there was a next activity to start, false if there |
| 4742 | * wasn't. In general, if true is returned you will then want to call |
| 4743 | * finish() on yourself. |
| 4744 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4745 | public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent, |
| 4746 | @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4747 | if (mParent == null) { |
| 4748 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4749 | intent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 4750 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4751 | return ActivityManagerNative.getDefault() |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4752 | .startNextMatchingActivity(mToken, intent, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4753 | } catch (RemoteException e) { |
| 4754 | // Empty |
| 4755 | } |
| 4756 | return false; |
| 4757 | } |
| 4758 | |
| 4759 | throw new UnsupportedOperationException( |
| 4760 | "startNextMatchingActivity can only be called from a top-level activity"); |
| 4761 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4762 | |
| 4763 | /** |
| 4764 | * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)} |
| 4765 | * with no options. |
| 4766 | * |
| 4767 | * @param child The activity making the call. |
| 4768 | * @param intent The intent to start. |
| 4769 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4770 | * |
| 4771 | * @throws android.content.ActivityNotFoundException |
| 4772 | * |
| 4773 | * @see #startActivity |
| 4774 | * @see #startActivityForResult |
| 4775 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4776 | public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4777 | int requestCode) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 4778 | startActivityFromChild(child, intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4779 | } |
| 4780 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4781 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4782 | * 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] | 4783 | * {@link #startActivity} or {@link #startActivityForResult} method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4784 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4785 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4786 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4787 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4788 | * @param child The activity making the call. |
| 4789 | * @param intent The intent to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4790 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4791 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4792 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4793 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4794 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4795 | * @throws android.content.ActivityNotFoundException |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4796 | * |
| 4797 | * @see #startActivity |
| 4798 | * @see #startActivityForResult |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4799 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4800 | public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4801 | int requestCode, @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4802 | Instrumentation.ActivityResult ar = |
| 4803 | mInstrumentation.execStartActivity( |
| 4804 | this, mMainThread.getApplicationThread(), mToken, child, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4805 | intent, requestCode, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4806 | if (ar != null) { |
| 4807 | mMainThread.sendActivityResult( |
| 4808 | mToken, child.mEmbeddedID, requestCode, |
| 4809 | ar.getResultCode(), ar.getResultData()); |
| 4810 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4811 | cancelInputsAndStartExitTransition(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4815 | * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)} |
| 4816 | * with no options. |
| 4817 | * |
| 4818 | * @param fragment The fragment making the call. |
| 4819 | * @param intent The intent to start. |
| 4820 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4821 | * |
| 4822 | * @throws android.content.ActivityNotFoundException |
| 4823 | * |
| 4824 | * @see Fragment#startActivity |
| 4825 | * @see Fragment#startActivityForResult |
| 4826 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4827 | public void startActivityFromFragment(@NonNull Fragment fragment, |
| 4828 | @RequiresPermission Intent intent, int requestCode) { |
George Mount | abb63cb | 2014-06-23 11:17:58 -0700 | [diff] [blame] | 4829 | startActivityFromFragment(fragment, intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4833 | * This is called when a Fragment in this activity calls its |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4834 | * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult} |
| 4835 | * method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4836 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4837 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4838 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4839 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4840 | * @param fragment The fragment making the call. |
| 4841 | * @param intent The intent to start. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4842 | * @param requestCode Reply request code. < 0 if reply is not requested. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4843 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4844 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4845 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4846 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4847 | * @throws android.content.ActivityNotFoundException |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4848 | * |
| 4849 | * @see Fragment#startActivity |
| 4850 | * @see Fragment#startActivityForResult |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4851 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4852 | public void startActivityFromFragment(@NonNull Fragment fragment, |
| 4853 | @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) { |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4854 | startActivityForResult(fragment.mWho, intent, requestCode, options); |
| 4855 | } |
| 4856 | |
| 4857 | /** |
| 4858 | * @hide |
| 4859 | */ |
| 4860 | @Override |
| 4861 | public void startActivityForResult( |
| 4862 | String who, Intent intent, int requestCode, @Nullable Bundle options) { |
Dianne Hackborn | a3acdb3 | 2015-06-08 17:07:40 -0700 | [diff] [blame] | 4863 | Uri referrer = onProvideReferrer(); |
| 4864 | if (referrer != null) { |
| 4865 | intent.putExtra(Intent.EXTRA_REFERRER, referrer); |
| 4866 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4867 | Instrumentation.ActivityResult ar = |
| 4868 | mInstrumentation.execStartActivity( |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4869 | this, mMainThread.getApplicationThread(), mToken, who, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4870 | intent, requestCode, options); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4871 | if (ar != null) { |
| 4872 | mMainThread.sendActivityResult( |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4873 | mToken, who, requestCode, |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4874 | ar.getResultCode(), ar.getResultData()); |
| 4875 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4876 | cancelInputsAndStartExitTransition(options); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | /** |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4880 | * @hide |
| 4881 | */ |
| 4882 | @Override |
| 4883 | public boolean canStartActivityForResult() { |
| 4884 | return true; |
| 4885 | } |
| 4886 | |
| 4887 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4888 | * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender, |
| 4889 | * int, Intent, int, int, int, Bundle)} with no options. |
| 4890 | */ |
| 4891 | public void startIntentSenderFromChild(Activity child, IntentSender intent, |
| 4892 | int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, |
| 4893 | int extraFlags) |
| 4894 | throws IntentSender.SendIntentException { |
| 4895 | startIntentSenderFromChild(child, intent, requestCode, fillInIntent, |
| 4896 | flagsMask, flagsValues, extraFlags, null); |
| 4897 | } |
| 4898 | |
| 4899 | /** |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4900 | * Like {@link #startActivityFromChild(Activity, Intent, int)}, but |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4901 | * taking a IntentSender; see |
Dianne Hackborn | ae22c05 | 2009-09-17 18:46:22 -0700 | [diff] [blame] | 4902 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)} |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4903 | * for more information. |
| 4904 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4905 | public void startIntentSenderFromChild(Activity child, IntentSender intent, |
| 4906 | int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4907 | int extraFlags, @Nullable Bundle options) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4908 | throws IntentSender.SendIntentException { |
| 4909 | startIntentSenderForResultInner(intent, requestCode, fillInIntent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4910 | flagsMask, flagsValues, child, options); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | /** |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4914 | * Call immediately after one of the flavors of {@link #startActivity(Intent)} |
| 4915 | * or {@link #finish} to specify an explicit transition animation to |
| 4916 | * perform next. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4917 | * |
| 4918 | * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative |
| 4919 | * to using this with starting activities is to supply the desired animation |
| 4920 | * information through a {@link ActivityOptions} bundle to |
| 4921 | * {@link #startActivity(Intent, Bundle) or a related function. This allows |
| 4922 | * you to specify a custom animation even when starting an activity from |
| 4923 | * outside the context of the current top activity. |
| 4924 | * |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4925 | * @param enterAnim A resource ID of the animation resource to use for |
Dianne Hackborn | 8b571a8 | 2009-09-25 16:09:43 -0700 | [diff] [blame] | 4926 | * the incoming activity. Use 0 for no animation. |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4927 | * @param exitAnim A resource ID of the animation resource to use for |
Dianne Hackborn | 8b571a8 | 2009-09-25 16:09:43 -0700 | [diff] [blame] | 4928 | * the outgoing activity. Use 0 for no animation. |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4929 | */ |
| 4930 | public void overridePendingTransition(int enterAnim, int exitAnim) { |
| 4931 | try { |
| 4932 | ActivityManagerNative.getDefault().overridePendingTransition( |
| 4933 | mToken, getPackageName(), enterAnim, exitAnim); |
| 4934 | } catch (RemoteException e) { |
| 4935 | } |
| 4936 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4937 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4938 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4939 | * Call this to set the result that your activity will return to its |
| 4940 | * caller. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4941 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4942 | * @param resultCode The result code to propagate back to the originating |
| 4943 | * activity, often RESULT_CANCELED or RESULT_OK |
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 | * @see #RESULT_CANCELED |
| 4946 | * @see #RESULT_OK |
| 4947 | * @see #RESULT_FIRST_USER |
| 4948 | * @see #setResult(int, Intent) |
| 4949 | */ |
| 4950 | public final void setResult(int resultCode) { |
| 4951 | synchronized (this) { |
| 4952 | mResultCode = resultCode; |
| 4953 | mResultData = null; |
| 4954 | } |
| 4955 | } |
| 4956 | |
| 4957 | /** |
| 4958 | * Call this to set the result that your activity will return to its |
| 4959 | * caller. |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4960 | * |
| 4961 | * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent |
| 4962 | * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION |
| 4963 | * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION |
| 4964 | * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the |
| 4965 | * Activity receiving the result access to the specific URIs in the Intent. |
| 4966 | * Access will remain until the Activity has finished (it will remain across the hosting |
| 4967 | * process being killed and other temporary destruction) and will be added |
| 4968 | * to any existing set of URI permissions it already holds. |
| 4969 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4970 | * @param resultCode The result code to propagate back to the originating |
| 4971 | * activity, often RESULT_CANCELED or RESULT_OK |
| 4972 | * @param data The data to propagate back to the originating activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4973 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4974 | * @see #RESULT_CANCELED |
| 4975 | * @see #RESULT_OK |
| 4976 | * @see #RESULT_FIRST_USER |
| 4977 | * @see #setResult(int) |
| 4978 | */ |
| 4979 | public final void setResult(int resultCode, Intent data) { |
| 4980 | synchronized (this) { |
| 4981 | mResultCode = resultCode; |
| 4982 | mResultData = data; |
| 4983 | } |
| 4984 | } |
| 4985 | |
| 4986 | /** |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 4987 | * Return information about who launched this activity. If the launching Intent |
| 4988 | * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER}, |
| 4989 | * that will be returned as-is; otherwise, if known, an |
| 4990 | * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the |
| 4991 | * package name that started the Intent will be returned. This may return null if no |
| 4992 | * referrer can be identified -- it is neither explicitly specified, nor is it known which |
| 4993 | * application package was involved. |
| 4994 | * |
| 4995 | * <p>If called while inside the handling of {@link #onNewIntent}, this function will |
| 4996 | * return the referrer that submitted that new intent to the activity. Otherwise, it |
| 4997 | * always returns the referrer of the original Intent.</p> |
| 4998 | * |
| 4999 | * <p>Note that this is <em>not</em> a security feature -- you can not trust the |
| 5000 | * referrer information, applications can spoof it.</p> |
| 5001 | */ |
| 5002 | @Nullable |
| 5003 | public Uri getReferrer() { |
| 5004 | Intent intent = getIntent(); |
| 5005 | Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER); |
| 5006 | if (referrer != null) { |
| 5007 | return referrer; |
| 5008 | } |
| 5009 | String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME); |
| 5010 | if (referrerName != null) { |
| 5011 | return Uri.parse(referrerName); |
| 5012 | } |
| 5013 | if (mReferrer != null) { |
| 5014 | return new Uri.Builder().scheme("android-app").authority(mReferrer).build(); |
| 5015 | } |
| 5016 | return null; |
| 5017 | } |
| 5018 | |
| 5019 | /** |
Dianne Hackborn | a3acdb3 | 2015-06-08 17:07:40 -0700 | [diff] [blame] | 5020 | * Override to generate the desired referrer for the content currently being shown |
| 5021 | * by the app. The default implementation returns null, meaning the referrer will simply |
| 5022 | * be the android-app: of the package name of this activity. Return a non-null Uri to |
| 5023 | * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it. |
| 5024 | */ |
| 5025 | public Uri onProvideReferrer() { |
| 5026 | return null; |
| 5027 | } |
| 5028 | |
| 5029 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5030 | * Return the name of the package that invoked this activity. This is who |
| 5031 | * the data in {@link #setResult setResult()} will be sent to. You can |
| 5032 | * use this information to validate that the recipient is allowed to |
| 5033 | * receive the data. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5034 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5035 | * <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] | 5036 | * did not use the {@link #startActivityForResult} |
| 5037 | * form that includes a request code), then the calling package will be |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5038 | * null.</p> |
| 5039 | * |
| 5040 | * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
| 5041 | * the result from this method was unstable. If the process hosting the calling |
| 5042 | * package was no longer running, it would return null instead of the proper package |
| 5043 | * name. You can use {@link #getCallingActivity()} and retrieve the package name |
| 5044 | * from that instead.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5045 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5046 | * @return The package of the activity that will receive your |
| 5047 | * reply, or null if none. |
| 5048 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5049 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5050 | public String getCallingPackage() { |
| 5051 | try { |
| 5052 | return ActivityManagerNative.getDefault().getCallingPackage(mToken); |
| 5053 | } catch (RemoteException e) { |
| 5054 | return null; |
| 5055 | } |
| 5056 | } |
| 5057 | |
| 5058 | /** |
| 5059 | * Return the name of the activity that invoked this activity. This is |
| 5060 | * who the data in {@link #setResult setResult()} will be sent to. You |
| 5061 | * can use this information to validate that the recipient is allowed to |
| 5062 | * receive the data. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5063 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5064 | * <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] | 5065 | * did not use the {@link #startActivityForResult} |
| 5066 | * form that includes a request code), then the calling package will be |
| 5067 | * null. |
| 5068 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5069 | * @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] | 5070 | * reply, or null if none. |
| 5071 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5072 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5073 | public ComponentName getCallingActivity() { |
| 5074 | try { |
| 5075 | return ActivityManagerNative.getDefault().getCallingActivity(mToken); |
| 5076 | } catch (RemoteException e) { |
| 5077 | return null; |
| 5078 | } |
| 5079 | } |
| 5080 | |
| 5081 | /** |
| 5082 | * Control whether this activity's main window is visible. This is intended |
| 5083 | * only for the special case of an activity that is not going to show a |
| 5084 | * UI itself, but can't just finish prior to onResume() because it needs |
| 5085 | * to wait for a service binding or such. Setting this to false allows |
| 5086 | * you to prevent your UI from being shown during that time. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5087 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5088 | * <p>The default value for this is taken from the |
| 5089 | * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme. |
| 5090 | */ |
| 5091 | public void setVisible(boolean visible) { |
| 5092 | if (mVisibleFromClient != visible) { |
| 5093 | mVisibleFromClient = visible; |
| 5094 | if (mVisibleFromServer) { |
| 5095 | if (visible) makeVisible(); |
| 5096 | else mDecor.setVisibility(View.INVISIBLE); |
| 5097 | } |
| 5098 | } |
| 5099 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5101 | void makeVisible() { |
| 5102 | if (!mWindowAdded) { |
| 5103 | ViewManager wm = getWindowManager(); |
| 5104 | wm.addView(mDecor, getWindow().getAttributes()); |
| 5105 | mWindowAdded = true; |
| 5106 | } |
| 5107 | mDecor.setVisibility(View.VISIBLE); |
| 5108 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5110 | /** |
| 5111 | * Check to see whether this activity is in the process of finishing, |
| 5112 | * either because you called {@link #finish} on it or someone else |
| 5113 | * has requested that it finished. This is often used in |
| 5114 | * {@link #onPause} to determine whether the activity is simply pausing or |
| 5115 | * completely finishing. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5116 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5117 | * @return If the activity is finishing, returns true; else returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5118 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5119 | * @see #finish |
| 5120 | */ |
| 5121 | public boolean isFinishing() { |
| 5122 | return mFinished; |
| 5123 | } |
| 5124 | |
| 5125 | /** |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 5126 | * Returns true if the final {@link #onDestroy()} call has been made |
| 5127 | * on the Activity, so this instance is now dead. |
| 5128 | */ |
| 5129 | public boolean isDestroyed() { |
| 5130 | return mDestroyed; |
| 5131 | } |
| 5132 | |
| 5133 | /** |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 5134 | * Check to see whether this activity is in the process of being destroyed in order to be |
| 5135 | * recreated with a new configuration. This is often used in |
| 5136 | * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed |
| 5137 | * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5138 | * |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 5139 | * @return If the activity is being torn down in order to be recreated with a new configuration, |
| 5140 | * returns true; else returns false. |
| 5141 | */ |
| 5142 | public boolean isChangingConfigurations() { |
| 5143 | return mChangingConfigurations; |
| 5144 | } |
| 5145 | |
| 5146 | /** |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 5147 | * Cause this Activity to be recreated with a new instance. This results |
| 5148 | * in essentially the same flow as when the Activity is created due to |
| 5149 | * a configuration change -- the current instance will go through its |
| 5150 | * lifecycle to {@link #onDestroy} and a new instance then created after it. |
| 5151 | */ |
| 5152 | public void recreate() { |
| 5153 | if (mParent != null) { |
| 5154 | throw new IllegalStateException("Can only be called on top-level activity"); |
| 5155 | } |
| 5156 | if (Looper.myLooper() != mMainThread.getLooper()) { |
| 5157 | throw new IllegalStateException("Must be called from main thread"); |
| 5158 | } |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 5159 | mMainThread.requestRelaunchActivity(mToken, null, null, 0, false, null, null, false, |
| 5160 | false /* preserveWindow */); |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 5161 | } |
| 5162 | |
| 5163 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5164 | * Finishes the current activity and specifies whether to remove the task associated with this |
| 5165 | * activity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5166 | */ |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5167 | private void finish(int finishTask) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5168 | if (mParent == null) { |
| 5169 | int resultCode; |
| 5170 | Intent resultData; |
| 5171 | synchronized (this) { |
| 5172 | resultCode = mResultCode; |
| 5173 | resultData = mResultData; |
| 5174 | } |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 5175 | if (false) Log.v(TAG, "Finishing self: token=" + mToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5176 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 5177 | if (resultData != null) { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 5178 | resultData.prepareToLeaveProcess(this); |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 5179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5180 | if (ActivityManagerNative.getDefault() |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5181 | .finishActivity(mToken, resultCode, resultData, finishTask)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5182 | mFinished = true; |
| 5183 | } |
| 5184 | } catch (RemoteException e) { |
| 5185 | // Empty |
| 5186 | } |
| 5187 | } else { |
| 5188 | mParent.finishFromChild(this); |
| 5189 | } |
| 5190 | } |
| 5191 | |
| 5192 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5193 | * Call this when your activity is done and should be closed. The |
| 5194 | * ActivityResult is propagated back to whoever launched you via |
| 5195 | * onActivityResult(). |
| 5196 | */ |
| 5197 | public void finish() { |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5198 | finish(DONT_FINISH_TASK_WITH_ACTIVITY); |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | /** |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 5202 | * Finish this activity as well as all activities immediately below it |
| 5203 | * in the current task that have the same affinity. This is typically |
| 5204 | * used when an application can be launched on to another task (such as |
| 5205 | * from an ACTION_VIEW of a content type it understands) and the user |
| 5206 | * has used the up navigation to switch out of the current task and in |
| 5207 | * to its own task. In this case, if the user has navigated down into |
| 5208 | * any other activities of the second application, all of those should |
| 5209 | * be removed from the original task as part of the task switch. |
| 5210 | * |
| 5211 | * <p>Note that this finish does <em>not</em> allow you to deliver results |
| 5212 | * to the previous activity, and an exception will be thrown if you are trying |
| 5213 | * to do so.</p> |
| 5214 | */ |
| 5215 | public void finishAffinity() { |
| 5216 | if (mParent != null) { |
| 5217 | throw new IllegalStateException("Can not be called from an embedded activity"); |
| 5218 | } |
| 5219 | if (mResultCode != RESULT_CANCELED || mResultData != null) { |
| 5220 | throw new IllegalStateException("Can not be called to deliver a result"); |
| 5221 | } |
| 5222 | try { |
| 5223 | if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) { |
| 5224 | mFinished = true; |
| 5225 | } |
| 5226 | } catch (RemoteException e) { |
| 5227 | // Empty |
| 5228 | } |
| 5229 | } |
| 5230 | |
| 5231 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5232 | * 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] | 5233 | * {@link #finish} method. The default implementation simply calls |
| 5234 | * finish() on this activity (the parent), finishing the entire group. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5235 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5236 | * @param child The activity making the call. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5237 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5238 | * @see #finish |
| 5239 | */ |
| 5240 | public void finishFromChild(Activity child) { |
| 5241 | finish(); |
| 5242 | } |
| 5243 | |
| 5244 | /** |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 5245 | * Reverses the Activity Scene entry Transition and triggers the calling Activity |
| 5246 | * to reverse its exit Transition. When the exit Transition completes, |
| 5247 | * {@link #finish()} is called. If no entry Transition was used, finish() is called |
| 5248 | * immediately and the Activity exit Transition is run. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5249 | * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[]) |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 5250 | */ |
Craig Mautner | 73f843d | 2014-05-19 09:42:28 -0700 | [diff] [blame] | 5251 | public void finishAfterTransition() { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5252 | if (!mActivityTransitionState.startExitBackTransition(this)) { |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 5253 | finish(); |
| 5254 | } |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5258 | * Force finish another activity that you had previously started with |
| 5259 | * {@link #startActivityForResult}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5260 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5261 | * @param requestCode The request code of the activity that you had |
| 5262 | * given to startActivityForResult(). If there are multiple |
| 5263 | * activities started with this request code, they |
| 5264 | * will all be finished. |
| 5265 | */ |
| 5266 | public void finishActivity(int requestCode) { |
| 5267 | if (mParent == null) { |
| 5268 | try { |
| 5269 | ActivityManagerNative.getDefault() |
| 5270 | .finishSubActivity(mToken, mEmbeddedID, requestCode); |
| 5271 | } catch (RemoteException e) { |
| 5272 | // Empty |
| 5273 | } |
| 5274 | } else { |
| 5275 | mParent.finishActivityFromChild(this, requestCode); |
| 5276 | } |
| 5277 | } |
| 5278 | |
| 5279 | /** |
| 5280 | * This is called when a child activity of this one calls its |
| 5281 | * finishActivity(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5282 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5283 | * @param child The activity making the call. |
| 5284 | * @param requestCode Request code that had been used to start the |
| 5285 | * activity. |
| 5286 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5287 | public void finishActivityFromChild(@NonNull Activity child, int requestCode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5288 | try { |
| 5289 | ActivityManagerNative.getDefault() |
| 5290 | .finishSubActivity(mToken, child.mEmbeddedID, requestCode); |
| 5291 | } catch (RemoteException e) { |
| 5292 | // Empty |
| 5293 | } |
| 5294 | } |
| 5295 | |
| 5296 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5297 | * Call this when your activity is done and should be closed and the task should be completely |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5298 | * removed as a part of finishing the root activity of the task. |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5299 | */ |
| 5300 | public void finishAndRemoveTask() { |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5301 | finish(FINISH_TASK_WITH_ROOT_ACTIVITY); |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5302 | } |
| 5303 | |
| 5304 | /** |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 5305 | * Ask that the local app instance of this activity be released to free up its memory. |
| 5306 | * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity -- |
| 5307 | * a new instance of the activity will later be re-created if needed due to the user |
| 5308 | * navigating back to it. |
| 5309 | * |
| 5310 | * @return Returns true if the activity was in a state that it has started the process |
| 5311 | * of destroying its current instance; returns false if for any reason this could not |
| 5312 | * be done: it is currently visible to the user, it is already being destroyed, it is |
| 5313 | * being finished, it hasn't yet saved its state, etc. |
| 5314 | */ |
| 5315 | public boolean releaseInstance() { |
| 5316 | try { |
| 5317 | return ActivityManagerNative.getDefault().releaseActivityInstance(mToken); |
| 5318 | } catch (RemoteException e) { |
| 5319 | // Empty |
| 5320 | } |
| 5321 | return false; |
| 5322 | } |
| 5323 | |
| 5324 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5325 | * Called when an activity you launched exits, giving you the requestCode |
| 5326 | * you started it with, the resultCode it returned, and any additional |
| 5327 | * data from it. The <var>resultCode</var> will be |
| 5328 | * {@link #RESULT_CANCELED} if the activity explicitly returned that, |
| 5329 | * didn't return any result, or crashed during its operation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5330 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5331 | * <p>You will receive this call immediately before onResume() when your |
| 5332 | * activity is re-starting. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5333 | * |
Ricardo Cervera | 92f6a74 | 2014-04-04 11:17:06 -0700 | [diff] [blame] | 5334 | * <p>This method is never invoked if your activity sets |
| 5335 | * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to |
| 5336 | * <code>true</code>. |
Ricardo Cervera | 93f94c2 | 2015-01-16 09:54:30 -0800 | [diff] [blame] | 5337 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5338 | * @param requestCode The integer request code originally supplied to |
| 5339 | * startActivityForResult(), allowing you to identify who this |
| 5340 | * result came from. |
| 5341 | * @param resultCode The integer result code returned by the child activity |
| 5342 | * through its setResult(). |
| 5343 | * @param data An Intent, which can return result data to the caller |
| 5344 | * (various data can be attached to Intent "extras"). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5345 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5346 | * @see #startActivityForResult |
| 5347 | * @see #createPendingResult |
| 5348 | * @see #setResult(int) |
| 5349 | */ |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 5350 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5351 | } |
| 5352 | |
| 5353 | /** |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5354 | * Called when an activity you launched with an activity transition exposes this |
| 5355 | * Activity through a returning activity transition, giving you the resultCode |
| 5356 | * and any additional data from it. This method will only be called if the activity |
| 5357 | * 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] | 5358 | * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5359 | * |
| 5360 | * <p>The purpose of this function is to let the called Activity send a hint about |
| 5361 | * its state so that this underlying Activity can prepare to be exposed. A call to |
| 5362 | * this method does not guarantee that the called Activity has or will be exiting soon. |
| 5363 | * It only indicates that it will expose this Activity's Window and it has |
| 5364 | * some data to pass to prepare it.</p> |
| 5365 | * |
| 5366 | * @param resultCode The integer result code returned by the child activity |
| 5367 | * through its setResult(). |
| 5368 | * @param data An Intent, which can return result data to the caller |
| 5369 | * (various data can be attached to Intent "extras"). |
| 5370 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 5371 | public void onActivityReenter(int resultCode, Intent data) { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5372 | } |
| 5373 | |
| 5374 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5375 | * Create a new PendingIntent object which you can hand to others |
| 5376 | * for them to use to send result data back to your |
| 5377 | * {@link #onActivityResult} callback. The created object will be either |
| 5378 | * one-shot (becoming invalid after a result is sent back) or multiple |
| 5379 | * (allowing any number of results to be sent through it). |
| 5380 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5381 | * @param requestCode Private request code for the sender that will be |
| 5382 | * associated with the result data when it is returned. The sender can not |
| 5383 | * modify this value, allowing you to identify incoming results. |
| 5384 | * @param data Default data to supply in the result, which may be modified |
| 5385 | * by the sender. |
| 5386 | * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT}, |
| 5387 | * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE}, |
| 5388 | * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT}, |
| 5389 | * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT}, |
| 5390 | * or any of the flags as supported by |
| 5391 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 5392 | * of the intent that can be supplied when the actual send happens. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5393 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5394 | * @return Returns an existing or new PendingIntent matching the given |
| 5395 | * parameters. May return null only if |
| 5396 | * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been |
| 5397 | * supplied. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5398 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5399 | * @see PendingIntent |
| 5400 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5401 | public PendingIntent createPendingResult(int requestCode, @NonNull Intent data, |
| 5402 | @PendingIntent.Flags int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5403 | String packageName = getPackageName(); |
| 5404 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 5405 | data.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5406 | IIntentSender target = |
| 5407 | ActivityManagerNative.getDefault().getIntentSender( |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 5408 | ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5409 | mParent == null ? mToken : mParent.mToken, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 5410 | mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null, |
| 5411 | UserHandle.myUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5412 | return target != null ? new PendingIntent(target) : null; |
| 5413 | } catch (RemoteException e) { |
| 5414 | // Empty |
| 5415 | } |
| 5416 | return null; |
| 5417 | } |
| 5418 | |
| 5419 | /** |
| 5420 | * Change the desired orientation of this activity. If the activity |
| 5421 | * is currently in the foreground or otherwise impacting the screen |
| 5422 | * orientation, the screen will immediately be changed (possibly causing |
| 5423 | * the activity to be restarted). Otherwise, this will be used the next |
| 5424 | * time the activity is visible. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5425 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5426 | * @param requestedOrientation An orientation constant as used in |
| 5427 | * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}. |
| 5428 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5429 | public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5430 | if (mParent == null) { |
| 5431 | try { |
| 5432 | ActivityManagerNative.getDefault().setRequestedOrientation( |
| 5433 | mToken, requestedOrientation); |
| 5434 | } catch (RemoteException e) { |
| 5435 | // Empty |
| 5436 | } |
| 5437 | } else { |
| 5438 | mParent.setRequestedOrientation(requestedOrientation); |
| 5439 | } |
| 5440 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5442 | /** |
| 5443 | * Return the current requested orientation of the activity. This will |
| 5444 | * either be the orientation requested in its component's manifest, or |
| 5445 | * the last requested orientation given to |
| 5446 | * {@link #setRequestedOrientation(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5447 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5448 | * @return Returns an orientation constant as used in |
| 5449 | * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}. |
| 5450 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5451 | @ActivityInfo.ScreenOrientation |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5452 | public int getRequestedOrientation() { |
| 5453 | if (mParent == null) { |
| 5454 | try { |
| 5455 | return ActivityManagerNative.getDefault() |
| 5456 | .getRequestedOrientation(mToken); |
| 5457 | } catch (RemoteException e) { |
| 5458 | // Empty |
| 5459 | } |
| 5460 | } else { |
| 5461 | return mParent.getRequestedOrientation(); |
| 5462 | } |
| 5463 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 5464 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5465 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5466 | /** |
| 5467 | * Return the identifier of the task this activity is in. This identifier |
| 5468 | * will remain the same for the lifetime of the activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5469 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5470 | * @return Task identifier, an opaque integer. |
| 5471 | */ |
| 5472 | public int getTaskId() { |
| 5473 | try { |
| 5474 | return ActivityManagerNative.getDefault() |
| 5475 | .getTaskForActivity(mToken, false); |
| 5476 | } catch (RemoteException e) { |
| 5477 | return -1; |
| 5478 | } |
| 5479 | } |
| 5480 | |
| 5481 | /** |
| 5482 | * Return whether this activity is the root of a task. The root is the |
| 5483 | * first activity in a task. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5484 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5485 | * @return True if this is the root activity, else false. |
| 5486 | */ |
| 5487 | public boolean isTaskRoot() { |
| 5488 | try { |
Filip Gruszczynski | 3d02671 | 2015-12-16 13:46:38 -0800 | [diff] [blame] | 5489 | return ActivityManagerNative.getDefault().getTaskForActivity(mToken, true) >= 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5490 | } catch (RemoteException e) { |
| 5491 | return false; |
| 5492 | } |
| 5493 | } |
| 5494 | |
| 5495 | /** |
| 5496 | * Move the task containing this activity to the back of the activity |
| 5497 | * stack. The activity's order within the task is unchanged. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5498 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5499 | * @param nonRoot If false then this only works if the activity is the root |
| 5500 | * of a task; if true it will work for any activity in |
| 5501 | * a task. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5502 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5503 | * @return If the task was moved (or it was already at the |
| 5504 | * back) true is returned, else false. |
| 5505 | */ |
| 5506 | public boolean moveTaskToBack(boolean nonRoot) { |
| 5507 | try { |
| 5508 | return ActivityManagerNative.getDefault().moveActivityTaskToBack( |
| 5509 | mToken, nonRoot); |
| 5510 | } catch (RemoteException e) { |
| 5511 | // Empty |
| 5512 | } |
| 5513 | return false; |
| 5514 | } |
| 5515 | |
| 5516 | /** |
| 5517 | * Returns class name for this activity with the package prefix removed. |
| 5518 | * This is the default name used to read and write settings. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5519 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5520 | * @return The local class name. |
| 5521 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5522 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5523 | public String getLocalClassName() { |
| 5524 | final String pkg = getPackageName(); |
| 5525 | final String cls = mComponent.getClassName(); |
| 5526 | int packageLen = pkg.length(); |
| 5527 | if (!cls.startsWith(pkg) || cls.length() <= packageLen |
| 5528 | || cls.charAt(packageLen) != '.') { |
| 5529 | return cls; |
| 5530 | } |
| 5531 | return cls.substring(packageLen+1); |
| 5532 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5533 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5534 | /** |
| 5535 | * Returns complete component name of this activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5536 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5537 | * @return Returns the complete component name for this activity |
| 5538 | */ |
| 5539 | public ComponentName getComponentName() |
| 5540 | { |
| 5541 | return mComponent; |
| 5542 | } |
| 5543 | |
| 5544 | /** |
| 5545 | * Retrieve a {@link SharedPreferences} object for accessing preferences |
| 5546 | * that are private to this activity. This simply calls the underlying |
| 5547 | * {@link #getSharedPreferences(String, int)} method by passing in this activity's |
| 5548 | * class name as the preferences name. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5549 | * |
| 5550 | * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 5551 | * operation. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5552 | * |
| 5553 | * @return Returns the single SharedPreferences instance that can be used |
| 5554 | * to retrieve and modify the preference values. |
| 5555 | */ |
| 5556 | public SharedPreferences getPreferences(int mode) { |
| 5557 | return getSharedPreferences(getLocalClassName(), mode); |
| 5558 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5559 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5560 | private void ensureSearchManager() { |
| 5561 | if (mSearchManager != null) { |
| 5562 | return; |
| 5563 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5564 | |
Amith Yamasani | e9ce3f0 | 2010-01-25 09:15:50 -0800 | [diff] [blame] | 5565 | mSearchManager = new SearchManager(this, null); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5566 | } |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5567 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5568 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5569 | public Object getSystemService(@ServiceName @NonNull String name) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5570 | if (getBaseContext() == null) { |
| 5571 | throw new IllegalStateException( |
| 5572 | "System services not available to Activities before onCreate()"); |
| 5573 | } |
| 5574 | |
| 5575 | if (WINDOW_SERVICE.equals(name)) { |
| 5576 | return mWindowManager; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 5577 | } else if (SEARCH_SERVICE.equals(name)) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5578 | ensureSearchManager(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 5579 | return mSearchManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5580 | } |
| 5581 | return super.getSystemService(name); |
| 5582 | } |
| 5583 | |
| 5584 | /** |
| 5585 | * Change the title associated with this activity. If this is a |
| 5586 | * top-level activity, the title for its window will change. If it |
| 5587 | * is an embedded activity, the parent can do whatever it wants |
| 5588 | * with it. |
| 5589 | */ |
| 5590 | public void setTitle(CharSequence title) { |
| 5591 | mTitle = title; |
| 5592 | onTitleChanged(title, mTitleColor); |
| 5593 | |
| 5594 | if (mParent != null) { |
| 5595 | mParent.onChildTitleChanged(this, title); |
| 5596 | } |
| 5597 | } |
| 5598 | |
| 5599 | /** |
| 5600 | * Change the title associated with this activity. If this is a |
| 5601 | * top-level activity, the title for its window will change. If it |
| 5602 | * is an embedded activity, the parent can do whatever it wants |
| 5603 | * with it. |
| 5604 | */ |
| 5605 | public void setTitle(int titleId) { |
| 5606 | setTitle(getText(titleId)); |
| 5607 | } |
| 5608 | |
Alan Viverette | 2525d9c | 2013-11-15 14:42:19 -0800 | [diff] [blame] | 5609 | /** |
| 5610 | * Change the color of the title associated with this activity. |
| 5611 | * <p> |
| 5612 | * This method is deprecated starting in API Level 11 and replaced by action |
| 5613 | * bar styles. For information on styling the Action Bar, read the <a |
| 5614 | * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer |
| 5615 | * guide. |
| 5616 | * |
| 5617 | * @deprecated Use action bar styles instead. |
| 5618 | */ |
| 5619 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5620 | public void setTitleColor(int textColor) { |
| 5621 | mTitleColor = textColor; |
| 5622 | onTitleChanged(mTitle, textColor); |
| 5623 | } |
| 5624 | |
| 5625 | public final CharSequence getTitle() { |
| 5626 | return mTitle; |
| 5627 | } |
| 5628 | |
| 5629 | public final int getTitleColor() { |
| 5630 | return mTitleColor; |
| 5631 | } |
| 5632 | |
| 5633 | protected void onTitleChanged(CharSequence title, int color) { |
| 5634 | if (mTitleReady) { |
| 5635 | final Window win = getWindow(); |
| 5636 | if (win != null) { |
| 5637 | win.setTitle(title); |
| 5638 | if (color != 0) { |
| 5639 | win.setTitleColor(color); |
| 5640 | } |
| 5641 | } |
Adam Powell | af2d859 | 2014-08-26 18:06:40 -0700 | [diff] [blame] | 5642 | if (mActionBar != null) { |
| 5643 | mActionBar.setWindowTitle(title); |
| 5644 | } |
Adam Powell | a557fdc | 2014-08-21 18:05:53 -0700 | [diff] [blame] | 5645 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | protected void onChildTitleChanged(Activity childActivity, CharSequence title) { |
| 5649 | } |
| 5650 | |
| 5651 | /** |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5652 | * Sets information describing the task with this activity for presentation inside the Recents |
| 5653 | * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task |
| 5654 | * are traversed in order from the topmost activity to the bottommost. The traversal continues |
| 5655 | * for each property until a suitable value is found. For each task the taskDescription will be |
| 5656 | * returned in {@link android.app.ActivityManager.TaskDescription}. |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5657 | * |
| 5658 | * @see ActivityManager#getRecentTasks |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5659 | * @see android.app.ActivityManager.TaskDescription |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5660 | * |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5661 | * @param taskDescription The TaskDescription properties that describe the task with this activity |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5662 | */ |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5663 | public void setTaskDescription(ActivityManager.TaskDescription taskDescription) { |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 5664 | if (mTaskDescription != taskDescription) { |
| 5665 | mTaskDescription.copyFrom(taskDescription); |
| 5666 | // Scale the icon down to something reasonable if it is provided |
| 5667 | if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) { |
| 5668 | final int size = ActivityManager.getLauncherLargeIconSizeInner(this); |
| 5669 | final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size, |
| 5670 | true); |
| 5671 | mTaskDescription.setIcon(icon); |
| 5672 | } |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5673 | } |
| 5674 | try { |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 5675 | ActivityManagerNative.getDefault().setTaskDescription(mToken, mTaskDescription); |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5676 | } catch (RemoteException e) { |
| 5677 | } |
| 5678 | } |
| 5679 | |
| 5680 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5681 | * Sets the visibility of the progress bar in the title. |
| 5682 | * <p> |
| 5683 | * In order for the progress bar to be shown, the feature must be requested |
| 5684 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5685 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5686 | * @param visible Whether to show the progress bars in the title. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5687 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5688 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5689 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5690 | public final void setProgressBarVisibility(boolean visible) { |
| 5691 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON : |
| 5692 | Window.PROGRESS_VISIBILITY_OFF); |
| 5693 | } |
| 5694 | |
| 5695 | /** |
| 5696 | * Sets the visibility of the indeterminate progress bar in the title. |
| 5697 | * <p> |
| 5698 | * In order for the progress bar to be shown, the feature must be requested |
| 5699 | * via {@link #requestWindowFeature(int)}. |
| 5700 | * |
| 5701 | * @param visible Whether to show the progress bars in the title. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5702 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5703 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5704 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5705 | public final void setProgressBarIndeterminateVisibility(boolean visible) { |
| 5706 | getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, |
| 5707 | visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF); |
| 5708 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5709 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5710 | /** |
| 5711 | * Sets whether the horizontal progress bar in the title should be indeterminate (the circular |
| 5712 | * is always indeterminate). |
| 5713 | * <p> |
| 5714 | * In order for the progress bar to be shown, the feature must be requested |
| 5715 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5716 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5717 | * @param indeterminate Whether the horizontal progress bar should be indeterminate. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5718 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5719 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5720 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5721 | public final void setProgressBarIndeterminate(boolean indeterminate) { |
| 5722 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 5723 | indeterminate ? Window.PROGRESS_INDETERMINATE_ON |
| 5724 | : Window.PROGRESS_INDETERMINATE_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5725 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5726 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5727 | /** |
| 5728 | * Sets the progress for the progress bars in the title. |
| 5729 | * <p> |
| 5730 | * In order for the progress bar to be shown, the feature must be requested |
| 5731 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5732 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5733 | * @param progress The progress for the progress bar. Valid ranges are from |
| 5734 | * 0 to 10000 (both inclusive). If 10000 is given, the progress |
| 5735 | * bar will be completely filled and will fade out. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5736 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5737 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5738 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5739 | public final void setProgress(int progress) { |
| 5740 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START); |
| 5741 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5742 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5743 | /** |
| 5744 | * Sets the secondary progress for the progress bar in the title. This |
| 5745 | * progress is drawn between the primary progress (set via |
| 5746 | * {@link #setProgress(int)} and the background. It can be ideal for media |
| 5747 | * scenarios such as showing the buffering progress while the default |
| 5748 | * progress shows the play progress. |
| 5749 | * <p> |
| 5750 | * In order for the progress bar to be shown, the feature must be requested |
| 5751 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5752 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5753 | * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from |
| 5754 | * 0 to 10000 (both inclusive). |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5755 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5756 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5757 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5758 | public final void setSecondaryProgress(int secondaryProgress) { |
| 5759 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, |
| 5760 | secondaryProgress + Window.PROGRESS_SECONDARY_START); |
| 5761 | } |
| 5762 | |
| 5763 | /** |
| 5764 | * Suggests an audio stream whose volume should be changed by the hardware |
| 5765 | * volume controls. |
| 5766 | * <p> |
| 5767 | * The suggested audio stream will be tied to the window of this Activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5768 | * Volume requests which are received while the Activity is in the |
| 5769 | * foreground will affect this stream. |
| 5770 | * <p> |
| 5771 | * It is not guaranteed that the hardware volume controls will always change |
| 5772 | * this stream's volume (for example, if a call is in progress, its stream's |
| 5773 | * volume may be changed instead). To reset back to the default, use |
| 5774 | * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}. |
| 5775 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5776 | * @param streamType The type of the audio stream whose volume should be |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5777 | * changed by the hardware volume controls. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5778 | */ |
| 5779 | public final void setVolumeControlStream(int streamType) { |
| 5780 | getWindow().setVolumeControlStream(streamType); |
| 5781 | } |
| 5782 | |
| 5783 | /** |
| 5784 | * Gets the suggested audio stream whose volume should be changed by the |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5785 | * hardware volume controls. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5786 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5787 | * @return The suggested audio stream type whose volume should be changed by |
| 5788 | * the hardware volume controls. |
| 5789 | * @see #setVolumeControlStream(int) |
| 5790 | */ |
| 5791 | public final int getVolumeControlStream() { |
| 5792 | return getWindow().getVolumeControlStream(); |
| 5793 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5794 | |
| 5795 | /** |
| 5796 | * Sets a {@link MediaController} to send media keys and volume changes to. |
| 5797 | * <p> |
| 5798 | * The controller will be tied to the window of this Activity. Media key and |
| 5799 | * volume events which are received while the Activity is in the foreground |
| 5800 | * will be forwarded to the controller and used to invoke transport controls |
| 5801 | * or adjust the volume. This may be used instead of or in addition to |
| 5802 | * {@link #setVolumeControlStream} to affect a specific session instead of a |
| 5803 | * specific stream. |
| 5804 | * <p> |
| 5805 | * It is not guaranteed that the hardware volume controls will always change |
| 5806 | * this session's volume (for example, if a call is in progress, its |
| 5807 | * stream's volume may be changed instead). To reset back to the default use |
| 5808 | * null as the controller. |
| 5809 | * |
| 5810 | * @param controller The controller for the session which should receive |
| 5811 | * media keys and volume changes. |
| 5812 | */ |
| 5813 | public final void setMediaController(MediaController controller) { |
| 5814 | getWindow().setMediaController(controller); |
| 5815 | } |
| 5816 | |
| 5817 | /** |
| 5818 | * Gets the controller which should be receiving media key and volume events |
| 5819 | * while this activity is in the foreground. |
| 5820 | * |
| 5821 | * @return The controller which should receive events. |
| 5822 | * @see #setMediaController(android.media.session.MediaController) |
| 5823 | */ |
| 5824 | public final MediaController getMediaController() { |
| 5825 | return getWindow().getMediaController(); |
| 5826 | } |
| 5827 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5828 | /** |
| 5829 | * Runs the specified action on the UI thread. If the current thread is the UI |
| 5830 | * thread, then the action is executed immediately. If the current thread is |
| 5831 | * not the UI thread, the action is posted to the event queue of the UI thread. |
| 5832 | * |
| 5833 | * @param action the action to run on the UI thread |
| 5834 | */ |
| 5835 | public final void runOnUiThread(Runnable action) { |
| 5836 | if (Thread.currentThread() != mUiThread) { |
| 5837 | mHandler.post(action); |
| 5838 | } else { |
| 5839 | action.run(); |
| 5840 | } |
| 5841 | } |
| 5842 | |
| 5843 | /** |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5844 | * Standard implementation of |
| 5845 | * {@link android.view.LayoutInflater.Factory#onCreateView} used when |
| 5846 | * inflating with the LayoutInflater returned by {@link #getSystemService}. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5847 | * This implementation does nothing and is for |
| 5848 | * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps |
| 5849 | * should use {@link #onCreateView(View, String, Context, AttributeSet)}. |
| 5850 | * |
| 5851 | * @see android.view.LayoutInflater#createView |
| 5852 | * @see android.view.Window#getLayoutInflater |
| 5853 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5854 | @Nullable |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5855 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
| 5856 | return null; |
| 5857 | } |
| 5858 | |
| 5859 | /** |
| 5860 | * Standard implementation of |
| 5861 | * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)} |
| 5862 | * used when inflating with the LayoutInflater returned by {@link #getSystemService}. |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5863 | * This implementation handles <fragment> tags to embed fragments inside |
| 5864 | * of the activity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5865 | * |
| 5866 | * @see android.view.LayoutInflater#createView |
| 5867 | * @see android.view.Window#getLayoutInflater |
| 5868 | */ |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5869 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5870 | if (!"fragment".equals(name)) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5871 | return onCreateView(name, context, attrs); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5872 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5873 | |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 5874 | return mFragments.onCreateView(parent, name, context, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5875 | } |
| 5876 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5877 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5878 | * 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] | 5879 | * you run "adb shell dumpsys activity <activity_component_name>". |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5880 | * |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5881 | * @param prefix Desired prefix to prepend at each line of output. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5882 | * @param fd The raw file descriptor that the dump is being sent to. |
| 5883 | * @param writer The PrintWriter to which you should dump your state. This will be |
| 5884 | * closed for you after you return. |
| 5885 | * @param args additional arguments to the dump request. |
| 5886 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5887 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 5888 | dumpInner(prefix, fd, writer, args); |
| 5889 | } |
| 5890 | |
| 5891 | void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5892 | writer.print(prefix); writer.print("Local Activity "); |
| 5893 | writer.print(Integer.toHexString(System.identityHashCode(this))); |
| 5894 | writer.println(" State:"); |
| 5895 | String innerPrefix = prefix + " "; |
| 5896 | writer.print(innerPrefix); writer.print("mResumed="); |
| 5897 | writer.print(mResumed); writer.print(" mStopped="); |
| 5898 | writer.print(mStopped); writer.print(" mFinished="); |
| 5899 | writer.println(mFinished); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5900 | writer.print(innerPrefix); writer.print("mChangingConfigurations="); |
| 5901 | writer.println(mChangingConfigurations); |
| 5902 | writer.print(innerPrefix); writer.print("mCurrentConfig="); |
| 5903 | writer.println(mCurrentConfig); |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5904 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 5905 | mFragments.dumpLoaders(innerPrefix, fd, writer, args); |
| 5906 | mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args); |
Dianne Hackborn | 57dd737 | 2015-07-27 18:11:14 -0700 | [diff] [blame] | 5907 | if (mVoiceInteractor != null) { |
| 5908 | mVoiceInteractor.dump(innerPrefix, fd, writer, args); |
| 5909 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5910 | |
Michael Wright | 5f48dc7 | 2013-11-01 12:42:49 -0700 | [diff] [blame] | 5911 | if (getWindow() != null && |
| 5912 | getWindow().peekDecorView() != null && |
| 5913 | getWindow().peekDecorView().getViewRootImpl() != null) { |
| 5914 | getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args); |
| 5915 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5916 | |
| 5917 | mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5918 | } |
| 5919 | |
| 5920 | /** |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5921 | * Bit indicating that this activity is "immersive" and should not be |
| 5922 | * interrupted by notifications if possible. |
| 5923 | * |
| 5924 | * This value is initially set by the manifest property |
| 5925 | * <code>android:immersive</code> but may be changed at runtime by |
| 5926 | * {@link #setImmersive}. |
| 5927 | * |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 5928 | * @see #setImmersive(boolean) |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5929 | * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 5930 | */ |
| 5931 | public boolean isImmersive() { |
| 5932 | try { |
| 5933 | return ActivityManagerNative.getDefault().isImmersive(mToken); |
| 5934 | } catch (RemoteException e) { |
| 5935 | return false; |
| 5936 | } |
| 5937 | } |
| 5938 | |
| 5939 | /** |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5940 | * Indication of whether this is the highest level activity in this task. Can be used to |
| 5941 | * determine whether an activity launched by this activity was placed in the same task or |
| 5942 | * another task. |
| 5943 | * |
| 5944 | * @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] | 5945 | */ |
George Mount | ff24328 | 2014-07-07 16:12:07 -0700 | [diff] [blame] | 5946 | private boolean isTopOfTask() { |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5947 | try { |
| 5948 | return ActivityManagerNative.getDefault().isTopOfTask(mToken); |
| 5949 | } catch (RemoteException e) { |
| 5950 | return false; |
| 5951 | } |
| 5952 | } |
| 5953 | |
| 5954 | /** |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5955 | * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a |
| 5956 | * fullscreen opaque Activity. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5957 | * <p> |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5958 | * 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] | 5959 | * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released. |
| 5960 | * <p> |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5961 | * This call has no effect on non-translucent activities or on activities with the |
| 5962 | * {@link android.R.attr#windowIsFloating} attribute. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5963 | * |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5964 | * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener, |
| 5965 | * ActivityOptions) |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5966 | * @see TranslucentConversionListener |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 5967 | * |
| 5968 | * @hide |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5969 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5970 | @SystemApi |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5971 | public void convertFromTranslucent() { |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5972 | try { |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5973 | mTranslucentCallback = null; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 5974 | if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) { |
| 5975 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true); |
| 5976 | } |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5977 | } catch (RemoteException e) { |
| 5978 | // pass |
| 5979 | } |
| 5980 | } |
| 5981 | |
| 5982 | /** |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5983 | * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from |
| 5984 | * opaque to translucent following a call to {@link #convertFromTranslucent()}. |
| 5985 | * <p> |
| 5986 | * Calling this allows the Activity behind this one to be seen again. Once all such Activities |
| 5987 | * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will |
| 5988 | * be called indicating that it is safe to make this activity translucent again. Until |
| 5989 | * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image |
| 5990 | * behind the frontmost Activity will be indeterminate. |
| 5991 | * <p> |
| 5992 | * This call has no effect on non-translucent activities or on activities with the |
| 5993 | * {@link android.R.attr#windowIsFloating} attribute. |
| 5994 | * |
| 5995 | * @param callback the method to call when all visible Activities behind this one have been |
| 5996 | * drawn and it is safe to make this Activity translucent again. |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5997 | * @param options activity options delivered to the activity below this one. The options |
| 5998 | * are retrieved using {@link #getActivityOptions}. |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 5999 | * @return <code>true</code> if Window was opaque and will become translucent or |
| 6000 | * <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] | 6001 | * |
| 6002 | * @see #convertFromTranslucent() |
| 6003 | * @see TranslucentConversionListener |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 6004 | * |
| 6005 | * @hide |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6006 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6007 | @SystemApi |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 6008 | public boolean convertToTranslucent(TranslucentConversionListener callback, |
Jose Lima | 5517ea7 | 2014-06-10 12:31:43 -0700 | [diff] [blame] | 6009 | ActivityOptions options) { |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6010 | boolean drawComplete; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6011 | try { |
| 6012 | mTranslucentCallback = callback; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 6013 | mChangeCanvasToTranslucent = |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6014 | ActivityManagerNative.getDefault().convertToTranslucent(mToken, options); |
George Mount | 9e18397 | 2014-09-03 12:35:09 -0700 | [diff] [blame] | 6015 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false); |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6016 | drawComplete = true; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6017 | } catch (RemoteException e) { |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6018 | // Make callback return as though it timed out. |
| 6019 | mChangeCanvasToTranslucent = false; |
| 6020 | drawComplete = false; |
| 6021 | } |
| 6022 | if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) { |
| 6023 | // Window is already translucent. |
| 6024 | mTranslucentCallback.onTranslucentConversionComplete(drawComplete); |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6025 | } |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 6026 | return mChangeCanvasToTranslucent; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6027 | } |
| 6028 | |
| 6029 | /** @hide */ |
| 6030 | void onTranslucentConversionComplete(boolean drawComplete) { |
| 6031 | if (mTranslucentCallback != null) { |
| 6032 | mTranslucentCallback.onTranslucentConversionComplete(drawComplete); |
| 6033 | mTranslucentCallback = null; |
| 6034 | } |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 6035 | if (mChangeCanvasToTranslucent) { |
| 6036 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false); |
| 6037 | } |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6038 | } |
| 6039 | |
Craig Mautner | eb8abf7 | 2014-07-02 15:04:09 -0700 | [diff] [blame] | 6040 | /** @hide */ |
| 6041 | public void onNewActivityOptions(ActivityOptions options) { |
| 6042 | mActivityTransitionState.setEnterActivityOptions(this, options); |
| 6043 | if (!mStopped) { |
| 6044 | mActivityTransitionState.enterReady(this); |
| 6045 | } |
| 6046 | } |
| 6047 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6048 | /** |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6049 | * Retrieve the ActivityOptions passed in from the launching activity or passed back |
| 6050 | * from an activity launched by this activity in its call to {@link |
| 6051 | * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)} |
| 6052 | * |
| 6053 | * @return The ActivityOptions passed to {@link #convertToTranslucent}. |
| 6054 | * @hide |
| 6055 | */ |
| 6056 | ActivityOptions getActivityOptions() { |
| 6057 | try { |
| 6058 | return ActivityManagerNative.getDefault().getActivityOptions(mToken); |
| 6059 | } catch (RemoteException e) { |
| 6060 | } |
| 6061 | return null; |
| 6062 | } |
| 6063 | |
| 6064 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6065 | * 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] | 6066 | * this method anytime between the start of {@link #onResume()} and the return from |
| 6067 | * {@link #onPause()}. If this call is successful then the activity will remain visible after |
| 6068 | * {@link #onPause()} is called, and is allowed to continue playing media in the background. |
| 6069 | * |
| 6070 | * <p>The actions of this call are reset each time that this activity is brought to the |
| 6071 | * front. That is, every time {@link #onResume()} is called the activity will be assumed |
| 6072 | * to not have requested visible behind. Therefore, if you want this activity to continue to |
| 6073 | * be visible in the background you must call this method again. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6074 | * |
| 6075 | * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop |
| 6076 | * for dialog and translucent activities. |
| 6077 | * |
Craig Mautner | 64ccb70 | 2014-10-01 09:38:40 -0700 | [diff] [blame] | 6078 | * <p>Under all circumstances, the activity must stop playing and release resources prior to or |
| 6079 | * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false. |
| 6080 | * |
| 6081 | * <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] | 6082 | * the next call to onResume. |
| 6083 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6084 | * @param visible true to notify the system that the activity wishes to be visible behind other |
| 6085 | * translucent activities, false to indicate otherwise. Resources must be |
| 6086 | * released when passing false to this method. |
Craig Mautner | 64ccb70 | 2014-10-01 09:38:40 -0700 | [diff] [blame] | 6087 | * @return the resulting visibiity state. If true the activity will remain visible beyond |
| 6088 | * {@link #onPause()} if the next activity is translucent or not fullscreen. If false |
| 6089 | * then the activity may not count on being visible behind other translucent activities, |
| 6090 | * and must stop any media playback and release resources. |
| 6091 | * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so |
| 6092 | * the return value must be checked. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6093 | * |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6094 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6095 | * @see #onBackgroundVisibleBehindChanged(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6096 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6097 | public boolean requestVisibleBehind(boolean visible) { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6098 | if (!mResumed) { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6099 | // Do not permit paused or stopped activities to do this. |
| 6100 | visible = false; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6101 | } |
| 6102 | try { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6103 | mVisibleBehind = ActivityManagerNative.getDefault() |
| 6104 | .requestVisibleBehind(mToken, visible) && visible; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6105 | } catch (RemoteException e) { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6106 | mVisibleBehind = false; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6107 | } |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6108 | return mVisibleBehind; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6109 | } |
| 6110 | |
| 6111 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6112 | * Called when a translucent activity over this activity is becoming opaque or another |
| 6113 | * activity is being launched. Activities that override this method must call |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6114 | * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6115 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6116 | * <p>When this method is called the activity has 500 msec to release any resources it may be |
| 6117 | * using while visible in the background. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6118 | * 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] | 6119 | * 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] | 6120 | * process. Otherwise {@link #onStop()} will be called following return. |
| 6121 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6122 | * @see #requestVisibleBehind(boolean) |
| 6123 | * @see #onBackgroundVisibleBehindChanged(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6124 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 6125 | @CallSuper |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6126 | public void onVisibleBehindCanceled() { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6127 | mCalled = true; |
| 6128 | } |
| 6129 | |
| 6130 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6131 | * Translucent activities may call this to determine if there is an activity below them that |
| 6132 | * is currently set to be visible in the background. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6133 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6134 | * @return true if an activity below is set to visible according to the most recent call to |
| 6135 | * {@link #requestVisibleBehind(boolean)}, false otherwise. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6136 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6137 | * @see #requestVisibleBehind(boolean) |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6138 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6139 | * @see #onBackgroundVisibleBehindChanged(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6140 | * @hide |
| 6141 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6142 | @SystemApi |
| 6143 | public boolean isBackgroundVisibleBehind() { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6144 | try { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6145 | return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken); |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6146 | } catch (RemoteException e) { |
| 6147 | } |
| 6148 | return false; |
| 6149 | } |
| 6150 | |
| 6151 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6152 | * The topmost foreground activity will receive this call when the background visibility state |
| 6153 | * of the activity below it changes. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6154 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6155 | * 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] | 6156 | * due to a background activity finishing itself. |
| 6157 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6158 | * @param visible true if a background activity is visible, false otherwise. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6159 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6160 | * @see #requestVisibleBehind(boolean) |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6161 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6162 | * @hide |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6163 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6164 | @SystemApi |
| 6165 | public void onBackgroundVisibleBehindChanged(boolean visible) { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6166 | } |
| 6167 | |
| 6168 | /** |
Craig Mautner | 8746a47 | 2014-07-24 15:12:54 -0700 | [diff] [blame] | 6169 | * Activities cannot draw during the period that their windows are animating in. In order |
| 6170 | * to know when it is safe to begin drawing they can override this method which will be |
| 6171 | * called when the entering animation has completed. |
| 6172 | */ |
| 6173 | public void onEnterAnimationComplete() { |
| 6174 | } |
| 6175 | |
| 6176 | /** |
Filip Gruszczynski | 6eafa90 | 2014-11-14 14:24:37 -0800 | [diff] [blame] | 6177 | * @hide |
| 6178 | */ |
| 6179 | public void dispatchEnterAnimationComplete() { |
| 6180 | onEnterAnimationComplete(); |
| 6181 | if (getWindow() != null && getWindow().getDecorView() != null) { |
| 6182 | getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete(); |
| 6183 | } |
| 6184 | } |
| 6185 | |
| 6186 | /** |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6187 | * Adjust the current immersive mode setting. |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 6188 | * |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6189 | * Note that changing this value will have no effect on the activity's |
| 6190 | * {@link android.content.pm.ActivityInfo} structure; that is, if |
| 6191 | * <code>android:immersive</code> is set to <code>true</code> |
| 6192 | * in the application's manifest entry for this activity, the {@link |
| 6193 | * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will |
| 6194 | * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 6195 | * FLAG_IMMERSIVE} bit set. |
| 6196 | * |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 6197 | * @see #isImmersive() |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6198 | * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 6199 | */ |
| 6200 | public void setImmersive(boolean i) { |
| 6201 | try { |
| 6202 | ActivityManagerNative.getDefault().setImmersive(mToken, i); |
| 6203 | } catch (RemoteException e) { |
| 6204 | // pass |
| 6205 | } |
| 6206 | } |
| 6207 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6208 | /** |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 6209 | * Enable or disable virtual reality (VR) mode. |
| 6210 | * |
| 6211 | * <p>VR mode is a hint to Android system services to switch to modes optimized for |
| 6212 | * high-performance stereoscopic rendering.</p> |
| 6213 | * |
| 6214 | * @param enabled {@code true} to enable this mode. |
| 6215 | */ |
| 6216 | public void setVrMode(boolean enabled) { |
| 6217 | try { |
| 6218 | ActivityManagerNative.getDefault().setVrMode(mToken, enabled); |
| 6219 | } catch (RemoteException e) { |
| 6220 | // pass |
| 6221 | } |
| 6222 | } |
| 6223 | |
| 6224 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6225 | * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}. |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6226 | * |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6227 | * @param callback Callback that will manage lifecycle events for this action mode |
| 6228 | * @return The ActionMode that was started, or null if it was canceled |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6229 | * |
| 6230 | * @see ActionMode |
| 6231 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 6232 | @Nullable |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 6233 | public ActionMode startActionMode(ActionMode.Callback callback) { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6234 | return mWindow.getDecorView().startActionMode(callback); |
| 6235 | } |
| 6236 | |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6237 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6238 | * Start an action mode of the given type. |
| 6239 | * |
| 6240 | * @param callback Callback that will manage lifecycle events for this action mode |
| 6241 | * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}. |
| 6242 | * @return The ActionMode that was started, or null if it was canceled |
| 6243 | * |
| 6244 | * @see ActionMode |
| 6245 | */ |
| 6246 | @Nullable |
| 6247 | public ActionMode startActionMode(ActionMode.Callback callback, int type) { |
| 6248 | return mWindow.getDecorView().startActionMode(callback, type); |
| 6249 | } |
| 6250 | |
| 6251 | /** |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6252 | * Give the Activity a chance to control the UI for an action mode requested |
| 6253 | * by the system. |
| 6254 | * |
| 6255 | * <p>Note: If you are looking for a notification callback that an action mode |
| 6256 | * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p> |
| 6257 | * |
| 6258 | * @param callback The callback that should control the new action mode |
| 6259 | * @return The new action mode, or <code>null</code> if the activity does not want to |
| 6260 | * provide special handling for this action mode. (It will be handled by the system.) |
| 6261 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 6262 | @Nullable |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6263 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6264 | public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6265 | // Only Primary ActionModes are represented in the ActionBar. |
| 6266 | if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) { |
| 6267 | initWindowDecorActionBar(); |
| 6268 | if (mActionBar != null) { |
| 6269 | return mActionBar.startActionMode(callback); |
| 6270 | } |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6271 | } |
| 6272 | return null; |
| 6273 | } |
| 6274 | |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6275 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6276 | * {@inheritDoc} |
| 6277 | */ |
| 6278 | @Nullable |
| 6279 | @Override |
| 6280 | public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) { |
| 6281 | try { |
| 6282 | mActionModeTypeStarting = type; |
| 6283 | return onWindowStartingActionMode(callback); |
| 6284 | } finally { |
| 6285 | mActionModeTypeStarting = ActionMode.TYPE_PRIMARY; |
| 6286 | } |
| 6287 | } |
| 6288 | |
| 6289 | /** |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6290 | * Notifies the Activity that an action mode has been started. |
| 6291 | * Activity subclasses overriding this method should call the superclass implementation. |
| 6292 | * |
| 6293 | * @param mode The new action mode. |
| 6294 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 6295 | @CallSuper |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6296 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6297 | public void onActionModeStarted(ActionMode mode) { |
| 6298 | } |
| 6299 | |
| 6300 | /** |
| 6301 | * Notifies the activity that an action mode has finished. |
| 6302 | * Activity subclasses overriding this method should call the superclass implementation. |
| 6303 | * |
| 6304 | * @param mode The action mode that just finished. |
| 6305 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 6306 | @CallSuper |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6307 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6308 | public void onActionModeFinished(ActionMode mode) { |
| 6309 | } |
| 6310 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6311 | /** |
| 6312 | * Returns true if the app should recreate the task when navigating 'up' from this activity |
| 6313 | * by using targetIntent. |
| 6314 | * |
| 6315 | * <p>If this method returns false the app can trivially call |
| 6316 | * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform |
| 6317 | * up navigation. If this method returns false, the app should synthesize a new task stack |
| 6318 | * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p> |
| 6319 | * |
| 6320 | * @param targetIntent An intent representing the target destination for up navigation |
| 6321 | * @return true if navigating up should recreate a new task stack, false if the same task |
| 6322 | * should be used for the destination |
| 6323 | */ |
| 6324 | public boolean shouldUpRecreateTask(Intent targetIntent) { |
| 6325 | try { |
| 6326 | PackageManager pm = getPackageManager(); |
| 6327 | ComponentName cn = targetIntent.getComponent(); |
| 6328 | if (cn == null) { |
| 6329 | cn = targetIntent.resolveActivity(pm); |
| 6330 | } |
| 6331 | ActivityInfo info = pm.getActivityInfo(cn, 0); |
| 6332 | if (info.taskAffinity == null) { |
| 6333 | return false; |
| 6334 | } |
Dianne Hackborn | 6f4d61f | 2014-08-21 17:50:42 -0700 | [diff] [blame] | 6335 | return ActivityManagerNative.getDefault() |
| 6336 | .shouldUpRecreateTask(mToken, info.taskAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6337 | } catch (RemoteException e) { |
| 6338 | return false; |
| 6339 | } catch (NameNotFoundException e) { |
| 6340 | return false; |
| 6341 | } |
| 6342 | } |
| 6343 | |
| 6344 | /** |
| 6345 | * Navigate from this activity to the activity specified by upIntent, finishing this activity |
| 6346 | * in the process. If the activity indicated by upIntent already exists in the task's history, |
| 6347 | * 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] | 6348 | * finished. |
| 6349 | * |
| 6350 | * <p>If the indicated activity does not appear in the history stack, this will finish |
| 6351 | * each activity in this task until the root activity of the task is reached, resulting in |
| 6352 | * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy |
| 6353 | * when an activity may be reached by a path not passing through a canonical parent |
| 6354 | * activity.</p> |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6355 | * |
| 6356 | * <p>This method should be used when performing up navigation from within the same task |
| 6357 | * as the destination. If up navigation should cross tasks in some cases, see |
| 6358 | * {@link #shouldUpRecreateTask(Intent)}.</p> |
| 6359 | * |
| 6360 | * @param upIntent An intent representing the target destination for up navigation |
| 6361 | * |
| 6362 | * @return true if up navigation successfully reached the activity indicated by upIntent and |
| 6363 | * upIntent was delivered to it. false if an instance of the indicated activity could |
| 6364 | * not be found and this activity was simply finished normally. |
| 6365 | */ |
| 6366 | public boolean navigateUpTo(Intent upIntent) { |
| 6367 | if (mParent == null) { |
| 6368 | ComponentName destInfo = upIntent.getComponent(); |
| 6369 | if (destInfo == null) { |
| 6370 | destInfo = upIntent.resolveActivity(getPackageManager()); |
| 6371 | if (destInfo == null) { |
| 6372 | return false; |
| 6373 | } |
| 6374 | upIntent = new Intent(upIntent); |
| 6375 | upIntent.setComponent(destInfo); |
| 6376 | } |
| 6377 | int resultCode; |
| 6378 | Intent resultData; |
| 6379 | synchronized (this) { |
| 6380 | resultCode = mResultCode; |
| 6381 | resultData = mResultData; |
| 6382 | } |
| 6383 | if (resultData != null) { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 6384 | resultData.prepareToLeaveProcess(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6385 | } |
| 6386 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 6387 | upIntent.prepareToLeaveProcess(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6388 | return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent, |
| 6389 | resultCode, resultData); |
| 6390 | } catch (RemoteException e) { |
| 6391 | return false; |
| 6392 | } |
| 6393 | } else { |
| 6394 | return mParent.navigateUpToFromChild(this, upIntent); |
| 6395 | } |
| 6396 | } |
| 6397 | |
| 6398 | /** |
| 6399 | * This is called when a child activity of this one calls its |
| 6400 | * {@link #navigateUpTo} method. The default implementation simply calls |
| 6401 | * navigateUpTo(upIntent) on this activity (the parent). |
| 6402 | * |
| 6403 | * @param child The activity making the call. |
| 6404 | * @param upIntent An intent representing the target destination for up navigation |
| 6405 | * |
| 6406 | * @return true if up navigation successfully reached the activity indicated by upIntent and |
| 6407 | * upIntent was delivered to it. false if an instance of the indicated activity could |
| 6408 | * not be found and this activity was simply finished normally. |
| 6409 | */ |
| 6410 | public boolean navigateUpToFromChild(Activity child, Intent upIntent) { |
| 6411 | return navigateUpTo(upIntent); |
| 6412 | } |
| 6413 | |
| 6414 | /** |
| 6415 | * Obtain an {@link Intent} that will launch an explicit target activity specified by |
| 6416 | * this activity's logical parent. The logical parent is named in the application's manifest |
| 6417 | * by the {@link android.R.attr#parentActivityName parentActivityName} attribute. |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 6418 | * Activity subclasses may override this method to modify the Intent returned by |
| 6419 | * super.getParentActivityIntent() or to implement a different mechanism of retrieving |
| 6420 | * the parent intent entirely. |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6421 | * |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 6422 | * @return a new Intent targeting the defined parent of this activity or null if |
| 6423 | * there is no valid parent. |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6424 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 6425 | @Nullable |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6426 | public Intent getParentActivityIntent() { |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 6427 | final String parentName = mActivityInfo.parentActivityName; |
| 6428 | if (TextUtils.isEmpty(parentName)) { |
| 6429 | return null; |
| 6430 | } |
Adam Powell | 5a4010c | 2012-09-16 15:14:05 -0700 | [diff] [blame] | 6431 | |
| 6432 | // If the parent itself has no parent, generate a main activity intent. |
| 6433 | final ComponentName target = new ComponentName(this, parentName); |
| 6434 | try { |
| 6435 | final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0); |
| 6436 | final String parentActivity = parentInfo.parentActivityName; |
| 6437 | final Intent parentIntent = parentActivity == null |
| 6438 | ? Intent.makeMainActivity(target) |
| 6439 | : new Intent().setComponent(target); |
| 6440 | return parentIntent; |
| 6441 | } catch (NameNotFoundException e) { |
| 6442 | Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName + |
| 6443 | "' in manifest"); |
| 6444 | return null; |
| 6445 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6446 | } |
| 6447 | |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6448 | /** |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6449 | * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6450 | * 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] | 6451 | * 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] | 6452 | * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6453 | * |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6454 | * @param callback Used to manipulate shared element transitions on the launched Activity. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6455 | */ |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6456 | public void setEnterSharedElementCallback(SharedElementCallback callback) { |
| 6457 | if (callback == null) { |
| 6458 | callback = SharedElementCallback.NULL_CALLBACK; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6459 | } |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6460 | mEnterTransitionListener = callback; |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6461 | } |
| 6462 | |
| 6463 | /** |
| 6464 | * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6465 | * 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] | 6466 | * will be called to handle shared elements on the <i>launching</i> Activity. Most |
| 6467 | * calls will only come when returning from the started Activity. |
George Mount | 9826f63 | 2014-09-11 08:50:09 -0700 | [diff] [blame] | 6468 | * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6469 | * |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6470 | * @param callback Used to manipulate shared element transitions on the launching Activity. |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6471 | */ |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6472 | public void setExitSharedElementCallback(SharedElementCallback callback) { |
| 6473 | if (callback == null) { |
| 6474 | callback = SharedElementCallback.NULL_CALLBACK; |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6475 | } |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6476 | mExitTransitionListener = callback; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6477 | } |
| 6478 | |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 6479 | /** |
| 6480 | * Postpone the entering activity transition when Activity was started with |
| 6481 | * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
| 6482 | * android.util.Pair[])}. |
| 6483 | * <p>This method gives the Activity the ability to delay starting the entering and |
| 6484 | * shared element transitions until all data is loaded. Until then, the Activity won't |
| 6485 | * draw into its window, leaving the window transparent. This may also cause the |
| 6486 | * returning animation to be delayed until data is ready. This method should be |
| 6487 | * called in {@link #onCreate(android.os.Bundle)} or in |
| 6488 | * {@link #onActivityReenter(int, android.content.Intent)}. |
| 6489 | * {@link #startPostponedEnterTransition()} must be called to allow the Activity to |
| 6490 | * start the transitions. If the Activity did not use |
| 6491 | * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
| 6492 | * android.util.Pair[])}, then this method does nothing.</p> |
| 6493 | */ |
| 6494 | public void postponeEnterTransition() { |
| 6495 | mActivityTransitionState.postponeEnterTransition(); |
| 6496 | } |
| 6497 | |
| 6498 | /** |
| 6499 | * Begin postponed transitions after {@link #postponeEnterTransition()} was called. |
| 6500 | * If postponeEnterTransition() was called, you must call startPostponedEnterTransition() |
| 6501 | * to have your Activity start drawing. |
| 6502 | */ |
| 6503 | public void startPostponedEnterTransition() { |
| 6504 | mActivityTransitionState.startPostponedEnterTransition(); |
| 6505 | } |
| 6506 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 6507 | /** |
| 6508 | * Create {@link DropPermissions} object bound to this activity and controlling the access |
| 6509 | * permissions for content URIs associated with the {@link DragEvent}. |
| 6510 | * @param event Drag event |
| 6511 | * @return The DropPermissions object used to control access to the content URIs. Null if |
| 6512 | * no content URIs are associated with the event or if permissions could not be granted. |
| 6513 | */ |
| 6514 | public DropPermissions requestDropPermissions(DragEvent event) { |
| 6515 | DropPermissions dropPermissions = DropPermissions.obtain(event); |
| 6516 | if (dropPermissions != null && dropPermissions.take(getActivityToken())) { |
| 6517 | return dropPermissions; |
| 6518 | } |
| 6519 | return null; |
| 6520 | } |
| 6521 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6522 | // ------------------ Internal API ------------------ |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6523 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6524 | final void setParent(Activity parent) { |
| 6525 | mParent = parent; |
| 6526 | } |
| 6527 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6528 | final void attach(Context context, ActivityThread aThread, |
| 6529 | Instrumentation instr, IBinder token, int ident, |
| 6530 | Application application, Intent intent, ActivityInfo info, |
| 6531 | CharSequence title, Activity parent, String id, |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6532 | NonConfigurationInstances lastNonConfigurationInstances, |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 6533 | Configuration config, String referrer, IVoiceInteractor voiceInteractor, |
| 6534 | Window window) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6535 | attachBaseContext(context); |
| 6536 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6537 | mFragments.attachHost(null /*parent*/); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6538 | |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 6539 | mWindow = new PhoneWindow(this, window); |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 6540 | mWindow.setWindowControllerCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6541 | mWindow.setCallback(this); |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 6542 | mWindow.setOnWindowDismissedCallback(this); |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 6543 | mWindow.getLayoutInflater().setPrivateFactory(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6544 | if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) { |
| 6545 | mWindow.setSoftInputMode(info.softInputMode); |
| 6546 | } |
Adam Powell | 269248d | 2011-08-02 10:26:54 -0700 | [diff] [blame] | 6547 | if (info.uiOptions != 0) { |
| 6548 | mWindow.setUiOptions(info.uiOptions); |
| 6549 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6550 | mUiThread = Thread.currentThread(); |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6552 | mMainThread = aThread; |
| 6553 | mInstrumentation = instr; |
| 6554 | mToken = token; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6555 | mIdent = ident; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6556 | mApplication = application; |
| 6557 | mIntent = intent; |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 6558 | mReferrer = referrer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6559 | mComponent = intent.getComponent(); |
| 6560 | mActivityInfo = info; |
| 6561 | mTitle = title; |
| 6562 | mParent = parent; |
| 6563 | mEmbeddedID = id; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6564 | mLastNonConfigurationInstances = lastNonConfigurationInstances; |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 6565 | if (voiceInteractor != null) { |
| 6566 | if (lastNonConfigurationInstances != null) { |
| 6567 | mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor; |
| 6568 | } else { |
| 6569 | mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this, |
| 6570 | Looper.myLooper()); |
| 6571 | } |
| 6572 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6573 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 6574 | mWindow.setWindowManager( |
| 6575 | (WindowManager)context.getSystemService(Context.WINDOW_SERVICE), |
| 6576 | mToken, mComponent.flattenToString(), |
Romain Guy | 529b60a | 2010-08-03 18:05:47 -0700 | [diff] [blame] | 6577 | (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6578 | if (mParent != null) { |
| 6579 | mWindow.setContainer(mParent.getWindow()); |
| 6580 | } |
| 6581 | mWindowManager = mWindow.getWindowManager(); |
| 6582 | mCurrentConfig = config; |
| 6583 | } |
| 6584 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 6585 | /** @hide */ |
| 6586 | public final IBinder getActivityToken() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6587 | return mParent != null ? mParent.getActivityToken() : mToken; |
| 6588 | } |
| 6589 | |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6590 | final void performCreateCommon() { |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 6591 | mVisibleFromClient = !mWindow.getWindowStyle().getBoolean( |
| 6592 | com.android.internal.R.styleable.Window_windowNoDisplay, false); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 6593 | mFragments.dispatchActivityCreated(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6594 | mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions()); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6595 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6596 | |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6597 | final void performCreate(Bundle icicle) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6598 | restoreHasCurrentPermissionRequest(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6599 | onCreate(icicle); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6600 | mActivityTransitionState.readState(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6601 | performCreateCommon(); |
| 6602 | } |
| 6603 | |
| 6604 | final void performCreate(Bundle icicle, PersistableBundle persistentState) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6605 | restoreHasCurrentPermissionRequest(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6606 | onCreate(icicle, persistentState); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6607 | mActivityTransitionState.readState(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6608 | performCreateCommon(); |
| 6609 | } |
| 6610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6611 | final void performStart() { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6612 | mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions()); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6613 | mFragments.noteStateNotSaved(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6614 | mCalled = false; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6615 | mFragments.execPendingActions(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6616 | mInstrumentation.callActivityOnStart(this); |
| 6617 | if (!mCalled) { |
| 6618 | throw new SuperNotCalledException( |
| 6619 | "Activity " + mComponent.toShortString() + |
| 6620 | " did not call through to super.onStart()"); |
| 6621 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6622 | mFragments.dispatchStart(); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6623 | mFragments.reportLoaderStart(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6624 | mActivityTransitionState.enterReady(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6625 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6626 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6627 | final void performRestart() { |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6628 | mFragments.noteStateNotSaved(); |
Dianne Hackborn | a21e3da | 2010-09-12 19:27:46 -0700 | [diff] [blame] | 6629 | |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 6630 | if (mToken != null && mParent == null) { |
| 6631 | // We might have view roots that were preserved during a relaunch, we need to start them |
| 6632 | // again. We don't need to check mStopped, the roots will check if they were actually |
| 6633 | // stopped. |
| 6634 | WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */); |
| 6635 | } |
| 6636 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6637 | if (mStopped) { |
| 6638 | mStopped = false; |
Dianne Hackborn | 185e3e2 | 2011-06-03 15:26:01 -0700 | [diff] [blame] | 6639 | |
| 6640 | synchronized (mManagedCursors) { |
| 6641 | final int N = mManagedCursors.size(); |
| 6642 | for (int i=0; i<N; i++) { |
| 6643 | ManagedCursor mc = mManagedCursors.get(i); |
| 6644 | if (mc.mReleased || mc.mUpdated) { |
| 6645 | if (!mc.mCursor.requery()) { |
Dianne Hackborn | a5445d3 | 2011-09-01 14:38:24 -0700 | [diff] [blame] | 6646 | if (getApplicationInfo().targetSdkVersion |
| 6647 | >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 6648 | throw new IllegalStateException( |
| 6649 | "trying to requery an already closed cursor " |
| 6650 | + mc.mCursor); |
| 6651 | } |
Dianne Hackborn | 185e3e2 | 2011-06-03 15:26:01 -0700 | [diff] [blame] | 6652 | } |
| 6653 | mc.mReleased = false; |
| 6654 | mc.mUpdated = false; |
| 6655 | } |
| 6656 | } |
| 6657 | } |
| 6658 | |
| 6659 | mCalled = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6660 | mInstrumentation.callActivityOnRestart(this); |
| 6661 | if (!mCalled) { |
| 6662 | throw new SuperNotCalledException( |
| 6663 | "Activity " + mComponent.toShortString() + |
| 6664 | " did not call through to super.onRestart()"); |
| 6665 | } |
| 6666 | performStart(); |
| 6667 | } |
| 6668 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6669 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6670 | final void performResume() { |
| 6671 | performRestart(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6672 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6673 | mFragments.execPendingActions(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6674 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6675 | mLastNonConfigurationInstances = null; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6676 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6677 | mCalled = false; |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6678 | // mResumed is set by the instrumentation |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6679 | mInstrumentation.callActivityOnResume(this); |
| 6680 | if (!mCalled) { |
| 6681 | throw new SuperNotCalledException( |
| 6682 | "Activity " + mComponent.toShortString() + |
| 6683 | " did not call through to super.onResume()"); |
| 6684 | } |
| 6685 | |
Todd Kennedy | ee8c9c6 | 2014-12-10 14:22:59 -0800 | [diff] [blame] | 6686 | // invisible activities must be finished before onResume() completes |
| 6687 | if (!mVisibleFromClient && !mFinished) { |
| 6688 | Log.w(TAG, "An activity without a UI must call finish() before onResume() completes"); |
| 6689 | if (getApplicationInfo().targetSdkVersion |
| 6690 | > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 6691 | throw new IllegalStateException( |
| 6692 | "Activity " + mComponent.toShortString() + |
| 6693 | " did not call finish() prior to onResume() completing"); |
| 6694 | } |
| 6695 | } |
| 6696 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6697 | // 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] | 6698 | mCalled = false; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6699 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6700 | mFragments.dispatchResume(); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6701 | mFragments.execPendingActions(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6702 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6703 | onPostResume(); |
| 6704 | if (!mCalled) { |
| 6705 | throw new SuperNotCalledException( |
| 6706 | "Activity " + mComponent.toShortString() + |
| 6707 | " did not call through to super.onPostResume()"); |
| 6708 | } |
| 6709 | } |
| 6710 | |
| 6711 | final void performPause() { |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 6712 | mDoReportFullyDrawn = false; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6713 | mFragments.dispatchPause(); |
Dianne Hackborn | e794e9f | 2010-08-24 12:32:10 -0700 | [diff] [blame] | 6714 | mCalled = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6715 | onPause(); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6716 | mResumed = false; |
Dianne Hackborn | e794e9f | 2010-08-24 12:32:10 -0700 | [diff] [blame] | 6717 | if (!mCalled && getApplicationInfo().targetSdkVersion |
| 6718 | >= android.os.Build.VERSION_CODES.GINGERBREAD) { |
| 6719 | throw new SuperNotCalledException( |
| 6720 | "Activity " + mComponent.toShortString() + |
| 6721 | " did not call through to super.onPause()"); |
| 6722 | } |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6723 | mResumed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6724 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6726 | final void performUserLeaving() { |
| 6727 | onUserInteraction(); |
| 6728 | onUserLeaveHint(); |
| 6729 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6730 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6731 | final void performStop() { |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 6732 | mDoReportFullyDrawn = false; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6733 | mFragments.doLoaderStop(mChangingConfigurations /*retain*/); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6734 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6735 | if (!mStopped) { |
| 6736 | if (mWindow != null) { |
| 6737 | mWindow.closeAllPanels(); |
| 6738 | } |
| 6739 | |
Dianne Hackborn | ce418e6 | 2011-03-01 14:31:38 -0800 | [diff] [blame] | 6740 | if (mToken != null && mParent == null) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 6741 | WindowManagerGlobal.getInstance().setStoppedState(mToken, true); |
Dianne Hackborn | ce418e6 | 2011-03-01 14:31:38 -0800 | [diff] [blame] | 6742 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6743 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6744 | mFragments.dispatchStop(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6745 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6746 | mCalled = false; |
| 6747 | mInstrumentation.callActivityOnStop(this); |
| 6748 | if (!mCalled) { |
| 6749 | throw new SuperNotCalledException( |
| 6750 | "Activity " + mComponent.toShortString() + |
| 6751 | " did not call through to super.onStop()"); |
| 6752 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6753 | |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 6754 | synchronized (mManagedCursors) { |
| 6755 | final int N = mManagedCursors.size(); |
| 6756 | for (int i=0; i<N; i++) { |
| 6757 | ManagedCursor mc = mManagedCursors.get(i); |
| 6758 | if (!mc.mReleased) { |
| 6759 | mc.mCursor.deactivate(); |
| 6760 | mc.mReleased = true; |
| 6761 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6762 | } |
| 6763 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6764 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6765 | mStopped = true; |
| 6766 | } |
| 6767 | mResumed = false; |
| 6768 | } |
| 6769 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6770 | final void performDestroy() { |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 6771 | mDestroyed = true; |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 6772 | mWindow.destroy(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6773 | mFragments.dispatchDestroy(); |
| 6774 | onDestroy(); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6775 | mFragments.doLoaderDestroy(); |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 6776 | if (mVoiceInteractor != null) { |
| 6777 | mVoiceInteractor.detachActivity(); |
| 6778 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6779 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6780 | |
| 6781 | /** |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6782 | * @hide |
| 6783 | */ |
| 6784 | public final boolean isResumed() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6785 | return mResumed; |
| 6786 | } |
| 6787 | |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6788 | private void storeHasCurrentPermissionRequest(Bundle bundle) { |
| 6789 | if (bundle != null && mHasCurrentPermissionsRequest) { |
| 6790 | bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true); |
| 6791 | } |
| 6792 | } |
| 6793 | |
| 6794 | private void restoreHasCurrentPermissionRequest(Bundle bundle) { |
| 6795 | if (bundle != null) { |
| 6796 | mHasCurrentPermissionsRequest = bundle.getBoolean( |
| 6797 | HAS_CURENT_PERMISSIONS_REQUEST_KEY, false); |
| 6798 | } |
| 6799 | } |
| 6800 | |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6801 | void dispatchActivityResult(String who, int requestCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6802 | int resultCode, Intent data) { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 6803 | if (false) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6804 | TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode |
| 6805 | + ", resCode=" + resultCode + ", data=" + data); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6806 | mFragments.noteStateNotSaved(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6807 | if (who == null) { |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 6808 | onActivityResult(requestCode, resultCode, data); |
| 6809 | } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) { |
| 6810 | who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length()); |
| 6811 | if (TextUtils.isEmpty(who)) { |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6812 | dispatchRequestPermissionsResult(requestCode, data); |
| 6813 | } else { |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 6814 | Fragment frag = mFragments.findFragmentByWho(who); |
| 6815 | if (frag != null) { |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 6816 | dispatchRequestPermissionsResultToFragment(requestCode, data, frag); |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6817 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 6818 | } |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 6819 | } else if (who.startsWith("@android:view:")) { |
| 6820 | ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews( |
| 6821 | getActivityToken()); |
| 6822 | for (ViewRootImpl viewRoot : views) { |
| 6823 | if (viewRoot.getView() != null |
| 6824 | && viewRoot.getView().dispatchActivityResult( |
| 6825 | who, requestCode, resultCode, data)) { |
| 6826 | return; |
| 6827 | } |
| 6828 | } |
| 6829 | } else { |
| 6830 | Fragment frag = mFragments.findFragmentByWho(who); |
| 6831 | if (frag != null) { |
| 6832 | frag.onActivityResult(requestCode, resultCode, data); |
| 6833 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6834 | } |
| 6835 | } |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6836 | |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6837 | /** |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 6838 | * 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] | 6839 | * current task. |
| 6840 | * |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 6841 | * This will prevent the user from launching other apps, going to settings, or reaching the |
| 6842 | * home screen. This does not include those apps whose {@link android.R.attr#lockTaskMode} |
| 6843 | * values permit launching while locked. |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6844 | * |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 6845 | * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true or |
| 6846 | * lockTaskMode=lockTaskModeAlways for this component then the app will go directly into |
| 6847 | * Lock Task mode. The user will not be able to exit this mode until |
| 6848 | * {@link Activity#stopLockTask()} is called. |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 6849 | * |
| 6850 | * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false |
| 6851 | * 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] | 6852 | * this mode. When entered through this method the user can exit at any time through |
| 6853 | * an action described by the request dialog. Calling stopLockTask will also exit the |
| 6854 | * mode. |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 6855 | * |
| 6856 | * @see android.R.attr#lockTaskMode |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6857 | */ |
Craig Mautner | aea74a5 | 2014-03-08 14:23:10 -0800 | [diff] [blame] | 6858 | public void startLockTask() { |
| 6859 | try { |
| 6860 | ActivityManagerNative.getDefault().startLockTaskMode(mToken); |
| 6861 | } catch (RemoteException e) { |
| 6862 | } |
| 6863 | } |
| 6864 | |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6865 | /** |
| 6866 | * Allow the user to switch away from the current task. |
| 6867 | * |
| 6868 | * Called to end the mode started by {@link Activity#startLockTask}. This |
| 6869 | * can only be called by activities that have successfully called |
| 6870 | * startLockTask previously. |
| 6871 | * |
| 6872 | * This will allow the user to exit this app and move onto other activities. |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 6873 | * <p>Note: This method should only be called when the activity is user-facing. That is, |
| 6874 | * between onResume() and onPause(). |
| 6875 | * <p>Note: If there are other tasks below this one that are also locked then calling this |
| 6876 | * method will immediately finish this task and resume the previous locked one, remaining in |
| 6877 | * lockTask mode. |
| 6878 | * |
| 6879 | * @see android.R.attr#lockTaskMode |
| 6880 | * @see ActivityManager#getLockTaskModeState() |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6881 | */ |
Craig Mautner | aea74a5 | 2014-03-08 14:23:10 -0800 | [diff] [blame] | 6882 | public void stopLockTask() { |
| 6883 | try { |
| 6884 | ActivityManagerNative.getDefault().stopLockTaskMode(); |
| 6885 | } catch (RemoteException e) { |
| 6886 | } |
| 6887 | } |
| 6888 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6889 | /** |
Craig Mautner | c21ae9e | 2015-04-15 09:45:42 -0700 | [diff] [blame] | 6890 | * Shows the user the system defined message for telling the user how to exit |
| 6891 | * lock task mode. The task containing this activity must be in lock task mode at the time |
| 6892 | * of this call for the message to be displayed. |
| 6893 | */ |
| 6894 | public void showLockTaskEscapeMessage() { |
| 6895 | try { |
| 6896 | ActivityManagerNative.getDefault().showLockTaskEscapeMessage(mToken); |
| 6897 | } catch (RemoteException e) { |
| 6898 | } |
| 6899 | } |
| 6900 | |
| 6901 | /** |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 6902 | * Set whether the caption should displayed directly on the content rather than push it down. |
| 6903 | * |
| 6904 | * This affects only freeform windows since they display the caption and only the main |
| 6905 | * window of the activity. The caption is used to drag the window around and also shows |
| 6906 | * maximize and close action buttons. |
| 6907 | */ |
| 6908 | public void overlayWithDecorCaption(boolean overlay) { |
| 6909 | mWindow.setOverlayDecorCaption(overlay); |
| 6910 | } |
| 6911 | |
| 6912 | /** |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6913 | * Interface for informing a translucent {@link Activity} once all visible activities below it |
| 6914 | * have completed drawing. This is necessary only after an {@link Activity} has been made |
| 6915 | * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn |
| 6916 | * translucent again following a call to {@link |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6917 | * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener, |
| 6918 | * ActivityOptions)} |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 6919 | * |
| 6920 | * @hide |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6921 | */ |
Jose Lima | 1491485 | 2014-08-14 09:14:12 -0700 | [diff] [blame] | 6922 | @SystemApi |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6923 | public interface TranslucentConversionListener { |
| 6924 | /** |
| 6925 | * Callback made following {@link Activity#convertToTranslucent} once all visible Activities |
| 6926 | * below the top one have been redrawn. Following this callback it is safe to make the top |
| 6927 | * Activity translucent because the underlying Activity has been drawn. |
| 6928 | * |
| 6929 | * @param drawComplete True if the background Activity has drawn itself. False if a timeout |
| 6930 | * occurred waiting for the Activity to complete drawing. |
| 6931 | * |
| 6932 | * @see Activity#convertFromTranslucent() |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6933 | * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions) |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6934 | */ |
| 6935 | public void onTranslucentConversionComplete(boolean drawComplete); |
| 6936 | } |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6937 | |
| 6938 | private void dispatchRequestPermissionsResult(int requestCode, Intent data) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6939 | mHasCurrentPermissionsRequest = false; |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 6940 | // If the package installer crashed we may have not data - best effort. |
| 6941 | String[] permissions = (data != null) ? data.getStringArrayExtra( |
| 6942 | PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0]; |
| 6943 | final int[] grantResults = (data != null) ? data.getIntArrayExtra( |
| 6944 | PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0]; |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6945 | onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 6946 | } |
| 6947 | |
| 6948 | private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data, |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 6949 | Fragment fragment) { |
| 6950 | // If the package installer crashed we may have not data - best effort. |
| 6951 | String[] permissions = (data != null) ? data.getStringArrayExtra( |
| 6952 | PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0]; |
| 6953 | final int[] grantResults = (data != null) ? data.getIntArrayExtra( |
| 6954 | PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0]; |
| 6955 | fragment.onRequestPermissionsResult(requestCode, permissions, grantResults); |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6956 | } |
| 6957 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6958 | class HostCallbacks extends FragmentHostCallback<Activity> { |
| 6959 | public HostCallbacks() { |
| 6960 | super(Activity.this /*activity*/); |
| 6961 | } |
| 6962 | |
| 6963 | @Override |
| 6964 | public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 6965 | Activity.this.dump(prefix, fd, writer, args); |
| 6966 | } |
| 6967 | |
| 6968 | @Override |
| 6969 | public boolean onShouldSaveFragmentState(Fragment fragment) { |
| 6970 | return !isFinishing(); |
| 6971 | } |
| 6972 | |
| 6973 | @Override |
| 6974 | public LayoutInflater onGetLayoutInflater() { |
| 6975 | final LayoutInflater result = Activity.this.getLayoutInflater(); |
| 6976 | if (onUseFragmentManagerInflaterFactory()) { |
| 6977 | return result.cloneInContext(Activity.this); |
| 6978 | } |
| 6979 | return result; |
| 6980 | } |
| 6981 | |
| 6982 | @Override |
| 6983 | public boolean onUseFragmentManagerInflaterFactory() { |
| 6984 | // Newer platform versions use the child fragment manager's LayoutInflaterFactory. |
| 6985 | return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; |
| 6986 | } |
| 6987 | |
| 6988 | @Override |
| 6989 | public Activity onGetHost() { |
| 6990 | return Activity.this; |
| 6991 | } |
| 6992 | |
| 6993 | @Override |
| 6994 | public void onInvalidateOptionsMenu() { |
| 6995 | Activity.this.invalidateOptionsMenu(); |
| 6996 | } |
| 6997 | |
| 6998 | @Override |
| 6999 | public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode, |
| 7000 | Bundle options) { |
| 7001 | Activity.this.startActivityFromFragment(fragment, intent, requestCode, options); |
| 7002 | } |
| 7003 | |
| 7004 | @Override |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 7005 | public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions, |
| 7006 | int requestCode) { |
| 7007 | String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho; |
| 7008 | Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions); |
| 7009 | startActivityForResult(who, intent, requestCode, null); |
| 7010 | } |
| 7011 | |
| 7012 | @Override |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 7013 | public boolean onHasWindowAnimations() { |
| 7014 | return getWindow() != null; |
| 7015 | } |
| 7016 | |
| 7017 | @Override |
| 7018 | public int onGetWindowAnimations() { |
| 7019 | final Window w = getWindow(); |
| 7020 | return (w == null) ? 0 : w.getAttributes().windowAnimations; |
| 7021 | } |
| 7022 | |
Todd Kennedy | 434bd65 | 2015-05-04 12:29:50 -0700 | [diff] [blame] | 7023 | @Override |
| 7024 | public void onAttachFragment(Fragment fragment) { |
| 7025 | Activity.this.onAttachFragment(fragment); |
| 7026 | } |
| 7027 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 7028 | @Nullable |
| 7029 | @Override |
| 7030 | public View onFindViewById(int id) { |
| 7031 | return Activity.this.findViewById(id); |
| 7032 | } |
| 7033 | |
| 7034 | @Override |
| 7035 | public boolean onHasView() { |
| 7036 | final Window w = getWindow(); |
| 7037 | return (w != null && w.peekDecorView() != null); |
| 7038 | } |
| 7039 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7040 | } |