blob: 0410a6e804cde5dbd6fc7214a9899df6ea5c089e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.app;
18
Winson Chung1af8eda2016-02-05 17:55:56 +000019import static java.lang.Character.MIN_VALUE;
20
Tor Norbyec615c6f2015-03-02 10:11:44 -080021import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
23import android.annotation.IdRes;
24import android.annotation.IntDef;
25import android.annotation.LayoutRes;
Tor Norbye83c68962015-03-10 20:55:31 -070026import android.annotation.MainThread;
Tor Norbyed9273d62013-05-30 15:59:53 -070027import android.annotation.NonNull;
Tor Norbye7b9c9122013-05-30 16:48:33 -070028import android.annotation.Nullable;
Tor Norbye788fc2b2015-07-05 16:10:42 -070029import android.annotation.RequiresPermission;
Tor Norbye7b9c9122013-05-30 16:48:33 -070030import android.annotation.StyleRes;
Jose Lima4b6c6692014-08-12 17:41:12 -070031import android.annotation.SystemApi;
Jason Monk62515be2014-05-21 16:06:19 -040032import android.app.admin.DevicePolicyManager;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070033import android.app.assist.AssistContent;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070034import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.ComponentName;
36import android.content.ContentResolver;
37import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050038import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070039import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070040import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070041import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.SharedPreferences;
43import android.content.pm.ActivityInfo;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080044import android.content.pm.ApplicationInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070045import android.content.pm.PackageManager;
46import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.res.Configuration;
48import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070049import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.database.Cursor;
51import android.graphics.Bitmap;
52import android.graphics.Canvas;
Winson2d476832016-02-17 14:53:46 -080053import android.graphics.Color;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.graphics.drawable.Drawable;
Clara Bayarrifcd7e802016-03-10 12:58:18 +000055import android.hardware.input.InputManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070057import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.net.Uri;
Dianne Hackborn8d374262009-09-14 21:21:52 -070059import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Handler;
62import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080063import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070064import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000065import android.os.PersistableBundle;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.os.RemoteException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080067import android.os.StrictMode;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080068import android.os.SystemProperties;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070069import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070072import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.text.method.TextKeyListener;
Winson Chung1af8eda2016-02-05 17:55:56 +000074import android.transition.Scene;
75import android.transition.TransitionManager;
76import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.util.EventLog;
79import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070080import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070081import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.util.SparseArray;
Winson Chung1af8eda2016-02-05 17:55:56 +000083import android.util.SuperNotCalledException;
Adam Powell6e346362010-07-23 10:18:23 -070084import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070086import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.view.ContextThemeWrapper;
Winson Chung1af8eda2016-02-05 17:55:56 +000088import android.view.DragEvent;
89import android.view.DropPermissions;
Clara Bayarrifcd7e802016-03-10 12:58:18 +000090import android.view.InputDevice;
91import android.view.KeyCharacterMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.view.KeyEvent;
Clara Bayarri75e09792015-07-29 16:20:40 +010093import android.view.KeyboardShortcutGroup;
94import android.view.KeyboardShortcutInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.LayoutInflater;
96import android.view.Menu;
97import android.view.MenuInflater;
98import android.view.MenuItem;
99import android.view.MotionEvent;
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700100import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -0700102import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -0700104import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.ViewManager;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000106import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.view.Window;
Winson Chung1af8eda2016-02-05 17:55:56 +0000108import android.view.Window.WindowControllerCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700110import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -0700111import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.widget.AdapterView;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800113import android.widget.Toast;
Winson Chung1af8eda2016-02-05 17:55:56 +0000114import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100115
Winson Chung1af8eda2016-02-05 17:55:56 +0000116import com.android.internal.app.IVoiceInteractor;
117import com.android.internal.app.ToolbarActionBar;
118import com.android.internal.app.WindowDecorActionBar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100119import com.android.internal.policy.PhoneWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
Dianne Hackborn625ac272010-09-17 18:29:22 -0700121import java.io.FileDescriptor;
122import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700123import java.lang.annotation.Retention;
124import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700125import java.util.ArrayList;
126import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700127import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
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:
RoboErik55011652014-07-09 15:05:53 -0700139 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * <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.
RoboErik55011652014-07-09 15:05:53 -0700146 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 * <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 &lt;activity&gt;}
156 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700157 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 * <p>Topics covered here:
159 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700160 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * <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 Fernandezb54e7a32011-10-03 15:09:50 -0700168 *
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 Hackborn291905e2010-08-17 15:17:15 -0700184 * <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 Project9066cfe2009-03-03 19:31:44 -0800192 * <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>
RoboErik55011652014-07-09 15:05:53 -0700200 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <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
RoboErik55011652014-07-09 15:05:53 -0700207 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * 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>
RoboErik55011652014-07-09 15:05:53 -0700227 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * <p><img src="../../../images/activity_lifecycle.png"
229 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700230 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <p>There are three key loops you may be interested in monitoring within your
232 * activity:
RoboErik55011652014-07-09 15:05:53 -0700233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * <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().
RoboErik55011652014-07-09 15:05:53 -0700242 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 * <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 Wakasaf76a50c2012-03-09 19:56:35 +0900250 * 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 Project9066cfe2009-03-03 19:31:44 -0800252 * can be called multiple times, as the activity becomes visible and hidden
253 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700254 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * <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>
RoboErik55011652014-07-09 15:05:53 -0700264 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 * <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();
RoboErik55011652014-07-09 15:05:53 -0700280 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * 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;">&nbsp;&nbsp;&nbsp;&nbsp;</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;">&nbsp;&nbsp;&nbsp;&nbsp;</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 Hackborn0aae2d42010-12-07 23:51:29 -0800354 * <td align="center"><font color="#800000"><strong>Pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB}</strong></font></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * <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 Giesing8b6216d2014-12-16 17:23:15 -0800389 * activity may be killed by the system <em>at any time</em> without another line
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 * 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
RoboErik55011652014-07-09 15:05:53 -0700396 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 * 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 Miyakawa5c40f3f2011-02-15 13:24:36 -0800401 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 * be called in every situation as described in its documentation.</p>
403 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800404 * <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 Project9066cfe2009-03-03 19:31:44 -0800412 * <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>
RoboErik55011652014-07-09 15:05:53 -0700420 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 * <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>
RoboErik55011652014-07-09 15:05:53 -0700427 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 * <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>
RoboErik55011652014-07-09 15:05:53 -0700437 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * <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>
RoboErik55011652014-07-09 15:05:53 -0700445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 * <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>
RoboErik55011652014-07-09 15:05:53 -0700455 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 * <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
RoboErik55011652014-07-09 15:05:53 -0700470 * {@link android.app.Activity#startActivityForResult(Intent, int)}
471 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700473 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 *
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 Wright5438e4e2014-07-25 14:05:42 -0700494 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 * 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 Hackborn0aae2d42010-12-07 23:51:29 -0800554 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * 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 Chong3fa98cb2012-04-13 11:17:24 -0700595 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * }
597 *
598 * protected void onPause() {
599 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700600 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 * SharedPreferences.Editor ed = mPrefs.edit();
602 * ed.putInt("view_mode", mCurViewMode);
603 * ed.commit();
604 * }
605 * }
606 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700607 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 * <a name="Permissions"></a>
609 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700610 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * <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 &lt;activity&gt;}
614 * tag. By doing so, other applications will need to declare a corresponding
615 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
616 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800617 *
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 Project9066cfe2009-03-03 19:31:44 -0800629 * <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.
RoboErik55011652014-07-09 15:05:53 -0700631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 * <a name="ProcessLifecycle"></a>
633 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700634 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 * <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).
RoboErik55011652014-07-09 15:05:53 -0700644 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * <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>
RoboErik55011652014-07-09 15:05:53 -0700672 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 * <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 */
684public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700685 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700687 OnCreateContextMenuListener, ComponentCallbacks2,
Skuhnece2faa52015-08-11 10:36:38 -0700688 Window.OnWindowDismissedCallback, WindowControllerCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700690 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
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 Ogunwaleba7881c2015-08-01 19:28:29 -0700699 /** @hide Task isn't finished when activity is finished */
700 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700701 /**
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 Ogunwaleba7881c2015-08-01 19:28:29 -0700705 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700706 /**
707 * @hide Task is finished along with the finishing activity, but it is not removed from
708 * recents.
709 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700710 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
711
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700712 static final String FRAGMENTS_TAG = "android:fragments";
713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 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 Hackborn8ea138c2010-01-26 18:01:04 -0800718 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700719 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
720 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721
Svetoslav970b59c2015-06-09 16:05:21 -0700722 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
723
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800724 private static class ManagedDialog {
725 Dialog mDialog;
726 Bundle mArgs;
727 }
728 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729
730 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
731 private Instrumentation mInstrumentation;
732 private IBinder mToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700733 private int mIdent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 /*package*/ String mEmbeddedID;
735 private Application mApplication;
Christopher Tateb70f3df2009-04-07 16:07:59 -0700736 /*package*/ Intent mIntent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800737 /*package*/ String mReferrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 private ComponentName mComponent;
739 /*package*/ ActivityInfo mActivityInfo;
740 /*package*/ ActivityThread mMainThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 Activity mParent;
742 boolean mCalled;
Jeff Hamilton52d32032011-01-08 15:31:26 -0600743 /*package*/ boolean mResumed;
Andrii Kulian58178f22016-03-16 13:44:56 -0700744 /*package*/ boolean mStopped;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 boolean mFinished;
746 boolean mStartedActivity;
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700747 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700748 private boolean mDoReportFullyDrawn = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700749 /** true if the activity is going through a transient pause */
750 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500751 /** 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 Project9066cfe2009-03-03 19:31:44 -0800753 /*package*/ int mConfigChangeFlags;
754 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100755 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700756 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700758 static final class NonConfigurationInstances {
759 Object activity;
760 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800761 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700762 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700763 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700764 }
765 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 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 Powelle43340c2014-03-17 19:10:43 -0700774 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700775 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776
Dianne Hackborn91097de2014-04-04 18:02:06 -0700777 private VoiceInteractor mVoiceInteractor;
778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 private CharSequence mTitle;
780 private int mTitleColor = 0;
781
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700782 // 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 Mautneree2e45a2014-06-27 12:10:03 -0700785
Jose Lima4b6c6692014-08-12 17:41:12 -0700786 // Most recent call to requestVisibleBehind().
787 boolean mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 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
RoboErik55011652014-07-09 15:05:53 -0700803 // protected by synchronized (this)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 int mResultCode = RESULT_CANCELED;
805 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700806
Craig Mautner5eda9b32013-07-02 11:58:16 -0700807 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700808 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700810 private SearchEvent mSearchEvent;
811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000813 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814
815 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
816 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700817
Winsonb6403152016-02-23 13:32:09 -0800818 private ActivityManager.TaskDescription mTaskDescription =
819 new ActivityManager.TaskDescription();
820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
822
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700823 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700824 private final Object mInstanceTracker = StrictMode.trackActivity(this);
825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700827
George Mount1fecfb22014-06-18 14:55:55 -0700828 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700829 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
830 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831
Svetoslavffb32b12015-10-15 16:54:00 -0700832 private boolean mHasCurrentPermissionsRequest;
George Mount5beb26172015-12-15 13:36:01 -0800833 private boolean mEatKeyUpEvent;
Svetoslavffb32b12015-10-15 16:54:00 -0700834
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800835 private static native String getDlWarning();
836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 /** Return the intent that started this activity. */
838 public Intent getIntent() {
839 return mIntent;
840 }
841
RoboErik55011652014-07-09 15:05:53 -0700842 /**
843 * Change the intent returned by {@link #getIntent}. This holds a
844 * reference to the given intent; it does not copy it. Often used in
845 * conjunction with {@link #onNewIntent}.
846 *
847 * @param newIntent The new Intent object to return from getIntent
848 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 * @see #getIntent
850 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700851 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 public void setIntent(Intent newIntent) {
853 mIntent = newIntent;
854 }
855
856 /** Return the application that owns this activity. */
857 public final Application getApplication() {
858 return mApplication;
859 }
860
861 /** Is this activity embedded inside of another activity? */
862 public final boolean isChild() {
863 return mParent != null;
864 }
RoboErik55011652014-07-09 15:05:53 -0700865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 /** Return the parent activity if this view is an embedded child. */
867 public final Activity getParent() {
868 return mParent;
869 }
870
871 /** Retrieve the window manager for showing custom windows. */
872 public WindowManager getWindowManager() {
873 return mWindowManager;
874 }
875
876 /**
877 * Retrieve the current {@link android.view.Window} for the activity.
878 * This can be used to directly access parts of the Window API that
879 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700880 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 * @return Window The current window, or null if the activity is not
882 * visual.
883 */
884 public Window getWindow() {
885 return mWindow;
886 }
887
888 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800889 * Return the LoaderManager for this activity, creating it if needed.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700890 */
891 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700892 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700893 }
RoboErik55011652014-07-09 15:05:53 -0700894
Dianne Hackbornc8017682010-07-06 13:34:38 -0700895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 * Calls {@link android.view.Window#getCurrentFocus} on the
897 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -0700898 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -0700900 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 * @see #getWindow
902 * @see android.view.Window#getCurrentFocus
903 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700904 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 public View getCurrentFocus() {
906 return mWindow != null ? mWindow.getCurrentFocus() : null;
907 }
908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 /**
910 * Called when the activity is starting. This is where most initialization
911 * should go: calling {@link #setContentView(int)} to inflate the
912 * activity's UI, using {@link #findViewById} to programmatically interact
913 * with widgets in the UI, calling
914 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
915 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -0700916 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 * <p>You can call {@link #finish} from within this function, in
918 * which case onDestroy() will be immediately called without any of the rest
919 * of the activity lifecycle ({@link #onStart}, {@link #onResume},
920 * {@link #onPause}, etc) executing.
RoboErik55011652014-07-09 15:05:53 -0700921 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 * <p><em>Derived classes must call through to the super class's
923 * implementation of this method. If they do not, an exception will be
924 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -0700925 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 * @param savedInstanceState If the activity is being re-initialized after
927 * previously being shut down then this Bundle contains the data it most
928 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -0700929 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 * @see #onStart
931 * @see #onSaveInstanceState
932 * @see #onRestoreInstanceState
933 * @see #onPostCreate
934 */
Tor Norbye83c68962015-03-10 20:55:31 -0700935 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -0800936 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -0700937 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700938 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700939 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700940 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700941 }
Adam Powelldd8fab22012-03-22 17:47:27 -0700942 if (mActivityInfo.parentActivityName != null) {
943 if (mActionBar == null) {
944 mEnableDefaultActionBarUp = true;
945 } else {
946 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
947 }
948 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700949 if (savedInstanceState != null) {
950 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
951 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
952 ? mLastNonConfigurationInstances.fragments : null);
953 }
954 mFragments.dispatchCreate();
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700955 getApplication().dispatchActivityCreated(this, savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -0700956 if (mVoiceInteractor != null) {
957 mVoiceInteractor.attachActivity(this);
958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 mCalled = true;
960 }
961
962 /**
Craig Mautnera0026042014-04-23 11:45:37 -0700963 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -0700964 * the attribute {@link android.R.attr#persistableMode} set to
965 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -0700966 *
967 * @param savedInstanceState if the activity is being re-initialized after
968 * previously being shut down then this Bundle contains the data it most
969 * recently supplied in {@link #onSaveInstanceState}.
970 * <b><i>Note: Otherwise it is null.</i></b>
971 * @param persistentState if the activity is being re-initialized after
972 * previously being shut down or powered off then this Bundle contains the data it most
973 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
974 * <b><i>Note: Otherwise it is null.</i></b>
975 *
976 * @see #onCreate(android.os.Bundle)
977 * @see #onStart
978 * @see #onSaveInstanceState
979 * @see #onRestoreInstanceState
980 * @see #onPostCreate
981 */
Craig Mautner7a629c22014-09-04 14:57:04 -0700982 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -0700983 @Nullable PersistableBundle persistentState) {
984 onCreate(savedInstanceState);
985 }
986
987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 * The hook for {@link ActivityThread} to restore the state of this activity.
989 *
990 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
991 * {@link #restoreManagedDialogs(android.os.Bundle)}.
992 *
993 * @param savedInstanceState contains the saved state
994 */
995 final void performRestoreInstanceState(Bundle savedInstanceState) {
996 onRestoreInstanceState(savedInstanceState);
997 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 }
999
1000 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001001 * The hook for {@link ActivityThread} to restore the state of this activity.
1002 *
1003 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1004 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1005 *
1006 * @param savedInstanceState contains the saved state
1007 * @param persistentState contains the persistable saved state
1008 */
1009 final void performRestoreInstanceState(Bundle savedInstanceState,
1010 PersistableBundle persistentState) {
1011 onRestoreInstanceState(savedInstanceState, persistentState);
1012 if (savedInstanceState != null) {
1013 restoreManagedDialogs(savedInstanceState);
1014 }
1015 }
1016
1017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * This method is called after {@link #onStart} when the activity is
1019 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001020 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * to restore their state, but it is sometimes convenient to do it here
1022 * after all of the initialization has been done or to allow subclasses to
1023 * decide whether to use your default implementation. The default
1024 * implementation of this method performs a restore of any view state that
1025 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001026 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 * <p>This method is called between {@link #onStart} and
1028 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001029 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001031 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 * @see #onCreate
1033 * @see #onPostCreate
1034 * @see #onResume
1035 * @see #onSaveInstanceState
1036 */
1037 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1038 if (mWindow != null) {
1039 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1040 if (windowState != null) {
1041 mWindow.restoreHierarchyState(windowState);
1042 }
1043 }
1044 }
Craig Mautnera0026042014-04-23 11:45:37 -07001045
1046 /**
1047 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001048 * created with the attribute {@link android.R.attr#persistableMode} set to
1049 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1050 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001051 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1052 *
1053 * <p>This method is called between {@link #onStart} and
1054 * {@link #onPostCreate}.
1055 *
1056 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1057 *
1058 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
1059 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}.
1060 *
1061 * @see #onRestoreInstanceState(Bundle)
1062 * @see #onCreate
1063 * @see #onPostCreate
1064 * @see #onResume
1065 * @see #onSaveInstanceState
1066 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001067 public void onRestoreInstanceState(Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001068 PersistableBundle persistentState) {
1069 if (savedInstanceState != null) {
1070 onRestoreInstanceState(savedInstanceState);
1071 }
1072 }
1073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
1075 * Restore the state of any saved managed dialogs.
1076 *
1077 * @param savedInstanceState The bundle to restore from.
1078 */
1079 private void restoreManagedDialogs(Bundle savedInstanceState) {
1080 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1081 if (b == null) {
1082 return;
1083 }
1084
1085 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1086 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001087 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 for (int i = 0; i < numDialogs; i++) {
1089 final Integer dialogId = ids[i];
1090 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1091 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001092 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1093 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001094 final ManagedDialog md = new ManagedDialog();
1095 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1096 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1097 if (md.mDialog != null) {
1098 mManagedDialogs.put(dialogId, md);
1099 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1100 md.mDialog.onRestoreInstanceState(dialogState);
1101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103 }
1104 }
1105
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001106 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1107 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001108 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001109 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001110 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001111 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001112 return dialog;
1113 }
1114
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001115 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 return SAVED_DIALOG_KEY_PREFIX + key;
1117 }
1118
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001119 private static String savedDialogArgsKeyFor(int key) {
1120 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122
1123 /**
1124 * Called when activity start-up is complete (after {@link #onStart}
1125 * and {@link #onRestoreInstanceState} have been called). Applications will
1126 * generally not implement this method; it is intended for system
1127 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001128 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 * <p><em>Derived classes must call through to the super class's
1130 * implementation of this method. If they do not, an exception will be
1131 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001132 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 * @param savedInstanceState If the activity is being re-initialized after
1134 * previously being shut down then this Bundle contains the data it most
1135 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1136 * @see #onCreate
1137 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001138 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001139 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 if (!isChild()) {
1141 mTitleReady = true;
1142 onTitleChanged(getTitle(), getTitleColor());
1143 }
Winsonb6403152016-02-23 13:32:09 -08001144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 mCalled = true;
1146 }
1147
1148 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001149 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001150 * created with the attribute {@link android.R.attr#persistableMode} set to
1151 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001152 *
1153 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1154 * @param persistentState The data caming from the PersistableBundle first
1155 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1156 *
1157 * @see #onCreate
1158 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001159 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001160 @Nullable PersistableBundle persistentState) {
1161 onPostCreate(savedInstanceState);
1162 }
1163
1164 /**
RoboErik55011652014-07-09 15:05:53 -07001165 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1166 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001167 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 *
1169 * <p><em>Derived classes must call through to the super class's
1170 * implementation of this method. If they do not, an exception will be
1171 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 * @see #onCreate
1174 * @see #onStop
1175 * @see #onResume
1176 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001177 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001179 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001181
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001182 mFragments.doLoaderStart();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001183
1184 getApplication().dispatchActivityStarted(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186
1187 /**
1188 * Called after {@link #onStop} when the current activity is being
1189 * re-displayed to the user (the user has navigated back to it). It will
1190 * be followed by {@link #onStart} and then {@link #onResume}.
1191 *
1192 * <p>For activities that are using raw {@link Cursor} objects (instead of
1193 * creating them through
1194 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1195 * this is usually the place
1196 * where the cursor should be requeried (because you had deactivated it in
1197 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001198 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 * <p><em>Derived classes must call through to the super class's
1200 * implementation of this method. If they do not, an exception will be
1201 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001202 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 * @see #onStop
1204 * @see #onStart
1205 * @see #onResume
1206 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001207 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 protected void onRestart() {
1209 mCalled = true;
1210 }
1211
1212 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001213 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1214 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1215 * to give the activity a hint that its state is no longer saved -- it will generally
1216 * be called after {@link #onSaveInstanceState} and prior to the activity being
1217 * resumed/started again.
1218 */
1219 public void onStateNotSaved() {
1220 }
1221
1222 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1224 * {@link #onPause}, for your activity to start interacting with the user.
1225 * This is a good place to begin animations, open exclusive-access devices
1226 * (such as the camera), etc.
1227 *
1228 * <p>Keep in mind that onResume is not the best indicator that your activity
1229 * is visible to the user; a system window such as the keyguard may be in
1230 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1231 * activity is visible to the user (for example, to resume a game).
1232 *
1233 * <p><em>Derived classes must call through to the super class's
1234 * implementation of this method. If they do not, an exception will be
1235 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 * @see #onRestoreInstanceState
1238 * @see #onRestart
1239 * @see #onPostResume
1240 * @see #onPause
1241 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001242 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001244 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001245 getApplication().dispatchActivityResumed(this);
George Mountbdc4d8d2016-03-23 13:10:13 -07001246 mActivityTransitionState.onResume(this, isTopOfTask());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 mCalled = true;
1248 }
1249
1250 /**
1251 * Called when activity resume is complete (after {@link #onResume} has
1252 * been called). Applications will generally not implement this method;
1253 * it is intended for system classes to do final setup after application
1254 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001255 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 * <p><em>Derived classes must call through to the super class's
1257 * implementation of this method. If they do not, an exception will be
1258 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001259 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 * @see #onResume
1261 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001262 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 protected void onPostResume() {
1264 final Window win = getWindow();
1265 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001266 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 mCalled = true;
1268 }
1269
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001270 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
1271 if (voiceInteractor == null) {
1272 mVoiceInteractor = null;
1273 } else {
1274 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1275 Looper.myLooper());
1276 }
1277 }
1278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001280 * Check whether this activity is running as part of a voice interaction with the user.
1281 * If true, it should perform its interaction with the user through the
1282 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1283 */
1284 public boolean isVoiceInteraction() {
1285 return mVoiceInteractor != null;
1286 }
1287
1288 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001289 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1290 * of a voice interaction. That is, returns true if this activity was directly
1291 * started by the voice interaction service as the initiation of a voice interaction.
1292 * Otherwise, for example if it was started by another activity while under voice
1293 * interaction, returns false.
1294 */
1295 public boolean isVoiceInteractionRoot() {
1296 try {
1297 return mVoiceInteractor != null
1298 && ActivityManagerNative.getDefault().isRootVoiceInteraction(mToken);
1299 } catch (RemoteException e) {
1300 }
1301 return false;
1302 }
1303
1304 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001305 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1306 * interact with this activity.
1307 */
1308 public VoiceInteractor getVoiceInteractor() {
1309 return mVoiceInteractor;
1310 }
1311
1312 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001313 * Queries whether the currently enabled voice interaction service supports returning
1314 * a voice interactor for use by the activity. This is valid only for the duration of the
1315 * activity.
1316 *
1317 * @return whether the current voice interaction service supports local voice interaction
1318 */
1319 public boolean isLocalVoiceInteractionSupported() {
1320 try {
1321 return ActivityManagerNative.getDefault().supportsLocalVoiceInteraction();
1322 } catch (RemoteException re) {
1323 }
1324 return false;
1325 }
1326
1327 /**
1328 * Starts a local voice interaction session. When ready,
1329 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1330 * to the registered voice interaction service.
1331 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1332 */
1333 public void startLocalVoiceInteraction(Bundle privateOptions) {
1334 try {
1335 ActivityManagerNative.getDefault().startLocalVoiceInteraction(mToken, privateOptions);
1336 } catch (RemoteException re) {
1337 }
1338 }
1339
1340 /**
1341 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1342 * voice interaction session being started. You can now retrieve a voice interactor using
1343 * {@link #getVoiceInteractor()}.
1344 */
1345 public void onLocalVoiceInteractionStarted() {
1346 Log.i(TAG, "onLocalVoiceInteractionStarted! " + getVoiceInteractor());
1347 }
1348
1349 /**
1350 * Callback to indicate that the local voice interaction has stopped for some
1351 * reason.
1352 */
1353 public void onLocalVoiceInteractionStopped() {
1354 Log.i(TAG, "onLocalVoiceInteractionStopped :( " + getVoiceInteractor());
1355 }
1356
1357 /**
1358 * Request to terminate the current voice interaction that was previously started
1359 * using {@link #startLocalVoiceInteraction(Bundle)}.
1360 */
1361 public void stopLocalVoiceInteraction() {
1362 try {
1363 ActivityManagerNative.getDefault().stopLocalVoiceInteraction(mToken);
1364 } catch (RemoteException re) {
1365 }
1366 }
1367
1368 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 * This is called for activities that set launchMode to "singleTop" in
1370 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1371 * flag when calling {@link #startActivity}. In either case, when the
1372 * activity is re-launched while at the top of the activity stack instead
1373 * of a new instance of the activity being started, onNewIntent() will be
1374 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001375 * re-launch it.
1376 *
1377 * <p>An activity will always be paused before receiving a new intent, so
1378 * you can count on {@link #onResume} being called after this method.
1379 *
1380 * <p>Note that {@link #getIntent} still returns the original Intent. You
1381 * can use {@link #setIntent} to update it to this new Intent.
1382 *
1383 * @param intent The new intent that was started for the activity.
1384 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001386 * @see #setIntent
1387 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 */
1389 protected void onNewIntent(Intent intent) {
1390 }
1391
1392 /**
1393 * The hook for {@link ActivityThread} to save the state of this activity.
1394 *
1395 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1396 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1397 *
1398 * @param outState The bundle to save the state to.
1399 */
1400 final void performSaveInstanceState(Bundle outState) {
1401 onSaveInstanceState(outState);
1402 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001403 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001404 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001405 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407
1408 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001409 * The hook for {@link ActivityThread} to save the state of this activity.
1410 *
1411 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1412 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1413 *
1414 * @param outState The bundle to save the state to.
1415 * @param outPersistentState The bundle to save persistent state to.
1416 */
1417 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1418 onSaveInstanceState(outState, outPersistentState);
1419 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001420 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001421 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1422 ", " + outPersistentState);
1423 }
1424
1425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 * Called to retrieve per-instance state from an activity before being killed
1427 * so that the state can be restored in {@link #onCreate} or
1428 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1429 * will be passed to both).
1430 *
1431 * <p>This method is called before an activity may be killed so that when it
1432 * comes back some time in the future it can restore its state. For example,
1433 * if activity B is launched in front of activity A, and at some point activity
1434 * A is killed to reclaim resources, activity A will have a chance to save the
1435 * current state of its user interface via this method so that when the user
1436 * returns to activity A, the state of the user interface can be restored
1437 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1438 *
1439 * <p>Do not confuse this method with activity lifecycle callbacks such as
1440 * {@link #onPause}, which is always called when an activity is being placed
1441 * in the background or on its way to destruction, or {@link #onStop} which
1442 * is called before destruction. One example of when {@link #onPause} and
1443 * {@link #onStop} is called and not this method is when a user navigates back
1444 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1445 * on B because that particular instance will never be restored, so the
1446 * system avoids calling it. An example when {@link #onPause} is called and
1447 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1448 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1449 * killed during the lifetime of B since the state of the user interface of
1450 * A will stay intact.
1451 *
1452 * <p>The default implementation takes care of most of the UI per-instance
1453 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1454 * view in the hierarchy that has an id, and by saving the id of the currently
1455 * focused view (all of which is restored by the default implementation of
1456 * {@link #onRestoreInstanceState}). If you override this method to save additional
1457 * information not captured by each individual view, you will likely want to
1458 * call through to the default implementation, otherwise be prepared to save
1459 * all of the state of each view yourself.
1460 *
1461 * <p>If called, this method will occur before {@link #onStop}. There are
1462 * no guarantees about whether it will occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001465 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 * @see #onCreate
1467 * @see #onRestoreInstanceState
1468 * @see #onPause
1469 */
1470 protected void onSaveInstanceState(Bundle outState) {
1471 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001472 Parcelable p = mFragments.saveAllState();
1473 if (p != null) {
1474 outState.putParcelable(FRAGMENTS_TAG, p);
1475 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001476 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 }
1478
1479 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001480 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001481 * created with the attribute {@link android.R.attr#persistableMode} set to
1482 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1483 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1484 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001485 *
1486 * @param outState Bundle in which to place your saved state.
1487 * @param outPersistentState State which will be saved across reboots.
1488 *
1489 * @see #onSaveInstanceState(Bundle)
1490 * @see #onCreate
1491 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1492 * @see #onPause
1493 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001494 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001495 onSaveInstanceState(outState);
1496 }
1497
1498 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 * Save the state of any managed dialogs.
1500 *
1501 * @param outState place to store the saved state.
1502 */
1503 private void saveManagedDialogs(Bundle outState) {
1504 if (mManagedDialogs == null) {
1505 return;
1506 }
1507
1508 final int numDialogs = mManagedDialogs.size();
1509 if (numDialogs == 0) {
1510 return;
1511 }
1512
1513 Bundle dialogState = new Bundle();
1514
1515 int[] ids = new int[mManagedDialogs.size()];
1516
1517 // save each dialog's bundle, gather the ids
1518 for (int i = 0; i < numDialogs; i++) {
1519 final int key = mManagedDialogs.keyAt(i);
1520 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001521 final ManagedDialog md = mManagedDialogs.valueAt(i);
1522 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1523 if (md.mArgs != null) {
1524 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 }
1527
1528 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1529 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1530 }
1531
1532
1533 /**
1534 * Called as part of the activity lifecycle when an activity is going into
1535 * the background, but has not (yet) been killed. The counterpart to
1536 * {@link #onResume}.
1537 *
1538 * <p>When activity B is launched in front of activity A, this callback will
1539 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1540 * so be sure to not do anything lengthy here.
1541 *
1542 * <p>This callback is mostly used for saving any persistent state the
1543 * activity is editing, to present a "edit in place" model to the user and
1544 * making sure nothing is lost if there are not enough resources to start
1545 * the new activity without first killing this one. This is also a good
1546 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001547 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 * as fast as possible, or to close resources that are exclusive access
1549 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001550 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * <p>In situations where the system needs more memory it may kill paused
1552 * processes to reclaim resources. Because of this, you should be sure
1553 * that all of your state is saved by the time you return from
1554 * this function. In general {@link #onSaveInstanceState} is used to save
1555 * per-instance state in the activity and this method is used to store
1556 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001557 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 * <p>After receiving this call you will usually receive a following call
1559 * to {@link #onStop} (after the next activity has been resumed and
1560 * displayed), however in some cases there will be a direct call back to
1561 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001562 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 * <p><em>Derived classes must call through to the super class's
1564 * implementation of this method. If they do not, an exception will be
1565 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001566 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 * @see #onResume
1568 * @see #onSaveInstanceState
1569 * @see #onStop
1570 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001571 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001573 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001574 getApplication().dispatchActivityPaused(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 mCalled = true;
1576 }
1577
1578 /**
1579 * Called as part of the activity lifecycle when an activity is about to go
1580 * into the background as the result of user choice. For example, when the
1581 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1582 * when an incoming phone call causes the in-call Activity to be automatically
1583 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1584 * the activity being interrupted. In cases when it is invoked, this method
1585 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001586 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 * <p>This callback and {@link #onUserInteraction} are intended to help
1588 * activities manage status bar notifications intelligently; specifically,
1589 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07001590 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 * @see #onUserInteraction()
1592 */
1593 protected void onUserLeaveHint() {
1594 }
RoboErik55011652014-07-09 15:05:53 -07001595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 /**
1597 * Generate a new thumbnail for this activity. This method is called before
1598 * pausing the activity, and should draw into <var>outBitmap</var> the
1599 * imagery for the desired thumbnail in the dimensions of that bitmap. It
1600 * can use the given <var>canvas</var>, which is configured to draw into the
1601 * bitmap, for rendering if desired.
RoboErik55011652014-07-09 15:05:53 -07001602 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001603 * <p>The default implementation returns fails and does not draw a thumbnail;
1604 * this will result in the platform creating its own thumbnail if needed.
RoboErik55011652014-07-09 15:05:53 -07001605 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 * @param outBitmap The bitmap to contain the thumbnail.
1607 * @param canvas Can be used to render into the bitmap.
RoboErik55011652014-07-09 15:05:53 -07001608 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 * @return Return true if you have drawn into the bitmap; otherwise after
1610 * you return it will be filled with a default thumbnail.
RoboErik55011652014-07-09 15:05:53 -07001611 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 * @see #onCreateDescription
1613 * @see #onSaveInstanceState
1614 * @see #onPause
1615 */
1616 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001617 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 }
1619
1620 /**
1621 * Generate a new description for this activity. This method is called
1622 * before pausing the activity and can, if desired, return some textual
1623 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001624 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 * <p>The default implementation returns null, which will cause you to
1626 * inherit the description from the previous activity. If all activities
1627 * return null, generally the label of the top activity will be used as the
1628 * description.
RoboErik55011652014-07-09 15:05:53 -07001629 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 * @return A description of what the user is doing. It should be short and
1631 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001632 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 * @see #onCreateThumbnail
1634 * @see #onSaveInstanceState
1635 * @see #onPause
1636 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001637 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 public CharSequence onCreateDescription() {
1639 return null;
1640 }
1641
1642 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001643 * This is called when the user is requesting an assist, to build a full
1644 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1645 * application. You can override this method to place into the bundle anything
1646 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001647 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001648 *
1649 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001650 * been registered with {@link Application#registerOnProvideAssistDataListener
1651 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001652 */
1653 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001654 }
1655
1656 /**
1657 * This is called when the user is requesting an assist, to provide references
1658 * to content related to the current activity. Before being called, the
1659 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
1660 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
1661 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
1662 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
1663 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
1664 *
1665 * <p>Custom implementation may adjust the content intent to better reflect the top-level
1666 * context of the activity, and fill in its ClipData with additional content of
1667 * interest that the user is currently viewing. For example, an image gallery application
1668 * that has launched in to an activity allowing the user to swipe through pictures should
1669 * modify the intent to reference the current image they are looking it; such an
1670 * application when showing a list of pictures should add a ClipData that has
1671 * references to all of the pictures currently visible on screen.</p>
1672 *
1673 * @param outContent The assist content to return.
1674 */
1675 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001676 }
1677
Clara Bayarri75e09792015-07-29 16:20:40 +01001678 @Override
Clara Bayarrifcd7e802016-03-10 12:58:18 +00001679 public void onProvideKeyboardShortcuts(
1680 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
Clara Bayarri75e09792015-07-29 16:20:40 +01001681 if (menu == null) {
1682 return;
1683 }
Clara Bayarrifcd7e802016-03-10 12:58:18 +00001684 final InputDevice inputDevice = InputManager.getInstance().getInputDevice(deviceId);
1685 if (inputDevice == null) {
1686 return;
1687 }
1688 final KeyCharacterMap keyCharacterMap = inputDevice.getKeyCharacterMap();
Clara Bayarri75e09792015-07-29 16:20:40 +01001689 KeyboardShortcutGroup group = null;
1690 int menuSize = menu.size();
1691 for (int i = 0; i < menuSize; ++i) {
1692 final MenuItem item = menu.getItem(i);
1693 final CharSequence title = item.getTitle();
1694 final char alphaShortcut = item.getAlphabeticShortcut();
1695 if (title != null && alphaShortcut != MIN_VALUE) {
1696 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00001697 final int resource = mApplication.getApplicationInfo().labelRes;
1698 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01001699 }
1700 group.addItem(new KeyboardShortcutInfo(
1701 title, alphaShortcut, KeyEvent.META_CTRL_ON));
1702 }
1703 }
1704 if (group != null) {
1705 data.add(group);
1706 }
1707 }
1708
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001709 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07001710 * Ask to have the current assistant shown to the user. This only works if the calling
1711 * activity is the current foreground activity. It is the same as calling
1712 * {@link android.service.voice.VoiceInteractionService#showSession
1713 * VoiceInteractionService.showSession} and requesting all of the possible context.
1714 * The receiver will always see
1715 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
1716 * @return Returns true if the assistant was successfully invoked, else false. For example
1717 * false will be returned if the caller is not the current top activity.
1718 */
1719 public boolean showAssist(Bundle args) {
1720 try {
1721 return ActivityManagerNative.getDefault().showAssistFromActivity(mToken, args);
1722 } catch (RemoteException e) {
1723 }
1724 return false;
1725 }
1726
1727 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 * Called when you are no longer visible to the user. You will next
1729 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1730 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001731 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 * <p>Note that this method may never be called, in low memory situations
1733 * where the system does not have enough memory to keep your activity's
1734 * process running after its {@link #onPause} method is called.
RoboErik55011652014-07-09 15:05:53 -07001735 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 * <p><em>Derived classes must call through to the super class's
1737 * implementation of this method. If they do not, an exception will be
1738 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001739 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 * @see #onRestart
1741 * @see #onResume
1742 * @see #onSaveInstanceState
1743 * @see #onDestroy
1744 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001745 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001747 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001748 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001749 mActivityTransitionState.onStop();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001750 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001751 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 mCalled = true;
1753 }
1754
1755 /**
1756 * Perform any final cleanup before an activity is destroyed. This can
1757 * happen either because the activity is finishing (someone called
1758 * {@link #finish} on it, or because the system is temporarily destroying
1759 * this instance of the activity to save space. You can distinguish
1760 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001761 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 * <p><em>Note: do not count on this method being called as a place for
1763 * saving data! For example, if an activity is editing data in a content
1764 * provider, those edits should be committed in either {@link #onPause} or
1765 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1766 * free resources like threads that are associated with an activity, so
1767 * that a destroyed activity does not leave such things around while the
1768 * rest of its application is still running. There are situations where
1769 * the system will simply kill the activity's hosting process without
1770 * calling this method (or any others) in it, so it should not be used to
1771 * do things that are intended to remain around after the process goes
1772 * away.
RoboErik55011652014-07-09 15:05:53 -07001773 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 * <p><em>Derived classes must call through to the super class's
1775 * implementation of this method. If they do not, an exception will be
1776 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001777 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 * @see #onPause
1779 * @see #onStop
1780 * @see #finish
1781 * @see #isFinishing
1782 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001783 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001785 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 mCalled = true;
1787
1788 // dismiss any dialogs we are managing.
1789 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 final int numDialogs = mManagedDialogs.size();
1791 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001792 final ManagedDialog md = mManagedDialogs.valueAt(i);
1793 if (md.mDialog.isShowing()) {
1794 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001797 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799
1800 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001801 synchronized (mManagedCursors) {
1802 int numCursors = mManagedCursors.size();
1803 for (int i = 0; i < numCursors; i++) {
1804 ManagedCursor c = mManagedCursors.get(i);
1805 if (c != null) {
1806 c.mCursor.close();
1807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001809 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 }
Amith Yamasani49860442010-03-17 20:54:10 -07001811
1812 // Close any open search dialog
1813 if (mSearchManager != null) {
1814 mSearchManager.stopSearch();
1815 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001816
Chris Banes21b25772016-01-04 20:41:59 +00001817 if (mActionBar != null) {
1818 mActionBar.onDestroy();
1819 }
1820
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001821 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823
1824 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001825 * Report to the system that your app is now fully drawn, purely for diagnostic
1826 * purposes (calling it does not impact the visible behavior of the activity).
1827 * This is only used to help instrument application launch times, so that the
1828 * app can report when it is fully in a usable state; without this, the only thing
1829 * the system itself can determine is the point at which the activity's window
1830 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001831 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001832 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001833 * entirely drawn your UI and populated with all of the significant data. You
1834 * can safely call this method any time after first launch as well, in which case
1835 * it will simply be ignored.
1836 */
1837 public void reportFullyDrawn() {
1838 if (mDoReportFullyDrawn) {
1839 mDoReportFullyDrawn = false;
1840 try {
1841 ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken);
1842 } catch (RemoteException e) {
1843 }
1844 }
1845 }
1846
1847 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08001848 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
1849 * visa-versa.
1850 * @see android.R.attr#resizeableActivity
1851 *
Andrii Kulian933076d2016-03-29 17:04:42 -07001852 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001853 */
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001854 @CallSuper
Andrii Kulian933076d2016-03-29 17:04:42 -07001855 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001856 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Andrii Kulian933076d2016-03-29 17:04:42 -07001857 "onMultiWindowModeChanged " + this + ": " + isInMultiWindowMode);
1858 mFragments.dispatchMultiWindowModeChanged(isInMultiWindowMode);
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001859 if (mWindow != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07001860 mWindow.onMultiWindowModeChanged();
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001861 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001862 }
1863
1864 /**
1865 * Returns true if the activity is currently in multi-window mode.
1866 * @see android.R.attr#resizeableActivity
1867 *
1868 * @return True if the activity is in multi-window mode.
1869 */
Andrii Kulian933076d2016-03-29 17:04:42 -07001870 public boolean isInMultiWindowMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001871 try {
Andrii Kulian933076d2016-03-29 17:04:42 -07001872 return ActivityManagerNative.getDefault().isInMultiWindowMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001873 } catch (RemoteException e) {
1874 }
1875 return false;
1876 }
1877
1878 /**
1879 * Called by the system when the activity changes to and from picture-in-picture mode.
1880 * @see android.R.attr#supportsPictureInPicture
1881 *
Andrii Kulian933076d2016-03-29 17:04:42 -07001882 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001883 */
Wale Ogunwale7c796812016-01-29 21:13:50 -08001884 @CallSuper
Andrii Kulian933076d2016-03-29 17:04:42 -07001885 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001886 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Andrii Kulian933076d2016-03-29 17:04:42 -07001887 "onPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode);
1888 mFragments.dispatchPictureInPictureModeChanged(isInPictureInPictureMode);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001889 }
1890
1891 /**
1892 * Returns true if the activity is currently in picture-in-picture mode.
1893 * @see android.R.attr#supportsPictureInPicture
1894 *
1895 * @return True if the activity is in picture-in-picture mode.
1896 */
Andrii Kulian933076d2016-03-29 17:04:42 -07001897 public boolean isInPictureInPictureMode() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001898 try {
Andrii Kulian933076d2016-03-29 17:04:42 -07001899 return ActivityManagerNative.getDefault().isInPictureInPictureMode(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001900 } catch (RemoteException e) {
1901 }
1902 return false;
1903 }
1904
1905 /**
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001906 * Puts the activity in picture-in-picture mode.
1907 * @see android.R.attr#supportsPictureInPicture
1908 */
Andrii Kulian933076d2016-03-29 17:04:42 -07001909 public void enterPictureInPictureMode() {
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001910 try {
Andrii Kulian933076d2016-03-29 17:04:42 -07001911 ActivityManagerNative.getDefault().enterPictureInPictureMode(mToken);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001912 } catch (RemoteException e) {
1913 }
1914 }
1915
1916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 * Called by the system when the device configuration changes while your
1918 * activity is running. Note that this will <em>only</em> be called if
1919 * you have selected configurations you would like to handle with the
1920 * {@link android.R.attr#configChanges} attribute in your manifest. If
1921 * any configuration change occurs that is not selected to be reported
1922 * by that attribute, then instead of reporting it the system will stop
1923 * and restart the activity (to have it launched with the new
1924 * configuration).
RoboErik55011652014-07-09 15:05:53 -07001925 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 * <p>At the time that this function has been called, your Resources
1927 * object will have been updated to return resource values matching the
1928 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07001929 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 * @param newConfig The new device configuration.
1931 */
1932 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001933 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01001935
Dianne Hackborn9d071802010-12-08 14:49:15 -08001936 mFragments.dispatchConfigurationChanged(newConfig);
1937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 if (mWindow != null) {
1939 // Pass the configuration changed event to the window
1940 mWindow.onConfigurationChanged(newConfig);
1941 }
Adam Powell45c0b192011-07-28 15:11:57 -07001942
1943 if (mActionBar != null) {
1944 // Do this last; the action bar will need to access
1945 // view changes from above.
1946 mActionBar.onConfigurationChanged(newConfig);
1947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
RoboErik55011652014-07-09 15:05:53 -07001949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 /**
1951 * If this activity is being destroyed because it can not handle a
1952 * configuration parameter being changed (and thus its
1953 * {@link #onConfigurationChanged(Configuration)} method is
1954 * <em>not</em> being called), then you can use this method to discover
1955 * the set of changes that have occurred while in the process of being
1956 * destroyed. Note that there is no guarantee that these will be
1957 * accurate (other changes could have happened at any time), so you should
1958 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07001959 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 * @return Returns a bit field of the configuration parameters that are
1961 * changing, as defined by the {@link android.content.res.Configuration}
1962 * class.
1963 */
1964 public int getChangingConfigurations() {
1965 return mConfigChangeFlags;
1966 }
RoboErik55011652014-07-09 15:05:53 -07001967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 /**
1969 * Retrieve the non-configuration instance data that was previously
1970 * returned by {@link #onRetainNonConfigurationInstance()}. This will
1971 * be available from the initial {@link #onCreate} and
1972 * {@link #onStart} calls to the new instance, allowing you to extract
1973 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07001974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 * <p>Note that the data you retrieve here should <em>only</em> be used
1976 * as an optimization for handling configuration changes. You should always
1977 * be able to handle getting a null pointer back, and an activity must
1978 * still be able to restore itself to its previous state (through the
1979 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
1980 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07001981 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08001982 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07001983 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08001984 * available on older platforms through the Android support libraries.
1985 *
1986 * @return the object previously returned by {@link #onRetainNonConfigurationInstance()}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001988 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001990 return mLastNonConfigurationInstances != null
1991 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
RoboErik55011652014-07-09 15:05:53 -07001993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 /**
1995 * Called by the system, as part of destroying an
1996 * activity due to a configuration change, when it is known that a new
1997 * instance will immediately be created for the new configuration. You
1998 * can return any object you like here, including the activity instance
1999 * itself, which can later be retrieved by calling
2000 * {@link #getLastNonConfigurationInstance()} in the new activity
2001 * instance.
RoboErik55011652014-07-09 15:05:53 -07002002 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002003 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2004 * or later, consider instead using a {@link Fragment} with
2005 * {@link Fragment#setRetainInstance(boolean)
2006 * Fragment.setRetainInstance(boolean}.</em>
2007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 * <p>This function is called purely as an optimization, and you must
2009 * not rely on it being called. When it is called, a number of guarantees
2010 * will be made to help optimize configuration switching:
2011 * <ul>
2012 * <li> The function will be called between {@link #onStop} and
2013 * {@link #onDestroy}.
2014 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002015 * created after this one's {@link #onDestroy()} is called. In particular,
2016 * <em>no</em> messages will be dispatched during this time (when the returned
2017 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 * <li> The object you return here will <em>always</em> be available from
2019 * the {@link #getLastNonConfigurationInstance()} method of the following
2020 * activity instance as described there.
2021 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002022 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 * <p>These guarantees are designed so that an activity can use this API
2024 * to propagate extensive state from the old to new activity instance, from
2025 * loaded bitmaps, to network connections, to evenly actively running
2026 * threads. Note that you should <em>not</em> propagate any data that
2027 * may change based on the configuration, including any data loaded from
2028 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002029 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002030 * <p>The guarantee of no message handling during the switch to the next
2031 * activity simplifies use with active objects. For example if your retained
2032 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2033 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2034 * not be called from the call here until you execute the next instance's
2035 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2036 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2037 * running in a separate thread.)
2038 *
Adam Powellc4df6bc2016-03-09 15:11:37 -08002039 * <p><strong>Note:</strong> For most cases you should use the {@link Fragment} API
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002040 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
Adam Powellc4df6bc2016-03-09 15:11:37 -08002041 * available on older platforms through the Android support libraries.
2042 *
2043 * @return any Object holding the desired state to propagate to the
2044 * next activity instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 */
2046 public Object onRetainNonConfigurationInstance() {
2047 return null;
2048 }
RoboErik55011652014-07-09 15:05:53 -07002049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 /**
2051 * Retrieve the non-configuration instance data that was previously
2052 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2053 * be available from the initial {@link #onCreate} and
2054 * {@link #onStart} calls to the new instance, allowing you to extract
2055 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002056 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 * <p>Note that the data you retrieve here should <em>only</em> be used
2058 * as an optimization for handling configuration changes. You should always
2059 * be able to handle getting a null pointer back, and an activity must
2060 * still be able to restore itself to its previous state (through the
2061 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2062 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002063 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 * @return Returns the object previously returned by
2065 * {@link #onRetainNonConfigurationChildInstances()}
2066 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002067 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002068 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2069 return mLastNonConfigurationInstances != null
2070 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 }
RoboErik55011652014-07-09 15:05:53 -07002072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 /**
2074 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2075 * it should return either a mapping from child activity id strings to arbitrary objects,
2076 * or null. This method is intended to be used by Activity framework subclasses that control a
2077 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2078 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2079 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002080 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2082 return null;
2083 }
RoboErik55011652014-07-09 15:05:53 -07002084
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002085 NonConfigurationInstances retainNonConfigurationInstances() {
2086 Object activity = onRetainNonConfigurationInstance();
2087 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002088 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002089 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
2090 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002091 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002092 return null;
2093 }
RoboErik55011652014-07-09 15:05:53 -07002094
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002095 NonConfigurationInstances nci = new NonConfigurationInstances();
2096 nci.activity = activity;
2097 nci.children = children;
2098 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002099 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002100 if (mVoiceInteractor != null) {
2101 mVoiceInteractor.retainInstance();
2102 nci.voiceInteractor = mVoiceInteractor;
2103 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002104 return nci;
2105 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002108 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002110 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002112
2113 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002114 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002115 mCalled = true;
2116 mFragments.dispatchTrimMemory(level);
2117 }
2118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002120 * Return the FragmentManager for interacting with fragments associated
2121 * with this activity.
2122 */
2123 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002124 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002125 }
RoboErik55011652014-07-09 15:05:53 -07002126
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002127 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002128 * Called when a Fragment is being attached to this activity, immediately
2129 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2130 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
2131 */
2132 public void onAttachFragment(Fragment fragment) {
2133 }
RoboErik55011652014-07-09 15:05:53 -07002134
Dianne Hackbornc8017682010-07-06 13:34:38 -07002135 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 * Wrapper around
2137 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2138 * that gives the resulting {@link Cursor} to call
2139 * {@link #startManagingCursor} so that the activity will manage its
2140 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002141 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002142 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2143 * or later, consider instead using {@link LoaderManager} instead, available
2144 * via {@link #getLoaderManager()}.</em>
2145 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002146 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2147 * this method, because the activity will do that for you at the appropriate time. However, if
2148 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2149 * not</em> automatically close the cursor and, in that case, you must call
2150 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 * @param uri The URI of the content provider to query.
2153 * @param projection List of columns to return.
2154 * @param selection SQL WHERE clause.
2155 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002156 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2160 * @see #startManagingCursor
2161 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002162 *
2163 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 */
Jason parks6ed50de2010-08-25 10:18:50 -05002165 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002166 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2167 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2169 if (c != null) {
2170 startManagingCursor(c);
2171 }
2172 return c;
2173 }
2174
2175 /**
2176 * Wrapper around
2177 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2178 * that gives the resulting {@link Cursor} to call
2179 * {@link #startManagingCursor} so that the activity will manage its
2180 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002181 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002182 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2183 * or later, consider instead using {@link LoaderManager} instead, available
2184 * via {@link #getLoaderManager()}.</em>
2185 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002186 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2187 * this method, because the activity will do that for you at the appropriate time. However, if
2188 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2189 * not</em> automatically close the cursor and, in that case, you must call
2190 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002191 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 * @param uri The URI of the content provider to query.
2193 * @param projection List of columns to return.
2194 * @param selection SQL WHERE clause.
2195 * @param selectionArgs The arguments to selection, if any ?s are pesent
2196 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002197 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002199 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2201 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002202 *
2203 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 */
Jason parks6ed50de2010-08-25 10:18:50 -05002205 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002206 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2207 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2209 if (c != null) {
2210 startManagingCursor(c);
2211 }
2212 return c;
2213 }
2214
2215 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 * This method allows the activity to take care of managing the given
2217 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2218 * That is, when the activity is stopped it will automatically call
2219 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2220 * it will call {@link Cursor#requery} for you. When the activity is
2221 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002222 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002223 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2224 * or later, consider instead using {@link LoaderManager} instead, available
2225 * via {@link #getLoaderManager()}.</em>
2226 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002227 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2228 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2229 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2230 * <em>will not</em> automatically close the cursor and, in that case, you must call
2231 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002232 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002234 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2236 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002237 *
2238 * @deprecated Use the new {@link android.content.CursorLoader} class with
2239 * {@link LoaderManager} instead; this is also
2240 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 */
Jason parks6ed50de2010-08-25 10:18:50 -05002242 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 public void startManagingCursor(Cursor c) {
2244 synchronized (mManagedCursors) {
2245 mManagedCursors.add(new ManagedCursor(c));
2246 }
2247 }
2248
2249 /**
2250 * Given a Cursor that was previously given to
2251 * {@link #startManagingCursor}, stop the activity's management of that
2252 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002253 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002254 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002255 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002256 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002259 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002261 *
2262 * @deprecated Use the new {@link android.content.CursorLoader} class with
2263 * {@link LoaderManager} instead; this is also
2264 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 */
Jason parks6ed50de2010-08-25 10:18:50 -05002266 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 public void stopManagingCursor(Cursor c) {
2268 synchronized (mManagedCursors) {
2269 final int N = mManagedCursors.size();
2270 for (int i=0; i<N; i++) {
2271 ManagedCursor mc = mManagedCursors.get(i);
2272 if (mc.mCursor == c) {
2273 mManagedCursors.remove(i);
2274 break;
2275 }
2276 }
2277 }
2278 }
2279
2280 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002281 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2282 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002283 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002285 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 }
2288
2289 /**
2290 * Finds a view that was identified by the id attribute from the XML that
2291 * was processed in {@link #onCreate}.
2292 *
2293 * @return The view if found or null otherwise.
2294 */
Scott Kennedyc0519552015-02-11 15:33:10 -08002295 @Nullable
Tor Norbye7b9c9122013-05-30 16:48:33 -07002296 public View findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 return getWindow().findViewById(id);
2298 }
RoboErik55011652014-07-09 15:05:53 -07002299
Adam Powell33b97432010-04-20 10:01:14 -07002300 /**
2301 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002302 *
Adam Powell33b97432010-04-20 10:01:14 -07002303 * @return The Activity's ActionBar, or null if it does not have one.
2304 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002305 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002306 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002307 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002308 return mActionBar;
2309 }
Adam Powelle43340c2014-03-17 19:10:43 -07002310
2311 /**
2312 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2313 * Activity window.
2314 *
2315 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2316 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2317 * a traditional window decor action bar. The toolbar's menu will be populated with the
2318 * Activity's options menu and the navigation button will be wired through the standard
2319 * {@link android.R.id#home home} menu select action.</p>
2320 *
2321 * <p>In order to use a Toolbar within the Activity's window content the application
2322 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2323 *
Chris Banesc7d6c322016-01-27 14:09:16 +00002324 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07002325 */
Adam Powell37780142014-06-01 13:31:00 -07002326 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00002327 final ActionBar ab = getActionBar();
2328 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002329 throw new IllegalStateException("This Activity already has an action bar supplied " +
2330 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2331 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2332 }
Chris Banes21b25772016-01-04 20:41:59 +00002333
2334 // If we reach here then we're setting a new action bar
2335 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01002336 mMenuInflater = null;
2337
Chris Banes21b25772016-01-04 20:41:59 +00002338 // If we have an action bar currently, destroy it
2339 if (ab != null) {
2340 ab.onDestroy();
2341 }
2342
Chris Banesc7d6c322016-01-27 14:09:16 +00002343 if (toolbar != null) {
2344 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2345 mActionBar = tbab;
2346 mWindow.setCallback(tbab.getWrappedWindowCallback());
2347 } else {
2348 mActionBar = null;
2349 // Re-set the original window callback since we may have already set a Toolbar wrapper
2350 mWindow.setCallback(this);
2351 }
2352
2353 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002354 }
RoboErik55011652014-07-09 15:05:53 -07002355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 /**
Adam Powell33b97432010-04-20 10:01:14 -07002357 * Creates a new ActionBar, locates the inflated ActionBarView,
2358 * initializes the ActionBar with the view, and sets mActionBar.
2359 */
Adam Powelle43340c2014-03-17 19:10:43 -07002360 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002361 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002362
2363 // Initializing the window decor can change window feature flags.
2364 // Make sure that we have the correct set before performing the test below.
2365 window.getDecorView();
2366
Adam Powell9b4c8042010-08-10 15:36:44 -07002367 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002368 return;
2369 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002370
Adam Powelle43340c2014-03-17 19:10:43 -07002371 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002372 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002373
2374 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2375 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002376 }
RoboErik55011652014-07-09 15:05:53 -07002377
Adam Powell33b97432010-04-20 10:01:14 -07002378 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 * Set the activity content from a layout resource. The resource will be
2380 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08002381 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002383 *
Romain Guy482b34a62011-01-20 10:59:28 -08002384 * @see #setContentView(android.view.View)
2385 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002387 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002389 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 }
2391
2392 /**
2393 * Set the activity content to an explicit view. This view is placed
2394 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002395 * view hierarchy. When calling this method, the layout parameters of the
2396 * specified view are ignored. Both the width and the height of the view are
2397 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2398 * your own layout parameters, invoke
2399 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2400 * instead.
RoboErik55011652014-07-09 15:05:53 -07002401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08002403 *
2404 * @see #setContentView(int)
2405 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 */
2407 public void setContentView(View view) {
2408 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002409 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 }
2411
2412 /**
2413 * Set the activity content to an explicit view. This view is placed
2414 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002415 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002416 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 * @param view The desired content to display.
2418 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08002419 *
2420 * @see #setContentView(android.view.View)
2421 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 */
2423 public void setContentView(View view, ViewGroup.LayoutParams params) {
2424 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002425 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 }
2427
2428 /**
2429 * Add an additional content view to the activity. Added after any existing
2430 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 * @param view The desired content to display.
2433 * @param params Layout parameters for the view.
2434 */
2435 public void addContentView(View view, ViewGroup.LayoutParams params) {
2436 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002437 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 }
2439
2440 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002441 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2442 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2443 *
2444 * <p>This method will return non-null after content has been initialized (e.g. by using
2445 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2446 *
2447 * @return This window's content TransitionManager or null if none is set.
2448 */
2449 public TransitionManager getContentTransitionManager() {
2450 return getWindow().getTransitionManager();
2451 }
2452
2453 /**
2454 * Set the {@link TransitionManager} to use for default transitions in this window.
2455 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2456 *
2457 * @param tm The TransitionManager to use for scene changes.
2458 */
2459 public void setContentTransitionManager(TransitionManager tm) {
2460 getWindow().setTransitionManager(tm);
2461 }
2462
2463 /**
2464 * Retrieve the {@link Scene} representing this window's current content.
2465 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2466 *
2467 * <p>This method will return null if the current content is not represented by a Scene.</p>
2468 *
2469 * @return Current Scene being shown or null
2470 */
2471 public Scene getContentScene() {
2472 return getWindow().getContentScene();
2473 }
2474
2475 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002476 * Sets whether this activity is finished when touched outside its window's
2477 * bounds.
2478 */
2479 public void setFinishOnTouchOutside(boolean finish) {
2480 mWindow.setCloseOnTouchOutside(finish);
2481 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002482
2483 /** @hide */
2484 @IntDef({
2485 DEFAULT_KEYS_DISABLE,
2486 DEFAULT_KEYS_DIALER,
2487 DEFAULT_KEYS_SHORTCUT,
2488 DEFAULT_KEYS_SEARCH_LOCAL,
2489 DEFAULT_KEYS_SEARCH_GLOBAL})
2490 @Retention(RetentionPolicy.SOURCE)
2491 @interface DefaultKeyMode {}
2492
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2495 * keys.
RoboErik55011652014-07-09 15:05:53 -07002496 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 * @see #setDefaultKeyMode
2498 */
2499 static public final int DEFAULT_KEYS_DISABLE = 0;
2500 /**
2501 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2502 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002503 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 * @see #setDefaultKeyMode
2505 */
2506 static public final int DEFAULT_KEYS_DIALER = 1;
2507 /**
2508 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2509 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002510 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002512 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 * @see #setDefaultKeyMode
2514 */
2515 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2516 /**
2517 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2518 * will start an application-defined search. (If the application or activity does not
2519 * actually define a search, the the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002520 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002522 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 * @see #setDefaultKeyMode
2524 */
2525 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2526
2527 /**
2528 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2529 * will start a global search (typically web search, but some platforms may define alternate
2530 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002531 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 * @see #setDefaultKeyMode
2535 */
2536 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2537
2538 /**
2539 * Select the default key handling for this activity. This controls what
2540 * will happen to key events that are not otherwise handled. The default
2541 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2542 * floor. Other modes allow you to launch the dialer
2543 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2544 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002545 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002547 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 * <p>Note that the mode selected here does not impact the default
2549 * handling of system keys, such as the "back" and "menu" keys, and your
2550 * activity and its views always get a first chance to receive and handle
2551 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002552 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 * @see #DEFAULT_KEYS_DISABLE
2556 * @see #DEFAULT_KEYS_DIALER
2557 * @see #DEFAULT_KEYS_SHORTCUT
2558 * @see #DEFAULT_KEYS_SEARCH_LOCAL
2559 * @see #DEFAULT_KEYS_SEARCH_GLOBAL
2560 * @see #onKeyDown
2561 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002562 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 // Some modes use a SpannableStringBuilder to track & dispatch input events
2566 // This list must remain in sync with the switch in onKeyDown()
2567 switch (mode) {
2568 case DEFAULT_KEYS_DISABLE:
2569 case DEFAULT_KEYS_SHORTCUT:
2570 mDefaultKeySsb = null; // not used in these modes
2571 break;
2572 case DEFAULT_KEYS_DIALER:
2573 case DEFAULT_KEYS_SEARCH_LOCAL:
2574 case DEFAULT_KEYS_SEARCH_GLOBAL:
2575 mDefaultKeySsb = new SpannableStringBuilder();
2576 Selection.setSelection(mDefaultKeySsb,0);
2577 break;
2578 default:
2579 throw new IllegalArgumentException();
2580 }
2581 }
2582
2583 /**
2584 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002585 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 * is inside a TextView will not trigger the event (unless it is a navigation
2587 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002588 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 * <p>If the focused view didn't want this event, this method is called.
2590 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002591 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
2592 * by calling {@link #onBackPressed()}, though the behavior varies based
2593 * on the application compatibility mode: for
2594 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
2595 * it will set up the dispatch to call {@link #onKeyUp} where the action
2596 * will be performed; for earlier applications, it will perform the
2597 * action immediately in on-down, as those versions of the platform
2598 * behaved.
RoboErik55011652014-07-09 15:05:53 -07002599 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002600 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002601 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07002602 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002604 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 * this event and it should continue to be propagated.
2606 * @see #onKeyUp
2607 * @see android.view.KeyEvent
2608 */
2609 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002610 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002611 if (getApplicationInfo().targetSdkVersion
2612 >= Build.VERSION_CODES.ECLAIR) {
2613 event.startTracking();
2614 } else {
2615 onBackPressed();
2616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 return true;
2618 }
RoboErik55011652014-07-09 15:05:53 -07002619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
2621 return false;
2622 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08002623 Window w = getWindow();
2624 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
2625 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
2626 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002627 return true;
2628 }
2629 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 } else {
2631 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
2632 boolean clearSpannable = false;
2633 boolean handled;
2634 if ((event.getRepeatCount() != 0) || event.isSystem()) {
2635 clearSpannable = true;
2636 handled = false;
2637 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002638 handled = TextKeyListener.getInstance().onKeyDown(
2639 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 if (handled && mDefaultKeySsb.length() > 0) {
2641 // something useable has been typed - dispatch it now.
2642
2643 final String str = mDefaultKeySsb.toString();
2644 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07002645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 switch (mDefaultKeyMode) {
2647 case DEFAULT_KEYS_DIALER:
2648 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
2649 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07002650 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 break;
2652 case DEFAULT_KEYS_SEARCH_LOCAL:
2653 startSearch(str, false, null, false);
2654 break;
2655 case DEFAULT_KEYS_SEARCH_GLOBAL:
2656 startSearch(str, false, null, true);
2657 break;
2658 }
2659 }
2660 }
2661 if (clearSpannable) {
2662 mDefaultKeySsb.clear();
2663 mDefaultKeySsb.clearSpans();
2664 Selection.setSelection(mDefaultKeySsb,0);
2665 }
2666 return handled;
2667 }
2668 }
2669
2670 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002671 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
2672 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
2673 * the event).
2674 */
2675 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2676 return false;
2677 }
2678
2679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002681 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 * is inside a TextView will not trigger the event (unless it is a navigation
2683 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002684 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002685 * <p>The default implementation handles KEYCODE_BACK to stop the activity
2686 * and go back.
RoboErik55011652014-07-09 15:05:53 -07002687 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002689 * further, or <code>false</code> to indicate that you have not handled
2690 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 * @see #onKeyDown
2692 * @see KeyEvent
2693 */
2694 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002695 if (getApplicationInfo().targetSdkVersion
2696 >= Build.VERSION_CODES.ECLAIR) {
2697 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
2698 && !event.isCanceled()) {
2699 onBackPressed();
2700 return true;
2701 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 return false;
2704 }
2705
2706 /**
2707 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
2708 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
2709 * the event).
2710 */
2711 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2712 return false;
2713 }
RoboErik55011652014-07-09 15:05:53 -07002714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002716 * Called when the activity has detected the user's press of the back
2717 * key. The default implementation simply finishes the current activity,
2718 * but you can override this to do whatever you want.
2719 */
2720 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07002721 if (mActionBar != null && mActionBar.collapseActionView()) {
2722 return;
2723 }
2724
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002725 if (!mFragments.getFragmentManager().popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07002726 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07002727 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002728 }
Jeff Brown64da12a2011-01-04 19:57:47 -08002729
2730 /**
2731 * Called when a key shortcut event is not handled by any of the views in the Activity.
2732 * Override this method to implement global key shortcuts for the Activity.
2733 * Key shortcuts can also be implemented by setting the
2734 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
2735 *
2736 * @param keyCode The value in event.getKeyCode().
2737 * @param event Description of the key event.
2738 * @return True if the key shortcut was handled.
2739 */
2740 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07002741 // Let the Action Bar have a chance at handling the shortcut.
2742 ActionBar actionBar = getActionBar();
2743 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08002744 }
2745
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002746 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 * Called when a touch screen event was not handled by any of the views
2748 * under it. This is most useful to process touch events that happen
2749 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07002750 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07002752 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 * @return Return true if you have consumed the event, false if you haven't.
2754 * The default implementation always returns false.
2755 */
2756 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002757 if (mWindow.shouldCloseOnTouch(this, event)) {
2758 finish();
2759 return true;
2760 }
RoboErik55011652014-07-09 15:05:53 -07002761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 return false;
2763 }
RoboErik55011652014-07-09 15:05:53 -07002764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 /**
2766 * Called when the trackball was moved and not handled by any of the
2767 * views inside of the activity. So, for example, if the trackball moves
2768 * while focus is on a button, you will receive a call here because
2769 * buttons do not normally do anything with trackball events. The call
2770 * here happens <em>before</em> trackball movements are converted to
2771 * DPAD key events, which then get sent back to the view hierarchy, and
2772 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07002773 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07002775 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 * @return Return true if you have consumed the event, false if you haven't.
2777 * The default implementation always returns false.
2778 */
2779 public boolean onTrackballEvent(MotionEvent event) {
2780 return false;
2781 }
Jeff Browncb1404e2011-01-15 18:14:15 -08002782
2783 /**
2784 * Called when a generic motion event was not handled by any of the
2785 * views inside of the activity.
2786 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08002787 * Generic motion events describe joystick movements, mouse hovers, track pad
2788 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08002789 * {@link MotionEvent#getSource() source} of the motion event specifies
2790 * the class of input that was received. Implementations of this method
2791 * must examine the bits in the source before processing the event.
2792 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08002793 * </p><p>
2794 * Generic motion events with source class
2795 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
2796 * are delivered to the view under the pointer. All other generic motion events are
2797 * delivered to the focused view.
2798 * </p><p>
2799 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
2800 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08002801 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08002802 *
2803 * @param event The generic motion event being processed.
2804 *
2805 * @return Return true if you have consumed the event, false if you haven't.
2806 * The default implementation always returns false.
2807 */
2808 public boolean onGenericMotionEvent(MotionEvent event) {
2809 return false;
2810 }
2811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 /**
2813 * Called whenever a key, touch, or trackball event is dispatched to the
2814 * activity. Implement this method if you wish to know that the user has
2815 * interacted with the device in some way while your activity is running.
2816 * This callback and {@link #onUserLeaveHint} are intended to help
2817 * activities manage status bar notifications intelligently; specifically,
2818 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07002819 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
2821 * be accompanied by calls to {@link #onUserInteraction}. This
2822 * ensures that your activity will be told of relevant user activity such
2823 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07002824 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 * <p>Note that this callback will be invoked for the touch down action
2826 * that begins a touch gesture, but may not be invoked for the touch-moved
2827 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07002828 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 * @see #onUserLeaveHint()
2830 */
2831 public void onUserInteraction() {
2832 }
RoboErik55011652014-07-09 15:05:53 -07002833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
2835 // Update window manager if: we have a view, that view is
2836 // attached to its parent (which will be a RootView), and
2837 // this activity is not embedded.
2838 if (mParent == null) {
2839 View decor = mDecor;
2840 if (decor != null && decor.getParent() != null) {
2841 getWindowManager().updateViewLayout(decor, params);
2842 }
2843 }
2844 }
2845
2846 public void onContentChanged() {
2847 }
2848
2849 /**
2850 * Called when the current {@link Window} of the activity gains or loses
2851 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002852 * to the user. The default implementation clears the key tracking
2853 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07002854 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002855 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 * is managed independently of activity lifecycles. As such, while focus
2857 * changes will generally have some relation to lifecycle changes (an
2858 * activity that is stopped will not generally get window focus), you
2859 * should not rely on any particular order between the callbacks here and
2860 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07002861 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 * <p>As a general rule, however, a resumed activity will have window
2863 * focus... unless it has displayed other dialogs or popups that take
2864 * input focus, in which case the activity itself will not have focus
2865 * when the other windows have it. Likewise, the system may display
2866 * system-level windows (such as the status bar notification panel or
2867 * a system alert) which will temporarily take window input focus without
2868 * pausing the foreground activity.
2869 *
2870 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07002871 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 * @see #hasWindowFocus()
2873 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002874 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 */
2876 public void onWindowFocusChanged(boolean hasFocus) {
2877 }
RoboErik55011652014-07-09 15:05:53 -07002878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002880 * Called when the main window associated with the activity has been
2881 * attached to the window manager.
2882 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
2883 * for more information.
2884 * @see View#onAttachedToWindow
2885 */
2886 public void onAttachedToWindow() {
2887 }
RoboErik55011652014-07-09 15:05:53 -07002888
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002889 /**
2890 * Called when the main window associated with the activity has been
2891 * detached from the window manager.
2892 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
2893 * for more information.
2894 * @see View#onDetachedFromWindow
2895 */
2896 public void onDetachedFromWindow() {
2897 }
RoboErik55011652014-07-09 15:05:53 -07002898
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002899 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 * Returns true if this activity's <em>main</em> window currently has window focus.
2901 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07002902 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07002904 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
2906 */
2907 public boolean hasWindowFocus() {
2908 Window w = getWindow();
2909 if (w != null) {
2910 View d = w.getDecorView();
2911 if (d != null) {
2912 return d.hasWindowFocus();
2913 }
2914 }
2915 return false;
2916 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002917
2918 /**
2919 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07002920 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002921 */
Adam Powell117b6952014-05-05 18:14:56 -07002922 @Override
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002923 public void onWindowDismissed(boolean finishTask) {
2924 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002925 }
RoboErik55011652014-07-09 15:05:53 -07002926
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002927
Skuhnece2faa52015-08-11 10:36:38 -07002928 /**
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08002929 * Moves the activity from
2930 * {@link android.app.ActivityManager.StackId#FREEFORM_WORKSPACE_STACK_ID} to
2931 * {@link android.app.ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} stack.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002932 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002933 * @hide
2934 */
2935 @Override
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08002936 public void exitFreeformMode() throws RemoteException {
2937 ActivityManagerNative.getDefault().exitFreeformMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002938 }
2939
2940 /** Returns the current stack Id for the window.
2941 * @hide
2942 */
2943 @Override
2944 public int getWindowStackId() throws RemoteException {
2945 return ActivityManagerNative.getDefault().getActivityStackId(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 }
2947
2948 /**
RoboErik55011652014-07-09 15:05:53 -07002949 * Called to process key events. You can override this to intercept all
2950 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07002952 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07002954 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 * @return boolean Return true if this event was consumed.
2956 */
2957 public boolean dispatchKeyEvent(KeyEvent event) {
2958 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07002959
2960 // Let action bars open menus in response to the menu key prioritized over
2961 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08002962 final int keyCode = event.getKeyCode();
2963 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07002964 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
2965 return true;
George Mountcbe28352016-01-06 16:24:26 -08002966 } else if (event.isCtrlPressed() &&
2967 event.getUnicodeChar(event.getMetaState() & ~KeyEvent.META_CTRL_MASK) == '<') {
2968 // Capture the Control-< and send focus to the ActionBar
George Mount5beb26172015-12-15 13:36:01 -08002969 final int action = event.getAction();
2970 if (action == KeyEvent.ACTION_DOWN) {
George Mountcbe28352016-01-06 16:24:26 -08002971 final ActionBar actionBar = getActionBar();
2972 if (actionBar != null && actionBar.isShowing() && actionBar.requestFocus()) {
2973 mEatKeyUpEvent = true;
2974 return true;
George Mount5beb26172015-12-15 13:36:01 -08002975 }
2976 } else if (action == KeyEvent.ACTION_UP && mEatKeyUpEvent) {
2977 mEatKeyUpEvent = false;
2978 return true;
2979 }
Adam Powell07a74542014-05-30 15:52:44 -07002980 }
2981
Dianne Hackborn8d374262009-09-14 21:21:52 -07002982 Window win = getWindow();
2983 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 return true;
2985 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002986 View decor = mDecor;
2987 if (decor == null) decor = win.getDecorView();
2988 return event.dispatch(this, decor != null
2989 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 }
2991
2992 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08002993 * Called to process a key shortcut event.
2994 * You can override this to intercept all key shortcut events before they are
2995 * dispatched to the window. Be sure to call this implementation for key shortcut
2996 * events that should be handled normally.
2997 *
2998 * @param event The key shortcut event.
2999 * @return True if this event was consumed.
3000 */
3001 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3002 onUserInteraction();
3003 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3004 return true;
3005 }
3006 return onKeyShortcut(event.getKeyCode(), event);
3007 }
3008
3009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 * Called to process touch screen events. You can override this to
3011 * intercept all touch screen events before they are dispatched to the
3012 * window. Be sure to call this implementation for touch screen events
3013 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003014 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003016 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 * @return boolean Return true if this event was consumed.
3018 */
3019 public boolean dispatchTouchEvent(MotionEvent ev) {
3020 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3021 onUserInteraction();
3022 }
3023 if (getWindow().superDispatchTouchEvent(ev)) {
3024 return true;
3025 }
3026 return onTouchEvent(ev);
3027 }
RoboErik55011652014-07-09 15:05:53 -07003028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 /**
3030 * Called to process trackball events. You can override this to
3031 * intercept all trackball events before they are dispatched to the
3032 * window. Be sure to call this implementation for trackball events
3033 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003036 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 * @return boolean Return true if this event was consumed.
3038 */
3039 public boolean dispatchTrackballEvent(MotionEvent ev) {
3040 onUserInteraction();
3041 if (getWindow().superDispatchTrackballEvent(ev)) {
3042 return true;
3043 }
3044 return onTrackballEvent(ev);
3045 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003046
Jeff Browncb1404e2011-01-15 18:14:15 -08003047 /**
3048 * Called to process generic motion events. You can override this to
3049 * intercept all generic motion events before they are dispatched to the
3050 * window. Be sure to call this implementation for generic motion events
3051 * that should be handled normally.
3052 *
3053 * @param ev The generic motion event.
3054 *
3055 * @return boolean Return true if this event was consumed.
3056 */
3057 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3058 onUserInteraction();
3059 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3060 return true;
3061 }
3062 return onGenericMotionEvent(ev);
3063 }
3064
svetoslavganov75986cf2009-05-14 22:28:01 -07003065 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3066 event.setClassName(getClass().getName());
3067 event.setPackageName(getPackageName());
3068
3069 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003070 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3071 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003072 event.setFullScreen(isFullScreen);
3073
3074 CharSequence title = getTitle();
3075 if (!TextUtils.isEmpty(title)) {
3076 event.getText().add(title);
3077 }
3078
3079 return true;
3080 }
3081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 /**
3083 * Default implementation of
3084 * {@link android.view.Window.Callback#onCreatePanelView}
3085 * for activities. This
3086 * simply returns null so that all panel sub-windows will have the default
3087 * menu behavior.
3088 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003089 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 public View onCreatePanelView(int featureId) {
3091 return null;
3092 }
3093
3094 /**
3095 * Default implementation of
3096 * {@link android.view.Window.Callback#onCreatePanelMenu}
3097 * for activities. This calls through to the new
3098 * {@link #onCreateOptionsMenu} method for the
3099 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3100 * so that subclasses of Activity don't need to deal with feature codes.
3101 */
3102 public boolean onCreatePanelMenu(int featureId, Menu menu) {
3103 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003104 boolean show = onCreateOptionsMenu(menu);
3105 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3106 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 }
3108 return false;
3109 }
3110
3111 /**
3112 * Default implementation of
3113 * {@link android.view.Window.Callback#onPreparePanel}
3114 * for activities. This
3115 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3116 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3117 * panel, so that subclasses of
3118 * Activity don't need to deal with feature codes.
3119 */
3120 public boolean onPreparePanel(int featureId, View view, Menu menu) {
3121 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
3122 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003123 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003124 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 }
3126 return true;
3127 }
3128
3129 /**
3130 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003131 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 * @return The default implementation returns true.
3133 */
3134 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003135 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003136 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003137 if (mActionBar != null) {
3138 mActionBar.dispatchMenuVisibilityChanged(true);
3139 } else {
3140 Log.e(TAG, "Tried to open action bar menu with no action bar");
3141 }
Adam Powell8515ee82010-11-30 14:09:55 -08003142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 return true;
3144 }
3145
3146 /**
3147 * Default implementation of
3148 * {@link android.view.Window.Callback#onMenuItemSelected}
3149 * for activities. This calls through to the new
3150 * {@link #onOptionsItemSelected} method for the
3151 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3152 * panel, so that subclasses of
3153 * Activity don't need to deal with feature codes.
3154 */
3155 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09003156 CharSequence titleCondensed = item.getTitleCondensed();
3157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 switch (featureId) {
3159 case Window.FEATURE_OPTIONS_PANEL:
3160 // Put event logging here so it gets called even if subclass
3161 // doesn't call through to superclass's implmeentation of each
3162 // of these methods below
zobject686a8052012-12-14 21:11:08 +09003163 if(titleCondensed != null) {
3164 EventLog.writeEvent(50000, 0, titleCondensed.toString());
3165 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003166 if (onOptionsItemSelected(item)) {
3167 return true;
3168 }
Adam Powell04d58112012-04-09 10:22:12 -07003169 if (mFragments.dispatchOptionsItemSelected(item)) {
3170 return true;
3171 }
3172 if (item.getItemId() == android.R.id.home && mActionBar != null &&
3173 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
3174 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07003175 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07003176 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07003177 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07003178 }
Adam Powell04d58112012-04-09 10:22:12 -07003179 }
3180 return false;
RoboErik55011652014-07-09 15:05:53 -07003181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09003183 if(titleCondensed != null) {
3184 EventLog.writeEvent(50000, 1, titleCondensed.toString());
3185 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07003186 if (onContextItemSelected(item)) {
3187 return true;
3188 }
3189 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08003190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 default:
3192 return false;
3193 }
3194 }
RoboErik55011652014-07-09 15:05:53 -07003195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 /**
3197 * Default implementation of
3198 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
3199 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
3200 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3201 * so that subclasses of Activity don't need to deal with feature codes.
3202 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
3203 * {@link #onContextMenuClosed(Menu)} will be called.
3204 */
3205 public void onPanelClosed(int featureId, Menu menu) {
3206 switch (featureId) {
3207 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003208 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 onOptionsMenuClosed(menu);
3210 break;
RoboErik55011652014-07-09 15:05:53 -07003211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 case Window.FEATURE_CONTEXT_MENU:
3213 onContextMenuClosed(menu);
3214 break;
Adam Powell8515ee82010-11-30 14:09:55 -08003215
3216 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07003217 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08003218 mActionBar.dispatchMenuVisibilityChanged(false);
3219 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 }
3221 }
3222
3223 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003224 * Declare that the options menu has changed, so should be recreated.
3225 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
3226 * time it needs to be displayed.
3227 */
3228 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003229 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3230 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003231 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
3232 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003233 }
RoboErik55011652014-07-09 15:05:53 -07003234
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003235 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 * Initialize the contents of the Activity's standard options menu. You
3237 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07003238 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 * <p>This is only called once, the first time the options menu is
3240 * displayed. To update the menu every time it is displayed, see
3241 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07003242 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07003244 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
3245 * they will be correctly ordered with application-defined menu items.
3246 * Deriving classes should always call through to the base implementation.
3247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 * <p>You can safely hold on to <var>menu</var> (and any items created
3249 * from it), making modifications to it as desired, until the next
3250 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07003251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 * <p>When you add items to the menu, you can implement the Activity's
3253 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07003254 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07003256 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 * @return You must return true for the menu to be displayed;
3258 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003259 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 * @see #onPrepareOptionsMenu
3261 * @see #onOptionsItemSelected
3262 */
3263 public boolean onCreateOptionsMenu(Menu menu) {
3264 if (mParent != null) {
3265 return mParent.onCreateOptionsMenu(menu);
3266 }
3267 return true;
3268 }
3269
3270 /**
3271 * Prepare the Screen's standard options menu to be displayed. This is
3272 * called right before the menu is shown, every time it is shown. You can
3273 * use this method to efficiently enable/disable items or otherwise
3274 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07003275 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 * <p>The default implementation updates the system menu items based on the
3277 * activity's state. Deriving classes should always call through to the
3278 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07003279 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 * @param menu The options menu as last shown or first initialized by
3281 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07003282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 * @return You must return true for the menu to be displayed;
3284 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 * @see #onCreateOptionsMenu
3287 */
3288 public boolean onPrepareOptionsMenu(Menu menu) {
3289 if (mParent != null) {
3290 return mParent.onPrepareOptionsMenu(menu);
3291 }
3292 return true;
3293 }
3294
3295 /**
3296 * This hook is called whenever an item in your options menu is selected.
3297 * The default implementation simply returns false to have the normal
3298 * processing happen (calling the item's Runnable or sending a message to
3299 * its Handler as appropriate). You can use this method for any items
3300 * for which you would like to do processing without those other
3301 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003302 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003304 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003305 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 * @return boolean Return false to allow normal menu processing to
3309 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003310 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 * @see #onCreateOptionsMenu
3312 */
3313 public boolean onOptionsItemSelected(MenuItem item) {
3314 if (mParent != null) {
3315 return mParent.onOptionsItemSelected(item);
3316 }
3317 return false;
3318 }
3319
3320 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003321 * This method is called whenever the user chooses to navigate Up within your application's
3322 * activity hierarchy from the action bar.
3323 *
3324 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3325 * was specified in the manifest for this activity or an activity-alias to it,
3326 * default Up navigation will be handled automatically. If any activity
3327 * along the parent chain requires extra Intent arguments, the Activity subclass
3328 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3329 * to supply those arguments.</p>
3330 *
3331 * <p>See <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
3332 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3333 * from the design guide for more information about navigating within your app.</p>
3334 *
3335 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3336 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3337 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3338 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3339 *
3340 * @return true if Up navigation completed successfully and this Activity was finished,
3341 * false otherwise.
3342 */
3343 public boolean onNavigateUp() {
3344 // Automatically handle hierarchical Up navigation if the proper
3345 // metadata is available.
3346 Intent upIntent = getParentActivityIntent();
3347 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003348 if (mActivityInfo.taskAffinity == null) {
3349 // Activities with a null affinity are special; they really shouldn't
3350 // specify a parent activity intent in the first place. Just finish
3351 // the current activity and call it a day.
3352 finish();
3353 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003354 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003355 onCreateNavigateUpTaskStack(b);
3356 onPrepareNavigateUpTaskStack(b);
3357 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003358
3359 // We can't finishAffinity if we have a result.
3360 // Fall back and simply finish the current activity instead.
3361 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3362 // Tell the developer what's going on to avoid hair-pulling.
3363 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3364 finish();
3365 } else {
3366 finishAffinity();
3367 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003368 } else {
3369 navigateUpTo(upIntent);
3370 }
3371 return true;
3372 }
3373 return false;
3374 }
3375
3376 /**
3377 * This is called when a child activity of this one attempts to navigate up.
3378 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3379 *
3380 * @param child The activity making the call.
3381 */
3382 public boolean onNavigateUpFromChild(Activity child) {
3383 return onNavigateUp();
3384 }
3385
3386 /**
3387 * Define the synthetic task stack that will be generated during Up navigation from
3388 * a different task.
3389 *
3390 * <p>The default implementation of this method adds the parent chain of this activity
3391 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3392 * may choose to override this method to construct the desired task stack in a different
3393 * way.</p>
3394 *
Adam Powellf0195952012-05-02 21:38:54 -07003395 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3396 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3397 * returned by {@link #getParentActivityIntent()}.</p>
3398 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003399 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3400 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3401 *
3402 * @param builder An empty TaskStackBuilder - the application should add intents representing
3403 * the desired task stack
3404 */
3405 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3406 builder.addParentStack(this);
3407 }
3408
3409 /**
3410 * Prepare the synthetic task stack that will be generated during Up navigation
3411 * from a different task.
3412 *
3413 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3414 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3415 * If any extra data should be added to these intents before launching the new task,
3416 * the application should override this method and add that data here.</p>
3417 *
3418 * @param builder A TaskStackBuilder that has been populated with Intents by
3419 * onCreateNavigateUpTaskStack.
3420 */
3421 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3422 }
3423
3424 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 * This hook is called whenever the options menu is being closed (either by the user canceling
3426 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003427 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 * @param menu The options menu as last shown or first initialized by
3429 * onCreateOptionsMenu().
3430 */
3431 public void onOptionsMenuClosed(Menu menu) {
3432 if (mParent != null) {
3433 mParent.onOptionsMenuClosed(menu);
3434 }
3435 }
RoboErik55011652014-07-09 15:05:53 -07003436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 /**
3438 * Programmatically opens the options menu. If the options menu is already
3439 * open, this method does nothing.
3440 */
3441 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003442 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3443 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003444 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 }
RoboErik55011652014-07-09 15:05:53 -07003447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 /**
3449 * Progammatically closes the options menu. If the options menu is already
3450 * closed, this method does nothing.
3451 */
3452 public void closeOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003453 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
3454 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 }
3457
3458 /**
3459 * Called when a context menu for the {@code view} is about to be shown.
3460 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3461 * time the context menu is about to be shown and should be populated for
3462 * the view (or item inside the view for {@link AdapterView} subclasses,
3463 * this can be found in the {@code menuInfo})).
3464 * <p>
3465 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3466 * item has been selected.
3467 * <p>
3468 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003469 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 */
3471 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3472 }
3473
3474 /**
3475 * Registers a context menu to be shown for the given view (multiple views
3476 * can show the context menu). This method will set the
3477 * {@link OnCreateContextMenuListener} on the view to this activity, so
3478 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3479 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003480 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 * @see #unregisterForContextMenu(View)
3482 * @param view The view that should show a context menu.
3483 */
3484 public void registerForContextMenu(View view) {
3485 view.setOnCreateContextMenuListener(this);
3486 }
RoboErik55011652014-07-09 15:05:53 -07003487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 /**
3489 * Prevents a context menu to be shown for the given view. This method will remove the
3490 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003491 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 * @see #registerForContextMenu(View)
3493 * @param view The view that should stop showing a context menu.
3494 */
3495 public void unregisterForContextMenu(View view) {
3496 view.setOnCreateContextMenuListener(null);
3497 }
RoboErik55011652014-07-09 15:05:53 -07003498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 /**
3500 * Programmatically opens the context menu for a particular {@code view}.
3501 * The {@code view} should have been added via
3502 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003503 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 * @param view The view to show the context menu for.
3505 */
3506 public void openContextMenu(View view) {
3507 view.showContextMenu();
3508 }
RoboErik55011652014-07-09 15:05:53 -07003509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 /**
3511 * Programmatically closes the most recently opened context menu, if showing.
3512 */
3513 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08003514 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
3515 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 }
RoboErik55011652014-07-09 15:05:53 -07003518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 /**
3520 * This hook is called whenever an item in a context menu is selected. The
3521 * default implementation simply returns false to have the normal processing
3522 * happen (calling the item's Runnable or sending a message to its Handler
3523 * as appropriate). You can use this method for any items for which you
3524 * would like to do processing without those other facilities.
3525 * <p>
3526 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3527 * View that added this menu item.
3528 * <p>
3529 * Derived classes should call through to the base class for it to perform
3530 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003531 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 * @param item The context menu item that was selected.
3533 * @return boolean Return false to allow normal context menu processing to
3534 * proceed, true to consume it here.
3535 */
3536 public boolean onContextItemSelected(MenuItem item) {
3537 if (mParent != null) {
3538 return mParent.onContextItemSelected(item);
3539 }
3540 return false;
3541 }
3542
3543 /**
3544 * This hook is called whenever the context menu is being closed (either by
3545 * the user canceling the menu with the back/menu button, or when an item is
3546 * selected).
RoboErik55011652014-07-09 15:05:53 -07003547 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 * @param menu The context menu that is being closed.
3549 */
3550 public void onContextMenuClosed(Menu menu) {
3551 if (mParent != null) {
3552 mParent.onContextMenuClosed(menu);
3553 }
3554 }
3555
3556 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003557 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003559 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 protected Dialog onCreateDialog(int id) {
3561 return null;
3562 }
3563
3564 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003565 * Callback for creating dialogs that are managed (saved and restored) for you
3566 * by the activity. The default implementation calls through to
3567 * {@link #onCreateDialog(int)} for compatibility.
3568 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003569 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3570 * or later, consider instead using a {@link DialogFragment} instead.</em>
3571 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003572 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3573 * this method the first time, and hang onto it thereafter. Any dialog
3574 * that is created by this method will automatically be saved and restored
3575 * for you, including whether it is showing.
3576 *
3577 * <p>If you would like the activity to manage saving and restoring dialogs
3578 * for you, you should override this method and handle any ids that are
3579 * passed to {@link #showDialog}.
3580 *
3581 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3582 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3583 *
3584 * @param id The id of the dialog.
3585 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3586 * @return The dialog. If you return null, the dialog will not be created.
3587 *
3588 * @see #onPrepareDialog(int, Dialog, Bundle)
3589 * @see #showDialog(int, Bundle)
3590 * @see #dismissDialog(int)
3591 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003592 *
3593 * @deprecated Use the new {@link DialogFragment} class with
3594 * {@link FragmentManager} instead; this is also
3595 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003596 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003597 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003598 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003599 protected Dialog onCreateDialog(int id, Bundle args) {
3600 return onCreateDialog(id);
3601 }
3602
3603 /**
3604 * @deprecated Old no-arguments version of
3605 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
3606 */
3607 @Deprecated
3608 protected void onPrepareDialog(int id, Dialog dialog) {
3609 dialog.setOwnerActivity(this);
3610 }
3611
3612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003614 * shown. The default implementation calls through to
3615 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07003616 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 * <p>
3618 * Override this if you need to update a managed dialog based on the state
3619 * of the application each time it is shown. For example, a time picker
3620 * dialog might want to be updated with the current time. You should call
3621 * through to the superclass's implementation. The default implementation
3622 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07003623 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 * @param id The id of the managed dialog.
3625 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003626 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3627 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 * @see #showDialog(int)
3629 * @see #dismissDialog(int)
3630 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003631 *
3632 * @deprecated Use the new {@link DialogFragment} class with
3633 * {@link FragmentManager} instead; this is also
3634 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003636 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003637 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
3638 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 }
3640
3641 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003642 * Simple version of {@link #showDialog(int, Bundle)} that does not
3643 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
3644 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003645 *
3646 * @deprecated Use the new {@link DialogFragment} class with
3647 * {@link FragmentManager} instead; this is also
3648 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003649 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003650 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003651 public final void showDialog(int id) {
3652 showDialog(id, null);
3653 }
3654
3655 /**
3656 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 * will be made with the same id the first time this is called for a given
3658 * id. From thereafter, the dialog will be automatically saved and restored.
3659 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003660 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3661 * or later, consider instead using a {@link DialogFragment} instead.</em>
3662 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003663 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 * be made to provide an opportunity to do any timely preparation.
3665 *
3666 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003667 * @param args Arguments to pass through to the dialog. These will be saved
3668 * and restored for you. Note that if the dialog is already created,
3669 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
3670 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08003671 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003672 * @return Returns true if the Dialog was created; false is returned if
3673 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07003674 *
Joe Onorato37296dc2009-07-31 17:58:55 -07003675 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003676 * @see #onCreateDialog(int, Bundle)
3677 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 * @see #dismissDialog(int)
3679 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003680 *
3681 * @deprecated Use the new {@link DialogFragment} class with
3682 * {@link FragmentManager} instead; this is also
3683 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003685 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003686 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003687 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003689 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003691 ManagedDialog md = mManagedDialogs.get(id);
3692 if (md == null) {
3693 md = new ManagedDialog();
3694 md.mDialog = createDialog(id, null, args);
3695 if (md.mDialog == null) {
3696 return false;
3697 }
3698 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 }
RoboErik55011652014-07-09 15:05:53 -07003700
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003701 md.mArgs = args;
3702 onPrepareDialog(id, md.mDialog, args);
3703 md.mDialog.show();
3704 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 }
3706
3707 /**
3708 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
3709 *
3710 * @param id The id of the managed dialog.
3711 *
3712 * @throws IllegalArgumentException if the id was not previously shown via
3713 * {@link #showDialog(int)}.
3714 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003715 * @see #onCreateDialog(int, Bundle)
3716 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 * @see #showDialog(int)
3718 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003719 *
3720 * @deprecated Use the new {@link DialogFragment} class with
3721 * {@link FragmentManager} instead; this is also
3722 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003724 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 public final void dismissDialog(int id) {
3726 if (mManagedDialogs == null) {
3727 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 }
RoboErik55011652014-07-09 15:05:53 -07003729
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003730 final ManagedDialog md = mManagedDialogs.get(id);
3731 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 throw missingDialog(id);
3733 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003734 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 }
3736
3737 /**
3738 * Creates an exception to throw if a user passed in a dialog id that is
3739 * unexpected.
3740 */
3741 private IllegalArgumentException missingDialog(int id) {
3742 return new IllegalArgumentException("no dialog with id " + id + " was ever "
3743 + "shown via Activity#showDialog");
3744 }
3745
3746 /**
3747 * Removes any internal references to a dialog managed by this Activity.
3748 * If the dialog is showing, it will dismiss it as part of the clean up.
3749 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003750 * <p>This can be useful if you know that you will never show a dialog again and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 * want to avoid the overhead of saving and restoring it in the future.
3752 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003753 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
3754 * will not throw an exception if you try to remove an ID that does not
3755 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07003756 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 * @param id The id of the managed dialog.
3758 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003759 * @see #onCreateDialog(int, Bundle)
3760 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 * @see #showDialog(int)
3762 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003763 *
3764 * @deprecated Use the new {@link DialogFragment} class with
3765 * {@link FragmentManager} instead; this is also
3766 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003768 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003770 if (mManagedDialogs != null) {
3771 final ManagedDialog md = mManagedDialogs.get(id);
3772 if (md != null) {
3773 md.mDialog.dismiss();
3774 mManagedDialogs.remove(id);
3775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 }
3778
3779 /**
3780 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07003781 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01003782 * <p>You can use this function as a simple way to launch the search UI, in response to a
RoboErik55011652014-07-09 15:05:53 -07003783 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01003784 * calling this function is the same as calling
3785 * {@link #startSearch startSearch(null, false, null, false)}, which launches
3786 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07003787 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 * <p>You can override this function to force global search, e.g. in response to a dedicated
3789 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07003790 *
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003791 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default
3792 * implementation changes to simply return false and you must supply your own custom
3793 * implementation if you want to support search.</p>
3794 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003795 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003796 * @return Returns {@code true} if search launched, and {@code false} if the activity does
3797 * not respond to search. The default implementation always returns {@code true}, except
3798 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07003799 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 * @see android.app.SearchManager
3801 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003802 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
3803 mSearchEvent = searchEvent;
3804 boolean result = onSearchRequested();
3805 mSearchEvent = null;
3806 return result;
3807 }
3808
3809 /**
3810 * @see #onSearchRequested(SearchEvent)
3811 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 public boolean onSearchRequested() {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003813 if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK)
3814 != Configuration.UI_MODE_TYPE_TELEVISION) {
3815 startSearch(null, false, null, false);
3816 return true;
3817 } else {
3818 return false;
3819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 }
RoboErik55011652014-07-09 15:05:53 -07003821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003823 * During the onSearchRequested() callbacks, this function will return the
3824 * {@link SearchEvent} that triggered the callback, if it exists.
3825 *
3826 * @return SearchEvent The SearchEvent that triggered the {@link
3827 * #onSearchRequested} callback.
3828 */
3829 public final SearchEvent getSearchEvent() {
3830 return mSearchEvent;
3831 }
3832
3833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07003835 *
3836 * <p>It is typically called from onSearchRequested(), either directly from
3837 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07003839 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 * is to inject specific data such as context data, it is preferred to <i>override</i>
3841 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07003842 *
3843 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07003845 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 * any further typing will replace it. This is useful for cases where an entire pre-formed
3847 * query is being inserted. If false, the selection point will be placed at the end of the
3848 * inserted query. This is useful when the inserted query is text that the user entered,
3849 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
3850 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07003851 * @param appSearchData An application can insert application-specific
3852 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 * searches. This data will be returned with SEARCH intent(s). Null if
3854 * no extra data is required.
3855 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07003856 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07003857 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07003859 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 * @see android.app.SearchManager
3861 * @see #onSearchRequested
3862 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003863 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
3864 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003865 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01003866 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07003867 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 }
3869
3870 /**
krosaend2d60142009-08-17 08:56:48 -07003871 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
3872 * the search dialog. Made available for testing purposes.
3873 *
3874 * @param query The query to trigger. If empty, the request will be ignored.
3875 * @param appSearchData An application can insert application-specific
3876 * context here, in order to improve quality or specificity of its own
3877 * searches. This data will be returned with SEARCH intent(s). Null if
3878 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07003879 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003880 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07003881 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01003882 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07003883 }
3884
3885 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 * Request that key events come to this activity. Use this if your
3887 * activity has no views with focus, but the activity still wants
3888 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07003889 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 * @see android.view.Window#takeKeyEvents
3891 */
3892 public void takeKeyEvents(boolean get) {
3893 getWindow().takeKeyEvents(get);
3894 }
3895
3896 /**
3897 * Enable extended window features. This is a convenience for calling
3898 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07003899 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 * @param featureId The desired feature as defined in
3901 * {@link android.view.Window}.
3902 * @return Returns true if the requested feature is supported and now
3903 * enabled.
RoboErik55011652014-07-09 15:05:53 -07003904 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 * @see android.view.Window#requestFeature
3906 */
3907 public final boolean requestWindowFeature(int featureId) {
3908 return getWindow().requestFeature(featureId);
3909 }
3910
3911 /**
3912 * Convenience for calling
3913 * {@link android.view.Window#setFeatureDrawableResource}.
3914 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003915 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 getWindow().setFeatureDrawableResource(featureId, resId);
3917 }
3918
3919 /**
3920 * Convenience for calling
3921 * {@link android.view.Window#setFeatureDrawableUri}.
3922 */
3923 public final void setFeatureDrawableUri(int featureId, Uri uri) {
3924 getWindow().setFeatureDrawableUri(featureId, uri);
3925 }
3926
3927 /**
3928 * Convenience for calling
3929 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
3930 */
3931 public final void setFeatureDrawable(int featureId, Drawable drawable) {
3932 getWindow().setFeatureDrawable(featureId, drawable);
3933 }
3934
3935 /**
3936 * Convenience for calling
3937 * {@link android.view.Window#setFeatureDrawableAlpha}.
3938 */
3939 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
3940 getWindow().setFeatureDrawableAlpha(featureId, alpha);
3941 }
3942
3943 /**
3944 * Convenience for calling
3945 * {@link android.view.Window#getLayoutInflater}.
3946 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003947 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 public LayoutInflater getLayoutInflater() {
3949 return getWindow().getLayoutInflater();
3950 }
3951
3952 /**
3953 * Returns a {@link MenuInflater} with this context.
3954 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003955 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07003957 // Make sure that action views can get an appropriate theme.
3958 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07003959 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07003960 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07003961 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07003962 } else {
3963 mMenuInflater = new MenuInflater(this);
3964 }
3965 }
3966 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003967 }
3968
3969 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07003970 public void setTheme(int resid) {
3971 super.setTheme(resid);
3972 mWindow.setTheme(resid);
3973 }
3974
3975 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07003976 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003977 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 if (mParent == null) {
3979 super.onApplyThemeResource(theme, resid, first);
3980 } else {
3981 try {
3982 theme.setTo(mParent.getTheme());
3983 } catch (Exception e) {
3984 // Empty
3985 }
3986 theme.applyStyle(resid, false);
3987 }
Winson073a5262016-02-29 16:04:40 -08003988
3989 // Get the primary color and update the TaskDescription for this activity
3990 TypedArray a = theme.obtainStyledAttributes(
3991 com.android.internal.R.styleable.ActivityTaskDescription);
3992 if (mTaskDescription.getPrimaryColor() == 0) {
3993 int colorPrimary = a.getColor(
3994 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
3995 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
3996 mTaskDescription.setPrimaryColor(colorPrimary);
3997 }
3998 }
3999 // For dev-preview only.
4000 if (mTaskDescription.getBackgroundColor() == 0) {
4001 int colorBackground = a.getColor(
4002 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4003 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4004 mTaskDescription.setBackgroundColor(colorBackground);
4005 }
4006 }
4007 a.recycle();
4008 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004009 }
4010
4011 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004012 * Requests permissions to be granted to this application. These permissions
4013 * must be requested in your manifest, they should not be granted to your app,
4014 * and they should have protection level {@link android.content.pm.PermissionInfo
4015 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
4016 * the platform or a third-party app.
4017 * <p>
4018 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4019 * are granted at install time if requested in the manifest. Signature permissions
4020 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4021 * install time if requested in the manifest and the signature of your app matches
4022 * the signature of the app declaring the permissions.
4023 * </p>
4024 * <p>
4025 * If your app does not have the requested permissions the user will be presented
4026 * with UI for accepting them. After the user has accepted or rejected the
4027 * requested permissions you will receive a callback on {@link
4028 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4029 * permissions were granted or not.
4030 * </p>
4031 * <p>
4032 * Note that requesting a permission does not guarantee it will be granted and
4033 * your app should be able to run without having this permission.
4034 * </p>
4035 * <p>
4036 * This method may start an activity allowing the user to choose which permissions
4037 * to grant and which to reject. Hence, you should be prepared that your activity
4038 * may be paused and resumed. Further, granting some permissions may require
4039 * a restart of you application. In such a case, the system will recreate the
4040 * activity stack before delivering the result to {@link
4041 * #onRequestPermissionsResult(int, String[], int[])}.
4042 * </p>
4043 * <p>
4044 * When checking whether you have a permission you should use {@link
4045 * #checkSelfPermission(String)}.
4046 * </p>
4047 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004048 * Calling this API for permissions already granted to your app would show UI
4049 * to the user to decide whether the app can still hold these permissions. This
4050 * can be useful if the way your app uses data guarded by the permissions
4051 * changes significantly.
4052 * </p>
4053 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004054 * You cannot request a permission if your activity sets {@link
4055 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4056 * <code>true</code> because in this case the activity would not receive
4057 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4058 * </p>
4059 * <p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004060 * A sample permissions request looks like this:
4061 * </p>
4062 * <code><pre><p>
4063 * private void showContacts() {
4064 * if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
4065 * != PackageManager.PERMISSION_GRANTED) {
4066 * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
4067 * PERMISSIONS_REQUEST_READ_CONTACTS);
4068 * } else {
4069 * doShowContacts();
4070 * }
4071 * }
4072 *
4073 * {@literal @}Override
4074 * public void onRequestPermissionsResult(int requestCode, String[] permissions,
4075 * int[] grantResults) {
4076 * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS
4077 * && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
4078 * showContacts();
4079 * }
4080 * }
4081 * </code></pre></p>
4082 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004083 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004084 * @param requestCode Application specific request code to match with a result
4085 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004086 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004087 *
4088 * @see #onRequestPermissionsResult(int, String[], int[])
4089 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004090 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004091 */
4092 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Svetoslavffb32b12015-10-15 16:54:00 -07004093 if (mHasCurrentPermissionsRequest) {
4094 Log.w(TAG, "Can reqeust only one set of permissions at a time");
4095 // Dispatch the callback with empty arrays which means a cancellation.
4096 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4097 return;
4098 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004099 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004100 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004101 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004102 }
4103
4104 /**
4105 * Callback for the result from requesting permissions. This method
4106 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004107 * <p>
4108 * <strong>Note:</strong> It is possible that the permissions request interaction
4109 * with the user is interrupted. In this case you will receive empty permissions
4110 * and results arrays which should be treated as a cancellation.
4111 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004112 *
4113 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4114 * @param permissions The requested permissions. Never null.
4115 * @param grantResults The grant results for the corresponding permissions
4116 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4117 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4118 *
4119 * @see #requestPermissions(String[], int)
4120 */
4121 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4122 @NonNull int[] grantResults) {
4123 /* callback - no nothing */
4124 }
4125
4126 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004127 * Gets whether you should show UI with rationale for requesting a permission.
4128 * You should do this only if you do not have the permission and the context in
4129 * which the permission is requested does not clearly communicate to the user
4130 * what would be the benefit from granting this permission.
4131 * <p>
4132 * For example, if you write a camera app, requesting the camera permission
4133 * would be expected by the user and no rationale for why it is requested is
4134 * needed. If however, the app needs location for tagging photos then a non-tech
4135 * savvy user may wonder how location is related to taking photos. In this case
4136 * you may choose to show UI with rationale of requesting this permission.
4137 * </p>
4138 *
4139 * @param permission A permission your app wants to request.
4140 * @return Whether you can show permission rationale UI.
4141 *
4142 * @see #checkSelfPermission(String)
4143 * @see #requestPermissions(String[], int)
4144 * @see #onRequestPermissionsResult(int, String[], int[])
4145 */
Svetoslav9ce9a152015-06-02 17:59:31 -07004146 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07004147 return getPackageManager().shouldShowRequestPermissionRationale(permission);
4148 }
4149
4150 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004151 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
4152 * with no options.
4153 *
4154 * @param intent The intent to start.
4155 * @param requestCode If >= 0, this code will be returned in
4156 * onActivityResult() when the activity exits.
4157 *
4158 * @throws android.content.ActivityNotFoundException
4159 *
George Mount0a778ed2013-12-13 13:35:36 -08004160 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07004161 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004162 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004163 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004164 }
4165
4166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 * Launch an activity for which you would like a result when it finished.
4168 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08004169 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07004170 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07004172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 * <p>Note that this method should only be used with Intent protocols
4174 * that are defined to return a result. In other protocols (such as
4175 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
4176 * not get the result when you expect. For example, if the activity you
4177 * are launching uses the singleTask launch mode, it will not run in your
4178 * task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004179 *
RoboErik55011652014-07-09 15:05:53 -07004180 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08004182 * activity, then your window will not be displayed until a result is
4183 * returned back from the started activity. This is to avoid visible
4184 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 * <p>This method throws {@link android.content.ActivityNotFoundException}
4187 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004188 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 * @param intent The intent to start.
4190 * @param requestCode If >= 0, this code will be returned in
4191 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004192 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004193 * See {@link android.content.Context#startActivity(Intent, Bundle)
4194 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004195 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004197 *
George Mount0a778ed2013-12-13 13:35:36 -08004198 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004200 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
4201 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 if (mParent == null) {
4203 Instrumentation.ActivityResult ar =
4204 mInstrumentation.execStartActivity(
4205 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004206 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 if (ar != null) {
4208 mMainThread.sendActivityResult(
4209 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
4210 ar.getResultData());
4211 }
4212 if (requestCode >= 0) {
4213 // If this start is requesting a result, we can avoid making
4214 // the activity visible until the result is received. Setting
4215 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4216 // activity hidden during this time, to avoid flickering.
4217 // This can only be done when a result is requested because
4218 // that guarantees we will get information back when the
4219 // activity is finished, no matter what happens to it.
4220 mStartedActivity = true;
4221 }
Adam Powell14874662013-07-18 19:42:41 -07004222
George Mount41725de2015-04-09 08:23:05 -07004223 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07004224 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004226 if (options != null) {
4227 mParent.startActivityFromChild(this, intent, requestCode, options);
4228 } else {
4229 // Note we want to go through this method for compatibility with
4230 // existing applications that may have overridden it.
4231 mParent.startActivityFromChild(this, intent, requestCode);
4232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 }
George Mount41725de2015-04-09 08:23:05 -07004234 }
4235
4236 /**
4237 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
4238 *
4239 * @param options The ActivityOptions bundle used to start an Activity.
4240 */
4241 private void cancelInputsAndStartExitTransition(Bundle options) {
4242 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
4243 if (decor != null) {
4244 decor.cancelPendingInputEvents();
4245 }
George Mountff243282014-07-07 16:12:07 -07004246 if (options != null && !isTopOfTask()) {
4247 mActivityTransitionState.startExitOutTransition(this, options);
4248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 }
4250
4251 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004252 * @hide Implement to provide correct calling token.
4253 */
Alexandra Gherghina83231452014-07-04 12:05:20 +01004254 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
4255 startActivityForResultAsUser(intent, requestCode, null, user);
4256 }
4257
4258 /**
4259 * @hide Implement to provide correct calling token.
4260 */
4261 public void startActivityForResultAsUser(Intent intent, int requestCode,
4262 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004263 if (mParent != null) {
4264 throw new RuntimeException("Can't be called from a child");
4265 }
4266 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
4267 this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode,
4268 options, user);
4269 if (ar != null) {
4270 mMainThread.sendActivityResult(
4271 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
4272 }
4273 if (requestCode >= 0) {
4274 // If this start is requesting a result, we can avoid making
4275 // the activity visible until the result is received. Setting
4276 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4277 // activity hidden during this time, to avoid flickering.
4278 // This can only be done when a result is requested because
4279 // that guarantees we will get information back when the
4280 // activity is finished, no matter what happens to it.
4281 mStartedActivity = true;
4282 }
4283
George Mount41725de2015-04-09 08:23:05 -07004284 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004285 }
4286
4287 /**
4288 * @hide Implement to provide correct calling token.
4289 */
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004290 public void startActivityAsUser(Intent intent, UserHandle user) {
4291 startActivityAsUser(intent, null, user);
4292 }
4293
4294 /**
4295 * @hide Implement to provide correct calling token.
4296 */
4297 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
4298 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004299 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004300 }
4301 Instrumentation.ActivityResult ar =
4302 mInstrumentation.execStartActivity(
4303 this, mMainThread.getApplicationThread(), mToken, this,
4304 intent, -1, options, user);
4305 if (ar != null) {
4306 mMainThread.sendActivityResult(
4307 mToken, mEmbeddedID, -1, ar.getResultCode(),
4308 ar.getResultData());
4309 }
George Mount41725de2015-04-09 08:23:05 -07004310 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004311 }
4312
4313 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004314 * Start a new activity as if it was started by the activity that started our
4315 * current activity. This is for the resolver and chooser activities, which operate
4316 * as intermediaries that dispatch their intent to the target the user selects -- to
4317 * do this, they must perform all security checks including permission grants as if
4318 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004319 * @param intent The Intent to start.
4320 * @param options ActivityOptions or null.
4321 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
4322 * caller it is doing the start is, is actually allowed to start the target activity.
4323 * If you set this to true, you must set an explicit component in the Intent and do any
4324 * appropriate security checks yourself.
4325 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004326 * @hide
4327 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004328 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
4329 boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004330 if (mParent != null) {
4331 throw new RuntimeException("Can't be called from a child");
4332 }
4333 Instrumentation.ActivityResult ar =
4334 mInstrumentation.execStartActivityAsCaller(
4335 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004336 intent, -1, options, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004337 if (ar != null) {
4338 mMainThread.sendActivityResult(
4339 mToken, mEmbeddedID, -1, ar.getResultCode(),
4340 ar.getResultData());
4341 }
George Mount41725de2015-04-09 08:23:05 -07004342 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004343 }
4344
4345 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004346 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
4347 * Intent, int, int, int, Bundle)} with no options.
4348 *
4349 * @param intent The IntentSender to launch.
4350 * @param requestCode If >= 0, this code will be returned in
4351 * onActivityResult() when the activity exits.
4352 * @param fillInIntent If non-null, this will be provided as the
4353 * intent parameter to {@link IntentSender#sendIntent}.
4354 * @param flagsMask Intent flags in the original IntentSender that you
4355 * would like to change.
4356 * @param flagsValues Desired values for any bits set in
4357 * <var>flagsMask</var>
4358 * @param extraFlags Always set to 0.
4359 */
4360 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004361 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004362 throws IntentSender.SendIntentException {
4363 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
4364 flagsValues, extraFlags, null);
4365 }
4366
4367 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004368 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004369 * to use a IntentSender to describe the activity to be started. If
4370 * the IntentSender is for an activity, that activity will be started
4371 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
4372 * here; otherwise, its associated action will be executed (such as
4373 * sending a broadcast) as if you had called
4374 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07004375 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004376 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004377 * @param requestCode If >= 0, this code will be returned in
4378 * onActivityResult() when the activity exits.
4379 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004380 * intent parameter to {@link IntentSender#sendIntent}.
4381 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004382 * would like to change.
4383 * @param flagsValues Desired values for any bits set in
4384 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004385 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004386 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004387 * See {@link android.content.Context#startActivity(Intent, Bundle)
4388 * Context.startActivity(Intent, Bundle)} for more details. If options
4389 * have also been supplied by the IntentSender, options given here will
4390 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004391 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004392 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004393 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004394 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004395 if (mParent == null) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004396 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004397 flagsMask, flagsValues, this, options);
4398 } else if (options != null) {
4399 mParent.startIntentSenderFromChild(this, intent, requestCode,
4400 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004401 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004402 // Note we want to go through this call for compatibility with
4403 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004404 mParent.startIntentSenderFromChild(this, intent, requestCode,
4405 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004406 }
4407 }
4408
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004409 private void startIntentSenderForResultInner(IntentSender intent, int requestCode,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004410 Intent fillInIntent, int flagsMask, int flagsValues, Activity activity,
4411 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004412 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004413 try {
4414 String resolvedType = null;
4415 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004416 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004417 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004418 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
4419 }
4420 int result = ActivityManagerNative.getDefault()
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004421 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004422 fillInIntent, resolvedType, mToken, activity.mEmbeddedID,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004423 requestCode, flagsMask, flagsValues, options);
4424 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004425 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004426 }
4427 Instrumentation.checkStartActivityResult(result, null);
4428 } catch (RemoteException e) {
4429 }
4430 if (requestCode >= 0) {
4431 // If this start is requesting a result, we can avoid making
4432 // the activity visible until the result is received. Setting
4433 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4434 // activity hidden during this time, to avoid flickering.
4435 // This can only be done when a result is requested because
4436 // that guarantees we will get information back when the
4437 // activity is finished, no matter what happens to it.
4438 mStartedActivity = true;
4439 }
4440 }
4441
4442 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004443 * Same as {@link #startActivity(Intent, Bundle)} with no options
4444 * specified.
4445 *
4446 * @param intent The intent to start.
4447 *
4448 * @throws android.content.ActivityNotFoundException
4449 *
4450 * @see {@link #startActivity(Intent, Bundle)}
4451 * @see #startActivityForResult
4452 */
4453 @Override
4454 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00004455 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004456 }
4457
4458 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 * Launch a new activity. You will not receive any information about when
4460 * the activity exits. This implementation overrides the base version,
4461 * providing information about
4462 * the activity performing the launch. Because of this additional
4463 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4464 * required; if not specified, the new activity will be added to the
4465 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07004466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 * <p>This method throws {@link android.content.ActivityNotFoundException}
4468 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004469 *
4470 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004471 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004472 * See {@link android.content.Context#startActivity(Intent, Bundle)
4473 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004474 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004476 *
4477 * @see {@link #startActivity(Intent)}
RoboErik55011652014-07-09 15:05:53 -07004478 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 */
4480 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004481 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004482 if (options != null) {
4483 startActivityForResult(intent, -1, options);
4484 } else {
4485 // Note we want to go through this call for compatibility with
4486 // applications that may have overridden the method.
4487 startActivityForResult(intent, -1);
4488 }
4489 }
4490
4491 /**
4492 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4493 * specified.
4494 *
4495 * @param intents The intents to start.
4496 *
4497 * @throws android.content.ActivityNotFoundException
4498 *
4499 * @see {@link #startActivities(Intent[], Bundle)}
4500 * @see #startActivityForResult
4501 */
4502 @Override
4503 public void startActivities(Intent[] intents) {
4504 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 }
4506
4507 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004508 * Launch a new activity. You will not receive any information about when
4509 * the activity exits. This implementation overrides the base version,
4510 * providing information about
4511 * the activity performing the launch. Because of this additional
4512 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4513 * required; if not specified, the new activity will be added to the
4514 * task of the caller.
4515 *
4516 * <p>This method throws {@link android.content.ActivityNotFoundException}
4517 * if there was no Activity found to run the given Intent.
4518 *
4519 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004520 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004521 * See {@link android.content.Context#startActivity(Intent, Bundle)
4522 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004523 *
4524 * @throws android.content.ActivityNotFoundException
4525 *
Dianne Hackborna4972e92012-03-14 10:38:05 -07004526 * @see {@link #startActivities(Intent[])}
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004527 * @see #startActivityForResult
4528 */
4529 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004530 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004531 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004532 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004533 }
4534
4535 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004536 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4537 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004538 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004539 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004540 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004541 * intent parameter to {@link IntentSender#sendIntent}.
4542 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004543 * would like to change.
4544 * @param flagsValues Desired values for any bits set in
4545 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004546 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004547 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004548 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004549 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004550 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004551 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
4552 extraFlags, null);
4553 }
4554
4555 /**
4556 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
4557 * to start; see
4558 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
4559 * for more information.
4560 *
4561 * @param intent The IntentSender to launch.
4562 * @param fillInIntent If non-null, this will be provided as the
4563 * intent parameter to {@link IntentSender#sendIntent}.
4564 * @param flagsMask Intent flags in the original IntentSender that you
4565 * would like to change.
4566 * @param flagsValues Desired values for any bits set in
4567 * <var>flagsMask</var>
4568 * @param extraFlags Always set to 0.
4569 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004570 * See {@link android.content.Context#startActivity(Intent, Bundle)
4571 * Context.startActivity(Intent, Bundle)} for more details. If options
4572 * have also been supplied by the IntentSender, options given here will
4573 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004574 */
4575 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004576 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004577 Bundle options) throws IntentSender.SendIntentException {
4578 if (options != null) {
4579 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4580 flagsValues, extraFlags, options);
4581 } else {
4582 // Note we want to go through this call for compatibility with
4583 // applications that may have overridden the method.
4584 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4585 flagsValues, extraFlags);
4586 }
4587 }
4588
4589 /**
4590 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
4591 * with no options.
4592 *
4593 * @param intent The intent to start.
4594 * @param requestCode If >= 0, this code will be returned in
4595 * onActivityResult() when the activity exits, as described in
4596 * {@link #startActivityForResult}.
4597 *
4598 * @return If a new activity was launched then true is returned; otherwise
4599 * false is returned and you must handle the Intent yourself.
4600 *
4601 * @see #startActivity
4602 * @see #startActivityForResult
4603 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004604 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4605 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004606 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004607 }
4608
4609 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004610 * A special variation to launch an activity only if a new activity
4611 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07004612 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07004614 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004615 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07004616 * and the activity
4617 * that handles <var>intent</var> is the same as your currently running
4618 * activity, then a new instance is not needed. In this case, instead of
4619 * the normal behavior of calling {@link #onNewIntent} this function will
4620 * return and you can handle the Intent yourself.
4621 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 * <p>This function can only be called from a top-level activity; if it is
4623 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07004624 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 * @param intent The intent to start.
4626 * @param requestCode If >= 0, this code will be returned in
4627 * onActivityResult() when the activity exits, as described in
4628 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004629 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004630 * See {@link android.content.Context#startActivity(Intent, Bundle)
4631 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004632 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004633 * @return If a new activity was launched then true is returned; otherwise
4634 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07004635 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 * @see #startActivity
4637 * @see #startActivityForResult
4638 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004639 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4640 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004642 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004643 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004644 Uri referrer = onProvideReferrer();
4645 if (referrer != null) {
4646 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4647 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004648 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004649 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004650 result = ActivityManagerNative.getDefault()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08004651 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07004652 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
4653 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
4654 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 } catch (RemoteException e) {
4656 // Empty
4657 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08004658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004659 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661 if (requestCode >= 0) {
4662 // If this start is requesting a result, we can avoid making
4663 // the activity visible until the result is received. Setting
4664 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4665 // activity hidden during this time, to avoid flickering.
4666 // This can only be done when a result is requested because
4667 // that guarantees we will get information back when the
4668 // activity is finished, no matter what happens to it.
4669 mStartedActivity = true;
4670 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004671 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 }
4673
4674 throw new UnsupportedOperationException(
4675 "startActivityIfNeeded can only be called from a top-level activity");
4676 }
4677
4678 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004679 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
4680 * no options.
4681 *
4682 * @param intent The intent to dispatch to the next activity. For
4683 * correct behavior, this must be the same as the Intent that started
4684 * your own activity; the only changes you can make are to the extras
4685 * inside of it.
4686 *
4687 * @return Returns a boolean indicating whether there was another Activity
4688 * to start: true if there was a next activity to start, false if there
4689 * wasn't. In general, if true is returned you will then want to call
4690 * finish() on yourself.
4691 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004692 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004693 return startNextMatchingActivity(intent, null);
4694 }
4695
4696 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 * Special version of starting an activity, for use when you are replacing
4698 * other activity components. You can use this to hand the Intent off
4699 * to the next Activity that can handle it. You typically call this in
4700 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07004701 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004702 * @param intent The intent to dispatch to the next activity. For
4703 * correct behavior, this must be the same as the Intent that started
4704 * your own activity; the only changes you can make are to the extras
4705 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004706 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004707 * See {@link android.content.Context#startActivity(Intent, Bundle)
4708 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004709 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 * @return Returns a boolean indicating whether there was another Activity
4711 * to start: true if there was a next activity to start, false if there
4712 * wasn't. In general, if true is returned you will then want to call
4713 * finish() on yourself.
4714 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004715 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
4716 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 if (mParent == null) {
4718 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004719 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004720 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004721 return ActivityManagerNative.getDefault()
Dianne Hackborna4972e92012-03-14 10:38:05 -07004722 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723 } catch (RemoteException e) {
4724 // Empty
4725 }
4726 return false;
4727 }
4728
4729 throw new UnsupportedOperationException(
4730 "startNextMatchingActivity can only be called from a top-level activity");
4731 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004732
4733 /**
4734 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
4735 * with no options.
4736 *
4737 * @param child The activity making the call.
4738 * @param intent The intent to start.
4739 * @param requestCode Reply request code. < 0 if reply is not requested.
4740 *
4741 * @throws android.content.ActivityNotFoundException
4742 *
4743 * @see #startActivity
4744 * @see #startActivityForResult
4745 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004746 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004747 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004748 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004749 }
4750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004751 /**
RoboErik55011652014-07-09 15:05:53 -07004752 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004753 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07004754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 * <p>This method throws {@link android.content.ActivityNotFoundException}
4756 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004757 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 * @param child The activity making the call.
4759 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004760 * @param requestCode Reply request code. < 0 if reply is not requested.
4761 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004762 * See {@link android.content.Context#startActivity(Intent, Bundle)
4763 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004764 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004765 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004766 *
4767 * @see #startActivity
4768 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004769 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004770 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004771 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004772 Instrumentation.ActivityResult ar =
4773 mInstrumentation.execStartActivity(
4774 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004775 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004776 if (ar != null) {
4777 mMainThread.sendActivityResult(
4778 mToken, child.mEmbeddedID, requestCode,
4779 ar.getResultCode(), ar.getResultData());
4780 }
George Mount41725de2015-04-09 08:23:05 -07004781 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004782 }
4783
4784 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004785 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
4786 * with no options.
4787 *
4788 * @param fragment The fragment making the call.
4789 * @param intent The intent to start.
4790 * @param requestCode Reply request code. < 0 if reply is not requested.
4791 *
4792 * @throws android.content.ActivityNotFoundException
4793 *
4794 * @see Fragment#startActivity
4795 * @see Fragment#startActivityForResult
4796 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004797 public void startActivityFromFragment(@NonNull Fragment fragment,
4798 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004799 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004800 }
4801
4802 /**
RoboErik55011652014-07-09 15:05:53 -07004803 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004804 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
4805 * method.
RoboErik55011652014-07-09 15:05:53 -07004806 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004807 * <p>This method throws {@link android.content.ActivityNotFoundException}
4808 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004809 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004810 * @param fragment The fragment making the call.
4811 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07004812 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004813 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004814 * See {@link android.content.Context#startActivity(Intent, Bundle)
4815 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004816 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004817 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004818 *
4819 * @see Fragment#startActivity
4820 * @see Fragment#startActivityForResult
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004821 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004822 public void startActivityFromFragment(@NonNull Fragment fragment,
4823 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004824 startActivityForResult(fragment.mWho, intent, requestCode, options);
4825 }
4826
4827 /**
4828 * @hide
4829 */
4830 @Override
4831 public void startActivityForResult(
4832 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004833 Uri referrer = onProvideReferrer();
4834 if (referrer != null) {
4835 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4836 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004837 Instrumentation.ActivityResult ar =
4838 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004839 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004840 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004841 if (ar != null) {
4842 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004843 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004844 ar.getResultCode(), ar.getResultData());
4845 }
George Mount41725de2015-04-09 08:23:05 -07004846 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004847 }
4848
4849 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004850 * @hide
4851 */
4852 @Override
4853 public boolean canStartActivityForResult() {
4854 return true;
4855 }
4856
4857 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004858 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
4859 * int, Intent, int, int, int, Bundle)} with no options.
4860 */
4861 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4862 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
4863 int extraFlags)
4864 throws IntentSender.SendIntentException {
4865 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
4866 flagsMask, flagsValues, extraFlags, null);
4867 }
4868
4869 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004870 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004871 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07004872 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004873 * for more information.
4874 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004875 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4876 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07004877 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004878 throws IntentSender.SendIntentException {
4879 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004880 flagsMask, flagsValues, child, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004881 }
4882
4883 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004884 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
4885 * or {@link #finish} to specify an explicit transition animation to
4886 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004887 *
4888 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
4889 * to using this with starting activities is to supply the desired animation
4890 * information through a {@link ActivityOptions} bundle to
4891 * {@link #startActivity(Intent, Bundle) or a related function. This allows
4892 * you to specify a custom animation even when starting an activity from
4893 * outside the context of the current top activity.
4894 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004895 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004896 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004897 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004898 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004899 */
4900 public void overridePendingTransition(int enterAnim, int exitAnim) {
4901 try {
4902 ActivityManagerNative.getDefault().overridePendingTransition(
4903 mToken, getPackageName(), enterAnim, exitAnim);
4904 } catch (RemoteException e) {
4905 }
4906 }
RoboErik55011652014-07-09 15:05:53 -07004907
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004908 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 * Call this to set the result that your activity will return to its
4910 * caller.
RoboErik55011652014-07-09 15:05:53 -07004911 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 * @param resultCode The result code to propagate back to the originating
4913 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07004914 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004915 * @see #RESULT_CANCELED
4916 * @see #RESULT_OK
4917 * @see #RESULT_FIRST_USER
4918 * @see #setResult(int, Intent)
4919 */
4920 public final void setResult(int resultCode) {
4921 synchronized (this) {
4922 mResultCode = resultCode;
4923 mResultData = null;
4924 }
4925 }
4926
4927 /**
4928 * Call this to set the result that your activity will return to its
4929 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004930 *
4931 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
4932 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
4933 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
4934 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
4935 * Activity receiving the result access to the specific URIs in the Intent.
4936 * Access will remain until the Activity has finished (it will remain across the hosting
4937 * process being killed and other temporary destruction) and will be added
4938 * to any existing set of URI permissions it already holds.
4939 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 * @param resultCode The result code to propagate back to the originating
4941 * activity, often RESULT_CANCELED or RESULT_OK
4942 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07004943 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 * @see #RESULT_CANCELED
4945 * @see #RESULT_OK
4946 * @see #RESULT_FIRST_USER
4947 * @see #setResult(int)
4948 */
4949 public final void setResult(int resultCode, Intent data) {
4950 synchronized (this) {
4951 mResultCode = resultCode;
4952 mResultData = data;
4953 }
4954 }
4955
4956 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004957 * Return information about who launched this activity. If the launching Intent
4958 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
4959 * that will be returned as-is; otherwise, if known, an
4960 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
4961 * package name that started the Intent will be returned. This may return null if no
4962 * referrer can be identified -- it is neither explicitly specified, nor is it known which
4963 * application package was involved.
4964 *
4965 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
4966 * return the referrer that submitted that new intent to the activity. Otherwise, it
4967 * always returns the referrer of the original Intent.</p>
4968 *
4969 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
4970 * referrer information, applications can spoof it.</p>
4971 */
4972 @Nullable
4973 public Uri getReferrer() {
4974 Intent intent = getIntent();
4975 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
4976 if (referrer != null) {
4977 return referrer;
4978 }
4979 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
4980 if (referrerName != null) {
4981 return Uri.parse(referrerName);
4982 }
4983 if (mReferrer != null) {
4984 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
4985 }
4986 return null;
4987 }
4988
4989 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004990 * Override to generate the desired referrer for the content currently being shown
4991 * by the app. The default implementation returns null, meaning the referrer will simply
4992 * be the android-app: of the package name of this activity. Return a non-null Uri to
4993 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
4994 */
4995 public Uri onProvideReferrer() {
4996 return null;
4997 }
4998
4999 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 * Return the name of the package that invoked this activity. This is who
5001 * the data in {@link #setResult setResult()} will be sent to. You can
5002 * use this information to validate that the recipient is allowed to
5003 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005004 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005005 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005006 * did not use the {@link #startActivityForResult}
5007 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005008 * null.</p>
5009 *
5010 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5011 * the result from this method was unstable. If the process hosting the calling
5012 * package was no longer running, it would return null instead of the proper package
5013 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5014 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005015 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 * @return The package of the activity that will receive your
5017 * reply, or null if none.
5018 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005019 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 public String getCallingPackage() {
5021 try {
5022 return ActivityManagerNative.getDefault().getCallingPackage(mToken);
5023 } catch (RemoteException e) {
5024 return null;
5025 }
5026 }
5027
5028 /**
5029 * Return the name of the activity that invoked this activity. This is
5030 * who the data in {@link #setResult setResult()} will be sent to. You
5031 * can use this information to validate that the recipient is allowed to
5032 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005033 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005034 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005035 * did not use the {@link #startActivityForResult}
5036 * form that includes a request code), then the calling package will be
5037 * null.
5038 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005039 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 * reply, or null if none.
5041 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005042 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 public ComponentName getCallingActivity() {
5044 try {
5045 return ActivityManagerNative.getDefault().getCallingActivity(mToken);
5046 } catch (RemoteException e) {
5047 return null;
5048 }
5049 }
5050
5051 /**
5052 * Control whether this activity's main window is visible. This is intended
5053 * only for the special case of an activity that is not going to show a
5054 * UI itself, but can't just finish prior to onResume() because it needs
5055 * to wait for a service binding or such. Setting this to false allows
5056 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005057 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 * <p>The default value for this is taken from the
5059 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
5060 */
5061 public void setVisible(boolean visible) {
5062 if (mVisibleFromClient != visible) {
5063 mVisibleFromClient = visible;
5064 if (mVisibleFromServer) {
5065 if (visible) makeVisible();
5066 else mDecor.setVisibility(View.INVISIBLE);
5067 }
5068 }
5069 }
RoboErik55011652014-07-09 15:05:53 -07005070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 void makeVisible() {
5072 if (!mWindowAdded) {
5073 ViewManager wm = getWindowManager();
5074 wm.addView(mDecor, getWindow().getAttributes());
5075 mWindowAdded = true;
5076 }
5077 mDecor.setVisibility(View.VISIBLE);
5078 }
RoboErik55011652014-07-09 15:05:53 -07005079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 /**
5081 * Check to see whether this activity is in the process of finishing,
5082 * either because you called {@link #finish} on it or someone else
5083 * has requested that it finished. This is often used in
5084 * {@link #onPause} to determine whether the activity is simply pausing or
5085 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07005086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07005088 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 * @see #finish
5090 */
5091 public boolean isFinishing() {
5092 return mFinished;
5093 }
5094
5095 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07005096 * Returns true if the final {@link #onDestroy()} call has been made
5097 * on the Activity, so this instance is now dead.
5098 */
5099 public boolean isDestroyed() {
5100 return mDestroyed;
5101 }
5102
5103 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005104 * Check to see whether this activity is in the process of being destroyed in order to be
5105 * recreated with a new configuration. This is often used in
5106 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
5107 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07005108 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005109 * @return If the activity is being torn down in order to be recreated with a new configuration,
5110 * returns true; else returns false.
5111 */
5112 public boolean isChangingConfigurations() {
5113 return mChangingConfigurations;
5114 }
5115
5116 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005117 * Cause this Activity to be recreated with a new instance. This results
5118 * in essentially the same flow as when the Activity is created due to
5119 * a configuration change -- the current instance will go through its
5120 * lifecycle to {@link #onDestroy} and a new instance then created after it.
5121 */
5122 public void recreate() {
5123 if (mParent != null) {
5124 throw new IllegalStateException("Can only be called on top-level activity");
5125 }
5126 if (Looper.myLooper() != mMainThread.getLooper()) {
5127 throw new IllegalStateException("Must be called from main thread");
5128 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07005129 mMainThread.requestRelaunchActivity(mToken, null, null, 0, false, null, null, false,
5130 false /* preserveWindow */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005131 }
5132
5133 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005134 * Finishes the current activity and specifies whether to remove the task associated with this
5135 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005137 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005138 if (mParent == null) {
5139 int resultCode;
5140 Intent resultData;
5141 synchronized (this) {
5142 resultCode = mResultCode;
5143 resultData = mResultData;
5144 }
Joe Onorato43a17652011-04-06 19:22:23 -07005145 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005147 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005148 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 if (ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07005151 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005152 mFinished = true;
5153 }
5154 } catch (RemoteException e) {
5155 // Empty
5156 }
5157 } else {
5158 mParent.finishFromChild(this);
5159 }
5160 }
5161
5162 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005163 * Call this when your activity is done and should be closed. The
5164 * ActivityResult is propagated back to whoever launched you via
5165 * onActivityResult().
5166 */
5167 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005168 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005169 }
5170
5171 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005172 * Finish this activity as well as all activities immediately below it
5173 * in the current task that have the same affinity. This is typically
5174 * used when an application can be launched on to another task (such as
5175 * from an ACTION_VIEW of a content type it understands) and the user
5176 * has used the up navigation to switch out of the current task and in
5177 * to its own task. In this case, if the user has navigated down into
5178 * any other activities of the second application, all of those should
5179 * be removed from the original task as part of the task switch.
5180 *
5181 * <p>Note that this finish does <em>not</em> allow you to deliver results
5182 * to the previous activity, and an exception will be thrown if you are trying
5183 * to do so.</p>
5184 */
5185 public void finishAffinity() {
5186 if (mParent != null) {
5187 throw new IllegalStateException("Can not be called from an embedded activity");
5188 }
5189 if (mResultCode != RESULT_CANCELED || mResultData != null) {
5190 throw new IllegalStateException("Can not be called to deliver a result");
5191 }
5192 try {
5193 if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) {
5194 mFinished = true;
5195 }
5196 } catch (RemoteException e) {
5197 // Empty
5198 }
5199 }
5200
5201 /**
RoboErik55011652014-07-09 15:05:53 -07005202 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 * {@link #finish} method. The default implementation simply calls
5204 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07005205 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005206 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07005207 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 * @see #finish
5209 */
5210 public void finishFromChild(Activity child) {
5211 finish();
5212 }
5213
5214 /**
George Mountcb4b7d92014-02-25 10:47:55 -08005215 * Reverses the Activity Scene entry Transition and triggers the calling Activity
5216 * to reverse its exit Transition. When the exit Transition completes,
5217 * {@link #finish()} is called. If no entry Transition was used, finish() is called
5218 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07005219 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08005220 */
Craig Mautner73f843d2014-05-19 09:42:28 -07005221 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07005222 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07005223 finish();
5224 }
George Mountcb4b7d92014-02-25 10:47:55 -08005225 }
5226
5227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 * Force finish another activity that you had previously started with
5229 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07005230 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 * @param requestCode The request code of the activity that you had
5232 * given to startActivityForResult(). If there are multiple
5233 * activities started with this request code, they
5234 * will all be finished.
5235 */
5236 public void finishActivity(int requestCode) {
5237 if (mParent == null) {
5238 try {
5239 ActivityManagerNative.getDefault()
5240 .finishSubActivity(mToken, mEmbeddedID, requestCode);
5241 } catch (RemoteException e) {
5242 // Empty
5243 }
5244 } else {
5245 mParent.finishActivityFromChild(this, requestCode);
5246 }
5247 }
5248
5249 /**
5250 * This is called when a child activity of this one calls its
5251 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07005252 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 * @param child The activity making the call.
5254 * @param requestCode Request code that had been used to start the
5255 * activity.
5256 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005257 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 try {
5259 ActivityManagerNative.getDefault()
5260 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
5261 } catch (RemoteException e) {
5262 // Empty
5263 }
5264 }
5265
5266 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005267 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005268 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07005269 */
5270 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005271 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005272 }
5273
5274 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005275 * Ask that the local app instance of this activity be released to free up its memory.
5276 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
5277 * a new instance of the activity will later be re-created if needed due to the user
5278 * navigating back to it.
5279 *
5280 * @return Returns true if the activity was in a state that it has started the process
5281 * of destroying its current instance; returns false if for any reason this could not
5282 * be done: it is currently visible to the user, it is already being destroyed, it is
5283 * being finished, it hasn't yet saved its state, etc.
5284 */
5285 public boolean releaseInstance() {
5286 try {
5287 return ActivityManagerNative.getDefault().releaseActivityInstance(mToken);
5288 } catch (RemoteException e) {
5289 // Empty
5290 }
5291 return false;
5292 }
5293
5294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 * Called when an activity you launched exits, giving you the requestCode
5296 * you started it with, the resultCode it returned, and any additional
5297 * data from it. The <var>resultCode</var> will be
5298 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
5299 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07005300 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 * <p>You will receive this call immediately before onResume() when your
5302 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07005303 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07005304 * <p>This method is never invoked if your activity sets
5305 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5306 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08005307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 * @param requestCode The integer request code originally supplied to
5309 * startActivityForResult(), allowing you to identify who this
5310 * result came from.
5311 * @param resultCode The integer result code returned by the child activity
5312 * through its setResult().
5313 * @param data An Intent, which can return result data to the caller
5314 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07005315 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 * @see #startActivityForResult
5317 * @see #createPendingResult
5318 * @see #setResult(int)
5319 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005320 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 }
5322
5323 /**
George Mount62ab9b72014-05-02 13:51:17 -07005324 * Called when an activity you launched with an activity transition exposes this
5325 * Activity through a returning activity transition, giving you the resultCode
5326 * and any additional data from it. This method will only be called if the activity
5327 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07005328 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07005329 *
5330 * <p>The purpose of this function is to let the called Activity send a hint about
5331 * its state so that this underlying Activity can prepare to be exposed. A call to
5332 * this method does not guarantee that the called Activity has or will be exiting soon.
5333 * It only indicates that it will expose this Activity's Window and it has
5334 * some data to pass to prepare it.</p>
5335 *
5336 * @param resultCode The integer result code returned by the child activity
5337 * through its setResult().
5338 * @param data An Intent, which can return result data to the caller
5339 * (various data can be attached to Intent "extras").
5340 */
Craig Mautner7a629c22014-09-04 14:57:04 -07005341 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07005342 }
5343
5344 /**
RoboErik55011652014-07-09 15:05:53 -07005345 * Create a new PendingIntent object which you can hand to others
5346 * for them to use to send result data back to your
5347 * {@link #onActivityResult} callback. The created object will be either
5348 * one-shot (becoming invalid after a result is sent back) or multiple
5349 * (allowing any number of results to be sent through it).
5350 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 * @param requestCode Private request code for the sender that will be
5352 * associated with the result data when it is returned. The sender can not
5353 * modify this value, allowing you to identify incoming results.
5354 * @param data Default data to supply in the result, which may be modified
5355 * by the sender.
5356 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
5357 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
5358 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
5359 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
5360 * or any of the flags as supported by
5361 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
5362 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07005363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 * @return Returns an existing or new PendingIntent matching the given
5365 * parameters. May return null only if
5366 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
5367 * supplied.
RoboErik55011652014-07-09 15:05:53 -07005368 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369 * @see PendingIntent
5370 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005371 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
5372 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 String packageName = getPackageName();
5374 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005375 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 IIntentSender target =
5377 ActivityManagerNative.getDefault().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07005378 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07005380 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
5381 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 return target != null ? new PendingIntent(target) : null;
5383 } catch (RemoteException e) {
5384 // Empty
5385 }
5386 return null;
5387 }
5388
5389 /**
5390 * Change the desired orientation of this activity. If the activity
5391 * is currently in the foreground or otherwise impacting the screen
5392 * orientation, the screen will immediately be changed (possibly causing
5393 * the activity to be restarted). Otherwise, this will be used the next
5394 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07005395 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 * @param requestedOrientation An orientation constant as used in
5397 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5398 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005399 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 if (mParent == null) {
5401 try {
5402 ActivityManagerNative.getDefault().setRequestedOrientation(
5403 mToken, requestedOrientation);
5404 } catch (RemoteException e) {
5405 // Empty
5406 }
5407 } else {
5408 mParent.setRequestedOrientation(requestedOrientation);
5409 }
5410 }
RoboErik55011652014-07-09 15:05:53 -07005411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 /**
5413 * Return the current requested orientation of the activity. This will
5414 * either be the orientation requested in its component's manifest, or
5415 * the last requested orientation given to
5416 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07005417 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005418 * @return Returns an orientation constant as used in
5419 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5420 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005421 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 public int getRequestedOrientation() {
5423 if (mParent == null) {
5424 try {
5425 return ActivityManagerNative.getDefault()
5426 .getRequestedOrientation(mToken);
5427 } catch (RemoteException e) {
5428 // Empty
5429 }
5430 } else {
5431 return mParent.getRequestedOrientation();
5432 }
5433 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
5434 }
RoboErik55011652014-07-09 15:05:53 -07005435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005436 /**
5437 * Return the identifier of the task this activity is in. This identifier
5438 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07005439 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005440 * @return Task identifier, an opaque integer.
5441 */
5442 public int getTaskId() {
5443 try {
5444 return ActivityManagerNative.getDefault()
5445 .getTaskForActivity(mToken, false);
5446 } catch (RemoteException e) {
5447 return -1;
5448 }
5449 }
5450
5451 /**
5452 * Return whether this activity is the root of a task. The root is the
5453 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07005454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005455 * @return True if this is the root activity, else false.
5456 */
5457 public boolean isTaskRoot() {
5458 try {
Filip Gruszczynski3d026712015-12-16 13:46:38 -08005459 return ActivityManagerNative.getDefault().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 } catch (RemoteException e) {
5461 return false;
5462 }
5463 }
5464
5465 /**
5466 * Move the task containing this activity to the back of the activity
5467 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07005468 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005469 * @param nonRoot If false then this only works if the activity is the root
5470 * of a task; if true it will work for any activity in
5471 * a task.
RoboErik55011652014-07-09 15:05:53 -07005472 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 * @return If the task was moved (or it was already at the
5474 * back) true is returned, else false.
5475 */
5476 public boolean moveTaskToBack(boolean nonRoot) {
5477 try {
5478 return ActivityManagerNative.getDefault().moveActivityTaskToBack(
5479 mToken, nonRoot);
5480 } catch (RemoteException e) {
5481 // Empty
5482 }
5483 return false;
5484 }
5485
5486 /**
5487 * Returns class name for this activity with the package prefix removed.
5488 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07005489 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 * @return The local class name.
5491 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005492 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 public String getLocalClassName() {
5494 final String pkg = getPackageName();
5495 final String cls = mComponent.getClassName();
5496 int packageLen = pkg.length();
5497 if (!cls.startsWith(pkg) || cls.length() <= packageLen
5498 || cls.charAt(packageLen) != '.') {
5499 return cls;
5500 }
5501 return cls.substring(packageLen+1);
5502 }
RoboErik55011652014-07-09 15:05:53 -07005503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 /**
5505 * Returns complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07005506 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005507 * @return Returns the complete component name for this activity
5508 */
5509 public ComponentName getComponentName()
5510 {
5511 return mComponent;
5512 }
5513
5514 /**
5515 * Retrieve a {@link SharedPreferences} object for accessing preferences
5516 * that are private to this activity. This simply calls the underlying
5517 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
5518 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07005519 *
5520 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07005521 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 *
5523 * @return Returns the single SharedPreferences instance that can be used
5524 * to retrieve and modify the preference values.
5525 */
5526 public SharedPreferences getPreferences(int mode) {
5527 return getSharedPreferences(getLocalClassName(), mode);
5528 }
RoboErik55011652014-07-09 15:05:53 -07005529
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005530 private void ensureSearchManager() {
5531 if (mSearchManager != null) {
5532 return;
5533 }
RoboErik55011652014-07-09 15:05:53 -07005534
Amith Yamasanie9ce3f02010-01-25 09:15:50 -08005535 mSearchManager = new SearchManager(this, null);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005536 }
Tor Norbyed9273d62013-05-30 15:59:53 -07005537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005539 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005540 if (getBaseContext() == null) {
5541 throw new IllegalStateException(
5542 "System services not available to Activities before onCreate()");
5543 }
5544
5545 if (WINDOW_SERVICE.equals(name)) {
5546 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005547 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005548 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005549 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 }
5551 return super.getSystemService(name);
5552 }
5553
5554 /**
5555 * Change the title associated with this activity. If this is a
5556 * top-level activity, the title for its window will change. If it
5557 * is an embedded activity, the parent can do whatever it wants
5558 * with it.
5559 */
5560 public void setTitle(CharSequence title) {
5561 mTitle = title;
5562 onTitleChanged(title, mTitleColor);
5563
5564 if (mParent != null) {
5565 mParent.onChildTitleChanged(this, title);
5566 }
5567 }
5568
5569 /**
5570 * Change the title associated with this activity. If this is a
5571 * top-level activity, the title for its window will change. If it
5572 * is an embedded activity, the parent can do whatever it wants
5573 * with it.
5574 */
5575 public void setTitle(int titleId) {
5576 setTitle(getText(titleId));
5577 }
5578
Alan Viverette2525d9c2013-11-15 14:42:19 -08005579 /**
5580 * Change the color of the title associated with this activity.
5581 * <p>
5582 * This method is deprecated starting in API Level 11 and replaced by action
5583 * bar styles. For information on styling the Action Bar, read the <a
5584 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
5585 * guide.
5586 *
5587 * @deprecated Use action bar styles instead.
5588 */
5589 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590 public void setTitleColor(int textColor) {
5591 mTitleColor = textColor;
5592 onTitleChanged(mTitle, textColor);
5593 }
5594
5595 public final CharSequence getTitle() {
5596 return mTitle;
5597 }
5598
5599 public final int getTitleColor() {
5600 return mTitleColor;
5601 }
5602
5603 protected void onTitleChanged(CharSequence title, int color) {
5604 if (mTitleReady) {
5605 final Window win = getWindow();
5606 if (win != null) {
5607 win.setTitle(title);
5608 if (color != 0) {
5609 win.setTitleColor(color);
5610 }
5611 }
Adam Powellaf2d8592014-08-26 18:06:40 -07005612 if (mActionBar != null) {
5613 mActionBar.setWindowTitle(title);
5614 }
Adam Powella557fdc2014-08-21 18:05:53 -07005615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 }
5617
5618 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
5619 }
5620
5621 /**
Winson Chunga449dc02014-05-16 11:15:04 -07005622 * Sets information describing the task with this activity for presentation inside the Recents
5623 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
5624 * are traversed in order from the topmost activity to the bottommost. The traversal continues
5625 * for each property until a suitable value is found. For each task the taskDescription will be
5626 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07005627 *
5628 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07005629 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07005630 *
Winson Chunga449dc02014-05-16 11:15:04 -07005631 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07005632 */
Winson Chunga449dc02014-05-16 11:15:04 -07005633 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08005634 if (mTaskDescription != taskDescription) {
5635 mTaskDescription.copyFrom(taskDescription);
5636 // Scale the icon down to something reasonable if it is provided
5637 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
5638 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
5639 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
5640 true);
5641 mTaskDescription.setIcon(icon);
5642 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07005643 }
5644 try {
Winsonb6403152016-02-23 13:32:09 -08005645 ActivityManagerNative.getDefault().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07005646 } catch (RemoteException e) {
5647 }
5648 }
5649
5650 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 * Sets the visibility of the progress bar in the title.
5652 * <p>
5653 * In order for the progress bar to be shown, the feature must be requested
5654 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005655 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005657 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005659 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 public final void setProgressBarVisibility(boolean visible) {
5661 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
5662 Window.PROGRESS_VISIBILITY_OFF);
5663 }
5664
5665 /**
5666 * Sets the visibility of the indeterminate progress bar in the title.
5667 * <p>
5668 * In order for the progress bar to be shown, the feature must be requested
5669 * via {@link #requestWindowFeature(int)}.
5670 *
5671 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005672 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005673 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005674 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675 public final void setProgressBarIndeterminateVisibility(boolean visible) {
5676 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
5677 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
5678 }
RoboErik55011652014-07-09 15:05:53 -07005679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 /**
5681 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
5682 * is always indeterminate).
5683 * <p>
5684 * In order for the progress bar to be shown, the feature must be requested
5685 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005688 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005690 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 public final void setProgressBarIndeterminate(boolean indeterminate) {
5692 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00005693 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
5694 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 }
RoboErik55011652014-07-09 15:05:53 -07005696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697 /**
5698 * Sets the progress for the progress bars in the title.
5699 * <p>
5700 * In order for the progress bar to be shown, the feature must be requested
5701 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005702 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 * @param progress The progress for the progress bar. Valid ranges are from
5704 * 0 to 10000 (both inclusive). If 10000 is given, the progress
5705 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005706 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005708 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 public final void setProgress(int progress) {
5710 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
5711 }
RoboErik55011652014-07-09 15:05:53 -07005712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 /**
5714 * Sets the secondary progress for the progress bar in the title. This
5715 * progress is drawn between the primary progress (set via
5716 * {@link #setProgress(int)} and the background. It can be ideal for media
5717 * scenarios such as showing the buffering progress while the default
5718 * progress shows the play progress.
5719 * <p>
5720 * In order for the progress bar to be shown, the feature must be requested
5721 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005722 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
5724 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04005725 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005726 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005727 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 public final void setSecondaryProgress(int secondaryProgress) {
5729 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
5730 secondaryProgress + Window.PROGRESS_SECONDARY_START);
5731 }
5732
5733 /**
5734 * Suggests an audio stream whose volume should be changed by the hardware
5735 * volume controls.
5736 * <p>
5737 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07005738 * Volume requests which are received while the Activity is in the
5739 * foreground will affect this stream.
5740 * <p>
5741 * It is not guaranteed that the hardware volume controls will always change
5742 * this stream's volume (for example, if a call is in progress, its stream's
5743 * volume may be changed instead). To reset back to the default, use
5744 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
5745 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07005747 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 */
5749 public final void setVolumeControlStream(int streamType) {
5750 getWindow().setVolumeControlStream(streamType);
5751 }
5752
5753 /**
5754 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07005755 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07005756 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 * @return The suggested audio stream type whose volume should be changed by
5758 * the hardware volume controls.
5759 * @see #setVolumeControlStream(int)
5760 */
5761 public final int getVolumeControlStream() {
5762 return getWindow().getVolumeControlStream();
5763 }
RoboErik55011652014-07-09 15:05:53 -07005764
5765 /**
5766 * Sets a {@link MediaController} to send media keys and volume changes to.
5767 * <p>
5768 * The controller will be tied to the window of this Activity. Media key and
5769 * volume events which are received while the Activity is in the foreground
5770 * will be forwarded to the controller and used to invoke transport controls
5771 * or adjust the volume. This may be used instead of or in addition to
5772 * {@link #setVolumeControlStream} to affect a specific session instead of a
5773 * specific stream.
5774 * <p>
5775 * It is not guaranteed that the hardware volume controls will always change
5776 * this session's volume (for example, if a call is in progress, its
5777 * stream's volume may be changed instead). To reset back to the default use
5778 * null as the controller.
5779 *
5780 * @param controller The controller for the session which should receive
5781 * media keys and volume changes.
5782 */
5783 public final void setMediaController(MediaController controller) {
5784 getWindow().setMediaController(controller);
5785 }
5786
5787 /**
5788 * Gets the controller which should be receiving media key and volume events
5789 * while this activity is in the foreground.
5790 *
5791 * @return The controller which should receive events.
5792 * @see #setMediaController(android.media.session.MediaController)
5793 */
5794 public final MediaController getMediaController() {
5795 return getWindow().getMediaController();
5796 }
5797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 /**
5799 * Runs the specified action on the UI thread. If the current thread is the UI
5800 * thread, then the action is executed immediately. If the current thread is
5801 * not the UI thread, the action is posted to the event queue of the UI thread.
5802 *
5803 * @param action the action to run on the UI thread
5804 */
5805 public final void runOnUiThread(Runnable action) {
5806 if (Thread.currentThread() != mUiThread) {
5807 mHandler.post(action);
5808 } else {
5809 action.run();
5810 }
5811 }
5812
5813 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005814 * Standard implementation of
5815 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
5816 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005817 * This implementation does nothing and is for
5818 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
5819 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
5820 *
5821 * @see android.view.LayoutInflater#createView
5822 * @see android.view.Window#getLayoutInflater
5823 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005824 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07005825 public View onCreateView(String name, Context context, AttributeSet attrs) {
5826 return null;
5827 }
5828
5829 /**
5830 * Standard implementation of
5831 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
5832 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005833 * This implementation handles <fragment> tags to embed fragments inside
5834 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 *
5836 * @see android.view.LayoutInflater#createView
5837 * @see android.view.Window#getLayoutInflater
5838 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07005839 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005840 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07005841 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005842 }
RoboErik55011652014-07-09 15:05:53 -07005843
Adam Powell371a8092014-06-20 12:51:12 -07005844 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005845 }
5846
Daniel Sandler69a48172010-06-23 16:29:36 -04005847 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07005848 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07005849 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07005850 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08005851 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005852 * @param fd The raw file descriptor that the dump is being sent to.
5853 * @param writer The PrintWriter to which you should dump your state. This will be
5854 * closed for you after you return.
5855 * @param args additional arguments to the dump request.
5856 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08005857 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07005858 dumpInner(prefix, fd, writer, args);
5859 }
5860
5861 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08005862 writer.print(prefix); writer.print("Local Activity ");
5863 writer.print(Integer.toHexString(System.identityHashCode(this)));
5864 writer.println(" State:");
5865 String innerPrefix = prefix + " ";
5866 writer.print(innerPrefix); writer.print("mResumed=");
5867 writer.print(mResumed); writer.print(" mStopped=");
5868 writer.print(mStopped); writer.print(" mFinished=");
5869 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08005870 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
5871 writer.println(mChangingConfigurations);
5872 writer.print(innerPrefix); writer.print("mCurrentConfig=");
5873 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07005874
Todd Kennedya5fc6f02015-04-14 18:22:54 -07005875 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
5876 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07005877 if (mVoiceInteractor != null) {
5878 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
5879 }
Jeff Brown5182c782013-10-15 20:31:52 -07005880
Michael Wright5f48dc72013-11-01 12:42:49 -07005881 if (getWindow() != null &&
5882 getWindow().peekDecorView() != null &&
5883 getWindow().peekDecorView().getViewRootImpl() != null) {
5884 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
5885 }
Jeff Brown5182c782013-10-15 20:31:52 -07005886
5887 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07005888 }
5889
5890 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04005891 * Bit indicating that this activity is "immersive" and should not be
5892 * interrupted by notifications if possible.
5893 *
5894 * This value is initially set by the manifest property
5895 * <code>android:immersive</code> but may be changed at runtime by
5896 * {@link #setImmersive}.
5897 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07005898 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04005899 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5900 */
5901 public boolean isImmersive() {
5902 try {
5903 return ActivityManagerNative.getDefault().isImmersive(mToken);
5904 } catch (RemoteException e) {
5905 return false;
5906 }
5907 }
5908
5909 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07005910 * Indication of whether this is the highest level activity in this task. Can be used to
5911 * determine whether an activity launched by this activity was placed in the same task or
5912 * another task.
5913 *
5914 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07005915 */
George Mountff243282014-07-07 16:12:07 -07005916 private boolean isTopOfTask() {
George Mountd0ca0e02016-03-29 10:32:56 -07005917 if (mToken == null || mWindow == null || !mWindowAdded) {
5918 return false;
5919 }
Craig Mautnerd61dc202014-07-07 11:09:11 -07005920 try {
5921 return ActivityManagerNative.getDefault().isTopOfTask(mToken);
5922 } catch (RemoteException e) {
5923 return false;
5924 }
5925 }
5926
5927 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07005928 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
5929 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005930 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005931 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005932 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
5933 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005934 * This call has no effect on non-translucent activities or on activities with the
5935 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005936 *
George Mount62ab9b72014-05-02 13:51:17 -07005937 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
5938 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07005939 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005940 *
5941 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07005942 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005943 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07005944 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07005945 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07005946 mTranslucentCallback = null;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005947 if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) {
5948 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
5949 }
Craig Mautner4addfc52013-06-25 08:05:45 -07005950 } catch (RemoteException e) {
5951 // pass
5952 }
5953 }
5954
5955 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07005956 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
5957 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
5958 * <p>
5959 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
5960 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
5961 * be called indicating that it is safe to make this activity translucent again. Until
5962 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
5963 * behind the frontmost Activity will be indeterminate.
5964 * <p>
5965 * This call has no effect on non-translucent activities or on activities with the
5966 * {@link android.R.attr#windowIsFloating} attribute.
5967 *
5968 * @param callback the method to call when all visible Activities behind this one have been
5969 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07005970 * @param options activity options delivered to the activity below this one. The options
5971 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07005972 * @return <code>true</code> if Window was opaque and will become translucent or
5973 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005974 *
5975 * @see #convertFromTranslucent()
5976 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005977 *
5978 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07005979 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005980 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07005981 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07005982 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07005983 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005984 try {
5985 mTranslucentCallback = callback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005986 mChangeCanvasToTranslucent =
Craig Mautner233ceee2014-05-09 17:05:11 -07005987 ActivityManagerNative.getDefault().convertToTranslucent(mToken, options);
George Mount9e183972014-09-03 12:35:09 -07005988 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07005989 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005990 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07005991 // Make callback return as though it timed out.
5992 mChangeCanvasToTranslucent = false;
5993 drawComplete = false;
5994 }
5995 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
5996 // Window is already translucent.
5997 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07005998 }
George Mount3cc716c2014-06-12 16:35:35 -07005999 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006000 }
6001
6002 /** @hide */
6003 void onTranslucentConversionComplete(boolean drawComplete) {
6004 if (mTranslucentCallback != null) {
6005 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
6006 mTranslucentCallback = null;
6007 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006008 if (mChangeCanvasToTranslucent) {
6009 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
6010 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006011 }
6012
Craig Mautnereb8abf72014-07-02 15:04:09 -07006013 /** @hide */
6014 public void onNewActivityOptions(ActivityOptions options) {
6015 mActivityTransitionState.setEnterActivityOptions(this, options);
6016 if (!mStopped) {
6017 mActivityTransitionState.enterReady(this);
6018 }
6019 }
6020
Craig Mautner5eda9b32013-07-02 11:58:16 -07006021 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07006022 * Retrieve the ActivityOptions passed in from the launching activity or passed back
6023 * from an activity launched by this activity in its call to {@link
6024 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
6025 *
6026 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
6027 * @hide
6028 */
6029 ActivityOptions getActivityOptions() {
6030 try {
6031 return ActivityManagerNative.getDefault().getActivityOptions(mToken);
6032 } catch (RemoteException e) {
6033 }
6034 return null;
6035 }
6036
6037 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006038 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07006039 * this method anytime between the start of {@link #onResume()} and the return from
6040 * {@link #onPause()}. If this call is successful then the activity will remain visible after
6041 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
6042 *
6043 * <p>The actions of this call are reset each time that this activity is brought to the
6044 * front. That is, every time {@link #onResume()} is called the activity will be assumed
6045 * to not have requested visible behind. Therefore, if you want this activity to continue to
6046 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006047 *
6048 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
6049 * for dialog and translucent activities.
6050 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006051 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
6052 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
6053 *
6054 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07006055 * the next call to onResume.
6056 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006057 * @param visible true to notify the system that the activity wishes to be visible behind other
6058 * translucent activities, false to indicate otherwise. Resources must be
6059 * released when passing false to this method.
Craig Mautner64ccb702014-10-01 09:38:40 -07006060 * @return the resulting visibiity state. If true the activity will remain visible beyond
6061 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
6062 * then the activity may not count on being visible behind other translucent activities,
6063 * and must stop any media playback and release resources.
6064 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
6065 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006066 *
Jose Limafcf70832014-08-27 23:09:05 -07006067 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006068 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006069 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006070 public boolean requestVisibleBehind(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006071 if (!mResumed) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006072 // Do not permit paused or stopped activities to do this.
6073 visible = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006074 }
6075 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07006076 mVisibleBehind = ActivityManagerNative.getDefault()
6077 .requestVisibleBehind(mToken, visible) && visible;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006078 } catch (RemoteException e) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006079 mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006080 }
Jose Lima4b6c6692014-08-12 17:41:12 -07006081 return mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006082 }
6083
6084 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006085 * Called when a translucent activity over this activity is becoming opaque or another
6086 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07006087 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006088 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006089 * <p>When this method is called the activity has 500 msec to release any resources it may be
6090 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006091 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07006092 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07006093 * process. Otherwise {@link #onStop()} will be called following return.
6094 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006095 * @see #requestVisibleBehind(boolean)
6096 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006097 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006098 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07006099 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006100 mCalled = true;
6101 }
6102
6103 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006104 * Translucent activities may call this to determine if there is an activity below them that
6105 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006106 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006107 * @return true if an activity below is set to visible according to the most recent call to
6108 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006109 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006110 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006111 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006112 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006113 * @hide
6114 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006115 @SystemApi
6116 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006117 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07006118 return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07006119 } catch (RemoteException e) {
6120 }
6121 return false;
6122 }
6123
6124 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006125 * The topmost foreground activity will receive this call when the background visibility state
6126 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006127 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006128 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07006129 * due to a background activity finishing itself.
6130 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006131 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006132 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006133 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006134 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006135 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07006136 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006137 @SystemApi
6138 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006139 }
6140
6141 /**
Craig Mautner8746a472014-07-24 15:12:54 -07006142 * Activities cannot draw during the period that their windows are animating in. In order
6143 * to know when it is safe to begin drawing they can override this method which will be
6144 * called when the entering animation has completed.
6145 */
6146 public void onEnterAnimationComplete() {
6147 }
6148
6149 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08006150 * @hide
6151 */
6152 public void dispatchEnterAnimationComplete() {
6153 onEnterAnimationComplete();
6154 if (getWindow() != null && getWindow().getDecorView() != null) {
6155 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
6156 }
6157 }
6158
6159 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006160 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07006161 *
Daniel Sandler69a48172010-06-23 16:29:36 -04006162 * Note that changing this value will have no effect on the activity's
6163 * {@link android.content.pm.ActivityInfo} structure; that is, if
6164 * <code>android:immersive</code> is set to <code>true</code>
6165 * in the application's manifest entry for this activity, the {@link
6166 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
6167 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6168 * FLAG_IMMERSIVE} bit set.
6169 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006170 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04006171 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6172 */
6173 public void setImmersive(boolean i) {
6174 try {
6175 ActivityManagerNative.getDefault().setImmersive(mToken, i);
6176 } catch (RemoteException e) {
6177 // pass
6178 }
6179 }
6180
Adam Powell6e346362010-07-23 10:18:23 -07006181 /**
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006182 * Enable or disable virtual reality (VR) mode.
6183 *
Ruben Brunke24b9a62016-02-16 21:38:24 -08006184 * <p>VR mode is a hint to Android system services to switch to a mode optimized for
6185 * high-performance stereoscopic rendering. This mode will be enabled while this Activity has
6186 * focus.</p>
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006187 *
6188 * @param enabled {@code true} to enable this mode.
Ruben Brunke24b9a62016-02-16 21:38:24 -08006189 * @param requestedComponent the name of the component to use as a
6190 * {@link android.service.vr.VrListenerService} while VR mode is enabled.
6191 *
6192 * @throws android.content.pm.PackageManager.NameNotFoundException;
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006193 */
Ruben Brunke24b9a62016-02-16 21:38:24 -08006194 public void setVrModeEnabled(boolean enabled, @NonNull ComponentName requestedComponent)
6195 throws PackageManager.NameNotFoundException {
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006196 try {
Ruben Brunke24b9a62016-02-16 21:38:24 -08006197 if (ActivityManagerNative.getDefault().setVrMode(mToken, enabled, requestedComponent)
6198 != 0) {
6199 throw new PackageManager.NameNotFoundException(
6200 requestedComponent.flattenToString());
6201 }
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006202 } catch (RemoteException e) {
6203 // pass
6204 }
6205 }
6206
6207 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006208 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07006209 *
Clara Bayarri4423d912015-03-02 19:42:48 +00006210 * @param callback Callback that will manage lifecycle events for this action mode
6211 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07006212 *
6213 * @see ActionMode
6214 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006215 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07006216 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07006217 return mWindow.getDecorView().startActionMode(callback);
6218 }
6219
Adam Powelldebf3be2010-11-15 18:58:48 -08006220 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006221 * Start an action mode of the given type.
6222 *
6223 * @param callback Callback that will manage lifecycle events for this action mode
6224 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
6225 * @return The ActionMode that was started, or null if it was canceled
6226 *
6227 * @see ActionMode
6228 */
6229 @Nullable
6230 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
6231 return mWindow.getDecorView().startActionMode(callback, type);
6232 }
6233
6234 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006235 * Give the Activity a chance to control the UI for an action mode requested
6236 * by the system.
6237 *
6238 * <p>Note: If you are looking for a notification callback that an action mode
6239 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
6240 *
6241 * @param callback The callback that should control the new action mode
6242 * @return The new action mode, or <code>null</code> if the activity does not want to
6243 * provide special handling for this action mode. (It will be handled by the system.)
6244 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006245 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07006246 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006247 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00006248 // Only Primary ActionModes are represented in the ActionBar.
6249 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
6250 initWindowDecorActionBar();
6251 if (mActionBar != null) {
6252 return mActionBar.startActionMode(callback);
6253 }
Adam Powell6e346362010-07-23 10:18:23 -07006254 }
6255 return null;
6256 }
6257
Adam Powelldebf3be2010-11-15 18:58:48 -08006258 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006259 * {@inheritDoc}
6260 */
6261 @Nullable
6262 @Override
6263 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
6264 try {
6265 mActionModeTypeStarting = type;
6266 return onWindowStartingActionMode(callback);
6267 } finally {
6268 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
6269 }
6270 }
6271
6272 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006273 * Notifies the Activity that an action mode has been started.
6274 * Activity subclasses overriding this method should call the superclass implementation.
6275 *
6276 * @param mode The new action mode.
6277 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006278 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006279 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006280 public void onActionModeStarted(ActionMode mode) {
6281 }
6282
6283 /**
6284 * Notifies the activity that an action mode has finished.
6285 * Activity subclasses overriding this method should call the superclass implementation.
6286 *
6287 * @param mode The action mode that just finished.
6288 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006289 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006290 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006291 public void onActionModeFinished(ActionMode mode) {
6292 }
6293
Adam Powelldd8fab22012-03-22 17:47:27 -07006294 /**
6295 * Returns true if the app should recreate the task when navigating 'up' from this activity
6296 * by using targetIntent.
6297 *
6298 * <p>If this method returns false the app can trivially call
6299 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
6300 * up navigation. If this method returns false, the app should synthesize a new task stack
6301 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
6302 *
6303 * @param targetIntent An intent representing the target destination for up navigation
6304 * @return true if navigating up should recreate a new task stack, false if the same task
6305 * should be used for the destination
6306 */
6307 public boolean shouldUpRecreateTask(Intent targetIntent) {
6308 try {
6309 PackageManager pm = getPackageManager();
6310 ComponentName cn = targetIntent.getComponent();
6311 if (cn == null) {
6312 cn = targetIntent.resolveActivity(pm);
6313 }
6314 ActivityInfo info = pm.getActivityInfo(cn, 0);
6315 if (info.taskAffinity == null) {
6316 return false;
6317 }
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07006318 return ActivityManagerNative.getDefault()
6319 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07006320 } catch (RemoteException e) {
6321 return false;
6322 } catch (NameNotFoundException e) {
6323 return false;
6324 }
6325 }
6326
6327 /**
6328 * Navigate from this activity to the activity specified by upIntent, finishing this activity
6329 * in the process. If the activity indicated by upIntent already exists in the task's history,
6330 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07006331 * finished.
6332 *
6333 * <p>If the indicated activity does not appear in the history stack, this will finish
6334 * each activity in this task until the root activity of the task is reached, resulting in
6335 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
6336 * when an activity may be reached by a path not passing through a canonical parent
6337 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07006338 *
6339 * <p>This method should be used when performing up navigation from within the same task
6340 * as the destination. If up navigation should cross tasks in some cases, see
6341 * {@link #shouldUpRecreateTask(Intent)}.</p>
6342 *
6343 * @param upIntent An intent representing the target destination for up navigation
6344 *
6345 * @return true if up navigation successfully reached the activity indicated by upIntent and
6346 * upIntent was delivered to it. false if an instance of the indicated activity could
6347 * not be found and this activity was simply finished normally.
6348 */
6349 public boolean navigateUpTo(Intent upIntent) {
6350 if (mParent == null) {
6351 ComponentName destInfo = upIntent.getComponent();
6352 if (destInfo == null) {
6353 destInfo = upIntent.resolveActivity(getPackageManager());
6354 if (destInfo == null) {
6355 return false;
6356 }
6357 upIntent = new Intent(upIntent);
6358 upIntent.setComponent(destInfo);
6359 }
6360 int resultCode;
6361 Intent resultData;
6362 synchronized (this) {
6363 resultCode = mResultCode;
6364 resultData = mResultData;
6365 }
6366 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006367 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006368 }
6369 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006370 upIntent.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006371 return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent,
6372 resultCode, resultData);
6373 } catch (RemoteException e) {
6374 return false;
6375 }
6376 } else {
6377 return mParent.navigateUpToFromChild(this, upIntent);
6378 }
6379 }
6380
6381 /**
6382 * This is called when a child activity of this one calls its
6383 * {@link #navigateUpTo} method. The default implementation simply calls
6384 * navigateUpTo(upIntent) on this activity (the parent).
6385 *
6386 * @param child The activity making the call.
6387 * @param upIntent An intent representing the target destination for up navigation
6388 *
6389 * @return true if up navigation successfully reached the activity indicated by upIntent and
6390 * upIntent was delivered to it. false if an instance of the indicated activity could
6391 * not be found and this activity was simply finished normally.
6392 */
6393 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
6394 return navigateUpTo(upIntent);
6395 }
6396
6397 /**
6398 * Obtain an {@link Intent} that will launch an explicit target activity specified by
6399 * this activity's logical parent. The logical parent is named in the application's manifest
6400 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07006401 * Activity subclasses may override this method to modify the Intent returned by
6402 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
6403 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07006404 *
Adam Powell04d58112012-04-09 10:22:12 -07006405 * @return a new Intent targeting the defined parent of this activity or null if
6406 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07006407 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006408 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07006409 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07006410 final String parentName = mActivityInfo.parentActivityName;
6411 if (TextUtils.isEmpty(parentName)) {
6412 return null;
6413 }
Adam Powell5a4010c2012-09-16 15:14:05 -07006414
6415 // If the parent itself has no parent, generate a main activity intent.
6416 final ComponentName target = new ComponentName(this, parentName);
6417 try {
6418 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
6419 final String parentActivity = parentInfo.parentActivityName;
6420 final Intent parentIntent = parentActivity == null
6421 ? Intent.makeMainActivity(target)
6422 : new Intent().setComponent(target);
6423 return parentIntent;
6424 } catch (NameNotFoundException e) {
6425 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
6426 "' in manifest");
6427 return null;
6428 }
Adam Powelldd8fab22012-03-22 17:47:27 -07006429 }
6430
George Mount31a21722014-03-24 17:44:36 -07006431 /**
George Mount62ab9b72014-05-02 13:51:17 -07006432 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006433 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006434 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07006435 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07006436 *
George Mount65580562014-08-29 08:15:48 -07006437 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07006438 */
George Mount65580562014-08-29 08:15:48 -07006439 public void setEnterSharedElementCallback(SharedElementCallback callback) {
6440 if (callback == null) {
6441 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07006442 }
George Mount65580562014-08-29 08:15:48 -07006443 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07006444 }
6445
6446 /**
6447 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006448 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006449 * will be called to handle shared elements on the <i>launching</i> Activity. Most
6450 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07006451 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07006452 *
George Mount65580562014-08-29 08:15:48 -07006453 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07006454 */
George Mount65580562014-08-29 08:15:48 -07006455 public void setExitSharedElementCallback(SharedElementCallback callback) {
6456 if (callback == null) {
6457 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07006458 }
George Mount65580562014-08-29 08:15:48 -07006459 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07006460 }
6461
George Mount8c2614c2014-06-10 11:12:01 -07006462 /**
6463 * Postpone the entering activity transition when Activity was started with
6464 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6465 * android.util.Pair[])}.
6466 * <p>This method gives the Activity the ability to delay starting the entering and
6467 * shared element transitions until all data is loaded. Until then, the Activity won't
6468 * draw into its window, leaving the window transparent. This may also cause the
6469 * returning animation to be delayed until data is ready. This method should be
6470 * called in {@link #onCreate(android.os.Bundle)} or in
6471 * {@link #onActivityReenter(int, android.content.Intent)}.
6472 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
6473 * start the transitions. If the Activity did not use
6474 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6475 * android.util.Pair[])}, then this method does nothing.</p>
6476 */
6477 public void postponeEnterTransition() {
6478 mActivityTransitionState.postponeEnterTransition();
6479 }
6480
6481 /**
6482 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
6483 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
6484 * to have your Activity start drawing.
6485 */
6486 public void startPostponedEnterTransition() {
6487 mActivityTransitionState.startPostponedEnterTransition();
6488 }
6489
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006490 /**
6491 * Create {@link DropPermissions} object bound to this activity and controlling the access
6492 * permissions for content URIs associated with the {@link DragEvent}.
6493 * @param event Drag event
6494 * @return The DropPermissions object used to control access to the content URIs. Null if
6495 * no content URIs are associated with the event or if permissions could not be granted.
6496 */
6497 public DropPermissions requestDropPermissions(DragEvent event) {
6498 DropPermissions dropPermissions = DropPermissions.obtain(event);
6499 if (dropPermissions != null && dropPermissions.take(getActivityToken())) {
6500 return dropPermissions;
6501 }
6502 return null;
6503 }
6504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07006506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 final void setParent(Activity parent) {
6508 mParent = parent;
6509 }
6510
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006511 final void attach(Context context, ActivityThread aThread,
6512 Instrumentation instr, IBinder token, int ident,
6513 Application application, Intent intent, ActivityInfo info,
6514 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006515 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006516 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
6517 Window window) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006518 attachBaseContext(context);
6519
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006520 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07006521
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006522 mWindow = new PhoneWindow(this, window);
Skuhnece2faa52015-08-11 10:36:38 -07006523 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07006525 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08006526 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
6528 mWindow.setSoftInputMode(info.softInputMode);
6529 }
Adam Powell269248d2011-08-02 10:26:54 -07006530 if (info.uiOptions != 0) {
6531 mWindow.setUiOptions(info.uiOptions);
6532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08006534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 mMainThread = aThread;
6536 mInstrumentation = instr;
6537 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006538 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 mApplication = application;
6540 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006541 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006542 mComponent = intent.getComponent();
6543 mActivityInfo = info;
6544 mTitle = title;
6545 mParent = parent;
6546 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006547 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07006548 if (voiceInteractor != null) {
6549 if (lastNonConfigurationInstances != null) {
6550 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
6551 } else {
6552 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
6553 Looper.myLooper());
6554 }
6555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556
Jeff Brown98365d72012-08-19 20:30:52 -07006557 mWindow.setWindowManager(
6558 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
6559 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07006560 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 if (mParent != null) {
6562 mWindow.setContainer(mParent.getWindow());
6563 }
6564 mWindowManager = mWindow.getWindowManager();
6565 mCurrentConfig = config;
6566 }
6567
Dianne Hackborn5320eb82012-05-18 12:05:04 -07006568 /** @hide */
6569 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006570 return mParent != null ? mParent.getActivityToken() : mToken;
6571 }
6572
Craig Mautnera0026042014-04-23 11:45:37 -07006573 final void performCreateCommon() {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006574 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
6575 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07006576 mFragments.dispatchActivityCreated();
George Mount62ab9b72014-05-02 13:51:17 -07006577 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006578 }
George Mount0a778ed2013-12-13 13:35:36 -08006579
Craig Mautnera0026042014-04-23 11:45:37 -07006580 final void performCreate(Bundle icicle) {
Svetoslavffb32b12015-10-15 16:54:00 -07006581 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006582 onCreate(icicle);
George Mount62ab9b72014-05-02 13:51:17 -07006583 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006584 performCreateCommon();
6585 }
6586
6587 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Svetoslavffb32b12015-10-15 16:54:00 -07006588 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006589 onCreate(icicle, persistentState);
George Mount62ab9b72014-05-02 13:51:17 -07006590 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006591 performCreateCommon();
6592 }
6593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 final void performStart() {
George Mount62ab9b72014-05-02 13:51:17 -07006595 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006596 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07006598 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006599 mInstrumentation.callActivityOnStart(this);
6600 if (!mCalled) {
6601 throw new SuperNotCalledException(
6602 "Activity " + mComponent.toShortString() +
6603 " did not call through to super.onStart()");
6604 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006605 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006606 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08006607
6608 // This property is set for all builds except final release
6609 boolean isDlwarningEnabled = SystemProperties.getInt("ro.bionic.ld.warning", 0) == 1;
6610 boolean isAppDebuggable =
6611 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
6612
6613 if (isAppDebuggable || isDlwarningEnabled) {
6614 String dlwarning = getDlWarning();
6615 if (dlwarning != null) {
Adam Powelld93f3b12016-03-03 08:43:37 -08006616 String appName = getApplicationInfo().loadLabel(getPackageManager())
6617 .toString();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08006618 String warning = "Detected problems with app native libraries\n" +
6619 "(please consult log for detail):\n" + dlwarning;
6620 if (isAppDebuggable) {
6621 new AlertDialog.Builder(this).
6622 setTitle(appName).
6623 setMessage(warning).
6624 setPositiveButton(android.R.string.ok, null).
6625 setCancelable(false).
6626 show();
6627 } else {
6628 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
6629 }
6630 }
6631 }
6632
George Mount62ab9b72014-05-02 13:51:17 -07006633 mActivityTransitionState.enterReady(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 }
RoboErik55011652014-07-09 15:05:53 -07006635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 final void performRestart() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006637 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07006638
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006639 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08006640 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006641 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
6642 }
6643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 if (mStopped) {
6645 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006646
6647 synchronized (mManagedCursors) {
6648 final int N = mManagedCursors.size();
6649 for (int i=0; i<N; i++) {
6650 ManagedCursor mc = mManagedCursors.get(i);
6651 if (mc.mReleased || mc.mUpdated) {
6652 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07006653 if (getApplicationInfo().targetSdkVersion
6654 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
6655 throw new IllegalStateException(
6656 "trying to requery an already closed cursor "
6657 + mc.mCursor);
6658 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006659 }
6660 mc.mReleased = false;
6661 mc.mUpdated = false;
6662 }
6663 }
6664 }
6665
6666 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006667 mInstrumentation.callActivityOnRestart(this);
6668 if (!mCalled) {
6669 throw new SuperNotCalledException(
6670 "Activity " + mComponent.toShortString() +
6671 " did not call through to super.onRestart()");
6672 }
6673 performStart();
6674 }
6675 }
RoboErik55011652014-07-09 15:05:53 -07006676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006677 final void performResume() {
6678 performRestart();
RoboErik55011652014-07-09 15:05:53 -07006679
Dianne Hackborn445646c2010-06-25 15:52:59 -07006680 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006681
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006682 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07006683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006684 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06006685 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006686 mInstrumentation.callActivityOnResume(this);
6687 if (!mCalled) {
6688 throw new SuperNotCalledException(
6689 "Activity " + mComponent.toShortString() +
6690 " did not call through to super.onResume()");
6691 }
6692
Todd Kennedyee8c9c62014-12-10 14:22:59 -08006693 // invisible activities must be finished before onResume() completes
6694 if (!mVisibleFromClient && !mFinished) {
6695 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
6696 if (getApplicationInfo().targetSdkVersion
6697 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
6698 throw new IllegalStateException(
6699 "Activity " + mComponent.toShortString() +
6700 " did not call finish() prior to onResume() completing");
6701 }
6702 }
6703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006704 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006705 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07006706
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006707 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07006708 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006710 onPostResume();
6711 if (!mCalled) {
6712 throw new SuperNotCalledException(
6713 "Activity " + mComponent.toShortString() +
6714 " did not call through to super.onPostResume()");
6715 }
6716 }
6717
6718 final void performPause() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006719 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006720 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006721 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006722 onPause();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006723 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006724 if (!mCalled && getApplicationInfo().targetSdkVersion
6725 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
6726 throw new SuperNotCalledException(
6727 "Activity " + mComponent.toShortString() +
6728 " did not call through to super.onPause()");
6729 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06006730 mResumed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006731 }
RoboErik55011652014-07-09 15:05:53 -07006732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006733 final void performUserLeaving() {
6734 onUserInteraction();
6735 onUserLeaveHint();
6736 }
RoboErik55011652014-07-09 15:05:53 -07006737
Chong Zhang7687f252016-02-26 12:03:33 -08006738 final void performStop(boolean preserveWindow) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006739 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006740 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
RoboErik55011652014-07-09 15:05:53 -07006741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 if (!mStopped) {
6743 if (mWindow != null) {
6744 mWindow.closeAllPanels();
6745 }
6746
Chong Zhang7687f252016-02-26 12:03:33 -08006747 // If we're preserving the window, don't setStoppedState to true, since we
6748 // need the window started immediately again. Stopping the window will
6749 // destroys hardware resources and causes flicker.
6750 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07006751 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08006752 }
RoboErik55011652014-07-09 15:05:53 -07006753
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006754 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07006755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006756 mCalled = false;
6757 mInstrumentation.callActivityOnStop(this);
6758 if (!mCalled) {
6759 throw new SuperNotCalledException(
6760 "Activity " + mComponent.toShortString() +
6761 " did not call through to super.onStop()");
6762 }
RoboErik55011652014-07-09 15:05:53 -07006763
Makoto Onuki2f6a0182010-02-22 13:26:59 -08006764 synchronized (mManagedCursors) {
6765 final int N = mManagedCursors.size();
6766 for (int i=0; i<N; i++) {
6767 ManagedCursor mc = mManagedCursors.get(i);
6768 if (!mc.mReleased) {
6769 mc.mCursor.deactivate();
6770 mc.mReleased = true;
6771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006772 }
6773 }
George Mount0a778ed2013-12-13 13:35:36 -08006774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 mStopped = true;
6776 }
6777 mResumed = false;
6778 }
6779
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006780 final void performDestroy() {
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006781 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07006782 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006783 mFragments.dispatchDestroy();
6784 onDestroy();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006785 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07006786 if (mVoiceInteractor != null) {
6787 mVoiceInteractor.detachActivity();
6788 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006789 }
George Mount0a778ed2013-12-13 13:35:36 -08006790
6791 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06006792 * @hide
6793 */
6794 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 return mResumed;
6796 }
6797
Svetoslavffb32b12015-10-15 16:54:00 -07006798 private void storeHasCurrentPermissionRequest(Bundle bundle) {
6799 if (bundle != null && mHasCurrentPermissionsRequest) {
6800 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
6801 }
6802 }
6803
6804 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
6805 if (bundle != null) {
6806 mHasCurrentPermissionsRequest = bundle.getBoolean(
6807 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
6808 }
6809 }
6810
George Mount0a778ed2013-12-13 13:35:36 -08006811 void dispatchActivityResult(String who, int requestCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 int resultCode, Intent data) {
Joe Onorato43a17652011-04-06 19:22:23 -07006813 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
6815 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006816 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006817 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07006818 onActivityResult(requestCode, resultCode, data);
6819 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
6820 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
6821 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08006822 dispatchRequestPermissionsResult(requestCode, data);
6823 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006824 Fragment frag = mFragments.findFragmentByWho(who);
6825 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07006826 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08006827 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006828 }
Svetoslav970b59c2015-06-09 16:05:21 -07006829 } else if (who.startsWith("@android:view:")) {
6830 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
6831 getActivityToken());
6832 for (ViewRootImpl viewRoot : views) {
6833 if (viewRoot.getView() != null
6834 && viewRoot.getView().dispatchActivityResult(
6835 who, requestCode, resultCode, data)) {
6836 return;
6837 }
6838 }
6839 } else {
6840 Fragment frag = mFragments.findFragmentByWho(who);
6841 if (frag != null) {
6842 frag.onActivityResult(requestCode, resultCode, data);
6843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844 }
6845 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006846
Jason Monka57e5e02014-05-07 10:06:48 -04006847 /**
Jason Monk62515be2014-05-21 16:06:19 -04006848 * Request to put this Activity in a mode where the user is locked to the
Jason Monka57e5e02014-05-07 10:06:48 -04006849 * current task.
6850 *
Craig Mautner15df08a2015-04-01 12:17:18 -07006851 * This will prevent the user from launching other apps, going to settings, or reaching the
6852 * home screen. This does not include those apps whose {@link android.R.attr#lockTaskMode}
6853 * values permit launching while locked.
Jason Monka57e5e02014-05-07 10:06:48 -04006854 *
Craig Mautner15df08a2015-04-01 12:17:18 -07006855 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true or
6856 * lockTaskMode=lockTaskModeAlways for this component then the app will go directly into
6857 * Lock Task mode. The user will not be able to exit this mode until
6858 * {@link Activity#stopLockTask()} is called.
Jason Monk62515be2014-05-21 16:06:19 -04006859 *
6860 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false
6861 * then the system will prompt the user with a dialog requesting permission to enter
Jason Monk8863b572014-07-29 13:49:32 -04006862 * this mode. When entered through this method the user can exit at any time through
6863 * an action described by the request dialog. Calling stopLockTask will also exit the
6864 * mode.
Craig Mautner15df08a2015-04-01 12:17:18 -07006865 *
6866 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04006867 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006868 public void startLockTask() {
6869 try {
6870 ActivityManagerNative.getDefault().startLockTaskMode(mToken);
6871 } catch (RemoteException e) {
6872 }
6873 }
6874
Jason Monka57e5e02014-05-07 10:06:48 -04006875 /**
6876 * Allow the user to switch away from the current task.
6877 *
6878 * Called to end the mode started by {@link Activity#startLockTask}. This
6879 * can only be called by activities that have successfully called
6880 * startLockTask previously.
6881 *
6882 * This will allow the user to exit this app and move onto other activities.
Craig Mautner15df08a2015-04-01 12:17:18 -07006883 * <p>Note: This method should only be called when the activity is user-facing. That is,
6884 * between onResume() and onPause().
6885 * <p>Note: If there are other tasks below this one that are also locked then calling this
6886 * method will immediately finish this task and resume the previous locked one, remaining in
6887 * lockTask mode.
6888 *
6889 * @see android.R.attr#lockTaskMode
6890 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04006891 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006892 public void stopLockTask() {
6893 try {
6894 ActivityManagerNative.getDefault().stopLockTaskMode();
6895 } catch (RemoteException e) {
6896 }
6897 }
6898
Craig Mautner5eda9b32013-07-02 11:58:16 -07006899 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07006900 * Shows the user the system defined message for telling the user how to exit
6901 * lock task mode. The task containing this activity must be in lock task mode at the time
6902 * of this call for the message to be displayed.
6903 */
6904 public void showLockTaskEscapeMessage() {
6905 try {
6906 ActivityManagerNative.getDefault().showLockTaskEscapeMessage(mToken);
6907 } catch (RemoteException e) {
6908 }
6909 }
6910
6911 /**
Andrii Kulian933076d2016-03-29 17:04:42 -07006912 * Check whether the caption on freeform windows is displayed directly on the content.
6913 *
6914 * @return True if caption is displayed on content, false if it pushes the content down.
6915 *
6916 * @see {@link #setOverlayWithDecorCaptionEnabled(boolean)}
6917 */
6918 public boolean isOverlayWithDecorCaptionEnabled() {
6919 return mWindow.isOverlayWithDecorCaptionEnabled();
6920 }
6921
6922 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08006923 * Set whether the caption should displayed directly on the content rather than push it down.
6924 *
6925 * This affects only freeform windows since they display the caption and only the main
6926 * window of the activity. The caption is used to drag the window around and also shows
6927 * maximize and close action buttons.
6928 */
Andrii Kulian933076d2016-03-29 17:04:42 -07006929 public void setOverlayWithDecorCaptionEnabled(boolean enabled) {
6930 mWindow.setOverlayWithDecorCaptionEnabled(enabled);
Filip Gruszczynski63250652015-11-18 14:43:01 -08006931 }
6932
6933 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006934 * Interface for informing a translucent {@link Activity} once all visible activities below it
6935 * have completed drawing. This is necessary only after an {@link Activity} has been made
6936 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
6937 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07006938 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6939 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07006940 *
6941 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006942 */
Jose Lima14914852014-08-14 09:14:12 -07006943 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006944 public interface TranslucentConversionListener {
6945 /**
6946 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
6947 * below the top one have been redrawn. Following this callback it is safe to make the top
6948 * Activity translucent because the underlying Activity has been drawn.
6949 *
6950 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
6951 * occurred waiting for the Activity to complete drawing.
6952 *
6953 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07006954 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006955 */
6956 public void onTranslucentConversionComplete(boolean drawComplete);
6957 }
Svetoslavc6d1c342015-02-26 14:44:43 -08006958
6959 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07006960 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07006961 // If the package installer crashed we may have not data - best effort.
6962 String[] permissions = (data != null) ? data.getStringArrayExtra(
6963 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
6964 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
6965 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08006966 onRequestPermissionsResult(requestCode, permissions, grantResults);
6967 }
6968
6969 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07006970 Fragment fragment) {
6971 // If the package installer crashed we may have not data - best effort.
6972 String[] permissions = (data != null) ? data.getStringArrayExtra(
6973 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
6974 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
6975 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
6976 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08006977 }
6978
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006979 class HostCallbacks extends FragmentHostCallback<Activity> {
6980 public HostCallbacks() {
6981 super(Activity.this /*activity*/);
6982 }
6983
6984 @Override
6985 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
6986 Activity.this.dump(prefix, fd, writer, args);
6987 }
6988
6989 @Override
6990 public boolean onShouldSaveFragmentState(Fragment fragment) {
6991 return !isFinishing();
6992 }
6993
6994 @Override
6995 public LayoutInflater onGetLayoutInflater() {
6996 final LayoutInflater result = Activity.this.getLayoutInflater();
6997 if (onUseFragmentManagerInflaterFactory()) {
6998 return result.cloneInContext(Activity.this);
6999 }
7000 return result;
7001 }
7002
7003 @Override
7004 public boolean onUseFragmentManagerInflaterFactory() {
7005 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
7006 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
7007 }
7008
7009 @Override
7010 public Activity onGetHost() {
7011 return Activity.this;
7012 }
7013
7014 @Override
7015 public void onInvalidateOptionsMenu() {
7016 Activity.this.invalidateOptionsMenu();
7017 }
7018
7019 @Override
7020 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
7021 Bundle options) {
7022 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
7023 }
7024
7025 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07007026 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
7027 int requestCode) {
7028 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
7029 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
7030 startActivityForResult(who, intent, requestCode, null);
7031 }
7032
7033 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007034 public boolean onHasWindowAnimations() {
7035 return getWindow() != null;
7036 }
7037
7038 @Override
7039 public int onGetWindowAnimations() {
7040 final Window w = getWindow();
7041 return (w == null) ? 0 : w.getAttributes().windowAnimations;
7042 }
7043
Todd Kennedy434bd652015-05-04 12:29:50 -07007044 @Override
7045 public void onAttachFragment(Fragment fragment) {
7046 Activity.this.onAttachFragment(fragment);
7047 }
7048
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007049 @Nullable
7050 @Override
7051 public View onFindViewById(int id) {
7052 return Activity.this.findViewById(id);
7053 }
7054
7055 @Override
7056 public boolean onHasView() {
7057 final Window w = getWindow();
7058 return (w != null && w.peekDecorView() != null);
7059 }
7060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007061}