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 | /** |
| 1122 | * Called when activity start-up is complete (after {@link #onStart} |
| 1123 | * and {@link #onRestoreInstanceState} have been called). Applications will |
| 1124 | * generally not implement this method; it is intended for system |
| 1125 | * classes to do final initialization after application code has run. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1126 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | * <p><em>Derived classes must call through to the super class's |
| 1128 | * implementation of this method. If they do not, an exception will be |
| 1129 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1130 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1131 | * @param savedInstanceState If the activity is being re-initialized after |
| 1132 | * previously being shut down then this Bundle contains the data it most |
| 1133 | * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b> |
| 1134 | * @see #onCreate |
| 1135 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1136 | @CallSuper |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1137 | protected void onPostCreate(@Nullable Bundle savedInstanceState) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | if (!isChild()) { |
| 1139 | mTitleReady = true; |
| 1140 | onTitleChanged(getTitle(), getTitleColor()); |
| 1141 | } |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 1142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1143 | mCalled = true; |
| 1144 | } |
| 1145 | |
| 1146 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1147 | * 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] | 1148 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1149 | * <code>persistAcrossReboots</code>. |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1150 | * |
| 1151 | * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState} |
| 1152 | * @param persistentState The data caming from the PersistableBundle first |
| 1153 | * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}. |
| 1154 | * |
| 1155 | * @see #onCreate |
| 1156 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1157 | public void onPostCreate(@Nullable Bundle savedInstanceState, |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1158 | @Nullable PersistableBundle persistentState) { |
| 1159 | onPostCreate(savedInstanceState); |
| 1160 | } |
| 1161 | |
| 1162 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1163 | * Called after {@link #onCreate} — or after {@link #onRestart} when |
| 1164 | * 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] | 1165 | * user. It will be followed by {@link #onResume}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | * |
| 1167 | * <p><em>Derived classes must call through to the super class's |
| 1168 | * implementation of this method. If they do not, an exception will be |
| 1169 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1170 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | * @see #onCreate |
| 1172 | * @see #onStop |
| 1173 | * @see #onResume |
| 1174 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1175 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1176 | protected void onStart() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1177 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | mCalled = true; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1179 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1180 | mFragments.doLoaderStart(); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1181 | |
| 1182 | getApplication().dispatchActivityStarted(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * Called after {@link #onStop} when the current activity is being |
| 1187 | * re-displayed to the user (the user has navigated back to it). It will |
| 1188 | * be followed by {@link #onStart} and then {@link #onResume}. |
| 1189 | * |
| 1190 | * <p>For activities that are using raw {@link Cursor} objects (instead of |
| 1191 | * creating them through |
| 1192 | * {@link #managedQuery(android.net.Uri , String[], String, String[], String)}, |
| 1193 | * this is usually the place |
| 1194 | * where the cursor should be requeried (because you had deactivated it in |
| 1195 | * {@link #onStop}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1196 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | * <p><em>Derived classes must call through to the super class's |
| 1198 | * implementation of this method. If they do not, an exception will be |
| 1199 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1200 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | * @see #onStop |
| 1202 | * @see #onStart |
| 1203 | * @see #onResume |
| 1204 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1205 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | protected void onRestart() { |
| 1207 | mCalled = true; |
| 1208 | } |
| 1209 | |
| 1210 | /** |
Dianne Hackborn | 6bdd3a1 | 2015-08-05 15:01:28 -0700 | [diff] [blame] | 1211 | * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks |
| 1212 | * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended |
| 1213 | * to give the activity a hint that its state is no longer saved -- it will generally |
| 1214 | * be called after {@link #onSaveInstanceState} and prior to the activity being |
| 1215 | * resumed/started again. |
| 1216 | */ |
| 1217 | public void onStateNotSaved() { |
| 1218 | } |
| 1219 | |
| 1220 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or |
| 1222 | * {@link #onPause}, for your activity to start interacting with the user. |
| 1223 | * This is a good place to begin animations, open exclusive-access devices |
| 1224 | * (such as the camera), etc. |
| 1225 | * |
| 1226 | * <p>Keep in mind that onResume is not the best indicator that your activity |
| 1227 | * is visible to the user; a system window such as the keyguard may be in |
| 1228 | * front. Use {@link #onWindowFocusChanged} to know for certain that your |
| 1229 | * activity is visible to the user (for example, to resume a game). |
| 1230 | * |
| 1231 | * <p><em>Derived classes must call through to the super class's |
| 1232 | * implementation of this method. If they do not, an exception will be |
| 1233 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1234 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1235 | * @see #onRestoreInstanceState |
| 1236 | * @see #onRestart |
| 1237 | * @see #onPostResume |
| 1238 | * @see #onPause |
| 1239 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1240 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1241 | protected void onResume() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1242 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1243 | getApplication().dispatchActivityResumed(this); |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 1244 | mActivityTransitionState.onResume(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | mCalled = true; |
| 1246 | } |
| 1247 | |
| 1248 | /** |
| 1249 | * Called when activity resume is complete (after {@link #onResume} has |
| 1250 | * been called). Applications will generally not implement this method; |
| 1251 | * it is intended for system classes to do final setup after application |
| 1252 | * resume code has run. |
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 #onResume |
| 1259 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1260 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1261 | protected void onPostResume() { |
| 1262 | final Window win = getWindow(); |
| 1263 | if (win != null) win.makeActive(); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 1264 | if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | mCalled = true; |
| 1266 | } |
| 1267 | |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1268 | void setVoiceInteractor(IVoiceInteractor voiceInteractor) { |
| 1269 | if (voiceInteractor == null) { |
| 1270 | mVoiceInteractor = null; |
| 1271 | } else { |
| 1272 | mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this, |
| 1273 | Looper.myLooper()); |
| 1274 | } |
| 1275 | } |
| 1276 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 1278 | * Check whether this activity is running as part of a voice interaction with the user. |
| 1279 | * If true, it should perform its interaction with the user through the |
| 1280 | * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}. |
| 1281 | */ |
| 1282 | public boolean isVoiceInteraction() { |
| 1283 | return mVoiceInteractor != null; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
Dianne Hackborn | fb81d09 | 2015-08-03 17:14:46 -0700 | [diff] [blame] | 1287 | * Like {@link #isVoiceInteraction}, but only returns true if this is also the root |
| 1288 | * of a voice interaction. That is, returns true if this activity was directly |
| 1289 | * started by the voice interaction service as the initiation of a voice interaction. |
| 1290 | * Otherwise, for example if it was started by another activity while under voice |
| 1291 | * interaction, returns false. |
| 1292 | */ |
| 1293 | public boolean isVoiceInteractionRoot() { |
| 1294 | try { |
| 1295 | return mVoiceInteractor != null |
| 1296 | && ActivityManagerNative.getDefault().isRootVoiceInteraction(mToken); |
| 1297 | } catch (RemoteException e) { |
| 1298 | } |
| 1299 | return false; |
| 1300 | } |
| 1301 | |
| 1302 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 1303 | * Retrieve the active {@link VoiceInteractor} that the user is going through to |
| 1304 | * interact with this activity. |
| 1305 | */ |
| 1306 | public VoiceInteractor getVoiceInteractor() { |
| 1307 | return mVoiceInteractor; |
| 1308 | } |
| 1309 | |
| 1310 | /** |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 1311 | * Queries whether the currently enabled voice interaction service supports returning |
| 1312 | * a voice interactor for use by the activity. This is valid only for the duration of the |
| 1313 | * activity. |
| 1314 | * |
| 1315 | * @return whether the current voice interaction service supports local voice interaction |
| 1316 | */ |
| 1317 | public boolean isLocalVoiceInteractionSupported() { |
| 1318 | try { |
| 1319 | return ActivityManagerNative.getDefault().supportsLocalVoiceInteraction(); |
| 1320 | } catch (RemoteException re) { |
| 1321 | } |
| 1322 | return false; |
| 1323 | } |
| 1324 | |
| 1325 | /** |
| 1326 | * Starts a local voice interaction session. When ready, |
| 1327 | * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options |
| 1328 | * to the registered voice interaction service. |
| 1329 | * @param privateOptions a Bundle of private arguments to the current voice interaction service |
| 1330 | */ |
| 1331 | public void startLocalVoiceInteraction(Bundle privateOptions) { |
| 1332 | try { |
| 1333 | ActivityManagerNative.getDefault().startLocalVoiceInteraction(mToken, privateOptions); |
| 1334 | } catch (RemoteException re) { |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a |
| 1340 | * voice interaction session being started. You can now retrieve a voice interactor using |
| 1341 | * {@link #getVoiceInteractor()}. |
| 1342 | */ |
| 1343 | public void onLocalVoiceInteractionStarted() { |
| 1344 | Log.i(TAG, "onLocalVoiceInteractionStarted! " + getVoiceInteractor()); |
| 1345 | } |
| 1346 | |
| 1347 | /** |
| 1348 | * Callback to indicate that the local voice interaction has stopped for some |
| 1349 | * reason. |
| 1350 | */ |
| 1351 | public void onLocalVoiceInteractionStopped() { |
| 1352 | Log.i(TAG, "onLocalVoiceInteractionStopped :( " + getVoiceInteractor()); |
| 1353 | } |
| 1354 | |
| 1355 | /** |
| 1356 | * Request to terminate the current voice interaction that was previously started |
| 1357 | * using {@link #startLocalVoiceInteraction(Bundle)}. |
| 1358 | */ |
| 1359 | public void stopLocalVoiceInteraction() { |
| 1360 | try { |
| 1361 | ActivityManagerNative.getDefault().stopLocalVoiceInteraction(mToken); |
| 1362 | } catch (RemoteException re) { |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1367 | * This is called for activities that set launchMode to "singleTop" in |
| 1368 | * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} |
| 1369 | * flag when calling {@link #startActivity}. In either case, when the |
| 1370 | * activity is re-launched while at the top of the activity stack instead |
| 1371 | * of a new instance of the activity being started, onNewIntent() will be |
| 1372 | * called on the existing instance with the Intent that was used to |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1373 | * re-launch it. |
| 1374 | * |
| 1375 | * <p>An activity will always be paused before receiving a new intent, so |
| 1376 | * you can count on {@link #onResume} being called after this method. |
| 1377 | * |
| 1378 | * <p>Note that {@link #getIntent} still returns the original Intent. You |
| 1379 | * can use {@link #setIntent} to update it to this new Intent. |
| 1380 | * |
| 1381 | * @param intent The new intent that was started for the activity. |
| 1382 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | * @see #getIntent |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1384 | * @see #setIntent |
| 1385 | * @see #onResume |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1386 | */ |
| 1387 | protected void onNewIntent(Intent intent) { |
| 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * The hook for {@link ActivityThread} to save the state of this activity. |
| 1392 | * |
| 1393 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} |
| 1394 | * and {@link #saveManagedDialogs(android.os.Bundle)}. |
| 1395 | * |
| 1396 | * @param outState The bundle to save the state to. |
| 1397 | */ |
| 1398 | final void performSaveInstanceState(Bundle outState) { |
| 1399 | onSaveInstanceState(outState); |
| 1400 | saveManagedDialogs(outState); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 1401 | mActivityTransitionState.saveState(outState); |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 1402 | storeHasCurrentPermissionRequest(outState); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1403 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1407 | * The hook for {@link ActivityThread} to save the state of this activity. |
| 1408 | * |
| 1409 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} |
| 1410 | * and {@link #saveManagedDialogs(android.os.Bundle)}. |
| 1411 | * |
| 1412 | * @param outState The bundle to save the state to. |
| 1413 | * @param outPersistentState The bundle to save persistent state to. |
| 1414 | */ |
| 1415 | final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { |
| 1416 | onSaveInstanceState(outState, outPersistentState); |
| 1417 | saveManagedDialogs(outState); |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 1418 | storeHasCurrentPermissionRequest(outState); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1419 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState + |
| 1420 | ", " + outPersistentState); |
| 1421 | } |
| 1422 | |
| 1423 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | * Called to retrieve per-instance state from an activity before being killed |
| 1425 | * so that the state can be restored in {@link #onCreate} or |
| 1426 | * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method |
| 1427 | * will be passed to both). |
| 1428 | * |
| 1429 | * <p>This method is called before an activity may be killed so that when it |
| 1430 | * comes back some time in the future it can restore its state. For example, |
| 1431 | * if activity B is launched in front of activity A, and at some point activity |
| 1432 | * A is killed to reclaim resources, activity A will have a chance to save the |
| 1433 | * current state of its user interface via this method so that when the user |
| 1434 | * returns to activity A, the state of the user interface can be restored |
| 1435 | * via {@link #onCreate} or {@link #onRestoreInstanceState}. |
| 1436 | * |
| 1437 | * <p>Do not confuse this method with activity lifecycle callbacks such as |
| 1438 | * {@link #onPause}, which is always called when an activity is being placed |
| 1439 | * in the background or on its way to destruction, or {@link #onStop} which |
| 1440 | * is called before destruction. One example of when {@link #onPause} and |
| 1441 | * {@link #onStop} is called and not this method is when a user navigates back |
| 1442 | * from activity B to activity A: there is no need to call {@link #onSaveInstanceState} |
| 1443 | * on B because that particular instance will never be restored, so the |
| 1444 | * system avoids calling it. An example when {@link #onPause} is called and |
| 1445 | * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A: |
| 1446 | * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't |
| 1447 | * killed during the lifetime of B since the state of the user interface of |
| 1448 | * A will stay intact. |
| 1449 | * |
| 1450 | * <p>The default implementation takes care of most of the UI per-instance |
| 1451 | * state for you by calling {@link android.view.View#onSaveInstanceState()} on each |
| 1452 | * view in the hierarchy that has an id, and by saving the id of the currently |
| 1453 | * focused view (all of which is restored by the default implementation of |
| 1454 | * {@link #onRestoreInstanceState}). If you override this method to save additional |
| 1455 | * information not captured by each individual view, you will likely want to |
| 1456 | * call through to the default implementation, otherwise be prepared to save |
| 1457 | * all of the state of each view yourself. |
| 1458 | * |
| 1459 | * <p>If called, this method will occur before {@link #onStop}. There are |
| 1460 | * no guarantees about whether it will occur before or after {@link #onPause}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1461 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | * @param outState Bundle in which to place your saved state. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1463 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | * @see #onCreate |
| 1465 | * @see #onRestoreInstanceState |
| 1466 | * @see #onPause |
| 1467 | */ |
| 1468 | protected void onSaveInstanceState(Bundle outState) { |
| 1469 | outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState()); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1470 | Parcelable p = mFragments.saveAllState(); |
| 1471 | if (p != null) { |
| 1472 | outState.putParcelable(FRAGMENTS_TAG, p); |
| 1473 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1474 | getApplication().dispatchActivitySaveInstanceState(this, outState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | /** |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1478 | * This is the same as {@link #onSaveInstanceState} but is called for activities |
Craig Mautner | 43e52ed | 2014-06-16 17:18:52 -0700 | [diff] [blame] | 1479 | * created with the attribute {@link android.R.attr#persistableMode} set to |
| 1480 | * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed |
| 1481 | * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)} |
| 1482 | * 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] | 1483 | * |
| 1484 | * @param outState Bundle in which to place your saved state. |
| 1485 | * @param outPersistentState State which will be saved across reboots. |
| 1486 | * |
| 1487 | * @see #onSaveInstanceState(Bundle) |
| 1488 | * @see #onCreate |
| 1489 | * @see #onRestoreInstanceState(Bundle, PersistableBundle) |
| 1490 | * @see #onPause |
| 1491 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 1492 | public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 1493 | onSaveInstanceState(outState); |
| 1494 | } |
| 1495 | |
| 1496 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | * Save the state of any managed dialogs. |
| 1498 | * |
| 1499 | * @param outState place to store the saved state. |
| 1500 | */ |
| 1501 | private void saveManagedDialogs(Bundle outState) { |
| 1502 | if (mManagedDialogs == null) { |
| 1503 | return; |
| 1504 | } |
| 1505 | |
| 1506 | final int numDialogs = mManagedDialogs.size(); |
| 1507 | if (numDialogs == 0) { |
| 1508 | return; |
| 1509 | } |
| 1510 | |
| 1511 | Bundle dialogState = new Bundle(); |
| 1512 | |
| 1513 | int[] ids = new int[mManagedDialogs.size()]; |
| 1514 | |
| 1515 | // save each dialog's bundle, gather the ids |
| 1516 | for (int i = 0; i < numDialogs; i++) { |
| 1517 | final int key = mManagedDialogs.keyAt(i); |
| 1518 | ids[i] = key; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1519 | final ManagedDialog md = mManagedDialogs.valueAt(i); |
| 1520 | dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState()); |
| 1521 | if (md.mArgs != null) { |
| 1522 | dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs); |
| 1523 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids); |
| 1527 | outState.putBundle(SAVED_DIALOGS_TAG, dialogState); |
| 1528 | } |
| 1529 | |
| 1530 | |
| 1531 | /** |
| 1532 | * Called as part of the activity lifecycle when an activity is going into |
| 1533 | * the background, but has not (yet) been killed. The counterpart to |
| 1534 | * {@link #onResume}. |
| 1535 | * |
| 1536 | * <p>When activity B is launched in front of activity A, this callback will |
| 1537 | * be invoked on A. B will not be created until A's {@link #onPause} returns, |
| 1538 | * so be sure to not do anything lengthy here. |
| 1539 | * |
| 1540 | * <p>This callback is mostly used for saving any persistent state the |
| 1541 | * activity is editing, to present a "edit in place" model to the user and |
| 1542 | * making sure nothing is lost if there are not enough resources to start |
| 1543 | * the new activity without first killing this one. This is also a good |
| 1544 | * 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] | 1545 | * 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] | 1546 | * as fast as possible, or to close resources that are exclusive access |
| 1547 | * such as the camera. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1548 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1549 | * <p>In situations where the system needs more memory it may kill paused |
| 1550 | * processes to reclaim resources. Because of this, you should be sure |
| 1551 | * that all of your state is saved by the time you return from |
| 1552 | * this function. In general {@link #onSaveInstanceState} is used to save |
| 1553 | * per-instance state in the activity and this method is used to store |
| 1554 | * global persistent data (in content providers, files, etc.) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1555 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1556 | * <p>After receiving this call you will usually receive a following call |
| 1557 | * to {@link #onStop} (after the next activity has been resumed and |
| 1558 | * displayed), however in some cases there will be a direct call back to |
| 1559 | * {@link #onResume} without going through the stopped state. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1560 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1561 | * <p><em>Derived classes must call through to the super class's |
| 1562 | * implementation of this method. If they do not, an exception will be |
| 1563 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1564 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | * @see #onResume |
| 1566 | * @see #onSaveInstanceState |
| 1567 | * @see #onStop |
| 1568 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1569 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1570 | protected void onPause() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1571 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1572 | getApplication().dispatchActivityPaused(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1573 | mCalled = true; |
| 1574 | } |
| 1575 | |
| 1576 | /** |
| 1577 | * Called as part of the activity lifecycle when an activity is about to go |
| 1578 | * into the background as the result of user choice. For example, when the |
| 1579 | * user presses the Home key, {@link #onUserLeaveHint} will be called, but |
| 1580 | * when an incoming phone call causes the in-call Activity to be automatically |
| 1581 | * brought to the foreground, {@link #onUserLeaveHint} will not be called on |
| 1582 | * the activity being interrupted. In cases when it is invoked, this method |
| 1583 | * is called right before the activity's {@link #onPause} callback. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1584 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | * <p>This callback and {@link #onUserInteraction} are intended to help |
| 1586 | * activities manage status bar notifications intelligently; specifically, |
| 1587 | * for helping activities determine the proper time to cancel a notfication. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1588 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | * @see #onUserInteraction() |
| 1590 | */ |
| 1591 | protected void onUserLeaveHint() { |
| 1592 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1593 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | /** |
| 1595 | * Generate a new thumbnail for this activity. This method is called before |
| 1596 | * pausing the activity, and should draw into <var>outBitmap</var> the |
| 1597 | * imagery for the desired thumbnail in the dimensions of that bitmap. It |
| 1598 | * can use the given <var>canvas</var>, which is configured to draw into the |
| 1599 | * bitmap, for rendering if desired. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1600 | * |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 1601 | * <p>The default implementation returns fails and does not draw a thumbnail; |
| 1602 | * this will result in the platform creating its own thumbnail if needed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1603 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1604 | * @param outBitmap The bitmap to contain the thumbnail. |
| 1605 | * @param canvas Can be used to render into the bitmap. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1606 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | * @return Return true if you have drawn into the bitmap; otherwise after |
| 1608 | * you return it will be filled with a default thumbnail. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1609 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | * @see #onCreateDescription |
| 1611 | * @see #onSaveInstanceState |
| 1612 | * @see #onPause |
| 1613 | */ |
| 1614 | public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) { |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 1615 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /** |
| 1619 | * Generate a new description for this activity. This method is called |
| 1620 | * before pausing the activity and can, if desired, return some textual |
| 1621 | * description of its current state to be displayed to the user. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1622 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1623 | * <p>The default implementation returns null, which will cause you to |
| 1624 | * inherit the description from the previous activity. If all activities |
| 1625 | * return null, generally the label of the top activity will be used as the |
| 1626 | * description. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1627 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | * @return A description of what the user is doing. It should be short and |
| 1629 | * sweet (only a few words). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1630 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1631 | * @see #onCreateThumbnail |
| 1632 | * @see #onSaveInstanceState |
| 1633 | * @see #onPause |
| 1634 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1635 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | public CharSequence onCreateDescription() { |
| 1637 | return null; |
| 1638 | } |
| 1639 | |
| 1640 | /** |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1641 | * This is called when the user is requesting an assist, to build a full |
| 1642 | * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current |
| 1643 | * application. You can override this method to place into the bundle anything |
| 1644 | * 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] | 1645 | * of the assist Intent. |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1646 | * |
| 1647 | * <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] | 1648 | * been registered with {@link Application#registerOnProvideAssistDataListener |
| 1649 | * Application.registerOnProvideAssistDataListener}. |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1650 | */ |
| 1651 | public void onProvideAssistData(Bundle data) { |
Dianne Hackborn | a83ce1d | 2015-03-11 15:16:13 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | /** |
| 1655 | * This is called when the user is requesting an assist, to provide references |
| 1656 | * to content related to the current activity. Before being called, the |
| 1657 | * {@code outContent} Intent is filled with the base Intent of the activity (the Intent |
| 1658 | * returned by {@link #getIntent()}). The Intent's extras are stripped of any types |
| 1659 | * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and |
| 1660 | * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and |
| 1661 | * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent. |
| 1662 | * |
| 1663 | * <p>Custom implementation may adjust the content intent to better reflect the top-level |
| 1664 | * context of the activity, and fill in its ClipData with additional content of |
| 1665 | * interest that the user is currently viewing. For example, an image gallery application |
| 1666 | * that has launched in to an activity allowing the user to swipe through pictures should |
| 1667 | * modify the intent to reference the current image they are looking it; such an |
| 1668 | * application when showing a list of pictures should add a ClipData that has |
| 1669 | * references to all of the pictures currently visible on screen.</p> |
| 1670 | * |
| 1671 | * @param outContent The assist content to return. |
| 1672 | */ |
| 1673 | public void onProvideAssistContent(AssistContent outContent) { |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 1676 | @Override |
| 1677 | public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, Menu menu) { |
| 1678 | if (menu == null) { |
| 1679 | return; |
| 1680 | } |
| 1681 | KeyboardShortcutGroup group = null; |
| 1682 | int menuSize = menu.size(); |
| 1683 | for (int i = 0; i < menuSize; ++i) { |
| 1684 | final MenuItem item = menu.getItem(i); |
| 1685 | final CharSequence title = item.getTitle(); |
| 1686 | final char alphaShortcut = item.getAlphabeticShortcut(); |
| 1687 | if (title != null && alphaShortcut != MIN_VALUE) { |
| 1688 | if (group == null) { |
Clara Bayarri | ada88b3 | 2016-02-02 18:08:01 +0000 | [diff] [blame] | 1689 | final int resource = mApplication.getApplicationInfo().labelRes; |
| 1690 | group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 1691 | } |
| 1692 | group.addItem(new KeyboardShortcutInfo( |
| 1693 | title, alphaShortcut, KeyEvent.META_CTRL_ON)); |
| 1694 | } |
| 1695 | } |
| 1696 | if (group != null) { |
| 1697 | data.add(group); |
| 1698 | } |
| 1699 | } |
| 1700 | |
Dianne Hackborn | f9c5e0f | 2013-01-23 14:39:13 -0800 | [diff] [blame] | 1701 | /** |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 1702 | * Ask to have the current assistant shown to the user. This only works if the calling |
| 1703 | * activity is the current foreground activity. It is the same as calling |
| 1704 | * {@link android.service.voice.VoiceInteractionService#showSession |
| 1705 | * VoiceInteractionService.showSession} and requesting all of the possible context. |
| 1706 | * The receiver will always see |
| 1707 | * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set. |
| 1708 | * @return Returns true if the assistant was successfully invoked, else false. For example |
| 1709 | * false will be returned if the caller is not the current top activity. |
| 1710 | */ |
| 1711 | public boolean showAssist(Bundle args) { |
| 1712 | try { |
| 1713 | return ActivityManagerNative.getDefault().showAssistFromActivity(mToken, args); |
| 1714 | } catch (RemoteException e) { |
| 1715 | } |
| 1716 | return false; |
| 1717 | } |
| 1718 | |
| 1719 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | * Called when you are no longer visible to the user. You will next |
| 1721 | * receive either {@link #onRestart}, {@link #onDestroy}, or nothing, |
| 1722 | * depending on later user activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1723 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1724 | * <p>Note that this method may never be called, in low memory situations |
| 1725 | * where the system does not have enough memory to keep your activity's |
| 1726 | * process running after its {@link #onPause} method is called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1727 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1728 | * <p><em>Derived classes must call through to the super class's |
| 1729 | * implementation of this method. If they do not, an exception will be |
| 1730 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1731 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1732 | * @see #onRestart |
| 1733 | * @see #onResume |
| 1734 | * @see #onSaveInstanceState |
| 1735 | * @see #onDestroy |
| 1736 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1737 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | protected void onStop() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1739 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this); |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 1740 | if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 1741 | mActivityTransitionState.onStop(); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1742 | getApplication().dispatchActivityStopped(this); |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 1743 | mTranslucentCallback = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1744 | mCalled = true; |
| 1745 | } |
| 1746 | |
| 1747 | /** |
| 1748 | * Perform any final cleanup before an activity is destroyed. This can |
| 1749 | * happen either because the activity is finishing (someone called |
| 1750 | * {@link #finish} on it, or because the system is temporarily destroying |
| 1751 | * this instance of the activity to save space. You can distinguish |
| 1752 | * between these two scenarios with the {@link #isFinishing} method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1753 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1754 | * <p><em>Note: do not count on this method being called as a place for |
| 1755 | * saving data! For example, if an activity is editing data in a content |
| 1756 | * provider, those edits should be committed in either {@link #onPause} or |
| 1757 | * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to |
| 1758 | * free resources like threads that are associated with an activity, so |
| 1759 | * that a destroyed activity does not leave such things around while the |
| 1760 | * rest of its application is still running. There are situations where |
| 1761 | * the system will simply kill the activity's hosting process without |
| 1762 | * calling this method (or any others) in it, so it should not be used to |
| 1763 | * do things that are intended to remain around after the process goes |
| 1764 | * away. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1765 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1766 | * <p><em>Derived classes must call through to the super class's |
| 1767 | * implementation of this method. If they do not, an exception will be |
| 1768 | * thrown.</em></p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1769 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1770 | * @see #onPause |
| 1771 | * @see #onStop |
| 1772 | * @see #finish |
| 1773 | * @see #isFinishing |
| 1774 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1775 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1776 | protected void onDestroy() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1777 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1778 | mCalled = true; |
| 1779 | |
| 1780 | // dismiss any dialogs we are managing. |
| 1781 | if (mManagedDialogs != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1782 | final int numDialogs = mManagedDialogs.size(); |
| 1783 | for (int i = 0; i < numDialogs; i++) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1784 | final ManagedDialog md = mManagedDialogs.valueAt(i); |
| 1785 | if (md.mDialog.isShowing()) { |
| 1786 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1787 | } |
| 1788 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1789 | mManagedDialogs = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1790 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | |
| 1792 | // close any cursors we are managing. |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 1793 | synchronized (mManagedCursors) { |
| 1794 | int numCursors = mManagedCursors.size(); |
| 1795 | for (int i = 0; i < numCursors; i++) { |
| 1796 | ManagedCursor c = mManagedCursors.get(i); |
| 1797 | if (c != null) { |
| 1798 | c.mCursor.close(); |
| 1799 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | } |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 1801 | mManagedCursors.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1802 | } |
Amith Yamasani | 4986044 | 2010-03-17 20:54:10 -0700 | [diff] [blame] | 1803 | |
| 1804 | // Close any open search dialog |
| 1805 | if (mSearchManager != null) { |
| 1806 | mSearchManager.stopSearch(); |
| 1807 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1808 | |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 1809 | if (mActionBar != null) { |
| 1810 | mActionBar.onDestroy(); |
| 1811 | } |
| 1812 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1813 | getApplication().dispatchActivityDestroyed(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | /** |
Dianne Hackborn | ebc15ef | 2013-10-09 17:36:57 -0700 | [diff] [blame] | 1817 | * Report to the system that your app is now fully drawn, purely for diagnostic |
| 1818 | * purposes (calling it does not impact the visible behavior of the activity). |
| 1819 | * This is only used to help instrument application launch times, so that the |
| 1820 | * app can report when it is fully in a usable state; without this, the only thing |
| 1821 | * the system itself can determine is the point at which the activity's window |
| 1822 | * 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] | 1823 | * measurement, you should always call this method after first launch (when |
Dianne Hackborn | ebc15ef | 2013-10-09 17:36:57 -0700 | [diff] [blame] | 1824 | * {@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] | 1825 | * entirely drawn your UI and populated with all of the significant data. You |
| 1826 | * can safely call this method any time after first launch as well, in which case |
| 1827 | * it will simply be ignored. |
| 1828 | */ |
| 1829 | public void reportFullyDrawn() { |
| 1830 | if (mDoReportFullyDrawn) { |
| 1831 | mDoReportFullyDrawn = false; |
| 1832 | try { |
| 1833 | ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken); |
| 1834 | } catch (RemoteException e) { |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | /** |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1840 | * Called by the system when the activity changes from fullscreen mode to multi-window mode and |
| 1841 | * visa-versa. |
| 1842 | * @see android.R.attr#resizeableActivity |
| 1843 | * |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1844 | * @param inMultiWindow True if the activity is in multi-window mode. |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1845 | */ |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1846 | @CallSuper |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1847 | public void onMultiWindowChanged(boolean inMultiWindow) { |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1848 | if (DEBUG_LIFECYCLE) Slog.v(TAG, |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1849 | "onMultiWindowChanged " + this + ": " + inMultiWindow); |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 1850 | mFragments.dispatchMultiWindowChanged(inMultiWindow); |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1851 | if (mWindow != null) { |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1852 | mWindow.onMultiWindowChanged(); |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1853 | } |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | /** |
| 1857 | * Returns true if the activity is currently in multi-window mode. |
| 1858 | * @see android.R.attr#resizeableActivity |
| 1859 | * |
| 1860 | * @return True if the activity is in multi-window mode. |
| 1861 | */ |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1862 | public boolean inMultiWindow() { |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1863 | try { |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1864 | return ActivityManagerNative.getDefault().inMultiWindow(mToken); |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1865 | } catch (RemoteException e) { |
| 1866 | } |
| 1867 | return false; |
| 1868 | } |
| 1869 | |
| 1870 | /** |
| 1871 | * Called by the system when the activity changes to and from picture-in-picture mode. |
| 1872 | * @see android.R.attr#supportsPictureInPicture |
| 1873 | * |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1874 | * @param inPictureInPicture True if the activity is in picture-in-picture mode. |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1875 | */ |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 1876 | @CallSuper |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1877 | public void onPictureInPictureChanged(boolean inPictureInPicture) { |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1878 | if (DEBUG_LIFECYCLE) Slog.v(TAG, |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1879 | "onPictureInPictureChanged " + this + ": " + inPictureInPicture); |
Wale Ogunwale | 7c79681 | 2016-01-29 21:13:50 -0800 | [diff] [blame] | 1880 | mFragments.dispatchPictureInPictureChanged(inPictureInPicture); |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | /** |
| 1884 | * Returns true if the activity is currently in picture-in-picture mode. |
| 1885 | * @see android.R.attr#supportsPictureInPicture |
| 1886 | * |
| 1887 | * @return True if the activity is in picture-in-picture mode. |
| 1888 | */ |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1889 | public boolean inPictureInPicture() { |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1890 | try { |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1891 | return ActivityManagerNative.getDefault().inPictureInPicture(mToken); |
Wale Ogunwale | 5f98609 | 2015-12-04 15:35:38 -0800 | [diff] [blame] | 1892 | } catch (RemoteException e) { |
| 1893 | } |
| 1894 | return false; |
| 1895 | } |
| 1896 | |
| 1897 | /** |
Wale Ogunwale | 9c604c7 | 2015-12-06 18:42:57 -0800 | [diff] [blame] | 1898 | * Puts the activity in picture-in-picture mode. |
| 1899 | * @see android.R.attr#supportsPictureInPicture |
| 1900 | */ |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1901 | public void enterPictureInPicture() { |
Wale Ogunwale | 9c604c7 | 2015-12-06 18:42:57 -0800 | [diff] [blame] | 1902 | try { |
Wale Ogunwale | 3b93a4d | 2016-01-29 17:46:53 -0800 | [diff] [blame] | 1903 | ActivityManagerNative.getDefault().enterPictureInPicture(mToken); |
Wale Ogunwale | 9c604c7 | 2015-12-06 18:42:57 -0800 | [diff] [blame] | 1904 | } catch (RemoteException e) { |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1909 | * Called by the system when the device configuration changes while your |
| 1910 | * activity is running. Note that this will <em>only</em> be called if |
| 1911 | * you have selected configurations you would like to handle with the |
| 1912 | * {@link android.R.attr#configChanges} attribute in your manifest. If |
| 1913 | * any configuration change occurs that is not selected to be reported |
| 1914 | * by that attribute, then instead of reporting it the system will stop |
| 1915 | * and restart the activity (to have it launched with the new |
| 1916 | * configuration). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1917 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1918 | * <p>At the time that this function has been called, your Resources |
| 1919 | * object will have been updated to return resource values matching the |
| 1920 | * new configuration. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1921 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1922 | * @param newConfig The new device configuration. |
| 1923 | */ |
| 1924 | public void onConfigurationChanged(Configuration newConfig) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 1925 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1926 | mCalled = true; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 1927 | |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1928 | mFragments.dispatchConfigurationChanged(newConfig); |
| 1929 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1930 | if (mWindow != null) { |
| 1931 | // Pass the configuration changed event to the window |
| 1932 | mWindow.onConfigurationChanged(newConfig); |
| 1933 | } |
Adam Powell | 45c0b19 | 2011-07-28 15:11:57 -0700 | [diff] [blame] | 1934 | |
| 1935 | if (mActionBar != null) { |
| 1936 | // Do this last; the action bar will need to access |
| 1937 | // view changes from above. |
| 1938 | mActionBar.onConfigurationChanged(newConfig); |
| 1939 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1940 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1941 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1942 | /** |
| 1943 | * If this activity is being destroyed because it can not handle a |
| 1944 | * configuration parameter being changed (and thus its |
| 1945 | * {@link #onConfigurationChanged(Configuration)} method is |
| 1946 | * <em>not</em> being called), then you can use this method to discover |
| 1947 | * the set of changes that have occurred while in the process of being |
| 1948 | * destroyed. Note that there is no guarantee that these will be |
| 1949 | * accurate (other changes could have happened at any time), so you should |
| 1950 | * only use this as an optimization hint. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1951 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1952 | * @return Returns a bit field of the configuration parameters that are |
| 1953 | * changing, as defined by the {@link android.content.res.Configuration} |
| 1954 | * class. |
| 1955 | */ |
| 1956 | public int getChangingConfigurations() { |
| 1957 | return mConfigChangeFlags; |
| 1958 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1959 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1960 | /** |
| 1961 | * Retrieve the non-configuration instance data that was previously |
| 1962 | * returned by {@link #onRetainNonConfigurationInstance()}. This will |
| 1963 | * be available from the initial {@link #onCreate} and |
| 1964 | * {@link #onStart} calls to the new instance, allowing you to extract |
| 1965 | * any useful dynamic state from the previous instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1966 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1967 | * <p>Note that the data you retrieve here should <em>only</em> be used |
| 1968 | * as an optimization for handling configuration changes. You should always |
| 1969 | * be able to handle getting a null pointer back, and an activity must |
| 1970 | * still be able to restore itself to its previous state (through the |
| 1971 | * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this |
| 1972 | * function returns null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1973 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1974 | * @return Returns the object previously returned by |
| 1975 | * {@link #onRetainNonConfigurationInstance()}. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 1976 | * |
| 1977 | * @deprecated Use the new {@link Fragment} API |
| 1978 | * {@link Fragment#setRetainInstance(boolean)} instead; this is also |
| 1979 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1980 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1981 | @Nullable |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 1982 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1983 | public Object getLastNonConfigurationInstance() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1984 | return mLastNonConfigurationInstances != null |
| 1985 | ? mLastNonConfigurationInstances.activity : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1986 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1987 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1988 | /** |
| 1989 | * Called by the system, as part of destroying an |
| 1990 | * activity due to a configuration change, when it is known that a new |
| 1991 | * instance will immediately be created for the new configuration. You |
| 1992 | * can return any object you like here, including the activity instance |
| 1993 | * itself, which can later be retrieved by calling |
| 1994 | * {@link #getLastNonConfigurationInstance()} in the new activity |
| 1995 | * instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 1996 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 1997 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 1998 | * or later, consider instead using a {@link Fragment} with |
| 1999 | * {@link Fragment#setRetainInstance(boolean) |
| 2000 | * Fragment.setRetainInstance(boolean}.</em> |
| 2001 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2002 | * <p>This function is called purely as an optimization, and you must |
| 2003 | * not rely on it being called. When it is called, a number of guarantees |
| 2004 | * will be made to help optimize configuration switching: |
| 2005 | * <ul> |
| 2006 | * <li> The function will be called between {@link #onStop} and |
| 2007 | * {@link #onDestroy}. |
| 2008 | * <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] | 2009 | * created after this one's {@link #onDestroy()} is called. In particular, |
| 2010 | * <em>no</em> messages will be dispatched during this time (when the returned |
| 2011 | * 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] | 2012 | * <li> The object you return here will <em>always</em> be available from |
| 2013 | * the {@link #getLastNonConfigurationInstance()} method of the following |
| 2014 | * activity instance as described there. |
| 2015 | * </ul> |
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 | * <p>These guarantees are designed so that an activity can use this API |
| 2018 | * to propagate extensive state from the old to new activity instance, from |
| 2019 | * loaded bitmaps, to network connections, to evenly actively running |
| 2020 | * threads. Note that you should <em>not</em> propagate any data that |
| 2021 | * may change based on the configuration, including any data loaded from |
| 2022 | * resources such as strings, layouts, or drawables. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2023 | * |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 2024 | * <p>The guarantee of no message handling during the switch to the next |
| 2025 | * activity simplifies use with active objects. For example if your retained |
| 2026 | * state is an {@link android.os.AsyncTask} you are guaranteed that its |
| 2027 | * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will |
| 2028 | * not be called from the call here until you execute the next instance's |
| 2029 | * {@link #onCreate(Bundle)}. (Note however that there is of course no such |
| 2030 | * guarantee for {@link android.os.AsyncTask#doInBackground} since that is |
| 2031 | * running in a separate thread.) |
| 2032 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2033 | * @return Return any Object holding the desired state to propagate to the |
| 2034 | * next activity instance. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2035 | * |
| 2036 | * @deprecated Use the new {@link Fragment} API |
| 2037 | * {@link Fragment#setRetainInstance(boolean)} instead; this is also |
| 2038 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2039 | */ |
| 2040 | public Object onRetainNonConfigurationInstance() { |
| 2041 | return null; |
| 2042 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2043 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2044 | /** |
| 2045 | * Retrieve the non-configuration instance data that was previously |
| 2046 | * returned by {@link #onRetainNonConfigurationChildInstances()}. This will |
| 2047 | * be available from the initial {@link #onCreate} and |
| 2048 | * {@link #onStart} calls to the new instance, allowing you to extract |
| 2049 | * any useful dynamic state from the previous instance. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2050 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2051 | * <p>Note that the data you retrieve here should <em>only</em> be used |
| 2052 | * as an optimization for handling configuration changes. You should always |
| 2053 | * be able to handle getting a null pointer back, and an activity must |
| 2054 | * still be able to restore itself to its previous state (through the |
| 2055 | * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this |
| 2056 | * function returns null. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2057 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2058 | * @return Returns the object previously returned by |
| 2059 | * {@link #onRetainNonConfigurationChildInstances()} |
| 2060 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2061 | @Nullable |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2062 | HashMap<String, Object> getLastNonConfigurationChildInstances() { |
| 2063 | return mLastNonConfigurationInstances != null |
| 2064 | ? mLastNonConfigurationInstances.children : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2065 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2066 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2067 | /** |
| 2068 | * This method is similar to {@link #onRetainNonConfigurationInstance()} except that |
| 2069 | * it should return either a mapping from child activity id strings to arbitrary objects, |
| 2070 | * or null. This method is intended to be used by Activity framework subclasses that control a |
| 2071 | * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply |
| 2072 | * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null. |
| 2073 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2074 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2075 | HashMap<String,Object> onRetainNonConfigurationChildInstances() { |
| 2076 | return null; |
| 2077 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2078 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2079 | NonConfigurationInstances retainNonConfigurationInstances() { |
| 2080 | Object activity = onRetainNonConfigurationInstance(); |
| 2081 | HashMap<String, Object> children = onRetainNonConfigurationChildInstances(); |
Adam Powell | 44ba79e | 2016-02-04 16:20:37 -0800 | [diff] [blame] | 2082 | FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig(); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2083 | ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig(); |
| 2084 | if (activity == null && children == null && fragments == null && loaders == null |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 2085 | && mVoiceInteractor == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2086 | return null; |
| 2087 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2088 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2089 | NonConfigurationInstances nci = new NonConfigurationInstances(); |
| 2090 | nci.activity = activity; |
| 2091 | nci.children = children; |
| 2092 | nci.fragments = fragments; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2093 | nci.loaders = loaders; |
Dianne Hackborn | 57dd737 | 2015-07-27 18:11:14 -0700 | [diff] [blame] | 2094 | if (mVoiceInteractor != null) { |
| 2095 | mVoiceInteractor.retainInstance(); |
| 2096 | nci.voiceInteractor = mVoiceInteractor; |
| 2097 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2098 | return nci; |
| 2099 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2101 | public void onLowMemory() { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 2102 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2103 | mCalled = true; |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 2104 | mFragments.dispatchLowMemory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2105 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2106 | |
| 2107 | public void onTrimMemory(int level) { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 2108 | if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 2109 | mCalled = true; |
| 2110 | mFragments.dispatchTrimMemory(level); |
| 2111 | } |
| 2112 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2113 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 2114 | * Return the FragmentManager for interacting with fragments associated |
| 2115 | * with this activity. |
| 2116 | */ |
| 2117 | public FragmentManager getFragmentManager() { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2118 | return mFragments.getFragmentManager(); |
Dianne Hackborn | 9e14e9f3 | 2010-07-14 11:07:38 -0700 | [diff] [blame] | 2119 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2120 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2121 | /** |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 2122 | * Called when a Fragment is being attached to this activity, immediately |
| 2123 | * after the call to its {@link Fragment#onAttach Fragment.onAttach()} |
| 2124 | * method and before {@link Fragment#onCreate Fragment.onCreate()}. |
| 2125 | */ |
| 2126 | public void onAttachFragment(Fragment fragment) { |
| 2127 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2128 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 2129 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2130 | * Wrapper around |
| 2131 | * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)} |
| 2132 | * that gives the resulting {@link Cursor} to call |
| 2133 | * {@link #startManagingCursor} so that the activity will manage its |
| 2134 | * lifecycle for you. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2135 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2136 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2137 | * or later, consider instead using {@link LoaderManager} instead, available |
| 2138 | * via {@link #getLoaderManager()}.</em> |
| 2139 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2140 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using |
| 2141 | * this method, because the activity will do that for you at the appropriate time. However, if |
| 2142 | * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will |
| 2143 | * not</em> automatically close the cursor and, in that case, you must call |
| 2144 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2145 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2146 | * @param uri The URI of the content provider to query. |
| 2147 | * @param projection List of columns to return. |
| 2148 | * @param selection SQL WHERE clause. |
| 2149 | * @param sortOrder SQL ORDER BY clause. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2150 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2151 | * @return The Cursor that was returned by query(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2152 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2153 | * @see ContentResolver#query(android.net.Uri , String[], String, String[], String) |
| 2154 | * @see #startManagingCursor |
| 2155 | * @hide |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2156 | * |
| 2157 | * @deprecated Use {@link CursorLoader} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2158 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2159 | @Deprecated |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2160 | public final Cursor managedQuery(Uri uri, String[] projection, String selection, |
| 2161 | String sortOrder) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2162 | Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder); |
| 2163 | if (c != null) { |
| 2164 | startManagingCursor(c); |
| 2165 | } |
| 2166 | return c; |
| 2167 | } |
| 2168 | |
| 2169 | /** |
| 2170 | * Wrapper around |
| 2171 | * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)} |
| 2172 | * that gives the resulting {@link Cursor} to call |
| 2173 | * {@link #startManagingCursor} so that the activity will manage its |
| 2174 | * lifecycle for you. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2175 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2176 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2177 | * or later, consider instead using {@link LoaderManager} instead, available |
| 2178 | * via {@link #getLoaderManager()}.</em> |
| 2179 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2180 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using |
| 2181 | * this method, because the activity will do that for you at the appropriate time. However, if |
| 2182 | * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will |
| 2183 | * not</em> automatically close the cursor and, in that case, you must call |
| 2184 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2185 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2186 | * @param uri The URI of the content provider to query. |
| 2187 | * @param projection List of columns to return. |
| 2188 | * @param selection SQL WHERE clause. |
| 2189 | * @param selectionArgs The arguments to selection, if any ?s are pesent |
| 2190 | * @param sortOrder SQL ORDER BY clause. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2191 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2192 | * @return The Cursor that was returned by query(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2193 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2194 | * @see ContentResolver#query(android.net.Uri , String[], String, String[], String) |
| 2195 | * @see #startManagingCursor |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2196 | * |
| 2197 | * @deprecated Use {@link CursorLoader} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2198 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2199 | @Deprecated |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2200 | public final Cursor managedQuery(Uri uri, String[] projection, String selection, |
| 2201 | String[] selectionArgs, String sortOrder) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2202 | Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder); |
| 2203 | if (c != null) { |
| 2204 | startManagingCursor(c); |
| 2205 | } |
| 2206 | return c; |
| 2207 | } |
| 2208 | |
| 2209 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2210 | * This method allows the activity to take care of managing the given |
| 2211 | * {@link Cursor}'s lifecycle for you based on the activity's lifecycle. |
| 2212 | * That is, when the activity is stopped it will automatically call |
| 2213 | * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted |
| 2214 | * it will call {@link Cursor#requery} for you. When the activity is |
| 2215 | * destroyed, all managed Cursors will be closed automatically. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2216 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2217 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 2218 | * or later, consider instead using {@link LoaderManager} instead, available |
| 2219 | * via {@link #getLoaderManager()}.</em> |
| 2220 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2221 | * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from |
| 2222 | * {@link #managedQuery}, because the activity will do that for you at the appropriate time. |
| 2223 | * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system |
| 2224 | * <em>will not</em> automatically close the cursor and, in that case, you must call |
| 2225 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2226 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2227 | * @param c The Cursor to be managed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2228 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2229 | * @see #managedQuery(android.net.Uri , String[], String, String[], String) |
| 2230 | * @see #stopManagingCursor |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2231 | * |
| 2232 | * @deprecated Use the new {@link android.content.CursorLoader} class with |
| 2233 | * {@link LoaderManager} instead; this is also |
| 2234 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2235 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2236 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2237 | public void startManagingCursor(Cursor c) { |
| 2238 | synchronized (mManagedCursors) { |
| 2239 | mManagedCursors.add(new ManagedCursor(c)); |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | /** |
| 2244 | * Given a Cursor that was previously given to |
| 2245 | * {@link #startManagingCursor}, stop the activity's management of that |
| 2246 | * cursor. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2247 | * |
Joe Fernandez | 8ef6970 | 2011-09-02 14:32:22 -0700 | [diff] [blame] | 2248 | * <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] | 2249 | * 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] | 2250 | * {@link Cursor#close()}.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2251 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2252 | * @param c The Cursor that was being managed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2253 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2254 | * @see #startManagingCursor |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 2255 | * |
| 2256 | * @deprecated Use the new {@link android.content.CursorLoader} class with |
| 2257 | * {@link LoaderManager} instead; this is also |
| 2258 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2259 | */ |
Jason parks | 6ed50de | 2010-08-25 10:18:50 -0500 | [diff] [blame] | 2260 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2261 | public void stopManagingCursor(Cursor c) { |
| 2262 | synchronized (mManagedCursors) { |
| 2263 | final int N = mManagedCursors.size(); |
| 2264 | for (int i=0; i<N; i++) { |
| 2265 | ManagedCursor mc = mManagedCursors.get(i); |
| 2266 | if (mc.mCursor == c) { |
| 2267 | mManagedCursors.remove(i); |
| 2268 | break; |
| 2269 | } |
| 2270 | } |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | /** |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 2275 | * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD} |
| 2276 | * this is a no-op. |
Dianne Hackborn | 4f3867e | 2010-12-14 22:09:51 -0800 | [diff] [blame] | 2277 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2278 | */ |
Dianne Hackborn | d3efa39 | 2010-09-01 17:34:12 -0700 | [diff] [blame] | 2279 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2280 | public void setPersistent(boolean isPersistent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | /** |
| 2284 | * Finds a view that was identified by the id attribute from the XML that |
| 2285 | * was processed in {@link #onCreate}. |
| 2286 | * |
| 2287 | * @return The view if found or null otherwise. |
| 2288 | */ |
Scott Kennedy | c051955 | 2015-02-11 15:33:10 -0800 | [diff] [blame] | 2289 | @Nullable |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 2290 | public View findViewById(@IdRes int id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2291 | return getWindow().findViewById(id); |
| 2292 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2293 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2294 | /** |
| 2295 | * Retrieve a reference to this activity's ActionBar. |
Adam Powell | 42c0fe8 | 2010-08-10 16:36:56 -0700 | [diff] [blame] | 2296 | * |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2297 | * @return The Activity's ActionBar, or null if it does not have one. |
| 2298 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2299 | @Nullable |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2300 | public ActionBar getActionBar() { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2301 | initWindowDecorActionBar(); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2302 | return mActionBar; |
| 2303 | } |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2304 | |
| 2305 | /** |
| 2306 | * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this |
| 2307 | * Activity window. |
| 2308 | * |
| 2309 | * <p>When set to a non-null value the {@link #getActionBar()} method will return |
| 2310 | * an {@link ActionBar} object that can be used to control the given toolbar as if it were |
| 2311 | * a traditional window decor action bar. The toolbar's menu will be populated with the |
| 2312 | * Activity's options menu and the navigation button will be wired through the standard |
| 2313 | * {@link android.R.id#home home} menu select action.</p> |
| 2314 | * |
| 2315 | * <p>In order to use a Toolbar within the Activity's window content the application |
| 2316 | * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p> |
| 2317 | * |
Chris Banes | c7d6c32 | 2016-01-27 14:09:16 +0000 | [diff] [blame] | 2318 | * @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] | 2319 | */ |
Adam Powell | 3778014 | 2014-06-01 13:31:00 -0700 | [diff] [blame] | 2320 | public void setActionBar(@Nullable Toolbar toolbar) { |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 2321 | final ActionBar ab = getActionBar(); |
| 2322 | if (ab instanceof WindowDecorActionBar) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2323 | throw new IllegalStateException("This Activity already has an action bar supplied " + |
| 2324 | "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " + |
| 2325 | "android:windowActionBar to false in your theme to use a Toolbar instead."); |
| 2326 | } |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 2327 | |
| 2328 | // If we reach here then we're setting a new action bar |
| 2329 | // 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] | 2330 | mMenuInflater = null; |
| 2331 | |
Chris Banes | 21b2577 | 2016-01-04 20:41:59 +0000 | [diff] [blame] | 2332 | // If we have an action bar currently, destroy it |
| 2333 | if (ab != null) { |
| 2334 | ab.onDestroy(); |
| 2335 | } |
| 2336 | |
Chris Banes | c7d6c32 | 2016-01-27 14:09:16 +0000 | [diff] [blame] | 2337 | if (toolbar != null) { |
| 2338 | final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this); |
| 2339 | mActionBar = tbab; |
| 2340 | mWindow.setCallback(tbab.getWrappedWindowCallback()); |
| 2341 | } else { |
| 2342 | mActionBar = null; |
| 2343 | // Re-set the original window callback since we may have already set a Toolbar wrapper |
| 2344 | mWindow.setCallback(this); |
| 2345 | } |
| 2346 | |
| 2347 | invalidateOptionsMenu(); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2348 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2350 | /** |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2351 | * Creates a new ActionBar, locates the inflated ActionBarView, |
| 2352 | * initializes the ActionBar with the view, and sets mActionBar. |
| 2353 | */ |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2354 | private void initWindowDecorActionBar() { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 2355 | Window window = getWindow(); |
Adam Powell | a593d98 | 2011-05-13 14:09:54 -0700 | [diff] [blame] | 2356 | |
| 2357 | // Initializing the window decor can change window feature flags. |
| 2358 | // Make sure that we have the correct set before performing the test below. |
| 2359 | window.getDecorView(); |
| 2360 | |
Adam Powell | 9b4c804 | 2010-08-10 15:36:44 -0700 | [diff] [blame] | 2361 | if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2362 | return; |
| 2363 | } |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 2364 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2365 | mActionBar = new WindowDecorActionBar(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 2366 | mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp); |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 2367 | |
| 2368 | mWindow.setDefaultIcon(mActivityInfo.getIconResource()); |
| 2369 | mWindow.setDefaultLogo(mActivityInfo.getLogoResource()); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2370 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2371 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2372 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2373 | * Set the activity content from a layout resource. The resource will be |
| 2374 | * inflated, adding all top-level views to the activity. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2375 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2376 | * @param layoutResID Resource ID to be inflated. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2377 | * |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2378 | * @see #setContentView(android.view.View) |
| 2379 | * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2380 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 2381 | public void setContentView(@LayoutRes int layoutResID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2382 | getWindow().setContentView(layoutResID); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2383 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | /** |
| 2387 | * Set the activity content to an explicit view. This view is placed |
| 2388 | * 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] | 2389 | * view hierarchy. When calling this method, the layout parameters of the |
| 2390 | * specified view are ignored. Both the width and the height of the view are |
| 2391 | * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use |
| 2392 | * your own layout parameters, invoke |
| 2393 | * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)} |
| 2394 | * instead. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2395 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2396 | * @param view The desired content to display. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2397 | * |
| 2398 | * @see #setContentView(int) |
| 2399 | * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2400 | */ |
| 2401 | public void setContentView(View view) { |
| 2402 | getWindow().setContentView(view); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2403 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | /** |
| 2407 | * Set the activity content to an explicit view. This view is placed |
| 2408 | * 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] | 2409 | * view hierarchy. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2410 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2411 | * @param view The desired content to display. |
| 2412 | * @param params Layout parameters for the view. |
Romain Guy | 482b34a6 | 2011-01-20 10:59:28 -0800 | [diff] [blame] | 2413 | * |
| 2414 | * @see #setContentView(android.view.View) |
| 2415 | * @see #setContentView(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2416 | */ |
| 2417 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 2418 | getWindow().setContentView(view, params); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2419 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | /** |
| 2423 | * Add an additional content view to the activity. Added after any existing |
| 2424 | * ones in the activity -- existing views are NOT removed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2425 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2426 | * @param view The desired content to display. |
| 2427 | * @param params Layout parameters for the view. |
| 2428 | */ |
| 2429 | public void addContentView(View view, ViewGroup.LayoutParams params) { |
| 2430 | getWindow().addContentView(view, params); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 2431 | initWindowDecorActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | /** |
Adam Powell | cfbe9be | 2013-11-06 14:58:58 -0800 | [diff] [blame] | 2435 | * Retrieve the {@link TransitionManager} responsible for default transitions in this window. |
| 2436 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2437 | * |
| 2438 | * <p>This method will return non-null after content has been initialized (e.g. by using |
| 2439 | * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p> |
| 2440 | * |
| 2441 | * @return This window's content TransitionManager or null if none is set. |
| 2442 | */ |
| 2443 | public TransitionManager getContentTransitionManager() { |
| 2444 | return getWindow().getTransitionManager(); |
| 2445 | } |
| 2446 | |
| 2447 | /** |
| 2448 | * Set the {@link TransitionManager} to use for default transitions in this window. |
| 2449 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2450 | * |
| 2451 | * @param tm The TransitionManager to use for scene changes. |
| 2452 | */ |
| 2453 | public void setContentTransitionManager(TransitionManager tm) { |
| 2454 | getWindow().setTransitionManager(tm); |
| 2455 | } |
| 2456 | |
| 2457 | /** |
| 2458 | * Retrieve the {@link Scene} representing this window's current content. |
| 2459 | * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}. |
| 2460 | * |
| 2461 | * <p>This method will return null if the current content is not represented by a Scene.</p> |
| 2462 | * |
| 2463 | * @return Current Scene being shown or null |
| 2464 | */ |
| 2465 | public Scene getContentScene() { |
| 2466 | return getWindow().getContentScene(); |
| 2467 | } |
| 2468 | |
| 2469 | /** |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2470 | * Sets whether this activity is finished when touched outside its window's |
| 2471 | * bounds. |
| 2472 | */ |
| 2473 | public void setFinishOnTouchOutside(boolean finish) { |
| 2474 | mWindow.setCloseOnTouchOutside(finish); |
| 2475 | } |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2476 | |
| 2477 | /** @hide */ |
| 2478 | @IntDef({ |
| 2479 | DEFAULT_KEYS_DISABLE, |
| 2480 | DEFAULT_KEYS_DIALER, |
| 2481 | DEFAULT_KEYS_SHORTCUT, |
| 2482 | DEFAULT_KEYS_SEARCH_LOCAL, |
| 2483 | DEFAULT_KEYS_SEARCH_GLOBAL}) |
| 2484 | @Retention(RetentionPolicy.SOURCE) |
| 2485 | @interface DefaultKeyMode {} |
| 2486 | |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2487 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2488 | * Use with {@link #setDefaultKeyMode} to turn off default handling of |
| 2489 | * keys. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2490 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2491 | * @see #setDefaultKeyMode |
| 2492 | */ |
| 2493 | static public final int DEFAULT_KEYS_DISABLE = 0; |
| 2494 | /** |
| 2495 | * Use with {@link #setDefaultKeyMode} to launch the dialer during default |
| 2496 | * key handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2497 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2498 | * @see #setDefaultKeyMode |
| 2499 | */ |
| 2500 | static public final int DEFAULT_KEYS_DIALER = 1; |
| 2501 | /** |
| 2502 | * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in |
| 2503 | * default key handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2504 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2505 | * <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] | 2506 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2507 | * @see #setDefaultKeyMode |
| 2508 | */ |
| 2509 | static public final int DEFAULT_KEYS_SHORTCUT = 2; |
| 2510 | /** |
| 2511 | * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes |
| 2512 | * will start an application-defined search. (If the application or activity does not |
| 2513 | * actually define a search, the the keys will be ignored.) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2514 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2515 | * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2516 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2517 | * @see #setDefaultKeyMode |
| 2518 | */ |
| 2519 | static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3; |
| 2520 | |
| 2521 | /** |
| 2522 | * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes |
| 2523 | * will start a global search (typically web search, but some platforms may define alternate |
| 2524 | * methods for global search) |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2525 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2526 | * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2527 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2528 | * @see #setDefaultKeyMode |
| 2529 | */ |
| 2530 | static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4; |
| 2531 | |
| 2532 | /** |
| 2533 | * Select the default key handling for this activity. This controls what |
| 2534 | * will happen to key events that are not otherwise handled. The default |
| 2535 | * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the |
| 2536 | * floor. Other modes allow you to launch the dialer |
| 2537 | * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options |
| 2538 | * menu without requiring the menu key be held down |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2539 | * ({@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] | 2540 | * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2541 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2542 | * <p>Note that the mode selected here does not impact the default |
| 2543 | * handling of system keys, such as the "back" and "menu" keys, and your |
| 2544 | * activity and its views always get a first chance to receive and handle |
| 2545 | * all application keys. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2546 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2547 | * @param mode The desired default key mode constant. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2548 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2549 | * @see #DEFAULT_KEYS_DISABLE |
| 2550 | * @see #DEFAULT_KEYS_DIALER |
| 2551 | * @see #DEFAULT_KEYS_SHORTCUT |
| 2552 | * @see #DEFAULT_KEYS_SEARCH_LOCAL |
| 2553 | * @see #DEFAULT_KEYS_SEARCH_GLOBAL |
| 2554 | * @see #onKeyDown |
| 2555 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2556 | public final void setDefaultKeyMode(@DefaultKeyMode int mode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2557 | mDefaultKeyMode = mode; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2558 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2559 | // Some modes use a SpannableStringBuilder to track & dispatch input events |
| 2560 | // This list must remain in sync with the switch in onKeyDown() |
| 2561 | switch (mode) { |
| 2562 | case DEFAULT_KEYS_DISABLE: |
| 2563 | case DEFAULT_KEYS_SHORTCUT: |
| 2564 | mDefaultKeySsb = null; // not used in these modes |
| 2565 | break; |
| 2566 | case DEFAULT_KEYS_DIALER: |
| 2567 | case DEFAULT_KEYS_SEARCH_LOCAL: |
| 2568 | case DEFAULT_KEYS_SEARCH_GLOBAL: |
| 2569 | mDefaultKeySsb = new SpannableStringBuilder(); |
| 2570 | Selection.setSelection(mDefaultKeySsb,0); |
| 2571 | break; |
| 2572 | default: |
| 2573 | throw new IllegalArgumentException(); |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | /** |
| 2578 | * 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] | 2579 | * 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] | 2580 | * is inside a TextView will not trigger the event (unless it is a navigation |
| 2581 | * to another object) because TextView handles its own key presses. |
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>If the focused view didn't want this event, this method is called. |
| 2584 | * |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2585 | * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK} |
| 2586 | * by calling {@link #onBackPressed()}, though the behavior varies based |
| 2587 | * on the application compatibility mode: for |
| 2588 | * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications, |
| 2589 | * it will set up the dispatch to call {@link #onKeyUp} where the action |
| 2590 | * will be performed; for earlier applications, it will perform the |
| 2591 | * action immediately in on-down, as those versions of the platform |
| 2592 | * behaved. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2593 | * |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2594 | * <p>Other additional default key handling may be performed |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2595 | * if configured with {@link #setDefaultKeyMode}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2596 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2597 | * @return Return <code>true</code> to prevent this event from being propagated |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2598 | * 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] | 2599 | * this event and it should continue to be propagated. |
| 2600 | * @see #onKeyUp |
| 2601 | * @see android.view.KeyEvent |
| 2602 | */ |
| 2603 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2604 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2605 | if (getApplicationInfo().targetSdkVersion |
| 2606 | >= Build.VERSION_CODES.ECLAIR) { |
| 2607 | event.startTracking(); |
| 2608 | } else { |
| 2609 | onBackPressed(); |
| 2610 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2611 | return true; |
| 2612 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2613 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2614 | if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) { |
| 2615 | return false; |
| 2616 | } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 2617 | Window w = getWindow(); |
| 2618 | if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 2619 | w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event, |
| 2620 | Menu.FLAG_ALWAYS_PERFORM_CLOSE)) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2621 | return true; |
| 2622 | } |
| 2623 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2624 | } else { |
| 2625 | // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_* |
| 2626 | boolean clearSpannable = false; |
| 2627 | boolean handled; |
| 2628 | if ((event.getRepeatCount() != 0) || event.isSystem()) { |
| 2629 | clearSpannable = true; |
| 2630 | handled = false; |
| 2631 | } else { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2632 | handled = TextKeyListener.getInstance().onKeyDown( |
| 2633 | null, mDefaultKeySsb, keyCode, event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2634 | if (handled && mDefaultKeySsb.length() > 0) { |
| 2635 | // something useable has been typed - dispatch it now. |
| 2636 | |
| 2637 | final String str = mDefaultKeySsb.toString(); |
| 2638 | clearSpannable = true; |
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 | switch (mDefaultKeyMode) { |
| 2641 | case DEFAULT_KEYS_DIALER: |
| 2642 | Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str)); |
| 2643 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2644 | startActivity(intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2645 | break; |
| 2646 | case DEFAULT_KEYS_SEARCH_LOCAL: |
| 2647 | startSearch(str, false, null, false); |
| 2648 | break; |
| 2649 | case DEFAULT_KEYS_SEARCH_GLOBAL: |
| 2650 | startSearch(str, false, null, true); |
| 2651 | break; |
| 2652 | } |
| 2653 | } |
| 2654 | } |
| 2655 | if (clearSpannable) { |
| 2656 | mDefaultKeySsb.clear(); |
| 2657 | mDefaultKeySsb.clearSpans(); |
| 2658 | Selection.setSelection(mDefaultKeySsb,0); |
| 2659 | } |
| 2660 | return handled; |
| 2661 | } |
| 2662 | } |
| 2663 | |
| 2664 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2665 | * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent) |
| 2666 | * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle |
| 2667 | * the event). |
| 2668 | */ |
| 2669 | public boolean onKeyLongPress(int keyCode, KeyEvent event) { |
| 2670 | return false; |
| 2671 | } |
| 2672 | |
| 2673 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2674 | * 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] | 2675 | * 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] | 2676 | * is inside a TextView will not trigger the event (unless it is a navigation |
| 2677 | * to another object) because TextView handles its own key presses. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2678 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2679 | * <p>The default implementation handles KEYCODE_BACK to stop the activity |
| 2680 | * and go back. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2681 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2682 | * @return Return <code>true</code> to prevent this event from being propagated |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2683 | * further, or <code>false</code> to indicate that you have not handled |
| 2684 | * this event and it should continue to be propagated. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2685 | * @see #onKeyDown |
| 2686 | * @see KeyEvent |
| 2687 | */ |
| 2688 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2689 | if (getApplicationInfo().targetSdkVersion |
| 2690 | >= Build.VERSION_CODES.ECLAIR) { |
| 2691 | if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking() |
| 2692 | && !event.isCanceled()) { |
| 2693 | onBackPressed(); |
| 2694 | return true; |
| 2695 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2696 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2697 | return false; |
| 2698 | } |
| 2699 | |
| 2700 | /** |
| 2701 | * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent) |
| 2702 | * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle |
| 2703 | * the event). |
| 2704 | */ |
| 2705 | public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
| 2706 | return false; |
| 2707 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2708 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2709 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2710 | * Called when the activity has detected the user's press of the back |
| 2711 | * key. The default implementation simply finishes the current activity, |
| 2712 | * but you can override this to do whatever you want. |
| 2713 | */ |
| 2714 | public void onBackPressed() { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2715 | if (mActionBar != null && mActionBar.collapseActionView()) { |
| 2716 | return; |
| 2717 | } |
| 2718 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 2719 | if (!mFragments.getFragmentManager().popBackStackImmediate()) { |
Craig Mautner | 73f843d | 2014-05-19 09:42:28 -0700 | [diff] [blame] | 2720 | finishAfterTransition(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 2721 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2722 | } |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 2723 | |
| 2724 | /** |
| 2725 | * Called when a key shortcut event is not handled by any of the views in the Activity. |
| 2726 | * Override this method to implement global key shortcuts for the Activity. |
| 2727 | * Key shortcuts can also be implemented by setting the |
| 2728 | * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items. |
| 2729 | * |
| 2730 | * @param keyCode The value in event.getKeyCode(). |
| 2731 | * @param event Description of the key event. |
| 2732 | * @return True if the key shortcut was handled. |
| 2733 | */ |
| 2734 | public boolean onKeyShortcut(int keyCode, KeyEvent event) { |
Abodunrinwa Toki | a04b7ad | 2015-06-30 17:44:04 -0700 | [diff] [blame] | 2735 | // Let the Action Bar have a chance at handling the shortcut. |
| 2736 | ActionBar actionBar = getActionBar(); |
| 2737 | return (actionBar != null && actionBar.onKeyShortcut(keyCode, event)); |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 2738 | } |
| 2739 | |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2740 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2741 | * Called when a touch screen event was not handled by any of the views |
| 2742 | * under it. This is most useful to process touch events that happen |
| 2743 | * outside of your window bounds, where there is no view to receive it. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2744 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2745 | * @param event The touch screen event being processed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2746 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2747 | * @return Return true if you have consumed the event, false if you haven't. |
| 2748 | * The default implementation always returns false. |
| 2749 | */ |
| 2750 | public boolean onTouchEvent(MotionEvent event) { |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2751 | if (mWindow.shouldCloseOnTouch(this, event)) { |
| 2752 | finish(); |
| 2753 | return true; |
| 2754 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2755 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2756 | return false; |
| 2757 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2758 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2759 | /** |
| 2760 | * Called when the trackball was moved and not handled by any of the |
| 2761 | * views inside of the activity. So, for example, if the trackball moves |
| 2762 | * while focus is on a button, you will receive a call here because |
| 2763 | * buttons do not normally do anything with trackball events. The call |
| 2764 | * here happens <em>before</em> trackball movements are converted to |
| 2765 | * DPAD key events, which then get sent back to the view hierarchy, and |
| 2766 | * will be processed at the point for things like focus navigation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2767 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2768 | * @param event The trackball event being processed. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2769 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2770 | * @return Return true if you have consumed the event, false if you haven't. |
| 2771 | * The default implementation always returns false. |
| 2772 | */ |
| 2773 | public boolean onTrackballEvent(MotionEvent event) { |
| 2774 | return false; |
| 2775 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2776 | |
| 2777 | /** |
| 2778 | * Called when a generic motion event was not handled by any of the |
| 2779 | * views inside of the activity. |
| 2780 | * <p> |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2781 | * Generic motion events describe joystick movements, mouse hovers, track pad |
| 2782 | * touches, scroll wheel movements and other input events. The |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2783 | * {@link MotionEvent#getSource() source} of the motion event specifies |
| 2784 | * the class of input that was received. Implementations of this method |
| 2785 | * must examine the bits in the source before processing the event. |
| 2786 | * The following code example shows how this is done. |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2787 | * </p><p> |
| 2788 | * Generic motion events with source class |
| 2789 | * {@link android.view.InputDevice#SOURCE_CLASS_POINTER} |
| 2790 | * are delivered to the view under the pointer. All other generic motion events are |
| 2791 | * delivered to the focused view. |
| 2792 | * </p><p> |
| 2793 | * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to |
| 2794 | * handle this event. |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2795 | * </p> |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2796 | * |
| 2797 | * @param event The generic motion event being processed. |
| 2798 | * |
| 2799 | * @return Return true if you have consumed the event, false if you haven't. |
| 2800 | * The default implementation always returns false. |
| 2801 | */ |
| 2802 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 2803 | return false; |
| 2804 | } |
| 2805 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2806 | /** |
| 2807 | * Called whenever a key, touch, or trackball event is dispatched to the |
| 2808 | * activity. Implement this method if you wish to know that the user has |
| 2809 | * interacted with the device in some way while your activity is running. |
| 2810 | * This callback and {@link #onUserLeaveHint} are intended to help |
| 2811 | * activities manage status bar notifications intelligently; specifically, |
| 2812 | * for helping activities determine the proper time to cancel a notfication. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2813 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2814 | * <p>All calls to your activity's {@link #onUserLeaveHint} callback will |
| 2815 | * be accompanied by calls to {@link #onUserInteraction}. This |
| 2816 | * ensures that your activity will be told of relevant user activity such |
| 2817 | * as pulling down the notification pane and touching an item there. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2818 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2819 | * <p>Note that this callback will be invoked for the touch down action |
| 2820 | * that begins a touch gesture, but may not be invoked for the touch-moved |
| 2821 | * and touch-up actions that follow. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2822 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2823 | * @see #onUserLeaveHint() |
| 2824 | */ |
| 2825 | public void onUserInteraction() { |
| 2826 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2827 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2828 | public void onWindowAttributesChanged(WindowManager.LayoutParams params) { |
| 2829 | // Update window manager if: we have a view, that view is |
| 2830 | // attached to its parent (which will be a RootView), and |
| 2831 | // this activity is not embedded. |
| 2832 | if (mParent == null) { |
| 2833 | View decor = mDecor; |
| 2834 | if (decor != null && decor.getParent() != null) { |
| 2835 | getWindowManager().updateViewLayout(decor, params); |
| 2836 | } |
| 2837 | } |
| 2838 | } |
| 2839 | |
| 2840 | public void onContentChanged() { |
| 2841 | } |
| 2842 | |
| 2843 | /** |
| 2844 | * Called when the current {@link Window} of the activity gains or loses |
| 2845 | * focus. This is the best indicator of whether this activity is visible |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2846 | * to the user. The default implementation clears the key tracking |
| 2847 | * state, so should always be called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2848 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2849 | * <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] | 2850 | * is managed independently of activity lifecycles. As such, while focus |
| 2851 | * changes will generally have some relation to lifecycle changes (an |
| 2852 | * activity that is stopped will not generally get window focus), you |
| 2853 | * should not rely on any particular order between the callbacks here and |
| 2854 | * those in the other lifecycle methods such as {@link #onResume}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2855 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2856 | * <p>As a general rule, however, a resumed activity will have window |
| 2857 | * focus... unless it has displayed other dialogs or popups that take |
| 2858 | * input focus, in which case the activity itself will not have focus |
| 2859 | * when the other windows have it. Likewise, the system may display |
| 2860 | * system-level windows (such as the status bar notification panel or |
| 2861 | * a system alert) which will temporarily take window input focus without |
| 2862 | * pausing the foreground activity. |
| 2863 | * |
| 2864 | * @param hasFocus Whether the window of this activity has focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2865 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2866 | * @see #hasWindowFocus() |
| 2867 | * @see #onResume |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2868 | * @see View#onWindowFocusChanged(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2869 | */ |
| 2870 | public void onWindowFocusChanged(boolean hasFocus) { |
| 2871 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2872 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2873 | /** |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2874 | * Called when the main window associated with the activity has been |
| 2875 | * attached to the window manager. |
| 2876 | * See {@link View#onAttachedToWindow() View.onAttachedToWindow()} |
| 2877 | * for more information. |
| 2878 | * @see View#onAttachedToWindow |
| 2879 | */ |
| 2880 | public void onAttachedToWindow() { |
| 2881 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2882 | |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2883 | /** |
| 2884 | * Called when the main window associated with the activity has been |
| 2885 | * detached from the window manager. |
| 2886 | * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()} |
| 2887 | * for more information. |
| 2888 | * @see View#onDetachedFromWindow |
| 2889 | */ |
| 2890 | public void onDetachedFromWindow() { |
| 2891 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2892 | |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2893 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2894 | * Returns true if this activity's <em>main</em> window currently has window focus. |
| 2895 | * Note that this is not the same as the view itself having focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2896 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2897 | * @return True if this activity's main window currently has window focus. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2898 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2899 | * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams) |
| 2900 | */ |
| 2901 | public boolean hasWindowFocus() { |
| 2902 | Window w = getWindow(); |
| 2903 | if (w != null) { |
| 2904 | View d = w.getDecorView(); |
| 2905 | if (d != null) { |
| 2906 | return d.hasWindowFocus(); |
| 2907 | } |
| 2908 | } |
| 2909 | return false; |
| 2910 | } |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2911 | |
| 2912 | /** |
| 2913 | * Called when the main window associated with the activity has been dismissed. |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 2914 | * @hide |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2915 | */ |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 2916 | @Override |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 2917 | public void onWindowDismissed(boolean finishTask) { |
| 2918 | finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY); |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 2919 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2920 | |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2921 | |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 2922 | /** |
Filip Gruszczynski | 411c06f | 2016-01-07 09:44:44 -0800 | [diff] [blame] | 2923 | * Moves the activity from |
| 2924 | * {@link android.app.ActivityManager.StackId#FREEFORM_WORKSPACE_STACK_ID} to |
| 2925 | * {@link android.app.ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} stack. |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2926 | * |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2927 | * @hide |
| 2928 | */ |
| 2929 | @Override |
Filip Gruszczynski | 411c06f | 2016-01-07 09:44:44 -0800 | [diff] [blame] | 2930 | public void exitFreeformMode() throws RemoteException { |
| 2931 | ActivityManagerNative.getDefault().exitFreeformMode(mToken); |
Wale Ogunwale | 868a5e1 | 2015-08-02 16:19:20 -0700 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | /** Returns the current stack Id for the window. |
| 2935 | * @hide |
| 2936 | */ |
| 2937 | @Override |
| 2938 | public int getWindowStackId() throws RemoteException { |
| 2939 | return ActivityManagerNative.getDefault().getActivityStackId(mToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2943 | * Called to process key events. You can override this to intercept all |
| 2944 | * 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] | 2945 | * this implementation for key events that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2946 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2947 | * @param event The key event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 2948 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2949 | * @return boolean Return true if this event was consumed. |
| 2950 | */ |
| 2951 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 2952 | onUserInteraction(); |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2953 | |
| 2954 | // Let action bars open menus in response to the menu key prioritized over |
| 2955 | // the window handling it |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 2956 | final int keyCode = event.getKeyCode(); |
| 2957 | if (keyCode == KeyEvent.KEYCODE_MENU && |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2958 | mActionBar != null && mActionBar.onMenuKeyEvent(event)) { |
| 2959 | return true; |
George Mount | cbe2835 | 2016-01-06 16:24:26 -0800 | [diff] [blame] | 2960 | } else if (event.isCtrlPressed() && |
| 2961 | event.getUnicodeChar(event.getMetaState() & ~KeyEvent.META_CTRL_MASK) == '<') { |
| 2962 | // Capture the Control-< and send focus to the ActionBar |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 2963 | final int action = event.getAction(); |
| 2964 | if (action == KeyEvent.ACTION_DOWN) { |
George Mount | cbe2835 | 2016-01-06 16:24:26 -0800 | [diff] [blame] | 2965 | final ActionBar actionBar = getActionBar(); |
| 2966 | if (actionBar != null && actionBar.isShowing() && actionBar.requestFocus()) { |
| 2967 | mEatKeyUpEvent = true; |
| 2968 | return true; |
George Mount | 5beb2617 | 2015-12-15 13:36:01 -0800 | [diff] [blame] | 2969 | } |
| 2970 | } else if (action == KeyEvent.ACTION_UP && mEatKeyUpEvent) { |
| 2971 | mEatKeyUpEvent = false; |
| 2972 | return true; |
| 2973 | } |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 2974 | } |
| 2975 | |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2976 | Window win = getWindow(); |
| 2977 | if (win.superDispatchKeyEvent(event)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2978 | return true; |
| 2979 | } |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2980 | View decor = mDecor; |
| 2981 | if (decor == null) decor = win.getDecorView(); |
| 2982 | return event.dispatch(this, decor != null |
| 2983 | ? decor.getKeyDispatcherState() : null, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | /** |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 2987 | * Called to process a key shortcut event. |
| 2988 | * You can override this to intercept all key shortcut events before they are |
| 2989 | * dispatched to the window. Be sure to call this implementation for key shortcut |
| 2990 | * events that should be handled normally. |
| 2991 | * |
| 2992 | * @param event The key shortcut event. |
| 2993 | * @return True if this event was consumed. |
| 2994 | */ |
| 2995 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
| 2996 | onUserInteraction(); |
| 2997 | if (getWindow().superDispatchKeyShortcutEvent(event)) { |
| 2998 | return true; |
| 2999 | } |
| 3000 | return onKeyShortcut(event.getKeyCode(), event); |
| 3001 | } |
| 3002 | |
| 3003 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3004 | * Called to process touch screen events. You can override this to |
| 3005 | * intercept all touch screen events before they are dispatched to the |
| 3006 | * window. Be sure to call this implementation for touch screen events |
| 3007 | * that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3008 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3009 | * @param ev The touch screen event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3010 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3011 | * @return boolean Return true if this event was consumed. |
| 3012 | */ |
| 3013 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 3014 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 3015 | onUserInteraction(); |
| 3016 | } |
| 3017 | if (getWindow().superDispatchTouchEvent(ev)) { |
| 3018 | return true; |
| 3019 | } |
| 3020 | return onTouchEvent(ev); |
| 3021 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3022 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3023 | /** |
| 3024 | * Called to process trackball events. You can override this to |
| 3025 | * intercept all trackball events before they are dispatched to the |
| 3026 | * window. Be sure to call this implementation for trackball events |
| 3027 | * that should be handled normally. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3028 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3029 | * @param ev The trackball event. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3030 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3031 | * @return boolean Return true if this event was consumed. |
| 3032 | */ |
| 3033 | public boolean dispatchTrackballEvent(MotionEvent ev) { |
| 3034 | onUserInteraction(); |
| 3035 | if (getWindow().superDispatchTrackballEvent(ev)) { |
| 3036 | return true; |
| 3037 | } |
| 3038 | return onTrackballEvent(ev); |
| 3039 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3040 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 3041 | /** |
| 3042 | * Called to process generic motion events. You can override this to |
| 3043 | * intercept all generic motion events before they are dispatched to the |
| 3044 | * window. Be sure to call this implementation for generic motion events |
| 3045 | * that should be handled normally. |
| 3046 | * |
| 3047 | * @param ev The generic motion event. |
| 3048 | * |
| 3049 | * @return boolean Return true if this event was consumed. |
| 3050 | */ |
| 3051 | public boolean dispatchGenericMotionEvent(MotionEvent ev) { |
| 3052 | onUserInteraction(); |
| 3053 | if (getWindow().superDispatchGenericMotionEvent(ev)) { |
| 3054 | return true; |
| 3055 | } |
| 3056 | return onGenericMotionEvent(ev); |
| 3057 | } |
| 3058 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3059 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 3060 | event.setClassName(getClass().getName()); |
| 3061 | event.setPackageName(getPackageName()); |
| 3062 | |
| 3063 | LayoutParams params = getWindow().getAttributes(); |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 3064 | boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) && |
| 3065 | (params.height == LayoutParams.MATCH_PARENT); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3066 | event.setFullScreen(isFullScreen); |
| 3067 | |
| 3068 | CharSequence title = getTitle(); |
| 3069 | if (!TextUtils.isEmpty(title)) { |
| 3070 | event.getText().add(title); |
| 3071 | } |
| 3072 | |
| 3073 | return true; |
| 3074 | } |
| 3075 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3076 | /** |
| 3077 | * Default implementation of |
| 3078 | * {@link android.view.Window.Callback#onCreatePanelView} |
| 3079 | * for activities. This |
| 3080 | * simply returns null so that all panel sub-windows will have the default |
| 3081 | * menu behavior. |
| 3082 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3083 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3084 | public View onCreatePanelView(int featureId) { |
| 3085 | return null; |
| 3086 | } |
| 3087 | |
| 3088 | /** |
| 3089 | * Default implementation of |
| 3090 | * {@link android.view.Window.Callback#onCreatePanelMenu} |
| 3091 | * for activities. This calls through to the new |
| 3092 | * {@link #onCreateOptionsMenu} method for the |
| 3093 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel, |
| 3094 | * so that subclasses of Activity don't need to deal with feature codes. |
| 3095 | */ |
| 3096 | public boolean onCreatePanelMenu(int featureId, Menu menu) { |
| 3097 | if (featureId == Window.FEATURE_OPTIONS_PANEL) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3098 | boolean show = onCreateOptionsMenu(menu); |
| 3099 | show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater()); |
| 3100 | return show; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3101 | } |
| 3102 | return false; |
| 3103 | } |
| 3104 | |
| 3105 | /** |
| 3106 | * Default implementation of |
| 3107 | * {@link android.view.Window.Callback#onPreparePanel} |
| 3108 | * for activities. This |
| 3109 | * calls through to the new {@link #onPrepareOptionsMenu} method for the |
| 3110 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} |
| 3111 | * panel, so that subclasses of |
| 3112 | * Activity don't need to deal with feature codes. |
| 3113 | */ |
| 3114 | public boolean onPreparePanel(int featureId, View view, Menu menu) { |
| 3115 | if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { |
| 3116 | boolean goforit = onPrepareOptionsMenu(menu); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3117 | goforit |= mFragments.dispatchPrepareOptionsMenu(menu); |
Adam Powell | ef31e7c | 2012-05-08 09:59:40 -0700 | [diff] [blame] | 3118 | return goforit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3119 | } |
| 3120 | return true; |
| 3121 | } |
| 3122 | |
| 3123 | /** |
| 3124 | * {@inheritDoc} |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3125 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3126 | * @return The default implementation returns true. |
| 3127 | */ |
| 3128 | public boolean onMenuOpened(int featureId, Menu menu) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3129 | if (featureId == Window.FEATURE_ACTION_BAR) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 3130 | initWindowDecorActionBar(); |
Adam Powell | 049dd3d | 2010-12-02 13:43:59 -0800 | [diff] [blame] | 3131 | if (mActionBar != null) { |
| 3132 | mActionBar.dispatchMenuVisibilityChanged(true); |
| 3133 | } else { |
| 3134 | Log.e(TAG, "Tried to open action bar menu with no action bar"); |
| 3135 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3136 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3137 | return true; |
| 3138 | } |
| 3139 | |
| 3140 | /** |
| 3141 | * Default implementation of |
| 3142 | * {@link android.view.Window.Callback#onMenuItemSelected} |
| 3143 | * for activities. This calls through to the new |
| 3144 | * {@link #onOptionsItemSelected} method for the |
| 3145 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} |
| 3146 | * panel, so that subclasses of |
| 3147 | * Activity don't need to deal with feature codes. |
| 3148 | */ |
| 3149 | public boolean onMenuItemSelected(int featureId, MenuItem item) { |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 3150 | CharSequence titleCondensed = item.getTitleCondensed(); |
| 3151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3152 | switch (featureId) { |
| 3153 | case Window.FEATURE_OPTIONS_PANEL: |
| 3154 | // Put event logging here so it gets called even if subclass |
| 3155 | // doesn't call through to superclass's implmeentation of each |
| 3156 | // of these methods below |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 3157 | if(titleCondensed != null) { |
| 3158 | EventLog.writeEvent(50000, 0, titleCondensed.toString()); |
| 3159 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3160 | if (onOptionsItemSelected(item)) { |
| 3161 | return true; |
| 3162 | } |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3163 | if (mFragments.dispatchOptionsItemSelected(item)) { |
| 3164 | return true; |
| 3165 | } |
| 3166 | if (item.getItemId() == android.R.id.home && mActionBar != null && |
| 3167 | (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) { |
| 3168 | if (mParent == null) { |
Adam Powell | 07304f529 | 2012-05-07 17:32:44 -0700 | [diff] [blame] | 3169 | return onNavigateUp(); |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3170 | } else { |
Adam Powell | 07304f529 | 2012-05-07 17:32:44 -0700 | [diff] [blame] | 3171 | return mParent.onNavigateUpFromChild(this); |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3172 | } |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 3173 | } |
| 3174 | return false; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3176 | case Window.FEATURE_CONTEXT_MENU: |
zobject | 686a805 | 2012-12-14 21:11:08 +0900 | [diff] [blame] | 3177 | if(titleCondensed != null) { |
| 3178 | EventLog.writeEvent(50000, 1, titleCondensed.toString()); |
| 3179 | } |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 3180 | if (onContextItemSelected(item)) { |
| 3181 | return true; |
| 3182 | } |
| 3183 | return mFragments.dispatchContextItemSelected(item); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3185 | default: |
| 3186 | return false; |
| 3187 | } |
| 3188 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3190 | /** |
| 3191 | * Default implementation of |
| 3192 | * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for |
| 3193 | * activities. This calls through to {@link #onOptionsMenuClosed(Menu)} |
| 3194 | * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel, |
| 3195 | * so that subclasses of Activity don't need to deal with feature codes. |
| 3196 | * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the |
| 3197 | * {@link #onContextMenuClosed(Menu)} will be called. |
| 3198 | */ |
| 3199 | public void onPanelClosed(int featureId, Menu menu) { |
| 3200 | switch (featureId) { |
| 3201 | case Window.FEATURE_OPTIONS_PANEL: |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3202 | mFragments.dispatchOptionsMenuClosed(menu); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3203 | onOptionsMenuClosed(menu); |
| 3204 | break; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3206 | case Window.FEATURE_CONTEXT_MENU: |
| 3207 | onContextMenuClosed(menu); |
| 3208 | break; |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3209 | |
| 3210 | case Window.FEATURE_ACTION_BAR: |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 3211 | initWindowDecorActionBar(); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 3212 | mActionBar.dispatchMenuVisibilityChanged(false); |
| 3213 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | /** |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3218 | * Declare that the options menu has changed, so should be recreated. |
| 3219 | * The {@link #onCreateOptionsMenu(Menu)} method will be called the next |
| 3220 | * time it needs to be displayed. |
| 3221 | */ |
| 3222 | public void invalidateOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3223 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 3224 | (mActionBar == null || !mActionBar.invalidateOptionsMenu())) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3225 | mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); |
| 3226 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3227 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3228 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 3229 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3230 | * Initialize the contents of the Activity's standard options menu. You |
| 3231 | * should place your menu items in to <var>menu</var>. |
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 | * <p>This is only called once, the first time the options menu is |
| 3234 | * displayed. To update the menu every time it is displayed, see |
| 3235 | * {@link #onPrepareOptionsMenu}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3236 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3237 | * <p>The default implementation populates the menu with standard system |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3238 | * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that |
| 3239 | * they will be correctly ordered with application-defined menu items. |
| 3240 | * Deriving classes should always call through to the base implementation. |
| 3241 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3242 | * <p>You can safely hold on to <var>menu</var> (and any items created |
| 3243 | * from it), making modifications to it as desired, until the next |
| 3244 | * time onCreateOptionsMenu() is called. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3245 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3246 | * <p>When you add items to the menu, you can implement the Activity's |
| 3247 | * {@link #onOptionsItemSelected} method to handle them there. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3248 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3249 | * @param menu The options menu in which you place your items. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3250 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3251 | * @return You must return true for the menu to be displayed; |
| 3252 | * if you return false it will not be shown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3253 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3254 | * @see #onPrepareOptionsMenu |
| 3255 | * @see #onOptionsItemSelected |
| 3256 | */ |
| 3257 | public boolean onCreateOptionsMenu(Menu menu) { |
| 3258 | if (mParent != null) { |
| 3259 | return mParent.onCreateOptionsMenu(menu); |
| 3260 | } |
| 3261 | return true; |
| 3262 | } |
| 3263 | |
| 3264 | /** |
| 3265 | * Prepare the Screen's standard options menu to be displayed. This is |
| 3266 | * called right before the menu is shown, every time it is shown. You can |
| 3267 | * use this method to efficiently enable/disable items or otherwise |
| 3268 | * dynamically modify the contents. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3269 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3270 | * <p>The default implementation updates the system menu items based on the |
| 3271 | * activity's state. Deriving classes should always call through to the |
| 3272 | * base class implementation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3273 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3274 | * @param menu The options menu as last shown or first initialized by |
| 3275 | * onCreateOptionsMenu(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3276 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3277 | * @return You must return true for the menu to be displayed; |
| 3278 | * if you return false it will not be shown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3279 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3280 | * @see #onCreateOptionsMenu |
| 3281 | */ |
| 3282 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 3283 | if (mParent != null) { |
| 3284 | return mParent.onPrepareOptionsMenu(menu); |
| 3285 | } |
| 3286 | return true; |
| 3287 | } |
| 3288 | |
| 3289 | /** |
| 3290 | * This hook is called whenever an item in your options menu is selected. |
| 3291 | * The default implementation simply returns false to have the normal |
| 3292 | * processing happen (calling the item's Runnable or sending a message to |
| 3293 | * its Handler as appropriate). You can use this method for any items |
| 3294 | * for which you would like to do processing without those other |
| 3295 | * facilities. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3296 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3297 | * <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] | 3298 | * perform the default menu handling.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3299 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3300 | * @param item The menu item that was selected. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3301 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3302 | * @return boolean Return false to allow normal menu processing to |
| 3303 | * proceed, true to consume it here. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3304 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3305 | * @see #onCreateOptionsMenu |
| 3306 | */ |
| 3307 | public boolean onOptionsItemSelected(MenuItem item) { |
| 3308 | if (mParent != null) { |
| 3309 | return mParent.onOptionsItemSelected(item); |
| 3310 | } |
| 3311 | return false; |
| 3312 | } |
| 3313 | |
| 3314 | /** |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3315 | * This method is called whenever the user chooses to navigate Up within your application's |
| 3316 | * activity hierarchy from the action bar. |
| 3317 | * |
| 3318 | * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName} |
| 3319 | * was specified in the manifest for this activity or an activity-alias to it, |
| 3320 | * default Up navigation will be handled automatically. If any activity |
| 3321 | * along the parent chain requires extra Intent arguments, the Activity subclass |
| 3322 | * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)} |
| 3323 | * to supply those arguments.</p> |
| 3324 | * |
| 3325 | * <p>See <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a> |
| 3326 | * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a> |
| 3327 | * from the design guide for more information about navigating within your app.</p> |
| 3328 | * |
| 3329 | * <p>See the {@link TaskStackBuilder} class and the Activity methods |
| 3330 | * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and |
| 3331 | * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation. |
| 3332 | * The AppNavigation sample application in the Android SDK is also available for reference.</p> |
| 3333 | * |
| 3334 | * @return true if Up navigation completed successfully and this Activity was finished, |
| 3335 | * false otherwise. |
| 3336 | */ |
| 3337 | public boolean onNavigateUp() { |
| 3338 | // Automatically handle hierarchical Up navigation if the proper |
| 3339 | // metadata is available. |
| 3340 | Intent upIntent = getParentActivityIntent(); |
| 3341 | if (upIntent != null) { |
Adam Powell | 0fc5b2b | 2012-07-18 18:20:29 -0700 | [diff] [blame] | 3342 | if (mActivityInfo.taskAffinity == null) { |
| 3343 | // Activities with a null affinity are special; they really shouldn't |
| 3344 | // specify a parent activity intent in the first place. Just finish |
| 3345 | // the current activity and call it a day. |
| 3346 | finish(); |
| 3347 | } else if (shouldUpRecreateTask(upIntent)) { |
Adam Powell | f78a844 | 2012-05-01 18:09:32 -0700 | [diff] [blame] | 3348 | TaskStackBuilder b = TaskStackBuilder.create(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3349 | onCreateNavigateUpTaskStack(b); |
| 3350 | onPrepareNavigateUpTaskStack(b); |
| 3351 | b.startActivities(); |
Adam Powell | 3d193d9 | 2012-05-15 16:51:55 -0700 | [diff] [blame] | 3352 | |
| 3353 | // We can't finishAffinity if we have a result. |
| 3354 | // Fall back and simply finish the current activity instead. |
| 3355 | if (mResultCode != RESULT_CANCELED || mResultData != null) { |
| 3356 | // Tell the developer what's going on to avoid hair-pulling. |
| 3357 | Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result"); |
| 3358 | finish(); |
| 3359 | } else { |
| 3360 | finishAffinity(); |
| 3361 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3362 | } else { |
| 3363 | navigateUpTo(upIntent); |
| 3364 | } |
| 3365 | return true; |
| 3366 | } |
| 3367 | return false; |
| 3368 | } |
| 3369 | |
| 3370 | /** |
| 3371 | * This is called when a child activity of this one attempts to navigate up. |
| 3372 | * The default implementation simply calls onNavigateUp() on this activity (the parent). |
| 3373 | * |
| 3374 | * @param child The activity making the call. |
| 3375 | */ |
| 3376 | public boolean onNavigateUpFromChild(Activity child) { |
| 3377 | return onNavigateUp(); |
| 3378 | } |
| 3379 | |
| 3380 | /** |
| 3381 | * Define the synthetic task stack that will be generated during Up navigation from |
| 3382 | * a different task. |
| 3383 | * |
| 3384 | * <p>The default implementation of this method adds the parent chain of this activity |
| 3385 | * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications |
| 3386 | * may choose to override this method to construct the desired task stack in a different |
| 3387 | * way.</p> |
| 3388 | * |
Adam Powell | f019595 | 2012-05-02 21:38:54 -0700 | [diff] [blame] | 3389 | * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()} |
| 3390 | * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent |
| 3391 | * returned by {@link #getParentActivityIntent()}.</p> |
| 3392 | * |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 3393 | * <p>Applications that wish to supply extra Intent parameters to the parent stack defined |
| 3394 | * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p> |
| 3395 | * |
| 3396 | * @param builder An empty TaskStackBuilder - the application should add intents representing |
| 3397 | * the desired task stack |
| 3398 | */ |
| 3399 | public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) { |
| 3400 | builder.addParentStack(this); |
| 3401 | } |
| 3402 | |
| 3403 | /** |
| 3404 | * Prepare the synthetic task stack that will be generated during Up navigation |
| 3405 | * from a different task. |
| 3406 | * |
| 3407 | * <p>This method receives the {@link TaskStackBuilder} with the constructed series of |
| 3408 | * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}. |
| 3409 | * If any extra data should be added to these intents before launching the new task, |
| 3410 | * the application should override this method and add that data here.</p> |
| 3411 | * |
| 3412 | * @param builder A TaskStackBuilder that has been populated with Intents by |
| 3413 | * onCreateNavigateUpTaskStack. |
| 3414 | */ |
| 3415 | public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) { |
| 3416 | } |
| 3417 | |
| 3418 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3419 | * This hook is called whenever the options menu is being closed (either by the user canceling |
| 3420 | * the menu with the back/menu button, or when an item is selected). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3421 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3422 | * @param menu The options menu as last shown or first initialized by |
| 3423 | * onCreateOptionsMenu(). |
| 3424 | */ |
| 3425 | public void onOptionsMenuClosed(Menu menu) { |
| 3426 | if (mParent != null) { |
| 3427 | mParent.onOptionsMenuClosed(menu); |
| 3428 | } |
| 3429 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3430 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3431 | /** |
| 3432 | * Programmatically opens the options menu. If the options menu is already |
| 3433 | * open, this method does nothing. |
| 3434 | */ |
| 3435 | public void openOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3436 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && |
| 3437 | (mActionBar == null || !mActionBar.openOptionsMenu())) { |
Adam Powell | 07a7454 | 2014-05-30 15:52:44 -0700 | [diff] [blame] | 3438 | mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null); |
| 3439 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3440 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3442 | /** |
| 3443 | * Progammatically closes the options menu. If the options menu is already |
| 3444 | * closed, this method does nothing. |
| 3445 | */ |
| 3446 | public void closeOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 3447 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) { |
| 3448 | mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL); |
| 3449 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | /** |
| 3453 | * Called when a context menu for the {@code view} is about to be shown. |
| 3454 | * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every |
| 3455 | * time the context menu is about to be shown and should be populated for |
| 3456 | * the view (or item inside the view for {@link AdapterView} subclasses, |
| 3457 | * this can be found in the {@code menuInfo})). |
| 3458 | * <p> |
| 3459 | * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an |
| 3460 | * item has been selected. |
| 3461 | * <p> |
| 3462 | * 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] | 3463 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3464 | */ |
| 3465 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 3466 | } |
| 3467 | |
| 3468 | /** |
| 3469 | * Registers a context menu to be shown for the given view (multiple views |
| 3470 | * can show the context menu). This method will set the |
| 3471 | * {@link OnCreateContextMenuListener} on the view to this activity, so |
| 3472 | * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be |
| 3473 | * called when it is time to show the context menu. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3474 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3475 | * @see #unregisterForContextMenu(View) |
| 3476 | * @param view The view that should show a context menu. |
| 3477 | */ |
| 3478 | public void registerForContextMenu(View view) { |
| 3479 | view.setOnCreateContextMenuListener(this); |
| 3480 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3481 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3482 | /** |
| 3483 | * Prevents a context menu to be shown for the given view. This method will remove the |
| 3484 | * {@link OnCreateContextMenuListener} on the view. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3485 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3486 | * @see #registerForContextMenu(View) |
| 3487 | * @param view The view that should stop showing a context menu. |
| 3488 | */ |
| 3489 | public void unregisterForContextMenu(View view) { |
| 3490 | view.setOnCreateContextMenuListener(null); |
| 3491 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3493 | /** |
| 3494 | * Programmatically opens the context menu for a particular {@code view}. |
| 3495 | * The {@code view} should have been added via |
| 3496 | * {@link #registerForContextMenu(View)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3497 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3498 | * @param view The view to show the context menu for. |
| 3499 | */ |
| 3500 | public void openContextMenu(View view) { |
| 3501 | view.showContextMenu(); |
| 3502 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3504 | /** |
| 3505 | * Programmatically closes the most recently opened context menu, if showing. |
| 3506 | */ |
| 3507 | public void closeContextMenu() { |
Jose Lima | bd769a1 | 2015-01-28 10:43:15 -0800 | [diff] [blame] | 3508 | if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) { |
| 3509 | mWindow.closePanel(Window.FEATURE_CONTEXT_MENU); |
| 3510 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3511 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3512 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3513 | /** |
| 3514 | * This hook is called whenever an item in a context menu is selected. The |
| 3515 | * default implementation simply returns false to have the normal processing |
| 3516 | * happen (calling the item's Runnable or sending a message to its Handler |
| 3517 | * as appropriate). You can use this method for any items for which you |
| 3518 | * would like to do processing without those other facilities. |
| 3519 | * <p> |
| 3520 | * Use {@link MenuItem#getMenuInfo()} to get extra information set by the |
| 3521 | * View that added this menu item. |
| 3522 | * <p> |
| 3523 | * Derived classes should call through to the base class for it to perform |
| 3524 | * the default menu handling. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3525 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3526 | * @param item The context menu item that was selected. |
| 3527 | * @return boolean Return false to allow normal context menu processing to |
| 3528 | * proceed, true to consume it here. |
| 3529 | */ |
| 3530 | public boolean onContextItemSelected(MenuItem item) { |
| 3531 | if (mParent != null) { |
| 3532 | return mParent.onContextItemSelected(item); |
| 3533 | } |
| 3534 | return false; |
| 3535 | } |
| 3536 | |
| 3537 | /** |
| 3538 | * This hook is called whenever the context menu is being closed (either by |
| 3539 | * the user canceling the menu with the back/menu button, or when an item is |
| 3540 | * selected). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3541 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3542 | * @param menu The context menu that is being closed. |
| 3543 | */ |
| 3544 | public void onContextMenuClosed(Menu menu) { |
| 3545 | if (mParent != null) { |
| 3546 | mParent.onContextMenuClosed(menu); |
| 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3551 | * @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] | 3552 | */ |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3553 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3554 | protected Dialog onCreateDialog(int id) { |
| 3555 | return null; |
| 3556 | } |
| 3557 | |
| 3558 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3559 | * Callback for creating dialogs that are managed (saved and restored) for you |
| 3560 | * by the activity. The default implementation calls through to |
| 3561 | * {@link #onCreateDialog(int)} for compatibility. |
| 3562 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 3563 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 3564 | * or later, consider instead using a {@link DialogFragment} instead.</em> |
| 3565 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3566 | * <p>If you use {@link #showDialog(int)}, the activity will call through to |
| 3567 | * this method the first time, and hang onto it thereafter. Any dialog |
| 3568 | * that is created by this method will automatically be saved and restored |
| 3569 | * for you, including whether it is showing. |
| 3570 | * |
| 3571 | * <p>If you would like the activity to manage saving and restoring dialogs |
| 3572 | * for you, you should override this method and handle any ids that are |
| 3573 | * passed to {@link #showDialog}. |
| 3574 | * |
| 3575 | * <p>If you would like an opportunity to prepare your dialog before it is shown, |
| 3576 | * override {@link #onPrepareDialog(int, Dialog, Bundle)}. |
| 3577 | * |
| 3578 | * @param id The id of the dialog. |
| 3579 | * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}. |
| 3580 | * @return The dialog. If you return null, the dialog will not be created. |
| 3581 | * |
| 3582 | * @see #onPrepareDialog(int, Dialog, Bundle) |
| 3583 | * @see #showDialog(int, Bundle) |
| 3584 | * @see #dismissDialog(int) |
| 3585 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3586 | * |
| 3587 | * @deprecated Use the new {@link DialogFragment} class with |
| 3588 | * {@link FragmentManager} instead; this is also |
| 3589 | * available on older platforms through the Android compatibility package. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3590 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3591 | @Nullable |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3592 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3593 | protected Dialog onCreateDialog(int id, Bundle args) { |
| 3594 | return onCreateDialog(id); |
| 3595 | } |
| 3596 | |
| 3597 | /** |
| 3598 | * @deprecated Old no-arguments version of |
| 3599 | * {@link #onPrepareDialog(int, Dialog, Bundle)}. |
| 3600 | */ |
| 3601 | @Deprecated |
| 3602 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 3603 | dialog.setOwnerActivity(this); |
| 3604 | } |
| 3605 | |
| 3606 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3607 | * Provides an opportunity to prepare a managed dialog before it is being |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3608 | * shown. The default implementation calls through to |
| 3609 | * {@link #onPrepareDialog(int, Dialog)} for compatibility. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3610 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3611 | * <p> |
| 3612 | * Override this if you need to update a managed dialog based on the state |
| 3613 | * of the application each time it is shown. For example, a time picker |
| 3614 | * dialog might want to be updated with the current time. You should call |
| 3615 | * through to the superclass's implementation. The default implementation |
| 3616 | * will set this Activity as the owner activity on the Dialog. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3617 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3618 | * @param id The id of the managed dialog. |
| 3619 | * @param dialog The dialog. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3620 | * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}. |
| 3621 | * @see #onCreateDialog(int, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3622 | * @see #showDialog(int) |
| 3623 | * @see #dismissDialog(int) |
| 3624 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3625 | * |
| 3626 | * @deprecated Use the new {@link DialogFragment} class with |
| 3627 | * {@link FragmentManager} instead; this is also |
| 3628 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3629 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3630 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3631 | protected void onPrepareDialog(int id, Dialog dialog, Bundle args) { |
| 3632 | onPrepareDialog(id, dialog); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3633 | } |
| 3634 | |
| 3635 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3636 | * Simple version of {@link #showDialog(int, Bundle)} that does not |
| 3637 | * take any arguments. Simply calls {@link #showDialog(int, Bundle)} |
| 3638 | * with null arguments. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3639 | * |
| 3640 | * @deprecated Use the new {@link DialogFragment} class with |
| 3641 | * {@link FragmentManager} instead; this is also |
| 3642 | * available on older platforms through the Android compatibility package. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3643 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3644 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3645 | public final void showDialog(int id) { |
| 3646 | showDialog(id, null); |
| 3647 | } |
| 3648 | |
| 3649 | /** |
| 3650 | * 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] | 3651 | * will be made with the same id the first time this is called for a given |
| 3652 | * id. From thereafter, the dialog will be automatically saved and restored. |
| 3653 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 3654 | * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 3655 | * or later, consider instead using a {@link DialogFragment} instead.</em> |
| 3656 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3657 | * <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] | 3658 | * be made to provide an opportunity to do any timely preparation. |
| 3659 | * |
| 3660 | * @param id The id of the managed dialog. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3661 | * @param args Arguments to pass through to the dialog. These will be saved |
| 3662 | * and restored for you. Note that if the dialog is already created, |
| 3663 | * {@link #onCreateDialog(int, Bundle)} will not be called with the new |
| 3664 | * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be. |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 3665 | * If you need to rebuild the dialog, call {@link #removeDialog(int)} first. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3666 | * @return Returns true if the Dialog was created; false is returned if |
| 3667 | * it is not created because {@link #onCreateDialog(int, Bundle)} returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3668 | * |
Joe Onorato | 37296dc | 2009-07-31 17:58:55 -0700 | [diff] [blame] | 3669 | * @see Dialog |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3670 | * @see #onCreateDialog(int, Bundle) |
| 3671 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3672 | * @see #dismissDialog(int) |
| 3673 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3674 | * |
| 3675 | * @deprecated Use the new {@link DialogFragment} class with |
| 3676 | * {@link FragmentManager} instead; this is also |
| 3677 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3678 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3679 | @Nullable |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3680 | @Deprecated |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3681 | public final boolean showDialog(int id, Bundle args) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3682 | if (mManagedDialogs == null) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3683 | mManagedDialogs = new SparseArray<ManagedDialog>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3684 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3685 | ManagedDialog md = mManagedDialogs.get(id); |
| 3686 | if (md == null) { |
| 3687 | md = new ManagedDialog(); |
| 3688 | md.mDialog = createDialog(id, null, args); |
| 3689 | if (md.mDialog == null) { |
| 3690 | return false; |
| 3691 | } |
| 3692 | mManagedDialogs.put(id, md); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3693 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3694 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3695 | md.mArgs = args; |
| 3696 | onPrepareDialog(id, md.mDialog, args); |
| 3697 | md.mDialog.show(); |
| 3698 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | /** |
| 3702 | * Dismiss a dialog that was previously shown via {@link #showDialog(int)}. |
| 3703 | * |
| 3704 | * @param id The id of the managed dialog. |
| 3705 | * |
| 3706 | * @throws IllegalArgumentException if the id was not previously shown via |
| 3707 | * {@link #showDialog(int)}. |
| 3708 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3709 | * @see #onCreateDialog(int, Bundle) |
| 3710 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3711 | * @see #showDialog(int) |
| 3712 | * @see #removeDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3713 | * |
| 3714 | * @deprecated Use the new {@link DialogFragment} class with |
| 3715 | * {@link FragmentManager} instead; this is also |
| 3716 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3717 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3718 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3719 | public final void dismissDialog(int id) { |
| 3720 | if (mManagedDialogs == null) { |
| 3721 | throw missingDialog(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3722 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3723 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3724 | final ManagedDialog md = mManagedDialogs.get(id); |
| 3725 | if (md == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3726 | throw missingDialog(id); |
| 3727 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3728 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | /** |
| 3732 | * Creates an exception to throw if a user passed in a dialog id that is |
| 3733 | * unexpected. |
| 3734 | */ |
| 3735 | private IllegalArgumentException missingDialog(int id) { |
| 3736 | return new IllegalArgumentException("no dialog with id " + id + " was ever " |
| 3737 | + "shown via Activity#showDialog"); |
| 3738 | } |
| 3739 | |
| 3740 | /** |
| 3741 | * Removes any internal references to a dialog managed by this Activity. |
| 3742 | * If the dialog is showing, it will dismiss it as part of the clean up. |
| 3743 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3744 | * <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] | 3745 | * want to avoid the overhead of saving and restoring it in the future. |
| 3746 | * |
Dianne Hackborn | d2ce8bbb | 2010-10-06 16:46:05 -0700 | [diff] [blame] | 3747 | * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function |
| 3748 | * will not throw an exception if you try to remove an ID that does not |
| 3749 | * currently have an associated dialog.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3750 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3751 | * @param id The id of the managed dialog. |
| 3752 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3753 | * @see #onCreateDialog(int, Bundle) |
| 3754 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3755 | * @see #showDialog(int) |
| 3756 | * @see #dismissDialog(int) |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 3757 | * |
| 3758 | * @deprecated Use the new {@link DialogFragment} class with |
| 3759 | * {@link FragmentManager} instead; this is also |
| 3760 | * available on older platforms through the Android compatibility package. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3761 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 3762 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3763 | public final void removeDialog(int id) { |
Dianne Hackborn | d2ce8bbb | 2010-10-06 16:46:05 -0700 | [diff] [blame] | 3764 | if (mManagedDialogs != null) { |
| 3765 | final ManagedDialog md = mManagedDialogs.get(id); |
| 3766 | if (md != null) { |
| 3767 | md.mDialog.dismiss(); |
| 3768 | mManagedDialogs.remove(id); |
| 3769 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3770 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | /** |
| 3774 | * 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] | 3775 | * |
Bjorn Bringert | 6266e40 | 2009-09-25 14:25:41 +0100 | [diff] [blame] | 3776 | * <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] | 3777 | * menu item, search button, or other widgets within your activity. Unless overidden, |
Bjorn Bringert | 6266e40 | 2009-09-25 14:25:41 +0100 | [diff] [blame] | 3778 | * calling this function is the same as calling |
| 3779 | * {@link #startSearch startSearch(null, false, null, false)}, which launches |
| 3780 | * search for the current activity as specified in its manifest, see {@link SearchManager}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3781 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3782 | * <p>You can override this function to force global search, e.g. in response to a dedicated |
| 3783 | * search key, or to block search entirely (by simply returning false). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3784 | * |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3785 | * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default |
| 3786 | * implementation changes to simply return false and you must supply your own custom |
| 3787 | * implementation if you want to support search.</p> |
| 3788 | * |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 3789 | * @param searchEvent The {@link SearchEvent} that signaled this search. |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3790 | * @return Returns {@code true} if search launched, and {@code false} if the activity does |
| 3791 | * not respond to search. The default implementation always returns {@code true}, except |
| 3792 | * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3793 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3794 | * @see android.app.SearchManager |
| 3795 | */ |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 3796 | public boolean onSearchRequested(@Nullable SearchEvent searchEvent) { |
| 3797 | mSearchEvent = searchEvent; |
| 3798 | boolean result = onSearchRequested(); |
| 3799 | mSearchEvent = null; |
| 3800 | return result; |
| 3801 | } |
| 3802 | |
| 3803 | /** |
| 3804 | * @see #onSearchRequested(SearchEvent) |
| 3805 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3806 | public boolean onSearchRequested() { |
Dianne Hackborn | fdf5b35 | 2014-10-08 17:43:48 -0700 | [diff] [blame] | 3807 | if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK) |
| 3808 | != Configuration.UI_MODE_TYPE_TELEVISION) { |
| 3809 | startSearch(null, false, null, false); |
| 3810 | return true; |
| 3811 | } else { |
| 3812 | return false; |
| 3813 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3814 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3815 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3816 | /** |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 3817 | * During the onSearchRequested() callbacks, this function will return the |
| 3818 | * {@link SearchEvent} that triggered the callback, if it exists. |
| 3819 | * |
| 3820 | * @return SearchEvent The SearchEvent that triggered the {@link |
| 3821 | * #onSearchRequested} callback. |
| 3822 | */ |
| 3823 | public final SearchEvent getSearchEvent() { |
| 3824 | return mSearchEvent; |
| 3825 | } |
| 3826 | |
| 3827 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3828 | * This hook is called to launch the search UI. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3829 | * |
| 3830 | * <p>It is typically called from onSearchRequested(), either directly from |
| 3831 | * 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] | 3832 | * 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] | 3833 | * 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] | 3834 | * is to inject specific data such as context data, it is preferred to <i>override</i> |
| 3835 | * onSearchRequested(), so that any callers to it will benefit from the override. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3836 | * |
| 3837 | * @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] | 3838 | * pre-entered text in the search query box. |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3839 | * @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] | 3840 | * any further typing will replace it. This is useful for cases where an entire pre-formed |
| 3841 | * query is being inserted. If false, the selection point will be placed at the end of the |
| 3842 | * inserted query. This is useful when the inserted query is text that the user entered, |
| 3843 | * and the user would expect to be able to keep typing. <i>This parameter is only meaningful |
| 3844 | * if initialQuery is a non-empty string.</i> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3845 | * @param appSearchData An application can insert application-specific |
| 3846 | * 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] | 3847 | * searches. This data will be returned with SEARCH intent(s). Null if |
| 3848 | * no extra data is required. |
| 3849 | * @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] | 3850 | * 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] | 3851 | * 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] | 3852 | * 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] | 3853 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3854 | * @see android.app.SearchManager |
| 3855 | * @see #onSearchRequested |
| 3856 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3857 | public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery, |
| 3858 | @Nullable Bundle appSearchData, boolean globalSearch) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3859 | ensureSearchManager(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 3860 | mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(), |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3861 | appSearchData, globalSearch); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | /** |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3865 | * Similar to {@link #startSearch}, but actually fires off the search query after invoking |
| 3866 | * the search dialog. Made available for testing purposes. |
| 3867 | * |
| 3868 | * @param query The query to trigger. If empty, the request will be ignored. |
| 3869 | * @param appSearchData An application can insert application-specific |
| 3870 | * context here, in order to improve quality or specificity of its own |
| 3871 | * searches. This data will be returned with SEARCH intent(s). Null if |
| 3872 | * no extra data is required. |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3873 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3874 | public void triggerSearch(String query, @Nullable Bundle appSearchData) { |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3875 | ensureSearchManager(); |
Bjorn Bringert | b782a2f | 2009-10-01 09:57:33 +0100 | [diff] [blame] | 3876 | mSearchManager.triggerSearch(query, getComponentName(), appSearchData); |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 3877 | } |
| 3878 | |
| 3879 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3880 | * Request that key events come to this activity. Use this if your |
| 3881 | * activity has no views with focus, but the activity still wants |
| 3882 | * a chance to process key events. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3883 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3884 | * @see android.view.Window#takeKeyEvents |
| 3885 | */ |
| 3886 | public void takeKeyEvents(boolean get) { |
| 3887 | getWindow().takeKeyEvents(get); |
| 3888 | } |
| 3889 | |
| 3890 | /** |
| 3891 | * Enable extended window features. This is a convenience for calling |
| 3892 | * {@link android.view.Window#requestFeature getWindow().requestFeature()}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3893 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3894 | * @param featureId The desired feature as defined in |
| 3895 | * {@link android.view.Window}. |
| 3896 | * @return Returns true if the requested feature is supported and now |
| 3897 | * enabled. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 3898 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3899 | * @see android.view.Window#requestFeature |
| 3900 | */ |
| 3901 | public final boolean requestWindowFeature(int featureId) { |
| 3902 | return getWindow().requestFeature(featureId); |
| 3903 | } |
| 3904 | |
| 3905 | /** |
| 3906 | * Convenience for calling |
| 3907 | * {@link android.view.Window#setFeatureDrawableResource}. |
| 3908 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 3909 | public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3910 | getWindow().setFeatureDrawableResource(featureId, resId); |
| 3911 | } |
| 3912 | |
| 3913 | /** |
| 3914 | * Convenience for calling |
| 3915 | * {@link android.view.Window#setFeatureDrawableUri}. |
| 3916 | */ |
| 3917 | public final void setFeatureDrawableUri(int featureId, Uri uri) { |
| 3918 | getWindow().setFeatureDrawableUri(featureId, uri); |
| 3919 | } |
| 3920 | |
| 3921 | /** |
| 3922 | * Convenience for calling |
| 3923 | * {@link android.view.Window#setFeatureDrawable(int, Drawable)}. |
| 3924 | */ |
| 3925 | public final void setFeatureDrawable(int featureId, Drawable drawable) { |
| 3926 | getWindow().setFeatureDrawable(featureId, drawable); |
| 3927 | } |
| 3928 | |
| 3929 | /** |
| 3930 | * Convenience for calling |
| 3931 | * {@link android.view.Window#setFeatureDrawableAlpha}. |
| 3932 | */ |
| 3933 | public final void setFeatureDrawableAlpha(int featureId, int alpha) { |
| 3934 | getWindow().setFeatureDrawableAlpha(featureId, alpha); |
| 3935 | } |
| 3936 | |
| 3937 | /** |
| 3938 | * Convenience for calling |
| 3939 | * {@link android.view.Window#getLayoutInflater}. |
| 3940 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3941 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3942 | public LayoutInflater getLayoutInflater() { |
| 3943 | return getWindow().getLayoutInflater(); |
| 3944 | } |
| 3945 | |
| 3946 | /** |
| 3947 | * Returns a {@link MenuInflater} with this context. |
| 3948 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 3949 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3950 | public MenuInflater getMenuInflater() { |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 3951 | // Make sure that action views can get an appropriate theme. |
| 3952 | if (mMenuInflater == null) { |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 3953 | initWindowDecorActionBar(); |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 3954 | if (mActionBar != null) { |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 3955 | mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this); |
Adam Powell | 88ab697 | 2011-07-28 11:25:01 -0700 | [diff] [blame] | 3956 | } else { |
| 3957 | mMenuInflater = new MenuInflater(this); |
| 3958 | } |
| 3959 | } |
| 3960 | return mMenuInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | @Override |
Filip Gruszczynski | 0daf210 | 2015-09-29 08:39:07 -0700 | [diff] [blame] | 3964 | public void setTheme(int resid) { |
| 3965 | super.setTheme(resid); |
| 3966 | mWindow.setTheme(resid); |
| 3967 | } |
| 3968 | |
| 3969 | @Override |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 3970 | protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3971 | boolean first) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3972 | if (mParent == null) { |
| 3973 | super.onApplyThemeResource(theme, resid, first); |
| 3974 | } else { |
| 3975 | try { |
| 3976 | theme.setTo(mParent.getTheme()); |
| 3977 | } catch (Exception e) { |
| 3978 | // Empty |
| 3979 | } |
| 3980 | theme.applyStyle(resid, false); |
| 3981 | } |
Winson | 073a526 | 2016-02-29 16:04:40 -0800 | [diff] [blame^] | 3982 | |
| 3983 | // Get the primary color and update the TaskDescription for this activity |
| 3984 | TypedArray a = theme.obtainStyledAttributes( |
| 3985 | com.android.internal.R.styleable.ActivityTaskDescription); |
| 3986 | if (mTaskDescription.getPrimaryColor() == 0) { |
| 3987 | int colorPrimary = a.getColor( |
| 3988 | com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0); |
| 3989 | if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) { |
| 3990 | mTaskDescription.setPrimaryColor(colorPrimary); |
| 3991 | } |
| 3992 | } |
| 3993 | // For dev-preview only. |
| 3994 | if (mTaskDescription.getBackgroundColor() == 0) { |
| 3995 | int colorBackground = a.getColor( |
| 3996 | com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0); |
| 3997 | if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) { |
| 3998 | mTaskDescription.setBackgroundColor(colorBackground); |
| 3999 | } |
| 4000 | } |
| 4001 | a.recycle(); |
| 4002 | setTaskDescription(mTaskDescription); |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | /** |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4006 | * Requests permissions to be granted to this application. These permissions |
| 4007 | * must be requested in your manifest, they should not be granted to your app, |
| 4008 | * and they should have protection level {@link android.content.pm.PermissionInfo |
| 4009 | * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by |
| 4010 | * the platform or a third-party app. |
| 4011 | * <p> |
| 4012 | * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL} |
| 4013 | * are granted at install time if requested in the manifest. Signature permissions |
| 4014 | * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at |
| 4015 | * install time if requested in the manifest and the signature of your app matches |
| 4016 | * the signature of the app declaring the permissions. |
| 4017 | * </p> |
| 4018 | * <p> |
| 4019 | * If your app does not have the requested permissions the user will be presented |
| 4020 | * with UI for accepting them. After the user has accepted or rejected the |
| 4021 | * requested permissions you will receive a callback on {@link |
| 4022 | * #onRequestPermissionsResult(int, String[], int[])} reporting whether the |
| 4023 | * permissions were granted or not. |
| 4024 | * </p> |
| 4025 | * <p> |
| 4026 | * Note that requesting a permission does not guarantee it will be granted and |
| 4027 | * your app should be able to run without having this permission. |
| 4028 | * </p> |
| 4029 | * <p> |
| 4030 | * This method may start an activity allowing the user to choose which permissions |
| 4031 | * to grant and which to reject. Hence, you should be prepared that your activity |
| 4032 | * may be paused and resumed. Further, granting some permissions may require |
| 4033 | * a restart of you application. In such a case, the system will recreate the |
| 4034 | * activity stack before delivering the result to {@link |
| 4035 | * #onRequestPermissionsResult(int, String[], int[])}. |
| 4036 | * </p> |
| 4037 | * <p> |
| 4038 | * When checking whether you have a permission you should use {@link |
| 4039 | * #checkSelfPermission(String)}. |
| 4040 | * </p> |
| 4041 | * <p> |
Svet Ganov | d7ad54e | 2015-08-21 06:18:09 -0700 | [diff] [blame] | 4042 | * Calling this API for permissions already granted to your app would show UI |
| 4043 | * to the user to decide whether the app can still hold these permissions. This |
| 4044 | * can be useful if the way your app uses data guarded by the permissions |
| 4045 | * changes significantly. |
| 4046 | * </p> |
| 4047 | * <p> |
Svet Ganov | 255aece | 2015-07-03 16:03:53 -0700 | [diff] [blame] | 4048 | * You cannot request a permission if your activity sets {@link |
| 4049 | * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to |
| 4050 | * <code>true</code> because in this case the activity would not receive |
| 4051 | * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}. |
| 4052 | * </p> |
| 4053 | * <p> |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4054 | * A sample permissions request looks like this: |
| 4055 | * </p> |
| 4056 | * <code><pre><p> |
| 4057 | * private void showContacts() { |
| 4058 | * if (checkSelfPermission(Manifest.permission.READ_CONTACTS) |
| 4059 | * != PackageManager.PERMISSION_GRANTED) { |
| 4060 | * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, |
| 4061 | * PERMISSIONS_REQUEST_READ_CONTACTS); |
| 4062 | * } else { |
| 4063 | * doShowContacts(); |
| 4064 | * } |
| 4065 | * } |
| 4066 | * |
| 4067 | * {@literal @}Override |
| 4068 | * public void onRequestPermissionsResult(int requestCode, String[] permissions, |
| 4069 | * int[] grantResults) { |
| 4070 | * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS |
| 4071 | * && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 4072 | * showContacts(); |
| 4073 | * } |
| 4074 | * } |
| 4075 | * </code></pre></p> |
| 4076 | * |
Svet Ganov | f66381c | 2016-02-18 20:02:36 -0800 | [diff] [blame] | 4077 | * @param permissions The requested permissions. Must me non-null and not empty. |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4078 | * @param requestCode Application specific request code to match with a result |
| 4079 | * reported to {@link #onRequestPermissionsResult(int, String[], int[])}. |
Svet Ganov | 255aece | 2015-07-03 16:03:53 -0700 | [diff] [blame] | 4080 | * Should be >= 0. |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4081 | * |
| 4082 | * @see #onRequestPermissionsResult(int, String[], int[]) |
| 4083 | * @see #checkSelfPermission(String) |
Svetoslav | aa048b6 | 2015-06-02 15:08:48 -0700 | [diff] [blame] | 4084 | * @see #shouldShowRequestPermissionRationale(String) |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4085 | */ |
| 4086 | public final void requestPermissions(@NonNull String[] permissions, int requestCode) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 4087 | if (mHasCurrentPermissionsRequest) { |
| 4088 | Log.w(TAG, "Can reqeust only one set of permissions at a time"); |
| 4089 | // Dispatch the callback with empty arrays which means a cancellation. |
| 4090 | onRequestPermissionsResult(requestCode, new String[0], new int[0]); |
| 4091 | return; |
| 4092 | } |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4093 | Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions); |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 4094 | startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null); |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 4095 | mHasCurrentPermissionsRequest = true; |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | /** |
| 4099 | * Callback for the result from requesting permissions. This method |
| 4100 | * is invoked for every call on {@link #requestPermissions(String[], int)}. |
Svet Ganov | 0f45c7c | 2015-07-16 16:28:51 -0700 | [diff] [blame] | 4101 | * <p> |
| 4102 | * <strong>Note:</strong> It is possible that the permissions request interaction |
| 4103 | * with the user is interrupted. In this case you will receive empty permissions |
| 4104 | * and results arrays which should be treated as a cancellation. |
| 4105 | * </p> |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 4106 | * |
| 4107 | * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}. |
| 4108 | * @param permissions The requested permissions. Never null. |
| 4109 | * @param grantResults The grant results for the corresponding permissions |
| 4110 | * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED} |
| 4111 | * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null. |
| 4112 | * |
| 4113 | * @see #requestPermissions(String[], int) |
| 4114 | */ |
| 4115 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
| 4116 | @NonNull int[] grantResults) { |
| 4117 | /* callback - no nothing */ |
| 4118 | } |
| 4119 | |
| 4120 | /** |
Svetoslav | 20770dd | 2015-05-29 15:43:04 -0700 | [diff] [blame] | 4121 | * Gets whether you should show UI with rationale for requesting a permission. |
| 4122 | * You should do this only if you do not have the permission and the context in |
| 4123 | * which the permission is requested does not clearly communicate to the user |
| 4124 | * what would be the benefit from granting this permission. |
| 4125 | * <p> |
| 4126 | * For example, if you write a camera app, requesting the camera permission |
| 4127 | * would be expected by the user and no rationale for why it is requested is |
| 4128 | * needed. If however, the app needs location for tagging photos then a non-tech |
| 4129 | * savvy user may wonder how location is related to taking photos. In this case |
| 4130 | * you may choose to show UI with rationale of requesting this permission. |
| 4131 | * </p> |
| 4132 | * |
| 4133 | * @param permission A permission your app wants to request. |
| 4134 | * @return Whether you can show permission rationale UI. |
| 4135 | * |
| 4136 | * @see #checkSelfPermission(String) |
| 4137 | * @see #requestPermissions(String[], int) |
| 4138 | * @see #onRequestPermissionsResult(int, String[], int[]) |
| 4139 | */ |
Svetoslav | 9ce9a15 | 2015-06-02 17:59:31 -0700 | [diff] [blame] | 4140 | public boolean shouldShowRequestPermissionRationale(@NonNull String permission) { |
Svetoslav | 20770dd | 2015-05-29 15:43:04 -0700 | [diff] [blame] | 4141 | return getPackageManager().shouldShowRequestPermissionRationale(permission); |
| 4142 | } |
| 4143 | |
| 4144 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4145 | * Same as calling {@link #startActivityForResult(Intent, int, Bundle)} |
| 4146 | * with no options. |
| 4147 | * |
| 4148 | * @param intent The intent to start. |
| 4149 | * @param requestCode If >= 0, this code will be returned in |
| 4150 | * onActivityResult() when the activity exits. |
| 4151 | * |
| 4152 | * @throws android.content.ActivityNotFoundException |
| 4153 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4154 | * @see #startActivity |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4155 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4156 | public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) { |
George Mount | abb63cb | 2014-06-23 11:17:58 -0700 | [diff] [blame] | 4157 | startActivityForResult(intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4161 | * Launch an activity for which you would like a result when it finished. |
| 4162 | * When this activity exits, your |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4163 | * onActivityResult() method will be called with the given requestCode. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4164 | * Using a negative requestCode is the same as calling |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4165 | * {@link #startActivity} (the activity is not launched as a sub-activity). |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4166 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4167 | * <p>Note that this method should only be used with Intent protocols |
| 4168 | * that are defined to return a result. In other protocols (such as |
| 4169 | * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may |
| 4170 | * not get the result when you expect. For example, if the activity you |
| 4171 | * are launching uses the singleTask launch mode, it will not run in your |
| 4172 | * task and thus you will immediately receive a cancel result. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4173 | * |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4174 | * <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] | 4175 | * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4176 | * activity, then your window will not be displayed until a result is |
| 4177 | * returned back from the started activity. This is to avoid visible |
| 4178 | * flickering when redirecting to another activity. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4179 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4180 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4181 | * if there was no Activity found to run the given Intent. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4182 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4183 | * @param intent The intent to start. |
| 4184 | * @param requestCode If >= 0, this code will be returned in |
| 4185 | * onActivityResult() when the activity exits. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4186 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4187 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4188 | * Context.startActivity(Intent, Bundle)} for more details. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4189 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4190 | * @throws android.content.ActivityNotFoundException |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4191 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 4192 | * @see #startActivity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4193 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4194 | public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, |
| 4195 | @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4196 | if (mParent == null) { |
| 4197 | Instrumentation.ActivityResult ar = |
| 4198 | mInstrumentation.execStartActivity( |
| 4199 | this, mMainThread.getApplicationThread(), mToken, this, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4200 | intent, requestCode, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4201 | if (ar != null) { |
| 4202 | mMainThread.sendActivityResult( |
| 4203 | mToken, mEmbeddedID, requestCode, ar.getResultCode(), |
| 4204 | ar.getResultData()); |
| 4205 | } |
| 4206 | if (requestCode >= 0) { |
| 4207 | // If this start is requesting a result, we can avoid making |
| 4208 | // the activity visible until the result is received. Setting |
| 4209 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4210 | // activity hidden during this time, to avoid flickering. |
| 4211 | // This can only be done when a result is requested because |
| 4212 | // that guarantees we will get information back when the |
| 4213 | // activity is finished, no matter what happens to it. |
| 4214 | mStartedActivity = true; |
| 4215 | } |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 4216 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4217 | cancelInputsAndStartExitTransition(options); |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 4218 | // 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] | 4219 | } else { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4220 | if (options != null) { |
| 4221 | mParent.startActivityFromChild(this, intent, requestCode, options); |
| 4222 | } else { |
| 4223 | // Note we want to go through this method for compatibility with |
| 4224 | // existing applications that may have overridden it. |
| 4225 | mParent.startActivityFromChild(this, intent, requestCode); |
| 4226 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4227 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | /** |
| 4231 | * Cancels pending inputs and if an Activity Transition is to be run, starts the transition. |
| 4232 | * |
| 4233 | * @param options The ActivityOptions bundle used to start an Activity. |
| 4234 | */ |
| 4235 | private void cancelInputsAndStartExitTransition(Bundle options) { |
| 4236 | final View decor = mWindow != null ? mWindow.peekDecorView() : null; |
| 4237 | if (decor != null) { |
| 4238 | decor.cancelPendingInputEvents(); |
| 4239 | } |
George Mount | ff24328 | 2014-07-07 16:12:07 -0700 | [diff] [blame] | 4240 | if (options != null && !isTopOfTask()) { |
| 4241 | mActivityTransitionState.startExitOutTransition(this, options); |
| 4242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | /** |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4246 | * @hide Implement to provide correct calling token. |
| 4247 | */ |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4248 | public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) { |
| 4249 | startActivityForResultAsUser(intent, requestCode, null, user); |
| 4250 | } |
| 4251 | |
| 4252 | /** |
| 4253 | * @hide Implement to provide correct calling token. |
| 4254 | */ |
| 4255 | public void startActivityForResultAsUser(Intent intent, int requestCode, |
| 4256 | @Nullable Bundle options, UserHandle user) { |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4257 | if (mParent != null) { |
| 4258 | throw new RuntimeException("Can't be called from a child"); |
| 4259 | } |
| 4260 | Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( |
| 4261 | this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, |
| 4262 | options, user); |
| 4263 | if (ar != null) { |
| 4264 | mMainThread.sendActivityResult( |
| 4265 | mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); |
| 4266 | } |
| 4267 | if (requestCode >= 0) { |
| 4268 | // If this start is requesting a result, we can avoid making |
| 4269 | // the activity visible until the result is received. Setting |
| 4270 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4271 | // activity hidden during this time, to avoid flickering. |
| 4272 | // This can only be done when a result is requested because |
| 4273 | // that guarantees we will get information back when the |
| 4274 | // activity is finished, no matter what happens to it. |
| 4275 | mStartedActivity = true; |
| 4276 | } |
| 4277 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4278 | cancelInputsAndStartExitTransition(options); |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | /** |
| 4282 | * @hide Implement to provide correct calling token. |
| 4283 | */ |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4284 | public void startActivityAsUser(Intent intent, UserHandle user) { |
| 4285 | startActivityAsUser(intent, null, user); |
| 4286 | } |
| 4287 | |
| 4288 | /** |
| 4289 | * @hide Implement to provide correct calling token. |
| 4290 | */ |
| 4291 | public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) { |
| 4292 | if (mParent != null) { |
Alexandra Gherghina | 8323145 | 2014-07-04 12:05:20 +0100 | [diff] [blame] | 4293 | throw new RuntimeException("Can't be called from a child"); |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4294 | } |
| 4295 | Instrumentation.ActivityResult ar = |
| 4296 | mInstrumentation.execStartActivity( |
| 4297 | this, mMainThread.getApplicationThread(), mToken, this, |
| 4298 | intent, -1, options, user); |
| 4299 | if (ar != null) { |
| 4300 | mMainThread.sendActivityResult( |
| 4301 | mToken, mEmbeddedID, -1, ar.getResultCode(), |
| 4302 | ar.getResultData()); |
| 4303 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4304 | cancelInputsAndStartExitTransition(options); |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | /** |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4308 | * Start a new activity as if it was started by the activity that started our |
| 4309 | * current activity. This is for the resolver and chooser activities, which operate |
| 4310 | * as intermediaries that dispatch their intent to the target the user selects -- to |
| 4311 | * do this, they must perform all security checks including permission grants as if |
| 4312 | * their launch had come from the original activity. |
Dianne Hackborn | a7cfbe0 | 2015-07-16 10:52:52 -0700 | [diff] [blame] | 4313 | * @param intent The Intent to start. |
| 4314 | * @param options ActivityOptions or null. |
| 4315 | * @param ignoreTargetSecurity If true, the activity manager will not check whether the |
| 4316 | * caller it is doing the start is, is actually allowed to start the target activity. |
| 4317 | * If you set this to true, you must set an explicit component in the Intent and do any |
| 4318 | * appropriate security checks yourself. |
| 4319 | * @param userId The user the new activity should run as. |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4320 | * @hide |
| 4321 | */ |
Dianne Hackborn | a7cfbe0 | 2015-07-16 10:52:52 -0700 | [diff] [blame] | 4322 | public void startActivityAsCaller(Intent intent, @Nullable Bundle options, |
| 4323 | boolean ignoreTargetSecurity, int userId) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4324 | if (mParent != null) { |
| 4325 | throw new RuntimeException("Can't be called from a child"); |
| 4326 | } |
| 4327 | Instrumentation.ActivityResult ar = |
| 4328 | mInstrumentation.execStartActivityAsCaller( |
| 4329 | this, mMainThread.getApplicationThread(), mToken, this, |
Dianne Hackborn | a7cfbe0 | 2015-07-16 10:52:52 -0700 | [diff] [blame] | 4330 | intent, -1, options, ignoreTargetSecurity, userId); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4331 | if (ar != null) { |
| 4332 | mMainThread.sendActivityResult( |
| 4333 | mToken, mEmbeddedID, -1, ar.getResultCode(), |
| 4334 | ar.getResultData()); |
| 4335 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4336 | cancelInputsAndStartExitTransition(options); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4340 | * Same as calling {@link #startIntentSenderForResult(IntentSender, int, |
| 4341 | * Intent, int, int, int, Bundle)} with no options. |
| 4342 | * |
| 4343 | * @param intent The IntentSender to launch. |
| 4344 | * @param requestCode If >= 0, this code will be returned in |
| 4345 | * onActivityResult() when the activity exits. |
| 4346 | * @param fillInIntent If non-null, this will be provided as the |
| 4347 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4348 | * @param flagsMask Intent flags in the original IntentSender that you |
| 4349 | * would like to change. |
| 4350 | * @param flagsValues Desired values for any bits set in |
| 4351 | * <var>flagsMask</var> |
| 4352 | * @param extraFlags Always set to 0. |
| 4353 | */ |
| 4354 | public void startIntentSenderForResult(IntentSender intent, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4355 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4356 | throws IntentSender.SendIntentException { |
| 4357 | startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask, |
| 4358 | flagsValues, extraFlags, null); |
| 4359 | } |
| 4360 | |
| 4361 | /** |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4362 | * Like {@link #startActivityForResult(Intent, int)}, but allowing you |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4363 | * to use a IntentSender to describe the activity to be started. If |
| 4364 | * the IntentSender is for an activity, that activity will be started |
| 4365 | * as if you had called the regular {@link #startActivityForResult(Intent, int)} |
| 4366 | * here; otherwise, its associated action will be executed (such as |
| 4367 | * sending a broadcast) as if you had called |
| 4368 | * {@link IntentSender#sendIntent IntentSender.sendIntent} on it. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4369 | * |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4370 | * @param intent The IntentSender to launch. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4371 | * @param requestCode If >= 0, this code will be returned in |
| 4372 | * onActivityResult() when the activity exits. |
| 4373 | * @param fillInIntent If non-null, this will be provided as the |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4374 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4375 | * @param flagsMask Intent flags in the original IntentSender that you |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4376 | * would like to change. |
| 4377 | * @param flagsValues Desired values for any bits set in |
| 4378 | * <var>flagsMask</var> |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4379 | * @param extraFlags Always set to 0. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4380 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4381 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4382 | * Context.startActivity(Intent, Bundle)} for more details. If options |
| 4383 | * have also been supplied by the IntentSender, options given here will |
| 4384 | * override any that conflict with those given by the IntentSender. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4385 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4386 | public void startIntentSenderForResult(IntentSender intent, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4387 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4388 | Bundle options) throws IntentSender.SendIntentException { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4389 | if (mParent == null) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4390 | startIntentSenderForResultInner(intent, requestCode, fillInIntent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4391 | flagsMask, flagsValues, this, options); |
| 4392 | } else if (options != null) { |
| 4393 | mParent.startIntentSenderFromChild(this, intent, requestCode, |
| 4394 | fillInIntent, flagsMask, flagsValues, extraFlags, options); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4395 | } else { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4396 | // Note we want to go through this call for compatibility with |
| 4397 | // existing applications that may have overridden the method. |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4398 | mParent.startIntentSenderFromChild(this, intent, requestCode, |
| 4399 | fillInIntent, flagsMask, flagsValues, extraFlags); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4400 | } |
| 4401 | } |
| 4402 | |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4403 | private void startIntentSenderForResultInner(IntentSender intent, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4404 | Intent fillInIntent, int flagsMask, int flagsValues, Activity activity, |
| 4405 | Bundle options) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4406 | throws IntentSender.SendIntentException { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4407 | try { |
| 4408 | String resolvedType = null; |
| 4409 | if (fillInIntent != null) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4410 | fillInIntent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 4411 | fillInIntent.prepareToLeaveProcess(this); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4412 | resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); |
| 4413 | } |
| 4414 | int result = ActivityManagerNative.getDefault() |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4415 | .startActivityIntentSender(mMainThread.getApplicationThread(), intent, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4416 | fillInIntent, resolvedType, mToken, activity.mEmbeddedID, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4417 | requestCode, flagsMask, flagsValues, options); |
| 4418 | if (result == ActivityManager.START_CANCELED) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4419 | throw new IntentSender.SendIntentException(); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4420 | } |
| 4421 | Instrumentation.checkStartActivityResult(result, null); |
| 4422 | } catch (RemoteException e) { |
| 4423 | } |
| 4424 | if (requestCode >= 0) { |
| 4425 | // If this start is requesting a result, we can avoid making |
| 4426 | // the activity visible until the result is received. Setting |
| 4427 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4428 | // activity hidden during this time, to avoid flickering. |
| 4429 | // This can only be done when a result is requested because |
| 4430 | // that guarantees we will get information back when the |
| 4431 | // activity is finished, no matter what happens to it. |
| 4432 | mStartedActivity = true; |
| 4433 | } |
| 4434 | } |
| 4435 | |
| 4436 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4437 | * Same as {@link #startActivity(Intent, Bundle)} with no options |
| 4438 | * specified. |
| 4439 | * |
| 4440 | * @param intent The intent to start. |
| 4441 | * |
| 4442 | * @throws android.content.ActivityNotFoundException |
| 4443 | * |
| 4444 | * @see {@link #startActivity(Intent, Bundle)} |
| 4445 | * @see #startActivityForResult |
| 4446 | */ |
| 4447 | @Override |
| 4448 | public void startActivity(Intent intent) { |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 4449 | this.startActivity(intent, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4453 | * Launch a new activity. You will not receive any information about when |
| 4454 | * the activity exits. This implementation overrides the base version, |
| 4455 | * providing information about |
| 4456 | * the activity performing the launch. Because of this additional |
| 4457 | * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not |
| 4458 | * required; if not specified, the new activity will be added to the |
| 4459 | * task of the caller. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4460 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4461 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4462 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4463 | * |
| 4464 | * @param intent The intent to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4465 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4466 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4467 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4468 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4469 | * @throws android.content.ActivityNotFoundException |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4470 | * |
| 4471 | * @see {@link #startActivity(Intent)} |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4472 | * @see #startActivityForResult |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4473 | */ |
| 4474 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4475 | public void startActivity(Intent intent, @Nullable Bundle options) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4476 | if (options != null) { |
| 4477 | startActivityForResult(intent, -1, options); |
| 4478 | } else { |
| 4479 | // Note we want to go through this call for compatibility with |
| 4480 | // applications that may have overridden the method. |
| 4481 | startActivityForResult(intent, -1); |
| 4482 | } |
| 4483 | } |
| 4484 | |
| 4485 | /** |
| 4486 | * Same as {@link #startActivities(Intent[], Bundle)} with no options |
| 4487 | * specified. |
| 4488 | * |
| 4489 | * @param intents The intents to start. |
| 4490 | * |
| 4491 | * @throws android.content.ActivityNotFoundException |
| 4492 | * |
| 4493 | * @see {@link #startActivities(Intent[], Bundle)} |
| 4494 | * @see #startActivityForResult |
| 4495 | */ |
| 4496 | @Override |
| 4497 | public void startActivities(Intent[] intents) { |
| 4498 | startActivities(intents, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | /** |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4502 | * Launch a new activity. You will not receive any information about when |
| 4503 | * the activity exits. This implementation overrides the base version, |
| 4504 | * providing information about |
| 4505 | * the activity performing the launch. Because of this additional |
| 4506 | * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not |
| 4507 | * required; if not specified, the new activity will be added to the |
| 4508 | * task of the caller. |
| 4509 | * |
| 4510 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4511 | * if there was no Activity found to run the given Intent. |
| 4512 | * |
| 4513 | * @param intents The intents to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4514 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4515 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4516 | * Context.startActivity(Intent, Bundle)} for more details. |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4517 | * |
| 4518 | * @throws android.content.ActivityNotFoundException |
| 4519 | * |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4520 | * @see {@link #startActivities(Intent[])} |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4521 | * @see #startActivityForResult |
| 4522 | */ |
| 4523 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4524 | public void startActivities(Intent[] intents, @Nullable Bundle options) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4525 | mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(), |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4526 | mToken, this, intents, options); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4530 | * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)} |
| 4531 | * with no options. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4532 | * |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4533 | * @param intent The IntentSender to launch. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4534 | * @param fillInIntent If non-null, this will be provided as the |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4535 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4536 | * @param flagsMask Intent flags in the original IntentSender that you |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4537 | * would like to change. |
| 4538 | * @param flagsValues Desired values for any bits set in |
| 4539 | * <var>flagsMask</var> |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4540 | * @param extraFlags Always set to 0. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4541 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4542 | public void startIntentSender(IntentSender intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4543 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4544 | throws IntentSender.SendIntentException { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4545 | startIntentSender(intent, fillInIntent, flagsMask, flagsValues, |
| 4546 | extraFlags, null); |
| 4547 | } |
| 4548 | |
| 4549 | /** |
| 4550 | * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender |
| 4551 | * to start; see |
| 4552 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)} |
| 4553 | * for more information. |
| 4554 | * |
| 4555 | * @param intent The IntentSender to launch. |
| 4556 | * @param fillInIntent If non-null, this will be provided as the |
| 4557 | * intent parameter to {@link IntentSender#sendIntent}. |
| 4558 | * @param flagsMask Intent flags in the original IntentSender that you |
| 4559 | * would like to change. |
| 4560 | * @param flagsValues Desired values for any bits set in |
| 4561 | * <var>flagsMask</var> |
| 4562 | * @param extraFlags Always set to 0. |
| 4563 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4564 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4565 | * Context.startActivity(Intent, Bundle)} for more details. If options |
| 4566 | * have also been supplied by the IntentSender, options given here will |
| 4567 | * override any that conflict with those given by the IntentSender. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4568 | */ |
| 4569 | public void startIntentSender(IntentSender intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4570 | @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4571 | Bundle options) throws IntentSender.SendIntentException { |
| 4572 | if (options != null) { |
| 4573 | startIntentSenderForResult(intent, -1, fillInIntent, flagsMask, |
| 4574 | flagsValues, extraFlags, options); |
| 4575 | } else { |
| 4576 | // Note we want to go through this call for compatibility with |
| 4577 | // applications that may have overridden the method. |
| 4578 | startIntentSenderForResult(intent, -1, fillInIntent, flagsMask, |
| 4579 | flagsValues, extraFlags); |
| 4580 | } |
| 4581 | } |
| 4582 | |
| 4583 | /** |
| 4584 | * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)} |
| 4585 | * with no options. |
| 4586 | * |
| 4587 | * @param intent The intent to start. |
| 4588 | * @param requestCode If >= 0, this code will be returned in |
| 4589 | * onActivityResult() when the activity exits, as described in |
| 4590 | * {@link #startActivityForResult}. |
| 4591 | * |
| 4592 | * @return If a new activity was launched then true is returned; otherwise |
| 4593 | * false is returned and you must handle the Intent yourself. |
| 4594 | * |
| 4595 | * @see #startActivity |
| 4596 | * @see #startActivityForResult |
| 4597 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4598 | public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent, |
| 4599 | int requestCode) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4600 | return startActivityIfNeeded(intent, requestCode, null); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4604 | * A special variation to launch an activity only if a new activity |
| 4605 | * instance is needed to handle the given Intent. In other words, this is |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4606 | * 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] | 4607 | * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4608 | * singleTask or singleTop |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4609 | * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode}, |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4610 | * and the activity |
| 4611 | * that handles <var>intent</var> is the same as your currently running |
| 4612 | * activity, then a new instance is not needed. In this case, instead of |
| 4613 | * the normal behavior of calling {@link #onNewIntent} this function will |
| 4614 | * return and you can handle the Intent yourself. |
| 4615 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4616 | * <p>This function can only be called from a top-level activity; if it is |
| 4617 | * called from a child activity, a runtime exception will be thrown. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4618 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4619 | * @param intent The intent to start. |
| 4620 | * @param requestCode If >= 0, this code will be returned in |
| 4621 | * onActivityResult() when the activity exits, as described in |
| 4622 | * {@link #startActivityForResult}. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4623 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4624 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4625 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4626 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4627 | * @return If a new activity was launched then true is returned; otherwise |
| 4628 | * false is returned and you must handle the Intent yourself. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4629 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4630 | * @see #startActivity |
| 4631 | * @see #startActivityForResult |
| 4632 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4633 | public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent, |
| 4634 | int requestCode, @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4635 | if (mParent == null) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4636 | int result = ActivityManager.START_RETURN_INTENT_TO_CALLER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4637 | try { |
Dianne Hackborn | a3acdb3 | 2015-06-08 17:07:40 -0700 | [diff] [blame] | 4638 | Uri referrer = onProvideReferrer(); |
| 4639 | if (referrer != null) { |
| 4640 | intent.putExtra(Intent.EXTRA_REFERRER, referrer); |
| 4641 | } |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4642 | intent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 4643 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4644 | result = ActivityManagerNative.getDefault() |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 4645 | .startActivity(mMainThread.getApplicationThread(), getBasePackageName(), |
Jeff Hao | 1b012d3 | 2014-08-20 10:35:34 -0700 | [diff] [blame] | 4646 | intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken, |
| 4647 | mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED, |
| 4648 | null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4649 | } catch (RemoteException e) { |
| 4650 | // Empty |
| 4651 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4652 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4653 | Instrumentation.checkStartActivityResult(result, intent); |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4654 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4655 | if (requestCode >= 0) { |
| 4656 | // If this start is requesting a result, we can avoid making |
| 4657 | // the activity visible until the result is received. Setting |
| 4658 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 4659 | // activity hidden during this time, to avoid flickering. |
| 4660 | // This can only be done when a result is requested because |
| 4661 | // that guarantees we will get information back when the |
| 4662 | // activity is finished, no matter what happens to it. |
| 4663 | mStartedActivity = true; |
| 4664 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4665 | return result != ActivityManager.START_RETURN_INTENT_TO_CALLER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | throw new UnsupportedOperationException( |
| 4669 | "startActivityIfNeeded can only be called from a top-level activity"); |
| 4670 | } |
| 4671 | |
| 4672 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4673 | * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with |
| 4674 | * no options. |
| 4675 | * |
| 4676 | * @param intent The intent to dispatch to the next activity. For |
| 4677 | * correct behavior, this must be the same as the Intent that started |
| 4678 | * your own activity; the only changes you can make are to the extras |
| 4679 | * inside of it. |
| 4680 | * |
| 4681 | * @return Returns a boolean indicating whether there was another Activity |
| 4682 | * to start: true if there was a next activity to start, false if there |
| 4683 | * wasn't. In general, if true is returned you will then want to call |
| 4684 | * finish() on yourself. |
| 4685 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4686 | public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4687 | return startNextMatchingActivity(intent, null); |
| 4688 | } |
| 4689 | |
| 4690 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4691 | * Special version of starting an activity, for use when you are replacing |
| 4692 | * other activity components. You can use this to hand the Intent off |
| 4693 | * to the next Activity that can handle it. You typically call this in |
| 4694 | * {@link #onCreate} with the Intent returned by {@link #getIntent}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4695 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4696 | * @param intent The intent to dispatch to the next activity. For |
| 4697 | * correct behavior, this must be the same as the Intent that started |
| 4698 | * your own activity; the only changes you can make are to the extras |
| 4699 | * inside of it. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4700 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4701 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4702 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4703 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4704 | * @return Returns a boolean indicating whether there was another Activity |
| 4705 | * to start: true if there was a next activity to start, false if there |
| 4706 | * wasn't. In general, if true is returned you will then want to call |
| 4707 | * finish() on yourself. |
| 4708 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4709 | public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent, |
| 4710 | @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4711 | if (mParent == null) { |
| 4712 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 4713 | intent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 4714 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4715 | return ActivityManagerNative.getDefault() |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4716 | .startNextMatchingActivity(mToken, intent, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4717 | } catch (RemoteException e) { |
| 4718 | // Empty |
| 4719 | } |
| 4720 | return false; |
| 4721 | } |
| 4722 | |
| 4723 | throw new UnsupportedOperationException( |
| 4724 | "startNextMatchingActivity can only be called from a top-level activity"); |
| 4725 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4726 | |
| 4727 | /** |
| 4728 | * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)} |
| 4729 | * with no options. |
| 4730 | * |
| 4731 | * @param child The activity making the call. |
| 4732 | * @param intent The intent to start. |
| 4733 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4734 | * |
| 4735 | * @throws android.content.ActivityNotFoundException |
| 4736 | * |
| 4737 | * @see #startActivity |
| 4738 | * @see #startActivityForResult |
| 4739 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4740 | public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4741 | int requestCode) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 4742 | startActivityFromChild(child, intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4743 | } |
| 4744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4745 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4746 | * 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] | 4747 | * {@link #startActivity} or {@link #startActivityForResult} method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4748 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4749 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4750 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4751 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4752 | * @param child The activity making the call. |
| 4753 | * @param intent The intent to start. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4754 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4755 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4756 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4757 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4758 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4759 | * @throws android.content.ActivityNotFoundException |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4760 | * |
| 4761 | * @see #startActivity |
| 4762 | * @see #startActivityForResult |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4763 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4764 | public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4765 | int requestCode, @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4766 | Instrumentation.ActivityResult ar = |
| 4767 | mInstrumentation.execStartActivity( |
| 4768 | this, mMainThread.getApplicationThread(), mToken, child, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4769 | intent, requestCode, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4770 | if (ar != null) { |
| 4771 | mMainThread.sendActivityResult( |
| 4772 | mToken, child.mEmbeddedID, requestCode, |
| 4773 | ar.getResultCode(), ar.getResultData()); |
| 4774 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4775 | cancelInputsAndStartExitTransition(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4779 | * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)} |
| 4780 | * with no options. |
| 4781 | * |
| 4782 | * @param fragment The fragment making the call. |
| 4783 | * @param intent The intent to start. |
| 4784 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 4785 | * |
| 4786 | * @throws android.content.ActivityNotFoundException |
| 4787 | * |
| 4788 | * @see Fragment#startActivity |
| 4789 | * @see Fragment#startActivityForResult |
| 4790 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4791 | public void startActivityFromFragment(@NonNull Fragment fragment, |
| 4792 | @RequiresPermission Intent intent, int requestCode) { |
George Mount | abb63cb | 2014-06-23 11:17:58 -0700 | [diff] [blame] | 4793 | startActivityFromFragment(fragment, intent, requestCode, null); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4797 | * This is called when a Fragment in this activity calls its |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4798 | * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult} |
| 4799 | * method. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4800 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4801 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 4802 | * if there was no Activity found to run the given Intent. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4803 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4804 | * @param fragment The fragment making the call. |
| 4805 | * @param intent The intent to start. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4806 | * @param requestCode Reply request code. < 0 if reply is not requested. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4807 | * @param options Additional options for how the Activity should be started. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4808 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 4809 | * Context.startActivity(Intent, Bundle)} for more details. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4810 | * |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4811 | * @throws android.content.ActivityNotFoundException |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4812 | * |
| 4813 | * @see Fragment#startActivity |
| 4814 | * @see Fragment#startActivityForResult |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4815 | */ |
Tor Norbye | 788fc2b | 2015-07-05 16:10:42 -0700 | [diff] [blame] | 4816 | public void startActivityFromFragment(@NonNull Fragment fragment, |
| 4817 | @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) { |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4818 | startActivityForResult(fragment.mWho, intent, requestCode, options); |
| 4819 | } |
| 4820 | |
| 4821 | /** |
| 4822 | * @hide |
| 4823 | */ |
| 4824 | @Override |
| 4825 | public void startActivityForResult( |
| 4826 | String who, Intent intent, int requestCode, @Nullable Bundle options) { |
Dianne Hackborn | a3acdb3 | 2015-06-08 17:07:40 -0700 | [diff] [blame] | 4827 | Uri referrer = onProvideReferrer(); |
| 4828 | if (referrer != null) { |
| 4829 | intent.putExtra(Intent.EXTRA_REFERRER, referrer); |
| 4830 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4831 | Instrumentation.ActivityResult ar = |
| 4832 | mInstrumentation.execStartActivity( |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4833 | this, mMainThread.getApplicationThread(), mToken, who, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4834 | intent, requestCode, options); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4835 | if (ar != null) { |
| 4836 | mMainThread.sendActivityResult( |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4837 | mToken, who, requestCode, |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4838 | ar.getResultCode(), ar.getResultData()); |
| 4839 | } |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 4840 | cancelInputsAndStartExitTransition(options); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | /** |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 4844 | * @hide |
| 4845 | */ |
| 4846 | @Override |
| 4847 | public boolean canStartActivityForResult() { |
| 4848 | return true; |
| 4849 | } |
| 4850 | |
| 4851 | /** |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4852 | * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender, |
| 4853 | * int, Intent, int, int, int, Bundle)} with no options. |
| 4854 | */ |
| 4855 | public void startIntentSenderFromChild(Activity child, IntentSender intent, |
| 4856 | int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, |
| 4857 | int extraFlags) |
| 4858 | throws IntentSender.SendIntentException { |
| 4859 | startIntentSenderFromChild(child, intent, requestCode, fillInIntent, |
| 4860 | flagsMask, flagsValues, extraFlags, null); |
| 4861 | } |
| 4862 | |
| 4863 | /** |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4864 | * Like {@link #startActivityFromChild(Activity, Intent, int)}, but |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4865 | * taking a IntentSender; see |
Dianne Hackborn | ae22c05 | 2009-09-17 18:46:22 -0700 | [diff] [blame] | 4866 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)} |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4867 | * for more information. |
| 4868 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4869 | public void startIntentSenderFromChild(Activity child, IntentSender intent, |
| 4870 | int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 4871 | int extraFlags, @Nullable Bundle options) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 4872 | throws IntentSender.SendIntentException { |
| 4873 | startIntentSenderForResultInner(intent, requestCode, fillInIntent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4874 | flagsMask, flagsValues, child, options); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | /** |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4878 | * Call immediately after one of the flavors of {@link #startActivity(Intent)} |
| 4879 | * or {@link #finish} to specify an explicit transition animation to |
| 4880 | * perform next. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4881 | * |
| 4882 | * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative |
| 4883 | * to using this with starting activities is to supply the desired animation |
| 4884 | * information through a {@link ActivityOptions} bundle to |
| 4885 | * {@link #startActivity(Intent, Bundle) or a related function. This allows |
| 4886 | * you to specify a custom animation even when starting an activity from |
| 4887 | * outside the context of the current top activity. |
| 4888 | * |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4889 | * @param enterAnim A resource ID of the animation resource to use for |
Dianne Hackborn | 8b571a8 | 2009-09-25 16:09:43 -0700 | [diff] [blame] | 4890 | * the incoming activity. Use 0 for no animation. |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4891 | * @param exitAnim A resource ID of the animation resource to use for |
Dianne Hackborn | 8b571a8 | 2009-09-25 16:09:43 -0700 | [diff] [blame] | 4892 | * the outgoing activity. Use 0 for no animation. |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4893 | */ |
| 4894 | public void overridePendingTransition(int enterAnim, int exitAnim) { |
| 4895 | try { |
| 4896 | ActivityManagerNative.getDefault().overridePendingTransition( |
| 4897 | mToken, getPackageName(), enterAnim, exitAnim); |
| 4898 | } catch (RemoteException e) { |
| 4899 | } |
| 4900 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4901 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 4902 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4903 | * Call this to set the result that your activity will return to its |
| 4904 | * caller. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4905 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4906 | * @param resultCode The result code to propagate back to the originating |
| 4907 | * activity, often RESULT_CANCELED or RESULT_OK |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4908 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4909 | * @see #RESULT_CANCELED |
| 4910 | * @see #RESULT_OK |
| 4911 | * @see #RESULT_FIRST_USER |
| 4912 | * @see #setResult(int, Intent) |
| 4913 | */ |
| 4914 | public final void setResult(int resultCode) { |
| 4915 | synchronized (this) { |
| 4916 | mResultCode = resultCode; |
| 4917 | mResultData = null; |
| 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | /** |
| 4922 | * Call this to set the result that your activity will return to its |
| 4923 | * caller. |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4924 | * |
| 4925 | * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent |
| 4926 | * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION |
| 4927 | * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION |
| 4928 | * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the |
| 4929 | * Activity receiving the result access to the specific URIs in the Intent. |
| 4930 | * Access will remain until the Activity has finished (it will remain across the hosting |
| 4931 | * process being killed and other temporary destruction) and will be added |
| 4932 | * to any existing set of URI permissions it already holds. |
| 4933 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4934 | * @param resultCode The result code to propagate back to the originating |
| 4935 | * activity, often RESULT_CANCELED or RESULT_OK |
| 4936 | * @param data The data to propagate back to the originating activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4937 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4938 | * @see #RESULT_CANCELED |
| 4939 | * @see #RESULT_OK |
| 4940 | * @see #RESULT_FIRST_USER |
| 4941 | * @see #setResult(int) |
| 4942 | */ |
| 4943 | public final void setResult(int resultCode, Intent data) { |
| 4944 | synchronized (this) { |
| 4945 | mResultCode = resultCode; |
| 4946 | mResultData = data; |
| 4947 | } |
| 4948 | } |
| 4949 | |
| 4950 | /** |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 4951 | * Return information about who launched this activity. If the launching Intent |
| 4952 | * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER}, |
| 4953 | * that will be returned as-is; otherwise, if known, an |
| 4954 | * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the |
| 4955 | * package name that started the Intent will be returned. This may return null if no |
| 4956 | * referrer can be identified -- it is neither explicitly specified, nor is it known which |
| 4957 | * application package was involved. |
| 4958 | * |
| 4959 | * <p>If called while inside the handling of {@link #onNewIntent}, this function will |
| 4960 | * return the referrer that submitted that new intent to the activity. Otherwise, it |
| 4961 | * always returns the referrer of the original Intent.</p> |
| 4962 | * |
| 4963 | * <p>Note that this is <em>not</em> a security feature -- you can not trust the |
| 4964 | * referrer information, applications can spoof it.</p> |
| 4965 | */ |
| 4966 | @Nullable |
| 4967 | public Uri getReferrer() { |
| 4968 | Intent intent = getIntent(); |
| 4969 | Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER); |
| 4970 | if (referrer != null) { |
| 4971 | return referrer; |
| 4972 | } |
| 4973 | String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME); |
| 4974 | if (referrerName != null) { |
| 4975 | return Uri.parse(referrerName); |
| 4976 | } |
| 4977 | if (mReferrer != null) { |
| 4978 | return new Uri.Builder().scheme("android-app").authority(mReferrer).build(); |
| 4979 | } |
| 4980 | return null; |
| 4981 | } |
| 4982 | |
| 4983 | /** |
Dianne Hackborn | a3acdb3 | 2015-06-08 17:07:40 -0700 | [diff] [blame] | 4984 | * Override to generate the desired referrer for the content currently being shown |
| 4985 | * by the app. The default implementation returns null, meaning the referrer will simply |
| 4986 | * be the android-app: of the package name of this activity. Return a non-null Uri to |
| 4987 | * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it. |
| 4988 | */ |
| 4989 | public Uri onProvideReferrer() { |
| 4990 | return null; |
| 4991 | } |
| 4992 | |
| 4993 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4994 | * Return the name of the package that invoked this activity. This is who |
| 4995 | * the data in {@link #setResult setResult()} will be sent to. You can |
| 4996 | * use this information to validate that the recipient is allowed to |
| 4997 | * receive the data. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 4998 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 4999 | * <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] | 5000 | * did not use the {@link #startActivityForResult} |
| 5001 | * form that includes a request code), then the calling package will be |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5002 | * null.</p> |
| 5003 | * |
| 5004 | * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, |
| 5005 | * the result from this method was unstable. If the process hosting the calling |
| 5006 | * package was no longer running, it would return null instead of the proper package |
| 5007 | * name. You can use {@link #getCallingActivity()} and retrieve the package name |
| 5008 | * from that instead.</p> |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5009 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5010 | * @return The package of the activity that will receive your |
| 5011 | * reply, or null if none. |
| 5012 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5013 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5014 | public String getCallingPackage() { |
| 5015 | try { |
| 5016 | return ActivityManagerNative.getDefault().getCallingPackage(mToken); |
| 5017 | } catch (RemoteException e) { |
| 5018 | return null; |
| 5019 | } |
| 5020 | } |
| 5021 | |
| 5022 | /** |
| 5023 | * Return the name of the activity that invoked this activity. This is |
| 5024 | * who the data in {@link #setResult setResult()} will be sent to. You |
| 5025 | * can use this information to validate that the recipient is allowed to |
| 5026 | * receive the data. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5027 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5028 | * <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] | 5029 | * did not use the {@link #startActivityForResult} |
| 5030 | * form that includes a request code), then the calling package will be |
| 5031 | * null. |
| 5032 | * |
Dianne Hackborn | 7b92467 | 2013-04-11 18:08:42 -0700 | [diff] [blame] | 5033 | * @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] | 5034 | * reply, or null if none. |
| 5035 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5036 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5037 | public ComponentName getCallingActivity() { |
| 5038 | try { |
| 5039 | return ActivityManagerNative.getDefault().getCallingActivity(mToken); |
| 5040 | } catch (RemoteException e) { |
| 5041 | return null; |
| 5042 | } |
| 5043 | } |
| 5044 | |
| 5045 | /** |
| 5046 | * Control whether this activity's main window is visible. This is intended |
| 5047 | * only for the special case of an activity that is not going to show a |
| 5048 | * UI itself, but can't just finish prior to onResume() because it needs |
| 5049 | * to wait for a service binding or such. Setting this to false allows |
| 5050 | * you to prevent your UI from being shown during that time. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5051 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5052 | * <p>The default value for this is taken from the |
| 5053 | * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme. |
| 5054 | */ |
| 5055 | public void setVisible(boolean visible) { |
| 5056 | if (mVisibleFromClient != visible) { |
| 5057 | mVisibleFromClient = visible; |
| 5058 | if (mVisibleFromServer) { |
| 5059 | if (visible) makeVisible(); |
| 5060 | else mDecor.setVisibility(View.INVISIBLE); |
| 5061 | } |
| 5062 | } |
| 5063 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5064 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5065 | void makeVisible() { |
| 5066 | if (!mWindowAdded) { |
| 5067 | ViewManager wm = getWindowManager(); |
| 5068 | wm.addView(mDecor, getWindow().getAttributes()); |
| 5069 | mWindowAdded = true; |
| 5070 | } |
| 5071 | mDecor.setVisibility(View.VISIBLE); |
| 5072 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5073 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5074 | /** |
| 5075 | * Check to see whether this activity is in the process of finishing, |
| 5076 | * either because you called {@link #finish} on it or someone else |
| 5077 | * has requested that it finished. This is often used in |
| 5078 | * {@link #onPause} to determine whether the activity is simply pausing or |
| 5079 | * completely finishing. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5080 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5081 | * @return If the activity is finishing, returns true; else returns false. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5082 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5083 | * @see #finish |
| 5084 | */ |
| 5085 | public boolean isFinishing() { |
| 5086 | return mFinished; |
| 5087 | } |
| 5088 | |
| 5089 | /** |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 5090 | * Returns true if the final {@link #onDestroy()} call has been made |
| 5091 | * on the Activity, so this instance is now dead. |
| 5092 | */ |
| 5093 | public boolean isDestroyed() { |
| 5094 | return mDestroyed; |
| 5095 | } |
| 5096 | |
| 5097 | /** |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 5098 | * Check to see whether this activity is in the process of being destroyed in order to be |
| 5099 | * recreated with a new configuration. This is often used in |
| 5100 | * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed |
| 5101 | * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5102 | * |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 5103 | * @return If the activity is being torn down in order to be recreated with a new configuration, |
| 5104 | * returns true; else returns false. |
| 5105 | */ |
| 5106 | public boolean isChangingConfigurations() { |
| 5107 | return mChangingConfigurations; |
| 5108 | } |
| 5109 | |
| 5110 | /** |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 5111 | * Cause this Activity to be recreated with a new instance. This results |
| 5112 | * in essentially the same flow as when the Activity is created due to |
| 5113 | * a configuration change -- the current instance will go through its |
| 5114 | * lifecycle to {@link #onDestroy} and a new instance then created after it. |
| 5115 | */ |
| 5116 | public void recreate() { |
| 5117 | if (mParent != null) { |
| 5118 | throw new IllegalStateException("Can only be called on top-level activity"); |
| 5119 | } |
| 5120 | if (Looper.myLooper() != mMainThread.getLooper()) { |
| 5121 | throw new IllegalStateException("Must be called from main thread"); |
| 5122 | } |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 5123 | mMainThread.requestRelaunchActivity(mToken, null, null, 0, false, null, null, false, |
| 5124 | false /* preserveWindow */); |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 5125 | } |
| 5126 | |
| 5127 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5128 | * Finishes the current activity and specifies whether to remove the task associated with this |
| 5129 | * activity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5130 | */ |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5131 | private void finish(int finishTask) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5132 | if (mParent == null) { |
| 5133 | int resultCode; |
| 5134 | Intent resultData; |
| 5135 | synchronized (this) { |
| 5136 | resultCode = mResultCode; |
| 5137 | resultData = mResultData; |
| 5138 | } |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 5139 | if (false) Log.v(TAG, "Finishing self: token=" + mToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5140 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 5141 | if (resultData != null) { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 5142 | resultData.prepareToLeaveProcess(this); |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 5143 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5144 | if (ActivityManagerNative.getDefault() |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5145 | .finishActivity(mToken, resultCode, resultData, finishTask)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5146 | mFinished = true; |
| 5147 | } |
| 5148 | } catch (RemoteException e) { |
| 5149 | // Empty |
| 5150 | } |
| 5151 | } else { |
| 5152 | mParent.finishFromChild(this); |
| 5153 | } |
| 5154 | } |
| 5155 | |
| 5156 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5157 | * Call this when your activity is done and should be closed. The |
| 5158 | * ActivityResult is propagated back to whoever launched you via |
| 5159 | * onActivityResult(). |
| 5160 | */ |
| 5161 | public void finish() { |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5162 | finish(DONT_FINISH_TASK_WITH_ACTIVITY); |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5163 | } |
| 5164 | |
| 5165 | /** |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 5166 | * Finish this activity as well as all activities immediately below it |
| 5167 | * in the current task that have the same affinity. This is typically |
| 5168 | * used when an application can be launched on to another task (such as |
| 5169 | * from an ACTION_VIEW of a content type it understands) and the user |
| 5170 | * has used the up navigation to switch out of the current task and in |
| 5171 | * to its own task. In this case, if the user has navigated down into |
| 5172 | * any other activities of the second application, all of those should |
| 5173 | * be removed from the original task as part of the task switch. |
| 5174 | * |
| 5175 | * <p>Note that this finish does <em>not</em> allow you to deliver results |
| 5176 | * to the previous activity, and an exception will be thrown if you are trying |
| 5177 | * to do so.</p> |
| 5178 | */ |
| 5179 | public void finishAffinity() { |
| 5180 | if (mParent != null) { |
| 5181 | throw new IllegalStateException("Can not be called from an embedded activity"); |
| 5182 | } |
| 5183 | if (mResultCode != RESULT_CANCELED || mResultData != null) { |
| 5184 | throw new IllegalStateException("Can not be called to deliver a result"); |
| 5185 | } |
| 5186 | try { |
| 5187 | if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) { |
| 5188 | mFinished = true; |
| 5189 | } |
| 5190 | } catch (RemoteException e) { |
| 5191 | // Empty |
| 5192 | } |
| 5193 | } |
| 5194 | |
| 5195 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5196 | * 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] | 5197 | * {@link #finish} method. The default implementation simply calls |
| 5198 | * finish() on this activity (the parent), finishing the entire group. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5199 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5200 | * @param child The activity making the call. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5201 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5202 | * @see #finish |
| 5203 | */ |
| 5204 | public void finishFromChild(Activity child) { |
| 5205 | finish(); |
| 5206 | } |
| 5207 | |
| 5208 | /** |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 5209 | * Reverses the Activity Scene entry Transition and triggers the calling Activity |
| 5210 | * to reverse its exit Transition. When the exit Transition completes, |
| 5211 | * {@link #finish()} is called. If no entry Transition was used, finish() is called |
| 5212 | * immediately and the Activity exit Transition is run. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5213 | * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[]) |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 5214 | */ |
Craig Mautner | 73f843d | 2014-05-19 09:42:28 -0700 | [diff] [blame] | 5215 | public void finishAfterTransition() { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5216 | if (!mActivityTransitionState.startExitBackTransition(this)) { |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 5217 | finish(); |
| 5218 | } |
George Mount | cb4b7d9 | 2014-02-25 10:47:55 -0800 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5222 | * Force finish another activity that you had previously started with |
| 5223 | * {@link #startActivityForResult}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5224 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5225 | * @param requestCode The request code of the activity that you had |
| 5226 | * given to startActivityForResult(). If there are multiple |
| 5227 | * activities started with this request code, they |
| 5228 | * will all be finished. |
| 5229 | */ |
| 5230 | public void finishActivity(int requestCode) { |
| 5231 | if (mParent == null) { |
| 5232 | try { |
| 5233 | ActivityManagerNative.getDefault() |
| 5234 | .finishSubActivity(mToken, mEmbeddedID, requestCode); |
| 5235 | } catch (RemoteException e) { |
| 5236 | // Empty |
| 5237 | } |
| 5238 | } else { |
| 5239 | mParent.finishActivityFromChild(this, requestCode); |
| 5240 | } |
| 5241 | } |
| 5242 | |
| 5243 | /** |
| 5244 | * This is called when a child activity of this one calls its |
| 5245 | * finishActivity(). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5246 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5247 | * @param child The activity making the call. |
| 5248 | * @param requestCode Request code that had been used to start the |
| 5249 | * activity. |
| 5250 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5251 | public void finishActivityFromChild(@NonNull Activity child, int requestCode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5252 | try { |
| 5253 | ActivityManagerNative.getDefault() |
| 5254 | .finishSubActivity(mToken, child.mEmbeddedID, requestCode); |
| 5255 | } catch (RemoteException e) { |
| 5256 | // Empty |
| 5257 | } |
| 5258 | } |
| 5259 | |
| 5260 | /** |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5261 | * 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] | 5262 | * removed as a part of finishing the root activity of the task. |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5263 | */ |
| 5264 | public void finishAndRemoveTask() { |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 5265 | finish(FINISH_TASK_WITH_ROOT_ACTIVITY); |
Winson Chung | 3b3f464 | 2014-04-22 10:08:18 -0700 | [diff] [blame] | 5266 | } |
| 5267 | |
| 5268 | /** |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 5269 | * Ask that the local app instance of this activity be released to free up its memory. |
| 5270 | * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity -- |
| 5271 | * a new instance of the activity will later be re-created if needed due to the user |
| 5272 | * navigating back to it. |
| 5273 | * |
| 5274 | * @return Returns true if the activity was in a state that it has started the process |
| 5275 | * of destroying its current instance; returns false if for any reason this could not |
| 5276 | * be done: it is currently visible to the user, it is already being destroyed, it is |
| 5277 | * being finished, it hasn't yet saved its state, etc. |
| 5278 | */ |
| 5279 | public boolean releaseInstance() { |
| 5280 | try { |
| 5281 | return ActivityManagerNative.getDefault().releaseActivityInstance(mToken); |
| 5282 | } catch (RemoteException e) { |
| 5283 | // Empty |
| 5284 | } |
| 5285 | return false; |
| 5286 | } |
| 5287 | |
| 5288 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5289 | * Called when an activity you launched exits, giving you the requestCode |
| 5290 | * you started it with, the resultCode it returned, and any additional |
| 5291 | * data from it. The <var>resultCode</var> will be |
| 5292 | * {@link #RESULT_CANCELED} if the activity explicitly returned that, |
| 5293 | * didn't return any result, or crashed during its operation. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5294 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5295 | * <p>You will receive this call immediately before onResume() when your |
| 5296 | * activity is re-starting. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5297 | * |
Ricardo Cervera | 92f6a74 | 2014-04-04 11:17:06 -0700 | [diff] [blame] | 5298 | * <p>This method is never invoked if your activity sets |
| 5299 | * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to |
| 5300 | * <code>true</code>. |
Ricardo Cervera | 93f94c2 | 2015-01-16 09:54:30 -0800 | [diff] [blame] | 5301 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5302 | * @param requestCode The integer request code originally supplied to |
| 5303 | * startActivityForResult(), allowing you to identify who this |
| 5304 | * result came from. |
| 5305 | * @param resultCode The integer result code returned by the child activity |
| 5306 | * through its setResult(). |
| 5307 | * @param data An Intent, which can return result data to the caller |
| 5308 | * (various data can be attached to Intent "extras"). |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5309 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5310 | * @see #startActivityForResult |
| 5311 | * @see #createPendingResult |
| 5312 | * @see #setResult(int) |
| 5313 | */ |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 5314 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | /** |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5318 | * Called when an activity you launched with an activity transition exposes this |
| 5319 | * Activity through a returning activity transition, giving you the resultCode |
| 5320 | * and any additional data from it. This method will only be called if the activity |
| 5321 | * 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] | 5322 | * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5323 | * |
| 5324 | * <p>The purpose of this function is to let the called Activity send a hint about |
| 5325 | * its state so that this underlying Activity can prepare to be exposed. A call to |
| 5326 | * this method does not guarantee that the called Activity has or will be exiting soon. |
| 5327 | * It only indicates that it will expose this Activity's Window and it has |
| 5328 | * some data to pass to prepare it.</p> |
| 5329 | * |
| 5330 | * @param resultCode The integer result code returned by the child activity |
| 5331 | * through its setResult(). |
| 5332 | * @param data An Intent, which can return result data to the caller |
| 5333 | * (various data can be attached to Intent "extras"). |
| 5334 | */ |
Craig Mautner | 7a629c2 | 2014-09-04 14:57:04 -0700 | [diff] [blame] | 5335 | public void onActivityReenter(int resultCode, Intent data) { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | /** |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5339 | * Create a new PendingIntent object which you can hand to others |
| 5340 | * for them to use to send result data back to your |
| 5341 | * {@link #onActivityResult} callback. The created object will be either |
| 5342 | * one-shot (becoming invalid after a result is sent back) or multiple |
| 5343 | * (allowing any number of results to be sent through it). |
| 5344 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5345 | * @param requestCode Private request code for the sender that will be |
| 5346 | * associated with the result data when it is returned. The sender can not |
| 5347 | * modify this value, allowing you to identify incoming results. |
| 5348 | * @param data Default data to supply in the result, which may be modified |
| 5349 | * by the sender. |
| 5350 | * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT}, |
| 5351 | * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE}, |
| 5352 | * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT}, |
| 5353 | * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT}, |
| 5354 | * or any of the flags as supported by |
| 5355 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 5356 | * of the intent that can be supplied when the actual send happens. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5357 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5358 | * @return Returns an existing or new PendingIntent matching the given |
| 5359 | * parameters. May return null only if |
| 5360 | * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been |
| 5361 | * supplied. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5362 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5363 | * @see PendingIntent |
| 5364 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5365 | public PendingIntent createPendingResult(int requestCode, @NonNull Intent data, |
| 5366 | @PendingIntent.Flags int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5367 | String packageName = getPackageName(); |
| 5368 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 5369 | data.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5370 | IIntentSender target = |
| 5371 | ActivityManagerNative.getDefault().getIntentSender( |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 5372 | ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5373 | mParent == null ? mToken : mParent.mToken, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 5374 | mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null, |
| 5375 | UserHandle.myUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5376 | return target != null ? new PendingIntent(target) : null; |
| 5377 | } catch (RemoteException e) { |
| 5378 | // Empty |
| 5379 | } |
| 5380 | return null; |
| 5381 | } |
| 5382 | |
| 5383 | /** |
| 5384 | * Change the desired orientation of this activity. If the activity |
| 5385 | * is currently in the foreground or otherwise impacting the screen |
| 5386 | * orientation, the screen will immediately be changed (possibly causing |
| 5387 | * the activity to be restarted). Otherwise, this will be used the next |
| 5388 | * time the activity is visible. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5389 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5390 | * @param requestedOrientation An orientation constant as used in |
| 5391 | * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}. |
| 5392 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5393 | public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5394 | if (mParent == null) { |
| 5395 | try { |
| 5396 | ActivityManagerNative.getDefault().setRequestedOrientation( |
| 5397 | mToken, requestedOrientation); |
| 5398 | } catch (RemoteException e) { |
| 5399 | // Empty |
| 5400 | } |
| 5401 | } else { |
| 5402 | mParent.setRequestedOrientation(requestedOrientation); |
| 5403 | } |
| 5404 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5405 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5406 | /** |
| 5407 | * Return the current requested orientation of the activity. This will |
| 5408 | * either be the orientation requested in its component's manifest, or |
| 5409 | * the last requested orientation given to |
| 5410 | * {@link #setRequestedOrientation(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5411 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5412 | * @return Returns an orientation constant as used in |
| 5413 | * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}. |
| 5414 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5415 | @ActivityInfo.ScreenOrientation |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5416 | public int getRequestedOrientation() { |
| 5417 | if (mParent == null) { |
| 5418 | try { |
| 5419 | return ActivityManagerNative.getDefault() |
| 5420 | .getRequestedOrientation(mToken); |
| 5421 | } catch (RemoteException e) { |
| 5422 | // Empty |
| 5423 | } |
| 5424 | } else { |
| 5425 | return mParent.getRequestedOrientation(); |
| 5426 | } |
| 5427 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 5428 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5429 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5430 | /** |
| 5431 | * Return the identifier of the task this activity is in. This identifier |
| 5432 | * will remain the same for the lifetime of the activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5433 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5434 | * @return Task identifier, an opaque integer. |
| 5435 | */ |
| 5436 | public int getTaskId() { |
| 5437 | try { |
| 5438 | return ActivityManagerNative.getDefault() |
| 5439 | .getTaskForActivity(mToken, false); |
| 5440 | } catch (RemoteException e) { |
| 5441 | return -1; |
| 5442 | } |
| 5443 | } |
| 5444 | |
| 5445 | /** |
| 5446 | * Return whether this activity is the root of a task. The root is the |
| 5447 | * first activity in a task. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5448 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5449 | * @return True if this is the root activity, else false. |
| 5450 | */ |
| 5451 | public boolean isTaskRoot() { |
| 5452 | try { |
Filip Gruszczynski | 3d02671 | 2015-12-16 13:46:38 -0800 | [diff] [blame] | 5453 | return ActivityManagerNative.getDefault().getTaskForActivity(mToken, true) >= 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5454 | } catch (RemoteException e) { |
| 5455 | return false; |
| 5456 | } |
| 5457 | } |
| 5458 | |
| 5459 | /** |
| 5460 | * Move the task containing this activity to the back of the activity |
| 5461 | * stack. The activity's order within the task is unchanged. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5462 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5463 | * @param nonRoot If false then this only works if the activity is the root |
| 5464 | * of a task; if true it will work for any activity in |
| 5465 | * a task. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5466 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5467 | * @return If the task was moved (or it was already at the |
| 5468 | * back) true is returned, else false. |
| 5469 | */ |
| 5470 | public boolean moveTaskToBack(boolean nonRoot) { |
| 5471 | try { |
| 5472 | return ActivityManagerNative.getDefault().moveActivityTaskToBack( |
| 5473 | mToken, nonRoot); |
| 5474 | } catch (RemoteException e) { |
| 5475 | // Empty |
| 5476 | } |
| 5477 | return false; |
| 5478 | } |
| 5479 | |
| 5480 | /** |
| 5481 | * Returns class name for this activity with the package prefix removed. |
| 5482 | * This is the default name used to read and write settings. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5483 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5484 | * @return The local class name. |
| 5485 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5486 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5487 | public String getLocalClassName() { |
| 5488 | final String pkg = getPackageName(); |
| 5489 | final String cls = mComponent.getClassName(); |
| 5490 | int packageLen = pkg.length(); |
| 5491 | if (!cls.startsWith(pkg) || cls.length() <= packageLen |
| 5492 | || cls.charAt(packageLen) != '.') { |
| 5493 | return cls; |
| 5494 | } |
| 5495 | return cls.substring(packageLen+1); |
| 5496 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5498 | /** |
| 5499 | * Returns complete component name of this activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5500 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5501 | * @return Returns the complete component name for this activity |
| 5502 | */ |
| 5503 | public ComponentName getComponentName() |
| 5504 | { |
| 5505 | return mComponent; |
| 5506 | } |
| 5507 | |
| 5508 | /** |
| 5509 | * Retrieve a {@link SharedPreferences} object for accessing preferences |
| 5510 | * that are private to this activity. This simply calls the underlying |
| 5511 | * {@link #getSharedPreferences(String, int)} method by passing in this activity's |
| 5512 | * class name as the preferences name. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5513 | * |
| 5514 | * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 5515 | * operation. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5516 | * |
| 5517 | * @return Returns the single SharedPreferences instance that can be used |
| 5518 | * to retrieve and modify the preference values. |
| 5519 | */ |
| 5520 | public SharedPreferences getPreferences(int mode) { |
| 5521 | return getSharedPreferences(getLocalClassName(), mode); |
| 5522 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5523 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5524 | private void ensureSearchManager() { |
| 5525 | if (mSearchManager != null) { |
| 5526 | return; |
| 5527 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5528 | |
Amith Yamasani | e9ce3f0 | 2010-01-25 09:15:50 -0800 | [diff] [blame] | 5529 | mSearchManager = new SearchManager(this, null); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5530 | } |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5531 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5532 | @Override |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5533 | public Object getSystemService(@ServiceName @NonNull String name) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5534 | if (getBaseContext() == null) { |
| 5535 | throw new IllegalStateException( |
| 5536 | "System services not available to Activities before onCreate()"); |
| 5537 | } |
| 5538 | |
| 5539 | if (WINDOW_SERVICE.equals(name)) { |
| 5540 | return mWindowManager; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 5541 | } else if (SEARCH_SERVICE.equals(name)) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5542 | ensureSearchManager(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 5543 | return mSearchManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5544 | } |
| 5545 | return super.getSystemService(name); |
| 5546 | } |
| 5547 | |
| 5548 | /** |
| 5549 | * Change the title associated with this activity. If this is a |
| 5550 | * top-level activity, the title for its window will change. If it |
| 5551 | * is an embedded activity, the parent can do whatever it wants |
| 5552 | * with it. |
| 5553 | */ |
| 5554 | public void setTitle(CharSequence title) { |
| 5555 | mTitle = title; |
| 5556 | onTitleChanged(title, mTitleColor); |
| 5557 | |
| 5558 | if (mParent != null) { |
| 5559 | mParent.onChildTitleChanged(this, title); |
| 5560 | } |
| 5561 | } |
| 5562 | |
| 5563 | /** |
| 5564 | * Change the title associated with this activity. If this is a |
| 5565 | * top-level activity, the title for its window will change. If it |
| 5566 | * is an embedded activity, the parent can do whatever it wants |
| 5567 | * with it. |
| 5568 | */ |
| 5569 | public void setTitle(int titleId) { |
| 5570 | setTitle(getText(titleId)); |
| 5571 | } |
| 5572 | |
Alan Viverette | 2525d9c | 2013-11-15 14:42:19 -0800 | [diff] [blame] | 5573 | /** |
| 5574 | * Change the color of the title associated with this activity. |
| 5575 | * <p> |
| 5576 | * This method is deprecated starting in API Level 11 and replaced by action |
| 5577 | * bar styles. For information on styling the Action Bar, read the <a |
| 5578 | * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer |
| 5579 | * guide. |
| 5580 | * |
| 5581 | * @deprecated Use action bar styles instead. |
| 5582 | */ |
| 5583 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5584 | public void setTitleColor(int textColor) { |
| 5585 | mTitleColor = textColor; |
| 5586 | onTitleChanged(mTitle, textColor); |
| 5587 | } |
| 5588 | |
| 5589 | public final CharSequence getTitle() { |
| 5590 | return mTitle; |
| 5591 | } |
| 5592 | |
| 5593 | public final int getTitleColor() { |
| 5594 | return mTitleColor; |
| 5595 | } |
| 5596 | |
| 5597 | protected void onTitleChanged(CharSequence title, int color) { |
| 5598 | if (mTitleReady) { |
| 5599 | final Window win = getWindow(); |
| 5600 | if (win != null) { |
| 5601 | win.setTitle(title); |
| 5602 | if (color != 0) { |
| 5603 | win.setTitleColor(color); |
| 5604 | } |
| 5605 | } |
Adam Powell | af2d859 | 2014-08-26 18:06:40 -0700 | [diff] [blame] | 5606 | if (mActionBar != null) { |
| 5607 | mActionBar.setWindowTitle(title); |
| 5608 | } |
Adam Powell | a557fdc | 2014-08-21 18:05:53 -0700 | [diff] [blame] | 5609 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | protected void onChildTitleChanged(Activity childActivity, CharSequence title) { |
| 5613 | } |
| 5614 | |
| 5615 | /** |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5616 | * Sets information describing the task with this activity for presentation inside the Recents |
| 5617 | * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task |
| 5618 | * are traversed in order from the topmost activity to the bottommost. The traversal continues |
| 5619 | * for each property until a suitable value is found. For each task the taskDescription will be |
| 5620 | * returned in {@link android.app.ActivityManager.TaskDescription}. |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5621 | * |
| 5622 | * @see ActivityManager#getRecentTasks |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5623 | * @see android.app.ActivityManager.TaskDescription |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5624 | * |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5625 | * @param taskDescription The TaskDescription properties that describe the task with this activity |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5626 | */ |
Winson Chung | a449dc0 | 2014-05-16 11:15:04 -0700 | [diff] [blame] | 5627 | public void setTaskDescription(ActivityManager.TaskDescription taskDescription) { |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 5628 | if (mTaskDescription != taskDescription) { |
| 5629 | mTaskDescription.copyFrom(taskDescription); |
| 5630 | // Scale the icon down to something reasonable if it is provided |
| 5631 | if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) { |
| 5632 | final int size = ActivityManager.getLauncherLargeIconSizeInner(this); |
| 5633 | final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size, |
| 5634 | true); |
| 5635 | mTaskDescription.setIcon(icon); |
| 5636 | } |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5637 | } |
| 5638 | try { |
Winson | b640315 | 2016-02-23 13:32:09 -0800 | [diff] [blame] | 5639 | ActivityManagerNative.getDefault().setTaskDescription(mToken, mTaskDescription); |
Craig Mautner | 2fbd754 | 2014-03-21 09:34:07 -0700 | [diff] [blame] | 5640 | } catch (RemoteException e) { |
| 5641 | } |
| 5642 | } |
| 5643 | |
| 5644 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5645 | * Sets the visibility of the progress bar in the title. |
| 5646 | * <p> |
| 5647 | * In order for the progress bar to be shown, the feature must be requested |
| 5648 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5649 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5650 | * @param visible Whether to show the progress bars in the title. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5651 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5652 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5653 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5654 | public final void setProgressBarVisibility(boolean visible) { |
| 5655 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON : |
| 5656 | Window.PROGRESS_VISIBILITY_OFF); |
| 5657 | } |
| 5658 | |
| 5659 | /** |
| 5660 | * Sets the visibility of the indeterminate progress bar in the title. |
| 5661 | * <p> |
| 5662 | * In order for the progress bar to be shown, the feature must be requested |
| 5663 | * via {@link #requestWindowFeature(int)}. |
| 5664 | * |
| 5665 | * @param visible Whether to show the progress bars in the title. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5666 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5667 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5668 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5669 | public final void setProgressBarIndeterminateVisibility(boolean visible) { |
| 5670 | getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, |
| 5671 | visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF); |
| 5672 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5673 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5674 | /** |
| 5675 | * Sets whether the horizontal progress bar in the title should be indeterminate (the circular |
| 5676 | * is always indeterminate). |
| 5677 | * <p> |
| 5678 | * In order for the progress bar to be shown, the feature must be requested |
| 5679 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5680 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5681 | * @param indeterminate Whether the horizontal progress bar should be indeterminate. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5682 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5683 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5684 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5685 | public final void setProgressBarIndeterminate(boolean indeterminate) { |
| 5686 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 5687 | indeterminate ? Window.PROGRESS_INDETERMINATE_ON |
| 5688 | : Window.PROGRESS_INDETERMINATE_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5689 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5690 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5691 | /** |
| 5692 | * Sets the progress for the progress bars in the title. |
| 5693 | * <p> |
| 5694 | * In order for the progress bar to be shown, the feature must be requested |
| 5695 | * via {@link #requestWindowFeature(int)}. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5696 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5697 | * @param progress The progress for the progress bar. Valid ranges are from |
| 5698 | * 0 to 10000 (both inclusive). If 10000 is given, the progress |
| 5699 | * bar will be completely filled and will fade out. |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5700 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5701 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5702 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5703 | public final void setProgress(int progress) { |
| 5704 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START); |
| 5705 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5706 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5707 | /** |
| 5708 | * Sets the secondary progress for the progress bar in the title. This |
| 5709 | * progress is drawn between the primary progress (set via |
| 5710 | * {@link #setProgress(int)} and the background. It can be ideal for media |
| 5711 | * scenarios such as showing the buffering progress while the default |
| 5712 | * progress shows the play progress. |
| 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 secondaryProgress The secondary progress for the progress bar. Valid ranges are from |
| 5718 | * 0 to 10000 (both inclusive). |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5719 | * @deprecated No longer supported starting in API 21. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5720 | */ |
Alan Viverette | 4aef7c8 | 2015-09-04 14:14:50 -0400 | [diff] [blame] | 5721 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5722 | public final void setSecondaryProgress(int secondaryProgress) { |
| 5723 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, |
| 5724 | secondaryProgress + Window.PROGRESS_SECONDARY_START); |
| 5725 | } |
| 5726 | |
| 5727 | /** |
| 5728 | * Suggests an audio stream whose volume should be changed by the hardware |
| 5729 | * volume controls. |
| 5730 | * <p> |
| 5731 | * The suggested audio stream will be tied to the window of this Activity. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5732 | * Volume requests which are received while the Activity is in the |
| 5733 | * foreground will affect this stream. |
| 5734 | * <p> |
| 5735 | * It is not guaranteed that the hardware volume controls will always change |
| 5736 | * this stream's volume (for example, if a call is in progress, its stream's |
| 5737 | * volume may be changed instead). To reset back to the default, use |
| 5738 | * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}. |
| 5739 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5740 | * @param streamType The type of the audio stream whose volume should be |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5741 | * changed by the hardware volume controls. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5742 | */ |
| 5743 | public final void setVolumeControlStream(int streamType) { |
| 5744 | getWindow().setVolumeControlStream(streamType); |
| 5745 | } |
| 5746 | |
| 5747 | /** |
| 5748 | * Gets the suggested audio stream whose volume should be changed by the |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5749 | * hardware volume controls. |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5750 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5751 | * @return The suggested audio stream type whose volume should be changed by |
| 5752 | * the hardware volume controls. |
| 5753 | * @see #setVolumeControlStream(int) |
| 5754 | */ |
| 5755 | public final int getVolumeControlStream() { |
| 5756 | return getWindow().getVolumeControlStream(); |
| 5757 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5758 | |
| 5759 | /** |
| 5760 | * Sets a {@link MediaController} to send media keys and volume changes to. |
| 5761 | * <p> |
| 5762 | * The controller will be tied to the window of this Activity. Media key and |
| 5763 | * volume events which are received while the Activity is in the foreground |
| 5764 | * will be forwarded to the controller and used to invoke transport controls |
| 5765 | * or adjust the volume. This may be used instead of or in addition to |
| 5766 | * {@link #setVolumeControlStream} to affect a specific session instead of a |
| 5767 | * specific stream. |
| 5768 | * <p> |
| 5769 | * It is not guaranteed that the hardware volume controls will always change |
| 5770 | * this session's volume (for example, if a call is in progress, its |
| 5771 | * stream's volume may be changed instead). To reset back to the default use |
| 5772 | * null as the controller. |
| 5773 | * |
| 5774 | * @param controller The controller for the session which should receive |
| 5775 | * media keys and volume changes. |
| 5776 | */ |
| 5777 | public final void setMediaController(MediaController controller) { |
| 5778 | getWindow().setMediaController(controller); |
| 5779 | } |
| 5780 | |
| 5781 | /** |
| 5782 | * Gets the controller which should be receiving media key and volume events |
| 5783 | * while this activity is in the foreground. |
| 5784 | * |
| 5785 | * @return The controller which should receive events. |
| 5786 | * @see #setMediaController(android.media.session.MediaController) |
| 5787 | */ |
| 5788 | public final MediaController getMediaController() { |
| 5789 | return getWindow().getMediaController(); |
| 5790 | } |
| 5791 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5792 | /** |
| 5793 | * Runs the specified action on the UI thread. If the current thread is the UI |
| 5794 | * thread, then the action is executed immediately. If the current thread is |
| 5795 | * not the UI thread, the action is posted to the event queue of the UI thread. |
| 5796 | * |
| 5797 | * @param action the action to run on the UI thread |
| 5798 | */ |
| 5799 | public final void runOnUiThread(Runnable action) { |
| 5800 | if (Thread.currentThread() != mUiThread) { |
| 5801 | mHandler.post(action); |
| 5802 | } else { |
| 5803 | action.run(); |
| 5804 | } |
| 5805 | } |
| 5806 | |
| 5807 | /** |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5808 | * Standard implementation of |
| 5809 | * {@link android.view.LayoutInflater.Factory#onCreateView} used when |
| 5810 | * inflating with the LayoutInflater returned by {@link #getSystemService}. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5811 | * This implementation does nothing and is for |
| 5812 | * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps |
| 5813 | * should use {@link #onCreateView(View, String, Context, AttributeSet)}. |
| 5814 | * |
| 5815 | * @see android.view.LayoutInflater#createView |
| 5816 | * @see android.view.Window#getLayoutInflater |
| 5817 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 5818 | @Nullable |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5819 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
| 5820 | return null; |
| 5821 | } |
| 5822 | |
| 5823 | /** |
| 5824 | * Standard implementation of |
| 5825 | * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)} |
| 5826 | * used when inflating with the LayoutInflater returned by {@link #getSystemService}. |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5827 | * This implementation handles <fragment> tags to embed fragments inside |
| 5828 | * of the activity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5829 | * |
| 5830 | * @see android.view.LayoutInflater#createView |
| 5831 | * @see android.view.Window#getLayoutInflater |
| 5832 | */ |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5833 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5834 | if (!"fragment".equals(name)) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5835 | return onCreateView(name, context, attrs); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 5836 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 5837 | |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 5838 | return mFragments.onCreateView(parent, name, context, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5839 | } |
| 5840 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5841 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5842 | * 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] | 5843 | * you run "adb shell dumpsys activity <activity_component_name>". |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5844 | * |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5845 | * @param prefix Desired prefix to prepend at each line of output. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5846 | * @param fd The raw file descriptor that the dump is being sent to. |
| 5847 | * @param writer The PrintWriter to which you should dump your state. This will be |
| 5848 | * closed for you after you return. |
| 5849 | * @param args additional arguments to the dump request. |
| 5850 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5851 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 5852 | dumpInner(prefix, fd, writer, args); |
| 5853 | } |
| 5854 | |
| 5855 | void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5856 | writer.print(prefix); writer.print("Local Activity "); |
| 5857 | writer.print(Integer.toHexString(System.identityHashCode(this))); |
| 5858 | writer.println(" State:"); |
| 5859 | String innerPrefix = prefix + " "; |
| 5860 | writer.print(innerPrefix); writer.print("mResumed="); |
| 5861 | writer.print(mResumed); writer.print(" mStopped="); |
| 5862 | writer.print(mStopped); writer.print(" mFinished="); |
| 5863 | writer.println(mFinished); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 5864 | writer.print(innerPrefix); writer.print("mChangingConfigurations="); |
| 5865 | writer.println(mChangingConfigurations); |
| 5866 | writer.print(innerPrefix); writer.print("mCurrentConfig="); |
| 5867 | writer.println(mCurrentConfig); |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5868 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 5869 | mFragments.dumpLoaders(innerPrefix, fd, writer, args); |
| 5870 | mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args); |
Dianne Hackborn | 57dd737 | 2015-07-27 18:11:14 -0700 | [diff] [blame] | 5871 | if (mVoiceInteractor != null) { |
| 5872 | mVoiceInteractor.dump(innerPrefix, fd, writer, args); |
| 5873 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5874 | |
Michael Wright | 5f48dc7 | 2013-11-01 12:42:49 -0700 | [diff] [blame] | 5875 | if (getWindow() != null && |
| 5876 | getWindow().peekDecorView() != null && |
| 5877 | getWindow().peekDecorView().getViewRootImpl() != null) { |
| 5878 | getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args); |
| 5879 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 5880 | |
| 5881 | mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 5882 | } |
| 5883 | |
| 5884 | /** |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5885 | * Bit indicating that this activity is "immersive" and should not be |
| 5886 | * interrupted by notifications if possible. |
| 5887 | * |
| 5888 | * This value is initially set by the manifest property |
| 5889 | * <code>android:immersive</code> but may be changed at runtime by |
| 5890 | * {@link #setImmersive}. |
| 5891 | * |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 5892 | * @see #setImmersive(boolean) |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 5893 | * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 5894 | */ |
| 5895 | public boolean isImmersive() { |
| 5896 | try { |
| 5897 | return ActivityManagerNative.getDefault().isImmersive(mToken); |
| 5898 | } catch (RemoteException e) { |
| 5899 | return false; |
| 5900 | } |
| 5901 | } |
| 5902 | |
| 5903 | /** |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5904 | * Indication of whether this is the highest level activity in this task. Can be used to |
| 5905 | * determine whether an activity launched by this activity was placed in the same task or |
| 5906 | * another task. |
| 5907 | * |
| 5908 | * @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] | 5909 | */ |
George Mount | ff24328 | 2014-07-07 16:12:07 -0700 | [diff] [blame] | 5910 | private boolean isTopOfTask() { |
Craig Mautner | d61dc20 | 2014-07-07 11:09:11 -0700 | [diff] [blame] | 5911 | try { |
| 5912 | return ActivityManagerNative.getDefault().isTopOfTask(mToken); |
| 5913 | } catch (RemoteException e) { |
| 5914 | return false; |
| 5915 | } |
| 5916 | } |
| 5917 | |
| 5918 | /** |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5919 | * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a |
| 5920 | * fullscreen opaque Activity. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5921 | * <p> |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5922 | * 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] | 5923 | * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released. |
| 5924 | * <p> |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5925 | * This call has no effect on non-translucent activities or on activities with the |
| 5926 | * {@link android.R.attr#windowIsFloating} attribute. |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5927 | * |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 5928 | * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener, |
| 5929 | * ActivityOptions) |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5930 | * @see TranslucentConversionListener |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 5931 | * |
| 5932 | * @hide |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5933 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5934 | @SystemApi |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5935 | public void convertFromTranslucent() { |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5936 | try { |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5937 | mTranslucentCallback = null; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 5938 | if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) { |
| 5939 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true); |
| 5940 | } |
Craig Mautner | 4addfc5 | 2013-06-25 08:05:45 -0700 | [diff] [blame] | 5941 | } catch (RemoteException e) { |
| 5942 | // pass |
| 5943 | } |
| 5944 | } |
| 5945 | |
| 5946 | /** |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5947 | * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from |
| 5948 | * opaque to translucent following a call to {@link #convertFromTranslucent()}. |
| 5949 | * <p> |
| 5950 | * Calling this allows the Activity behind this one to be seen again. Once all such Activities |
| 5951 | * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will |
| 5952 | * be called indicating that it is safe to make this activity translucent again. Until |
| 5953 | * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image |
| 5954 | * behind the frontmost Activity will be indeterminate. |
| 5955 | * <p> |
| 5956 | * This call has no effect on non-translucent activities or on activities with the |
| 5957 | * {@link android.R.attr#windowIsFloating} attribute. |
| 5958 | * |
| 5959 | * @param callback the method to call when all visible Activities behind this one have been |
| 5960 | * drawn and it is safe to make this Activity translucent again. |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5961 | * @param options activity options delivered to the activity below this one. The options |
| 5962 | * are retrieved using {@link #getActivityOptions}. |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 5963 | * @return <code>true</code> if Window was opaque and will become translucent or |
| 5964 | * <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] | 5965 | * |
| 5966 | * @see #convertFromTranslucent() |
| 5967 | * @see TranslucentConversionListener |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 5968 | * |
| 5969 | * @hide |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5970 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 5971 | @SystemApi |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 5972 | public boolean convertToTranslucent(TranslucentConversionListener callback, |
Jose Lima | 5517ea7 | 2014-06-10 12:31:43 -0700 | [diff] [blame] | 5973 | ActivityOptions options) { |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5974 | boolean drawComplete; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5975 | try { |
| 5976 | mTranslucentCallback = callback; |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 5977 | mChangeCanvasToTranslucent = |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5978 | ActivityManagerNative.getDefault().convertToTranslucent(mToken, options); |
George Mount | 9e18397 | 2014-09-03 12:35:09 -0700 | [diff] [blame] | 5979 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false); |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5980 | drawComplete = true; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5981 | } catch (RemoteException e) { |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 5982 | // Make callback return as though it timed out. |
| 5983 | mChangeCanvasToTranslucent = false; |
| 5984 | drawComplete = false; |
| 5985 | } |
| 5986 | if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) { |
| 5987 | // Window is already translucent. |
| 5988 | mTranslucentCallback.onTranslucentConversionComplete(drawComplete); |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5989 | } |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 5990 | return mChangeCanvasToTranslucent; |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 5991 | } |
| 5992 | |
| 5993 | /** @hide */ |
| 5994 | void onTranslucentConversionComplete(boolean drawComplete) { |
| 5995 | if (mTranslucentCallback != null) { |
| 5996 | mTranslucentCallback.onTranslucentConversionComplete(drawComplete); |
| 5997 | mTranslucentCallback = null; |
| 5998 | } |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 5999 | if (mChangeCanvasToTranslucent) { |
| 6000 | WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false); |
| 6001 | } |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6002 | } |
| 6003 | |
Craig Mautner | eb8abf7 | 2014-07-02 15:04:09 -0700 | [diff] [blame] | 6004 | /** @hide */ |
| 6005 | public void onNewActivityOptions(ActivityOptions options) { |
| 6006 | mActivityTransitionState.setEnterActivityOptions(this, options); |
| 6007 | if (!mStopped) { |
| 6008 | mActivityTransitionState.enterReady(this); |
| 6009 | } |
| 6010 | } |
| 6011 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6012 | /** |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6013 | * Retrieve the ActivityOptions passed in from the launching activity or passed back |
| 6014 | * from an activity launched by this activity in its call to {@link |
| 6015 | * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)} |
| 6016 | * |
| 6017 | * @return The ActivityOptions passed to {@link #convertToTranslucent}. |
| 6018 | * @hide |
| 6019 | */ |
| 6020 | ActivityOptions getActivityOptions() { |
| 6021 | try { |
| 6022 | return ActivityManagerNative.getDefault().getActivityOptions(mToken); |
| 6023 | } catch (RemoteException e) { |
| 6024 | } |
| 6025 | return null; |
| 6026 | } |
| 6027 | |
| 6028 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6029 | * 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] | 6030 | * this method anytime between the start of {@link #onResume()} and the return from |
| 6031 | * {@link #onPause()}. If this call is successful then the activity will remain visible after |
| 6032 | * {@link #onPause()} is called, and is allowed to continue playing media in the background. |
| 6033 | * |
| 6034 | * <p>The actions of this call are reset each time that this activity is brought to the |
| 6035 | * front. That is, every time {@link #onResume()} is called the activity will be assumed |
| 6036 | * to not have requested visible behind. Therefore, if you want this activity to continue to |
| 6037 | * be visible in the background you must call this method again. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6038 | * |
| 6039 | * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop |
| 6040 | * for dialog and translucent activities. |
| 6041 | * |
Craig Mautner | 64ccb70 | 2014-10-01 09:38:40 -0700 | [diff] [blame] | 6042 | * <p>Under all circumstances, the activity must stop playing and release resources prior to or |
| 6043 | * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false. |
| 6044 | * |
| 6045 | * <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] | 6046 | * the next call to onResume. |
| 6047 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6048 | * @param visible true to notify the system that the activity wishes to be visible behind other |
| 6049 | * translucent activities, false to indicate otherwise. Resources must be |
| 6050 | * released when passing false to this method. |
Craig Mautner | 64ccb70 | 2014-10-01 09:38:40 -0700 | [diff] [blame] | 6051 | * @return the resulting visibiity state. If true the activity will remain visible beyond |
| 6052 | * {@link #onPause()} if the next activity is translucent or not fullscreen. If false |
| 6053 | * then the activity may not count on being visible behind other translucent activities, |
| 6054 | * and must stop any media playback and release resources. |
| 6055 | * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so |
| 6056 | * the return value must be checked. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6057 | * |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6058 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6059 | * @see #onBackgroundVisibleBehindChanged(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6060 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6061 | public boolean requestVisibleBehind(boolean visible) { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6062 | if (!mResumed) { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6063 | // Do not permit paused or stopped activities to do this. |
| 6064 | visible = false; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6065 | } |
| 6066 | try { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6067 | mVisibleBehind = ActivityManagerNative.getDefault() |
| 6068 | .requestVisibleBehind(mToken, visible) && visible; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6069 | } catch (RemoteException e) { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6070 | mVisibleBehind = false; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6071 | } |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6072 | return mVisibleBehind; |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6076 | * Called when a translucent activity over this activity is becoming opaque or another |
| 6077 | * activity is being launched. Activities that override this method must call |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6078 | * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6079 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6080 | * <p>When this method is called the activity has 500 msec to release any resources it may be |
| 6081 | * using while visible in the background. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6082 | * 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] | 6083 | * 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] | 6084 | * process. Otherwise {@link #onStop()} will be called following return. |
| 6085 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6086 | * @see #requestVisibleBehind(boolean) |
| 6087 | * @see #onBackgroundVisibleBehindChanged(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6088 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 6089 | @CallSuper |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6090 | public void onVisibleBehindCanceled() { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6091 | mCalled = true; |
| 6092 | } |
| 6093 | |
| 6094 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6095 | * Translucent activities may call this to determine if there is an activity below them that |
| 6096 | * is currently set to be visible in the background. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6097 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6098 | * @return true if an activity below is set to visible according to the most recent call to |
| 6099 | * {@link #requestVisibleBehind(boolean)}, false otherwise. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6100 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6101 | * @see #requestVisibleBehind(boolean) |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6102 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6103 | * @see #onBackgroundVisibleBehindChanged(boolean) |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6104 | * @hide |
| 6105 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6106 | @SystemApi |
| 6107 | public boolean isBackgroundVisibleBehind() { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6108 | try { |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6109 | return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken); |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6110 | } catch (RemoteException e) { |
| 6111 | } |
| 6112 | return false; |
| 6113 | } |
| 6114 | |
| 6115 | /** |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6116 | * The topmost foreground activity will receive this call when the background visibility state |
| 6117 | * of the activity below it changes. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6118 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6119 | * 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] | 6120 | * due to a background activity finishing itself. |
| 6121 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6122 | * @param visible true if a background activity is visible, false otherwise. |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6123 | * |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6124 | * @see #requestVisibleBehind(boolean) |
Jose Lima | fcf7083 | 2014-08-27 23:09:05 -0700 | [diff] [blame] | 6125 | * @see #onVisibleBehindCanceled() |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6126 | * @hide |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6127 | */ |
Jose Lima | 4b6c669 | 2014-08-12 17:41:12 -0700 | [diff] [blame] | 6128 | @SystemApi |
| 6129 | public void onBackgroundVisibleBehindChanged(boolean visible) { |
Craig Mautner | ee2e45a | 2014-06-27 12:10:03 -0700 | [diff] [blame] | 6130 | } |
| 6131 | |
| 6132 | /** |
Craig Mautner | 8746a47 | 2014-07-24 15:12:54 -0700 | [diff] [blame] | 6133 | * Activities cannot draw during the period that their windows are animating in. In order |
| 6134 | * to know when it is safe to begin drawing they can override this method which will be |
| 6135 | * called when the entering animation has completed. |
| 6136 | */ |
| 6137 | public void onEnterAnimationComplete() { |
| 6138 | } |
| 6139 | |
| 6140 | /** |
Filip Gruszczynski | 6eafa90 | 2014-11-14 14:24:37 -0800 | [diff] [blame] | 6141 | * @hide |
| 6142 | */ |
| 6143 | public void dispatchEnterAnimationComplete() { |
| 6144 | onEnterAnimationComplete(); |
| 6145 | if (getWindow() != null && getWindow().getDecorView() != null) { |
| 6146 | getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete(); |
| 6147 | } |
| 6148 | } |
| 6149 | |
| 6150 | /** |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6151 | * Adjust the current immersive mode setting. |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 6152 | * |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6153 | * Note that changing this value will have no effect on the activity's |
| 6154 | * {@link android.content.pm.ActivityInfo} structure; that is, if |
| 6155 | * <code>android:immersive</code> is set to <code>true</code> |
| 6156 | * in the application's manifest entry for this activity, the {@link |
| 6157 | * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will |
| 6158 | * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 6159 | * FLAG_IMMERSIVE} bit set. |
| 6160 | * |
Christopher Tate | 73c2aee | 2012-03-15 16:27:14 -0700 | [diff] [blame] | 6161 | * @see #isImmersive() |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6162 | * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE |
| 6163 | */ |
| 6164 | public void setImmersive(boolean i) { |
| 6165 | try { |
| 6166 | ActivityManagerNative.getDefault().setImmersive(mToken, i); |
| 6167 | } catch (RemoteException e) { |
| 6168 | // pass |
| 6169 | } |
| 6170 | } |
| 6171 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6172 | /** |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 6173 | * Enable or disable virtual reality (VR) mode. |
| 6174 | * |
| 6175 | * <p>VR mode is a hint to Android system services to switch to modes optimized for |
| 6176 | * high-performance stereoscopic rendering.</p> |
| 6177 | * |
| 6178 | * @param enabled {@code true} to enable this mode. |
| 6179 | */ |
| 6180 | public void setVrMode(boolean enabled) { |
| 6181 | try { |
| 6182 | ActivityManagerNative.getDefault().setVrMode(mToken, enabled); |
| 6183 | } catch (RemoteException e) { |
| 6184 | // pass |
| 6185 | } |
| 6186 | } |
| 6187 | |
| 6188 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6189 | * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}. |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6190 | * |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6191 | * @param callback Callback that will manage lifecycle events for this action mode |
| 6192 | * @return The ActionMode that was started, or null if it was canceled |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6193 | * |
| 6194 | * @see ActionMode |
| 6195 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 6196 | @Nullable |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 6197 | public ActionMode startActionMode(ActionMode.Callback callback) { |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6198 | return mWindow.getDecorView().startActionMode(callback); |
| 6199 | } |
| 6200 | |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6201 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6202 | * Start an action mode of the given type. |
| 6203 | * |
| 6204 | * @param callback Callback that will manage lifecycle events for this action mode |
| 6205 | * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}. |
| 6206 | * @return The ActionMode that was started, or null if it was canceled |
| 6207 | * |
| 6208 | * @see ActionMode |
| 6209 | */ |
| 6210 | @Nullable |
| 6211 | public ActionMode startActionMode(ActionMode.Callback callback, int type) { |
| 6212 | return mWindow.getDecorView().startActionMode(callback, type); |
| 6213 | } |
| 6214 | |
| 6215 | /** |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6216 | * Give the Activity a chance to control the UI for an action mode requested |
| 6217 | * by the system. |
| 6218 | * |
| 6219 | * <p>Note: If you are looking for a notification callback that an action mode |
| 6220 | * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p> |
| 6221 | * |
| 6222 | * @param callback The callback that should control the new action mode |
| 6223 | * @return The new action mode, or <code>null</code> if the activity does not want to |
| 6224 | * provide special handling for this action mode. (It will be handled by the system.) |
| 6225 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 6226 | @Nullable |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6227 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6228 | public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6229 | // Only Primary ActionModes are represented in the ActionBar. |
| 6230 | if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) { |
| 6231 | initWindowDecorActionBar(); |
| 6232 | if (mActionBar != null) { |
| 6233 | return mActionBar.startActionMode(callback); |
| 6234 | } |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 6235 | } |
| 6236 | return null; |
| 6237 | } |
| 6238 | |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6239 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 6240 | * {@inheritDoc} |
| 6241 | */ |
| 6242 | @Nullable |
| 6243 | @Override |
| 6244 | public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) { |
| 6245 | try { |
| 6246 | mActionModeTypeStarting = type; |
| 6247 | return onWindowStartingActionMode(callback); |
| 6248 | } finally { |
| 6249 | mActionModeTypeStarting = ActionMode.TYPE_PRIMARY; |
| 6250 | } |
| 6251 | } |
| 6252 | |
| 6253 | /** |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6254 | * Notifies the Activity that an action mode has been started. |
| 6255 | * Activity subclasses overriding this method should call the superclass implementation. |
| 6256 | * |
| 6257 | * @param mode The new action mode. |
| 6258 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 6259 | @CallSuper |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6260 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6261 | public void onActionModeStarted(ActionMode mode) { |
| 6262 | } |
| 6263 | |
| 6264 | /** |
| 6265 | * Notifies the activity that an action mode has finished. |
| 6266 | * Activity subclasses overriding this method should call the superclass implementation. |
| 6267 | * |
| 6268 | * @param mode The action mode that just finished. |
| 6269 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 6270 | @CallSuper |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6271 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 6272 | public void onActionModeFinished(ActionMode mode) { |
| 6273 | } |
| 6274 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6275 | /** |
| 6276 | * Returns true if the app should recreate the task when navigating 'up' from this activity |
| 6277 | * by using targetIntent. |
| 6278 | * |
| 6279 | * <p>If this method returns false the app can trivially call |
| 6280 | * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform |
| 6281 | * up navigation. If this method returns false, the app should synthesize a new task stack |
| 6282 | * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p> |
| 6283 | * |
| 6284 | * @param targetIntent An intent representing the target destination for up navigation |
| 6285 | * @return true if navigating up should recreate a new task stack, false if the same task |
| 6286 | * should be used for the destination |
| 6287 | */ |
| 6288 | public boolean shouldUpRecreateTask(Intent targetIntent) { |
| 6289 | try { |
| 6290 | PackageManager pm = getPackageManager(); |
| 6291 | ComponentName cn = targetIntent.getComponent(); |
| 6292 | if (cn == null) { |
| 6293 | cn = targetIntent.resolveActivity(pm); |
| 6294 | } |
| 6295 | ActivityInfo info = pm.getActivityInfo(cn, 0); |
| 6296 | if (info.taskAffinity == null) { |
| 6297 | return false; |
| 6298 | } |
Dianne Hackborn | 6f4d61f | 2014-08-21 17:50:42 -0700 | [diff] [blame] | 6299 | return ActivityManagerNative.getDefault() |
| 6300 | .shouldUpRecreateTask(mToken, info.taskAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6301 | } catch (RemoteException e) { |
| 6302 | return false; |
| 6303 | } catch (NameNotFoundException e) { |
| 6304 | return false; |
| 6305 | } |
| 6306 | } |
| 6307 | |
| 6308 | /** |
| 6309 | * Navigate from this activity to the activity specified by upIntent, finishing this activity |
| 6310 | * in the process. If the activity indicated by upIntent already exists in the task's history, |
| 6311 | * 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] | 6312 | * finished. |
| 6313 | * |
| 6314 | * <p>If the indicated activity does not appear in the history stack, this will finish |
| 6315 | * each activity in this task until the root activity of the task is reached, resulting in |
| 6316 | * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy |
| 6317 | * when an activity may be reached by a path not passing through a canonical parent |
| 6318 | * activity.</p> |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6319 | * |
| 6320 | * <p>This method should be used when performing up navigation from within the same task |
| 6321 | * as the destination. If up navigation should cross tasks in some cases, see |
| 6322 | * {@link #shouldUpRecreateTask(Intent)}.</p> |
| 6323 | * |
| 6324 | * @param upIntent An intent representing the target destination for up navigation |
| 6325 | * |
| 6326 | * @return true if up navigation successfully reached the activity indicated by upIntent and |
| 6327 | * upIntent was delivered to it. false if an instance of the indicated activity could |
| 6328 | * not be found and this activity was simply finished normally. |
| 6329 | */ |
| 6330 | public boolean navigateUpTo(Intent upIntent) { |
| 6331 | if (mParent == null) { |
| 6332 | ComponentName destInfo = upIntent.getComponent(); |
| 6333 | if (destInfo == null) { |
| 6334 | destInfo = upIntent.resolveActivity(getPackageManager()); |
| 6335 | if (destInfo == null) { |
| 6336 | return false; |
| 6337 | } |
| 6338 | upIntent = new Intent(upIntent); |
| 6339 | upIntent.setComponent(destInfo); |
| 6340 | } |
| 6341 | int resultCode; |
| 6342 | Intent resultData; |
| 6343 | synchronized (this) { |
| 6344 | resultCode = mResultCode; |
| 6345 | resultData = mResultData; |
| 6346 | } |
| 6347 | if (resultData != null) { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 6348 | resultData.prepareToLeaveProcess(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6349 | } |
| 6350 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 6351 | upIntent.prepareToLeaveProcess(this); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6352 | return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent, |
| 6353 | resultCode, resultData); |
| 6354 | } catch (RemoteException e) { |
| 6355 | return false; |
| 6356 | } |
| 6357 | } else { |
| 6358 | return mParent.navigateUpToFromChild(this, upIntent); |
| 6359 | } |
| 6360 | } |
| 6361 | |
| 6362 | /** |
| 6363 | * This is called when a child activity of this one calls its |
| 6364 | * {@link #navigateUpTo} method. The default implementation simply calls |
| 6365 | * navigateUpTo(upIntent) on this activity (the parent). |
| 6366 | * |
| 6367 | * @param child The activity making the call. |
| 6368 | * @param upIntent An intent representing the target destination for up navigation |
| 6369 | * |
| 6370 | * @return true if up navigation successfully reached the activity indicated by upIntent and |
| 6371 | * upIntent was delivered to it. false if an instance of the indicated activity could |
| 6372 | * not be found and this activity was simply finished normally. |
| 6373 | */ |
| 6374 | public boolean navigateUpToFromChild(Activity child, Intent upIntent) { |
| 6375 | return navigateUpTo(upIntent); |
| 6376 | } |
| 6377 | |
| 6378 | /** |
| 6379 | * Obtain an {@link Intent} that will launch an explicit target activity specified by |
| 6380 | * this activity's logical parent. The logical parent is named in the application's manifest |
| 6381 | * by the {@link android.R.attr#parentActivityName parentActivityName} attribute. |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 6382 | * Activity subclasses may override this method to modify the Intent returned by |
| 6383 | * super.getParentActivityIntent() or to implement a different mechanism of retrieving |
| 6384 | * the parent intent entirely. |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6385 | * |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 6386 | * @return a new Intent targeting the defined parent of this activity or null if |
| 6387 | * there is no valid parent. |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6388 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 6389 | @Nullable |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6390 | public Intent getParentActivityIntent() { |
Adam Powell | 04d5811 | 2012-04-09 10:22:12 -0700 | [diff] [blame] | 6391 | final String parentName = mActivityInfo.parentActivityName; |
| 6392 | if (TextUtils.isEmpty(parentName)) { |
| 6393 | return null; |
| 6394 | } |
Adam Powell | 5a4010c | 2012-09-16 15:14:05 -0700 | [diff] [blame] | 6395 | |
| 6396 | // If the parent itself has no parent, generate a main activity intent. |
| 6397 | final ComponentName target = new ComponentName(this, parentName); |
| 6398 | try { |
| 6399 | final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0); |
| 6400 | final String parentActivity = parentInfo.parentActivityName; |
| 6401 | final Intent parentIntent = parentActivity == null |
| 6402 | ? Intent.makeMainActivity(target) |
| 6403 | : new Intent().setComponent(target); |
| 6404 | return parentIntent; |
| 6405 | } catch (NameNotFoundException e) { |
| 6406 | Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName + |
| 6407 | "' in manifest"); |
| 6408 | return null; |
| 6409 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 6410 | } |
| 6411 | |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6412 | /** |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6413 | * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6414 | * 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] | 6415 | * 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] | 6416 | * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6417 | * |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6418 | * @param callback Used to manipulate shared element transitions on the launched Activity. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6419 | */ |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6420 | public void setEnterSharedElementCallback(SharedElementCallback callback) { |
| 6421 | if (callback == null) { |
| 6422 | callback = SharedElementCallback.NULL_CALLBACK; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6423 | } |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6424 | mEnterTransitionListener = callback; |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | /** |
| 6428 | * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6429 | * 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] | 6430 | * will be called to handle shared elements on the <i>launching</i> Activity. Most |
| 6431 | * calls will only come when returning from the started Activity. |
George Mount | 9826f63 | 2014-09-11 08:50:09 -0700 | [diff] [blame] | 6432 | * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}. |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6433 | * |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6434 | * @param callback Used to manipulate shared element transitions on the launching Activity. |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6435 | */ |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6436 | public void setExitSharedElementCallback(SharedElementCallback callback) { |
| 6437 | if (callback == null) { |
| 6438 | callback = SharedElementCallback.NULL_CALLBACK; |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6439 | } |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 6440 | mExitTransitionListener = callback; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 6441 | } |
| 6442 | |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 6443 | /** |
| 6444 | * Postpone the entering activity transition when Activity was started with |
| 6445 | * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
| 6446 | * android.util.Pair[])}. |
| 6447 | * <p>This method gives the Activity the ability to delay starting the entering and |
| 6448 | * shared element transitions until all data is loaded. Until then, the Activity won't |
| 6449 | * draw into its window, leaving the window transparent. This may also cause the |
| 6450 | * returning animation to be delayed until data is ready. This method should be |
| 6451 | * called in {@link #onCreate(android.os.Bundle)} or in |
| 6452 | * {@link #onActivityReenter(int, android.content.Intent)}. |
| 6453 | * {@link #startPostponedEnterTransition()} must be called to allow the Activity to |
| 6454 | * start the transitions. If the Activity did not use |
| 6455 | * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, |
| 6456 | * android.util.Pair[])}, then this method does nothing.</p> |
| 6457 | */ |
| 6458 | public void postponeEnterTransition() { |
| 6459 | mActivityTransitionState.postponeEnterTransition(); |
| 6460 | } |
| 6461 | |
| 6462 | /** |
| 6463 | * Begin postponed transitions after {@link #postponeEnterTransition()} was called. |
| 6464 | * If postponeEnterTransition() was called, you must call startPostponedEnterTransition() |
| 6465 | * to have your Activity start drawing. |
| 6466 | */ |
| 6467 | public void startPostponedEnterTransition() { |
| 6468 | mActivityTransitionState.startPostponedEnterTransition(); |
| 6469 | } |
| 6470 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 6471 | /** |
| 6472 | * Create {@link DropPermissions} object bound to this activity and controlling the access |
| 6473 | * permissions for content URIs associated with the {@link DragEvent}. |
| 6474 | * @param event Drag event |
| 6475 | * @return The DropPermissions object used to control access to the content URIs. Null if |
| 6476 | * no content URIs are associated with the event or if permissions could not be granted. |
| 6477 | */ |
| 6478 | public DropPermissions requestDropPermissions(DragEvent event) { |
| 6479 | DropPermissions dropPermissions = DropPermissions.obtain(event); |
| 6480 | if (dropPermissions != null && dropPermissions.take(getActivityToken())) { |
| 6481 | return dropPermissions; |
| 6482 | } |
| 6483 | return null; |
| 6484 | } |
| 6485 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6486 | // ------------------ Internal API ------------------ |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6487 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6488 | final void setParent(Activity parent) { |
| 6489 | mParent = parent; |
| 6490 | } |
| 6491 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6492 | final void attach(Context context, ActivityThread aThread, |
| 6493 | Instrumentation instr, IBinder token, int ident, |
| 6494 | Application application, Intent intent, ActivityInfo info, |
| 6495 | CharSequence title, Activity parent, String id, |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6496 | NonConfigurationInstances lastNonConfigurationInstances, |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 6497 | Configuration config, String referrer, IVoiceInteractor voiceInteractor, |
| 6498 | Window window) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6499 | attachBaseContext(context); |
| 6500 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6501 | mFragments.attachHost(null /*parent*/); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6502 | |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 6503 | mWindow = new PhoneWindow(this, window); |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 6504 | mWindow.setWindowControllerCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6505 | mWindow.setCallback(this); |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 6506 | mWindow.setOnWindowDismissedCallback(this); |
Dianne Hackborn | 420829e | 2011-01-28 11:30:35 -0800 | [diff] [blame] | 6507 | mWindow.getLayoutInflater().setPrivateFactory(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6508 | if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) { |
| 6509 | mWindow.setSoftInputMode(info.softInputMode); |
| 6510 | } |
Adam Powell | 269248d | 2011-08-02 10:26:54 -0700 | [diff] [blame] | 6511 | if (info.uiOptions != 0) { |
| 6512 | mWindow.setUiOptions(info.uiOptions); |
| 6513 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6514 | mUiThread = Thread.currentThread(); |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6515 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6516 | mMainThread = aThread; |
| 6517 | mInstrumentation = instr; |
| 6518 | mToken = token; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6519 | mIdent = ident; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6520 | mApplication = application; |
| 6521 | mIntent = intent; |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 6522 | mReferrer = referrer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6523 | mComponent = intent.getComponent(); |
| 6524 | mActivityInfo = info; |
| 6525 | mTitle = title; |
| 6526 | mParent = parent; |
| 6527 | mEmbeddedID = id; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6528 | mLastNonConfigurationInstances = lastNonConfigurationInstances; |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 6529 | if (voiceInteractor != null) { |
| 6530 | if (lastNonConfigurationInstances != null) { |
| 6531 | mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor; |
| 6532 | } else { |
| 6533 | mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this, |
| 6534 | Looper.myLooper()); |
| 6535 | } |
| 6536 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6537 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 6538 | mWindow.setWindowManager( |
| 6539 | (WindowManager)context.getSystemService(Context.WINDOW_SERVICE), |
| 6540 | mToken, mComponent.flattenToString(), |
Romain Guy | 529b60a | 2010-08-03 18:05:47 -0700 | [diff] [blame] | 6541 | (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6542 | if (mParent != null) { |
| 6543 | mWindow.setContainer(mParent.getWindow()); |
| 6544 | } |
| 6545 | mWindowManager = mWindow.getWindowManager(); |
| 6546 | mCurrentConfig = config; |
| 6547 | } |
| 6548 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 6549 | /** @hide */ |
| 6550 | public final IBinder getActivityToken() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6551 | return mParent != null ? mParent.getActivityToken() : mToken; |
| 6552 | } |
| 6553 | |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6554 | final void performCreateCommon() { |
Dianne Hackborn | 30c9bd8 | 2010-12-01 16:07:40 -0800 | [diff] [blame] | 6555 | mVisibleFromClient = !mWindow.getWindowStyle().getBoolean( |
| 6556 | com.android.internal.R.styleable.Window_windowNoDisplay, false); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 6557 | mFragments.dispatchActivityCreated(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6558 | mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions()); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6559 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6560 | |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6561 | final void performCreate(Bundle icicle) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6562 | restoreHasCurrentPermissionRequest(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6563 | onCreate(icicle); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6564 | mActivityTransitionState.readState(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6565 | performCreateCommon(); |
| 6566 | } |
| 6567 | |
| 6568 | final void performCreate(Bundle icicle, PersistableBundle persistentState) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6569 | restoreHasCurrentPermissionRequest(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6570 | onCreate(icicle, persistentState); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6571 | mActivityTransitionState.readState(icicle); |
Craig Mautner | a002604 | 2014-04-23 11:45:37 -0700 | [diff] [blame] | 6572 | performCreateCommon(); |
| 6573 | } |
| 6574 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6575 | final void performStart() { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6576 | mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions()); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6577 | mFragments.noteStateNotSaved(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6578 | mCalled = false; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6579 | mFragments.execPendingActions(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6580 | mInstrumentation.callActivityOnStart(this); |
| 6581 | if (!mCalled) { |
| 6582 | throw new SuperNotCalledException( |
| 6583 | "Activity " + mComponent.toShortString() + |
| 6584 | " did not call through to super.onStart()"); |
| 6585 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6586 | mFragments.dispatchStart(); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6587 | mFragments.reportLoaderStart(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 6588 | mActivityTransitionState.enterReady(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6589 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6590 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6591 | final void performRestart() { |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6592 | mFragments.noteStateNotSaved(); |
Dianne Hackborn | a21e3da | 2010-09-12 19:27:46 -0700 | [diff] [blame] | 6593 | |
Filip Gruszczynski | a59ac9c | 2015-09-10 18:28:48 -0700 | [diff] [blame] | 6594 | if (mToken != null && mParent == null) { |
| 6595 | // We might have view roots that were preserved during a relaunch, we need to start them |
| 6596 | // again. We don't need to check mStopped, the roots will check if they were actually |
| 6597 | // stopped. |
| 6598 | WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */); |
| 6599 | } |
| 6600 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6601 | if (mStopped) { |
| 6602 | mStopped = false; |
Dianne Hackborn | 185e3e2 | 2011-06-03 15:26:01 -0700 | [diff] [blame] | 6603 | |
| 6604 | synchronized (mManagedCursors) { |
| 6605 | final int N = mManagedCursors.size(); |
| 6606 | for (int i=0; i<N; i++) { |
| 6607 | ManagedCursor mc = mManagedCursors.get(i); |
| 6608 | if (mc.mReleased || mc.mUpdated) { |
| 6609 | if (!mc.mCursor.requery()) { |
Dianne Hackborn | a5445d3 | 2011-09-01 14:38:24 -0700 | [diff] [blame] | 6610 | if (getApplicationInfo().targetSdkVersion |
| 6611 | >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 6612 | throw new IllegalStateException( |
| 6613 | "trying to requery an already closed cursor " |
| 6614 | + mc.mCursor); |
| 6615 | } |
Dianne Hackborn | 185e3e2 | 2011-06-03 15:26:01 -0700 | [diff] [blame] | 6616 | } |
| 6617 | mc.mReleased = false; |
| 6618 | mc.mUpdated = false; |
| 6619 | } |
| 6620 | } |
| 6621 | } |
| 6622 | |
| 6623 | mCalled = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6624 | mInstrumentation.callActivityOnRestart(this); |
| 6625 | if (!mCalled) { |
| 6626 | throw new SuperNotCalledException( |
| 6627 | "Activity " + mComponent.toShortString() + |
| 6628 | " did not call through to super.onRestart()"); |
| 6629 | } |
| 6630 | performStart(); |
| 6631 | } |
| 6632 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6633 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6634 | final void performResume() { |
| 6635 | performRestart(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6636 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6637 | mFragments.execPendingActions(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6638 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 6639 | mLastNonConfigurationInstances = null; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6640 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6641 | mCalled = false; |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6642 | // mResumed is set by the instrumentation |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6643 | mInstrumentation.callActivityOnResume(this); |
| 6644 | if (!mCalled) { |
| 6645 | throw new SuperNotCalledException( |
| 6646 | "Activity " + mComponent.toShortString() + |
| 6647 | " did not call through to super.onResume()"); |
| 6648 | } |
| 6649 | |
Todd Kennedy | ee8c9c6 | 2014-12-10 14:22:59 -0800 | [diff] [blame] | 6650 | // invisible activities must be finished before onResume() completes |
| 6651 | if (!mVisibleFromClient && !mFinished) { |
| 6652 | Log.w(TAG, "An activity without a UI must call finish() before onResume() completes"); |
| 6653 | if (getApplicationInfo().targetSdkVersion |
| 6654 | > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 6655 | throw new IllegalStateException( |
| 6656 | "Activity " + mComponent.toShortString() + |
| 6657 | " did not call finish() prior to onResume() completing"); |
| 6658 | } |
| 6659 | } |
| 6660 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6661 | // 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] | 6662 | mCalled = false; |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6663 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6664 | mFragments.dispatchResume(); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 6665 | mFragments.execPendingActions(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6666 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6667 | onPostResume(); |
| 6668 | if (!mCalled) { |
| 6669 | throw new SuperNotCalledException( |
| 6670 | "Activity " + mComponent.toShortString() + |
| 6671 | " did not call through to super.onPostResume()"); |
| 6672 | } |
| 6673 | } |
| 6674 | |
| 6675 | final void performPause() { |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 6676 | mDoReportFullyDrawn = false; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6677 | mFragments.dispatchPause(); |
Dianne Hackborn | e794e9f | 2010-08-24 12:32:10 -0700 | [diff] [blame] | 6678 | mCalled = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6679 | onPause(); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6680 | mResumed = false; |
Dianne Hackborn | e794e9f | 2010-08-24 12:32:10 -0700 | [diff] [blame] | 6681 | if (!mCalled && getApplicationInfo().targetSdkVersion |
| 6682 | >= android.os.Build.VERSION_CODES.GINGERBREAD) { |
| 6683 | throw new SuperNotCalledException( |
| 6684 | "Activity " + mComponent.toShortString() + |
| 6685 | " did not call through to super.onPause()"); |
| 6686 | } |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6687 | mResumed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6688 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6689 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6690 | final void performUserLeaving() { |
| 6691 | onUserInteraction(); |
| 6692 | onUserLeaveHint(); |
| 6693 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6694 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6695 | final void performStop() { |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 6696 | mDoReportFullyDrawn = false; |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6697 | mFragments.doLoaderStop(mChangingConfigurations /*retain*/); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6698 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6699 | if (!mStopped) { |
| 6700 | if (mWindow != null) { |
| 6701 | mWindow.closeAllPanels(); |
| 6702 | } |
| 6703 | |
Dianne Hackborn | ce418e6 | 2011-03-01 14:31:38 -0800 | [diff] [blame] | 6704 | if (mToken != null && mParent == null) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 6705 | WindowManagerGlobal.getInstance().setStoppedState(mToken, true); |
Dianne Hackborn | ce418e6 | 2011-03-01 14:31:38 -0800 | [diff] [blame] | 6706 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6707 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6708 | mFragments.dispatchStop(); |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6709 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6710 | mCalled = false; |
| 6711 | mInstrumentation.callActivityOnStop(this); |
| 6712 | if (!mCalled) { |
| 6713 | throw new SuperNotCalledException( |
| 6714 | "Activity " + mComponent.toShortString() + |
| 6715 | " did not call through to super.onStop()"); |
| 6716 | } |
RoboErik | 5501165 | 2014-07-09 15:05:53 -0700 | [diff] [blame] | 6717 | |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 6718 | synchronized (mManagedCursors) { |
| 6719 | final int N = mManagedCursors.size(); |
| 6720 | for (int i=0; i<N; i++) { |
| 6721 | ManagedCursor mc = mManagedCursors.get(i); |
| 6722 | if (!mc.mReleased) { |
| 6723 | mc.mCursor.deactivate(); |
| 6724 | mc.mReleased = true; |
| 6725 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6726 | } |
| 6727 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6728 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6729 | mStopped = true; |
| 6730 | } |
| 6731 | mResumed = false; |
| 6732 | } |
| 6733 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6734 | final void performDestroy() { |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 6735 | mDestroyed = true; |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 6736 | mWindow.destroy(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6737 | mFragments.dispatchDestroy(); |
| 6738 | onDestroy(); |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6739 | mFragments.doLoaderDestroy(); |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 6740 | if (mVoiceInteractor != null) { |
| 6741 | mVoiceInteractor.detachActivity(); |
| 6742 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 6743 | } |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6744 | |
| 6745 | /** |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 6746 | * @hide |
| 6747 | */ |
| 6748 | public final boolean isResumed() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6749 | return mResumed; |
| 6750 | } |
| 6751 | |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6752 | private void storeHasCurrentPermissionRequest(Bundle bundle) { |
| 6753 | if (bundle != null && mHasCurrentPermissionsRequest) { |
| 6754 | bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true); |
| 6755 | } |
| 6756 | } |
| 6757 | |
| 6758 | private void restoreHasCurrentPermissionRequest(Bundle bundle) { |
| 6759 | if (bundle != null) { |
| 6760 | mHasCurrentPermissionsRequest = bundle.getBoolean( |
| 6761 | HAS_CURENT_PERMISSIONS_REQUEST_KEY, false); |
| 6762 | } |
| 6763 | } |
| 6764 | |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 6765 | void dispatchActivityResult(String who, int requestCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6766 | int resultCode, Intent data) { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 6767 | if (false) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6768 | TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode |
| 6769 | + ", resCode=" + resultCode + ", data=" + data); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 6770 | mFragments.noteStateNotSaved(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6771 | if (who == null) { |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 6772 | onActivityResult(requestCode, resultCode, data); |
| 6773 | } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) { |
| 6774 | who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length()); |
| 6775 | if (TextUtils.isEmpty(who)) { |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6776 | dispatchRequestPermissionsResult(requestCode, data); |
| 6777 | } else { |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 6778 | Fragment frag = mFragments.findFragmentByWho(who); |
| 6779 | if (frag != null) { |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 6780 | dispatchRequestPermissionsResultToFragment(requestCode, data, frag); |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6781 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 6782 | } |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 6783 | } else if (who.startsWith("@android:view:")) { |
| 6784 | ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews( |
| 6785 | getActivityToken()); |
| 6786 | for (ViewRootImpl viewRoot : views) { |
| 6787 | if (viewRoot.getView() != null |
| 6788 | && viewRoot.getView().dispatchActivityResult( |
| 6789 | who, requestCode, resultCode, data)) { |
| 6790 | return; |
| 6791 | } |
| 6792 | } |
| 6793 | } else { |
| 6794 | Fragment frag = mFragments.findFragmentByWho(who); |
| 6795 | if (frag != null) { |
| 6796 | frag.onActivityResult(requestCode, resultCode, data); |
| 6797 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6798 | } |
| 6799 | } |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6800 | |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6801 | /** |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 6802 | * 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] | 6803 | * current task. |
| 6804 | * |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 6805 | * This will prevent the user from launching other apps, going to settings, or reaching the |
| 6806 | * home screen. This does not include those apps whose {@link android.R.attr#lockTaskMode} |
| 6807 | * values permit launching while locked. |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6808 | * |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 6809 | * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true or |
| 6810 | * lockTaskMode=lockTaskModeAlways for this component then the app will go directly into |
| 6811 | * Lock Task mode. The user will not be able to exit this mode until |
| 6812 | * {@link Activity#stopLockTask()} is called. |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 6813 | * |
| 6814 | * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false |
| 6815 | * 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] | 6816 | * this mode. When entered through this method the user can exit at any time through |
| 6817 | * an action described by the request dialog. Calling stopLockTask will also exit the |
| 6818 | * mode. |
Craig Mautner | 15df08a | 2015-04-01 12:17:18 -0700 | [diff] [blame] | 6819 | * |
| 6820 | * @see android.R.attr#lockTaskMode |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6821 | */ |
Craig Mautner | aea74a5 | 2014-03-08 14:23:10 -0800 | [diff] [blame] | 6822 | public void startLockTask() { |
| 6823 | try { |
| 6824 | ActivityManagerNative.getDefault().startLockTaskMode(mToken); |
| 6825 | } catch (RemoteException e) { |
| 6826 | } |
| 6827 | } |
| 6828 | |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6829 | /** |
| 6830 | * Allow the user to switch away from the current task. |
| 6831 | * |
| 6832 | * Called to end the mode started by {@link Activity#startLockTask}. This |
| 6833 | * can only be called by activities that have successfully called |
| 6834 | * startLockTask previously. |
| 6835 | * |
| 6836 | * 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] | 6837 | * <p>Note: This method should only be called when the activity is user-facing. That is, |
| 6838 | * between onResume() and onPause(). |
| 6839 | * <p>Note: If there are other tasks below this one that are also locked then calling this |
| 6840 | * method will immediately finish this task and resume the previous locked one, remaining in |
| 6841 | * lockTask mode. |
| 6842 | * |
| 6843 | * @see android.R.attr#lockTaskMode |
| 6844 | * @see ActivityManager#getLockTaskModeState() |
Jason Monk | a57e5e0 | 2014-05-07 10:06:48 -0400 | [diff] [blame] | 6845 | */ |
Craig Mautner | aea74a5 | 2014-03-08 14:23:10 -0800 | [diff] [blame] | 6846 | public void stopLockTask() { |
| 6847 | try { |
| 6848 | ActivityManagerNative.getDefault().stopLockTaskMode(); |
| 6849 | } catch (RemoteException e) { |
| 6850 | } |
| 6851 | } |
| 6852 | |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6853 | /** |
Craig Mautner | c21ae9e | 2015-04-15 09:45:42 -0700 | [diff] [blame] | 6854 | * Shows the user the system defined message for telling the user how to exit |
| 6855 | * lock task mode. The task containing this activity must be in lock task mode at the time |
| 6856 | * of this call for the message to be displayed. |
| 6857 | */ |
| 6858 | public void showLockTaskEscapeMessage() { |
| 6859 | try { |
| 6860 | ActivityManagerNative.getDefault().showLockTaskEscapeMessage(mToken); |
| 6861 | } catch (RemoteException e) { |
| 6862 | } |
| 6863 | } |
| 6864 | |
| 6865 | /** |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 6866 | * Set whether the caption should displayed directly on the content rather than push it down. |
| 6867 | * |
| 6868 | * This affects only freeform windows since they display the caption and only the main |
| 6869 | * window of the activity. The caption is used to drag the window around and also shows |
| 6870 | * maximize and close action buttons. |
| 6871 | */ |
| 6872 | public void overlayWithDecorCaption(boolean overlay) { |
| 6873 | mWindow.setOverlayDecorCaption(overlay); |
| 6874 | } |
| 6875 | |
| 6876 | /** |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6877 | * Interface for informing a translucent {@link Activity} once all visible activities below it |
| 6878 | * have completed drawing. This is necessary only after an {@link Activity} has been made |
| 6879 | * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn |
| 6880 | * translucent again following a call to {@link |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 6881 | * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener, |
| 6882 | * ActivityOptions)} |
Chet Haase | abd3d77 | 2013-09-11 14:33:05 -0700 | [diff] [blame] | 6883 | * |
| 6884 | * @hide |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6885 | */ |
Jose Lima | 1491485 | 2014-08-14 09:14:12 -0700 | [diff] [blame] | 6886 | @SystemApi |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6887 | public interface TranslucentConversionListener { |
| 6888 | /** |
| 6889 | * Callback made following {@link Activity#convertToTranslucent} once all visible Activities |
| 6890 | * below the top one have been redrawn. Following this callback it is safe to make the top |
| 6891 | * Activity translucent because the underlying Activity has been drawn. |
| 6892 | * |
| 6893 | * @param drawComplete True if the background Activity has drawn itself. False if a timeout |
| 6894 | * occurred waiting for the Activity to complete drawing. |
| 6895 | * |
| 6896 | * @see Activity#convertFromTranslucent() |
Craig Mautner | 233ceee | 2014-05-09 17:05:11 -0700 | [diff] [blame] | 6897 | * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions) |
Craig Mautner | 5eda9b3 | 2013-07-02 11:58:16 -0700 | [diff] [blame] | 6898 | */ |
| 6899 | public void onTranslucentConversionComplete(boolean drawComplete); |
| 6900 | } |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6901 | |
| 6902 | private void dispatchRequestPermissionsResult(int requestCode, Intent data) { |
Svetoslav | ffb32b1 | 2015-10-15 16:54:00 -0700 | [diff] [blame] | 6903 | mHasCurrentPermissionsRequest = false; |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 6904 | // If the package installer crashed we may have not data - best effort. |
| 6905 | String[] permissions = (data != null) ? data.getStringArrayExtra( |
| 6906 | PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0]; |
| 6907 | final int[] grantResults = (data != null) ? data.getIntArrayExtra( |
| 6908 | PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0]; |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6909 | onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 6910 | } |
| 6911 | |
| 6912 | private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data, |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 6913 | Fragment fragment) { |
| 6914 | // If the package installer crashed we may have not data - best effort. |
| 6915 | String[] permissions = (data != null) ? data.getStringArrayExtra( |
| 6916 | PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0]; |
| 6917 | final int[] grantResults = (data != null) ? data.getIntArrayExtra( |
| 6918 | PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0]; |
| 6919 | fragment.onRequestPermissionsResult(requestCode, permissions, grantResults); |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 6920 | } |
| 6921 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6922 | class HostCallbacks extends FragmentHostCallback<Activity> { |
| 6923 | public HostCallbacks() { |
| 6924 | super(Activity.this /*activity*/); |
| 6925 | } |
| 6926 | |
| 6927 | @Override |
| 6928 | public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 6929 | Activity.this.dump(prefix, fd, writer, args); |
| 6930 | } |
| 6931 | |
| 6932 | @Override |
| 6933 | public boolean onShouldSaveFragmentState(Fragment fragment) { |
| 6934 | return !isFinishing(); |
| 6935 | } |
| 6936 | |
| 6937 | @Override |
| 6938 | public LayoutInflater onGetLayoutInflater() { |
| 6939 | final LayoutInflater result = Activity.this.getLayoutInflater(); |
| 6940 | if (onUseFragmentManagerInflaterFactory()) { |
| 6941 | return result.cloneInContext(Activity.this); |
| 6942 | } |
| 6943 | return result; |
| 6944 | } |
| 6945 | |
| 6946 | @Override |
| 6947 | public boolean onUseFragmentManagerInflaterFactory() { |
| 6948 | // Newer platform versions use the child fragment manager's LayoutInflaterFactory. |
| 6949 | return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; |
| 6950 | } |
| 6951 | |
| 6952 | @Override |
| 6953 | public Activity onGetHost() { |
| 6954 | return Activity.this; |
| 6955 | } |
| 6956 | |
| 6957 | @Override |
| 6958 | public void onInvalidateOptionsMenu() { |
| 6959 | Activity.this.invalidateOptionsMenu(); |
| 6960 | } |
| 6961 | |
| 6962 | @Override |
| 6963 | public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode, |
| 6964 | Bundle options) { |
| 6965 | Activity.this.startActivityFromFragment(fragment, intent, requestCode, options); |
| 6966 | } |
| 6967 | |
| 6968 | @Override |
Svetoslav | 970b59c | 2015-06-09 16:05:21 -0700 | [diff] [blame] | 6969 | public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions, |
| 6970 | int requestCode) { |
| 6971 | String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho; |
| 6972 | Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions); |
| 6973 | startActivityForResult(who, intent, requestCode, null); |
| 6974 | } |
| 6975 | |
| 6976 | @Override |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6977 | public boolean onHasWindowAnimations() { |
| 6978 | return getWindow() != null; |
| 6979 | } |
| 6980 | |
| 6981 | @Override |
| 6982 | public int onGetWindowAnimations() { |
| 6983 | final Window w = getWindow(); |
| 6984 | return (w == null) ? 0 : w.getAttributes().windowAnimations; |
| 6985 | } |
| 6986 | |
Todd Kennedy | 434bd65 | 2015-05-04 12:29:50 -0700 | [diff] [blame] | 6987 | @Override |
| 6988 | public void onAttachFragment(Fragment fragment) { |
| 6989 | Activity.this.onAttachFragment(fragment); |
| 6990 | } |
| 6991 | |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 6992 | @Nullable |
| 6993 | @Override |
| 6994 | public View onFindViewById(int id) { |
| 6995 | return Activity.this.findViewById(id); |
| 6996 | } |
| 6997 | |
| 6998 | @Override |
| 6999 | public boolean onHasView() { |
| 7000 | final Window w = getWindow(); |
| 7001 | return (w != null && w.peekDecorView() != null); |
| 7002 | } |
| 7003 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7004 | } |