blob: 90d5d59e7d7683df12ed58cd9802e62c29e10e9a [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;
Adam Powelldd8fab22012-03-22 17:47:27 -070044import android.content.pm.PackageManager;
45import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.res.Configuration;
47import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070048import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.database.Cursor;
50import android.graphics.Bitmap;
51import android.graphics.Canvas;
Winson2d476832016-02-17 14:53:46 -080052import android.graphics.Color;
Winson Chung1af8eda2016-02-05 17:55:56 +000053import android.graphics.Paint;
54import android.graphics.drawable.ColorDrawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.graphics.drawable.Drawable;
Winson Chung1af8eda2016-02-05 17:55:56 +000056import android.graphics.drawable.InsetDrawable;
57import android.graphics.drawable.LayerDrawable;
58import android.graphics.drawable.ShapeDrawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070060import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.net.Uri;
Dianne Hackborn8d374262009-09-14 21:21:52 -070062import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Handler;
65import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080066import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070067import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000068import android.os.PersistableBundle;
svetoslavganov75986cf2009-05-14 22:28:01 -070069import android.os.RemoteException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080070import android.os.StrictMode;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070071import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.text.Selection;
73import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.text.method.TextKeyListener;
Winson Chung1af8eda2016-02-05 17:55:56 +000076import android.transition.Scene;
77import android.transition.TransitionManager;
78import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.util.EventLog;
81import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070082import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070083import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.SparseArray;
Winson Chung1af8eda2016-02-05 17:55:56 +000085import android.util.SuperNotCalledException;
Adam Powell6e346362010-07-23 10:18:23 -070086import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070088import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.view.ContextThemeWrapper;
Winson Chung1af8eda2016-02-05 17:55:56 +000090import android.view.DragEvent;
91import android.view.DropPermissions;
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;
Winson Chung1af8eda2016-02-05 17:55:56 +0000113import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100114
Winson Chung1af8eda2016-02-05 17:55:56 +0000115import com.android.internal.app.IVoiceInteractor;
116import com.android.internal.app.ToolbarActionBar;
117import com.android.internal.app.WindowDecorActionBar;
118import com.android.internal.policy.DecorView;
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 private boolean mStopped;
745 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 /** Return the intent that started this activity. */
836 public Intent getIntent() {
837 return mIntent;
838 }
839
RoboErik55011652014-07-09 15:05:53 -0700840 /**
841 * Change the intent returned by {@link #getIntent}. This holds a
842 * reference to the given intent; it does not copy it. Often used in
843 * conjunction with {@link #onNewIntent}.
844 *
845 * @param newIntent The new Intent object to return from getIntent
846 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 * @see #getIntent
848 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700849 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 public void setIntent(Intent newIntent) {
851 mIntent = newIntent;
852 }
853
854 /** Return the application that owns this activity. */
855 public final Application getApplication() {
856 return mApplication;
857 }
858
859 /** Is this activity embedded inside of another activity? */
860 public final boolean isChild() {
861 return mParent != null;
862 }
RoboErik55011652014-07-09 15:05:53 -0700863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 /** Return the parent activity if this view is an embedded child. */
865 public final Activity getParent() {
866 return mParent;
867 }
868
869 /** Retrieve the window manager for showing custom windows. */
870 public WindowManager getWindowManager() {
871 return mWindowManager;
872 }
873
874 /**
875 * Retrieve the current {@link android.view.Window} for the activity.
876 * This can be used to directly access parts of the Window API that
877 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700878 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 * @return Window The current window, or null if the activity is not
880 * visual.
881 */
882 public Window getWindow() {
883 return mWindow;
884 }
885
886 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800887 * Return the LoaderManager for this activity, creating it if needed.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700888 */
889 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700890 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700891 }
RoboErik55011652014-07-09 15:05:53 -0700892
Dianne Hackbornc8017682010-07-06 13:34:38 -0700893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 * Calls {@link android.view.Window#getCurrentFocus} on the
895 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -0700896 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -0700898 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 * @see #getWindow
900 * @see android.view.Window#getCurrentFocus
901 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700902 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 public View getCurrentFocus() {
904 return mWindow != null ? mWindow.getCurrentFocus() : null;
905 }
906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 /**
908 * Called when the activity is starting. This is where most initialization
909 * should go: calling {@link #setContentView(int)} to inflate the
910 * activity's UI, using {@link #findViewById} to programmatically interact
911 * with widgets in the UI, calling
912 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
913 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -0700914 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 * <p>You can call {@link #finish} from within this function, in
916 * which case onDestroy() will be immediately called without any of the rest
917 * of the activity lifecycle ({@link #onStart}, {@link #onResume},
918 * {@link #onPause}, etc) executing.
RoboErik55011652014-07-09 15:05:53 -0700919 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 * <p><em>Derived classes must call through to the super class's
921 * implementation of this method. If they do not, an exception will be
922 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -0700923 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 * @param savedInstanceState If the activity is being re-initialized after
925 * previously being shut down then this Bundle contains the data it most
926 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -0700927 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 * @see #onStart
929 * @see #onSaveInstanceState
930 * @see #onRestoreInstanceState
931 * @see #onPostCreate
932 */
Tor Norbye83c68962015-03-10 20:55:31 -0700933 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -0800934 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -0700935 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700936 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700937 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700938 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700939 }
Adam Powelldd8fab22012-03-22 17:47:27 -0700940 if (mActivityInfo.parentActivityName != null) {
941 if (mActionBar == null) {
942 mEnableDefaultActionBarUp = true;
943 } else {
944 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
945 }
946 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700947 if (savedInstanceState != null) {
948 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
949 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
950 ? mLastNonConfigurationInstances.fragments : null);
951 }
952 mFragments.dispatchCreate();
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700953 getApplication().dispatchActivityCreated(this, savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -0700954 if (mVoiceInteractor != null) {
955 mVoiceInteractor.attachActivity(this);
956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 mCalled = true;
958 }
959
960 /**
Craig Mautnera0026042014-04-23 11:45:37 -0700961 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -0700962 * the attribute {@link android.R.attr#persistableMode} set to
963 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -0700964 *
965 * @param savedInstanceState if the activity is being re-initialized after
966 * previously being shut down then this Bundle contains the data it most
967 * recently supplied in {@link #onSaveInstanceState}.
968 * <b><i>Note: Otherwise it is null.</i></b>
969 * @param persistentState if the activity is being re-initialized after
970 * previously being shut down or powered off then this Bundle contains the data it most
971 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
972 * <b><i>Note: Otherwise it is null.</i></b>
973 *
974 * @see #onCreate(android.os.Bundle)
975 * @see #onStart
976 * @see #onSaveInstanceState
977 * @see #onRestoreInstanceState
978 * @see #onPostCreate
979 */
Craig Mautner7a629c22014-09-04 14:57:04 -0700980 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -0700981 @Nullable PersistableBundle persistentState) {
982 onCreate(savedInstanceState);
983 }
984
985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 * The hook for {@link ActivityThread} to restore the state of this activity.
987 *
988 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
989 * {@link #restoreManagedDialogs(android.os.Bundle)}.
990 *
991 * @param savedInstanceState contains the saved state
992 */
993 final void performRestoreInstanceState(Bundle savedInstanceState) {
994 onRestoreInstanceState(savedInstanceState);
995 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
997
998 /**
Craig Mautnera0026042014-04-23 11:45:37 -0700999 * The hook for {@link ActivityThread} to restore the state of this activity.
1000 *
1001 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1002 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1003 *
1004 * @param savedInstanceState contains the saved state
1005 * @param persistentState contains the persistable saved state
1006 */
1007 final void performRestoreInstanceState(Bundle savedInstanceState,
1008 PersistableBundle persistentState) {
1009 onRestoreInstanceState(savedInstanceState, persistentState);
1010 if (savedInstanceState != null) {
1011 restoreManagedDialogs(savedInstanceState);
1012 }
1013 }
1014
1015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 * This method is called after {@link #onStart} when the activity is
1017 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001018 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * to restore their state, but it is sometimes convenient to do it here
1020 * after all of the initialization has been done or to allow subclasses to
1021 * decide whether to use your default implementation. The default
1022 * implementation of this method performs a restore of any view state that
1023 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001024 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * <p>This method is called between {@link #onStart} and
1026 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001027 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001029 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 * @see #onCreate
1031 * @see #onPostCreate
1032 * @see #onResume
1033 * @see #onSaveInstanceState
1034 */
1035 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1036 if (mWindow != null) {
1037 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1038 if (windowState != null) {
1039 mWindow.restoreHierarchyState(windowState);
1040 }
1041 }
1042 }
Craig Mautnera0026042014-04-23 11:45:37 -07001043
1044 /**
1045 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001046 * created with the attribute {@link android.R.attr#persistableMode} set to
1047 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1048 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001049 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1050 *
1051 * <p>This method is called between {@link #onStart} and
1052 * {@link #onPostCreate}.
1053 *
1054 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1055 *
1056 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
1057 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}.
1058 *
1059 * @see #onRestoreInstanceState(Bundle)
1060 * @see #onCreate
1061 * @see #onPostCreate
1062 * @see #onResume
1063 * @see #onSaveInstanceState
1064 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001065 public void onRestoreInstanceState(Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001066 PersistableBundle persistentState) {
1067 if (savedInstanceState != null) {
1068 onRestoreInstanceState(savedInstanceState);
1069 }
1070 }
1071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 /**
1073 * Restore the state of any saved managed dialogs.
1074 *
1075 * @param savedInstanceState The bundle to restore from.
1076 */
1077 private void restoreManagedDialogs(Bundle savedInstanceState) {
1078 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1079 if (b == null) {
1080 return;
1081 }
1082
1083 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1084 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001085 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 for (int i = 0; i < numDialogs; i++) {
1087 final Integer dialogId = ids[i];
1088 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1089 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001090 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1091 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001092 final ManagedDialog md = new ManagedDialog();
1093 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1094 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1095 if (md.mDialog != null) {
1096 mManagedDialogs.put(dialogId, md);
1097 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1098 md.mDialog.onRestoreInstanceState(dialogState);
1099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
1101 }
1102 }
1103
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001104 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1105 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001106 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001107 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001108 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001109 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001110 return dialog;
1111 }
1112
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001113 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 return SAVED_DIALOG_KEY_PREFIX + key;
1115 }
1116
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001117 private static String savedDialogArgsKeyFor(int key) {
1118 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120
1121 /**
1122 * Called when activity start-up is complete (after {@link #onStart}
1123 * and {@link #onRestoreInstanceState} have been called). Applications will
1124 * generally not implement this method; it is intended for system
1125 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001126 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 * <p><em>Derived classes must call through to the super class's
1128 * implementation of this method. If they do not, an exception will be
1129 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001130 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 * @param savedInstanceState If the activity is being re-initialized after
1132 * previously being shut down then this Bundle contains the data it most
1133 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1134 * @see #onCreate
1135 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001136 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001137 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 if (!isChild()) {
1139 mTitleReady = true;
1140 onTitleChanged(getTitle(), getTitleColor());
1141 }
Winsonb6403152016-02-23 13:32:09 -08001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 mCalled = true;
1144 }
1145
1146 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001147 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001148 * created with the attribute {@link android.R.attr#persistableMode} set to
1149 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001150 *
1151 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1152 * @param persistentState The data caming from the PersistableBundle first
1153 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1154 *
1155 * @see #onCreate
1156 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001157 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001158 @Nullable PersistableBundle persistentState) {
1159 onPostCreate(savedInstanceState);
1160 }
1161
1162 /**
RoboErik55011652014-07-09 15:05:53 -07001163 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1164 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001165 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 *
1167 * <p><em>Derived classes must call through to the super class's
1168 * implementation of this method. If they do not, an exception will be
1169 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001170 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 * @see #onCreate
1172 * @see #onStop
1173 * @see #onResume
1174 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001175 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001177 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001179
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001180 mFragments.doLoaderStart();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001181
1182 getApplication().dispatchActivityStarted(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
1184
1185 /**
1186 * Called after {@link #onStop} when the current activity is being
1187 * re-displayed to the user (the user has navigated back to it). It will
1188 * be followed by {@link #onStart} and then {@link #onResume}.
1189 *
1190 * <p>For activities that are using raw {@link Cursor} objects (instead of
1191 * creating them through
1192 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1193 * this is usually the place
1194 * where the cursor should be requeried (because you had deactivated it in
1195 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001196 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 * <p><em>Derived classes must call through to the super class's
1198 * implementation of this method. If they do not, an exception will be
1199 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001200 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 * @see #onStop
1202 * @see #onStart
1203 * @see #onResume
1204 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001205 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 protected void onRestart() {
1207 mCalled = true;
1208 }
1209
1210 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001211 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1212 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1213 * to give the activity a hint that its state is no longer saved -- it will generally
1214 * be called after {@link #onSaveInstanceState} and prior to the activity being
1215 * resumed/started again.
1216 */
1217 public void onStateNotSaved() {
1218 }
1219
1220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1222 * {@link #onPause}, for your activity to start interacting with the user.
1223 * This is a good place to begin animations, open exclusive-access devices
1224 * (such as the camera), etc.
1225 *
1226 * <p>Keep in mind that onResume is not the best indicator that your activity
1227 * is visible to the user; a system window such as the keyguard may be in
1228 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1229 * activity is visible to the user (for example, to resume a game).
1230 *
1231 * <p><em>Derived classes must call through to the super class's
1232 * implementation of this method. If they do not, an exception will be
1233 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001234 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 * @see #onRestoreInstanceState
1236 * @see #onRestart
1237 * @see #onPostResume
1238 * @see #onPause
1239 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001240 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001242 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001243 getApplication().dispatchActivityResumed(this);
George Mount8cab50a2014-05-15 09:57:17 -07001244 mActivityTransitionState.onResume();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 mCalled = true;
1246 }
1247
1248 /**
1249 * Called when activity resume is complete (after {@link #onResume} has
1250 * been called). Applications will generally not implement this method;
1251 * it is intended for system classes to do final setup after application
1252 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 * <p><em>Derived classes must call through to the super class's
1255 * implementation of this method. If they do not, an exception will be
1256 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 * @see #onResume
1259 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001260 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 protected void onPostResume() {
1262 final Window win = getWindow();
1263 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001264 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 mCalled = true;
1266 }
1267
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001268 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
1269 if (voiceInteractor == null) {
1270 mVoiceInteractor = null;
1271 } else {
1272 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1273 Looper.myLooper());
1274 }
1275 }
1276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001278 * Check whether this activity is running as part of a voice interaction with the user.
1279 * If true, it should perform its interaction with the user through the
1280 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1281 */
1282 public boolean isVoiceInteraction() {
1283 return mVoiceInteractor != null;
1284 }
1285
1286 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001287 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1288 * of a voice interaction. That is, returns true if this activity was directly
1289 * started by the voice interaction service as the initiation of a voice interaction.
1290 * Otherwise, for example if it was started by another activity while under voice
1291 * interaction, returns false.
1292 */
1293 public boolean isVoiceInteractionRoot() {
1294 try {
1295 return mVoiceInteractor != null
1296 && ActivityManagerNative.getDefault().isRootVoiceInteraction(mToken);
1297 } catch (RemoteException e) {
1298 }
1299 return false;
1300 }
1301
1302 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001303 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1304 * interact with this activity.
1305 */
1306 public VoiceInteractor getVoiceInteractor() {
1307 return mVoiceInteractor;
1308 }
1309
1310 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001311 * Queries whether the currently enabled voice interaction service supports returning
1312 * a voice interactor for use by the activity. This is valid only for the duration of the
1313 * activity.
1314 *
1315 * @return whether the current voice interaction service supports local voice interaction
1316 */
1317 public boolean isLocalVoiceInteractionSupported() {
1318 try {
1319 return ActivityManagerNative.getDefault().supportsLocalVoiceInteraction();
1320 } catch (RemoteException re) {
1321 }
1322 return false;
1323 }
1324
1325 /**
1326 * Starts a local voice interaction session. When ready,
1327 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1328 * to the registered voice interaction service.
1329 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1330 */
1331 public void startLocalVoiceInteraction(Bundle privateOptions) {
1332 try {
1333 ActivityManagerNative.getDefault().startLocalVoiceInteraction(mToken, privateOptions);
1334 } catch (RemoteException re) {
1335 }
1336 }
1337
1338 /**
1339 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1340 * voice interaction session being started. You can now retrieve a voice interactor using
1341 * {@link #getVoiceInteractor()}.
1342 */
1343 public void onLocalVoiceInteractionStarted() {
1344 Log.i(TAG, "onLocalVoiceInteractionStarted! " + getVoiceInteractor());
1345 }
1346
1347 /**
1348 * Callback to indicate that the local voice interaction has stopped for some
1349 * reason.
1350 */
1351 public void onLocalVoiceInteractionStopped() {
1352 Log.i(TAG, "onLocalVoiceInteractionStopped :( " + getVoiceInteractor());
1353 }
1354
1355 /**
1356 * Request to terminate the current voice interaction that was previously started
1357 * using {@link #startLocalVoiceInteraction(Bundle)}.
1358 */
1359 public void stopLocalVoiceInteraction() {
1360 try {
1361 ActivityManagerNative.getDefault().stopLocalVoiceInteraction(mToken);
1362 } catch (RemoteException re) {
1363 }
1364 }
1365
1366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 * This is called for activities that set launchMode to "singleTop" in
1368 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1369 * flag when calling {@link #startActivity}. In either case, when the
1370 * activity is re-launched while at the top of the activity stack instead
1371 * of a new instance of the activity being started, onNewIntent() will be
1372 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001373 * re-launch it.
1374 *
1375 * <p>An activity will always be paused before receiving a new intent, so
1376 * you can count on {@link #onResume} being called after this method.
1377 *
1378 * <p>Note that {@link #getIntent} still returns the original Intent. You
1379 * can use {@link #setIntent} to update it to this new Intent.
1380 *
1381 * @param intent The new intent that was started for the activity.
1382 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001384 * @see #setIntent
1385 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 */
1387 protected void onNewIntent(Intent intent) {
1388 }
1389
1390 /**
1391 * The hook for {@link ActivityThread} to save the state of this activity.
1392 *
1393 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1394 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1395 *
1396 * @param outState The bundle to save the state to.
1397 */
1398 final void performSaveInstanceState(Bundle outState) {
1399 onSaveInstanceState(outState);
1400 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001401 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001402 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001403 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
1405
1406 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001407 * The hook for {@link ActivityThread} to save the state of this activity.
1408 *
1409 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1410 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1411 *
1412 * @param outState The bundle to save the state to.
1413 * @param outPersistentState The bundle to save persistent state to.
1414 */
1415 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1416 onSaveInstanceState(outState, outPersistentState);
1417 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001418 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001419 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1420 ", " + outPersistentState);
1421 }
1422
1423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 * Called to retrieve per-instance state from an activity before being killed
1425 * so that the state can be restored in {@link #onCreate} or
1426 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1427 * will be passed to both).
1428 *
1429 * <p>This method is called before an activity may be killed so that when it
1430 * comes back some time in the future it can restore its state. For example,
1431 * if activity B is launched in front of activity A, and at some point activity
1432 * A is killed to reclaim resources, activity A will have a chance to save the
1433 * current state of its user interface via this method so that when the user
1434 * returns to activity A, the state of the user interface can be restored
1435 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1436 *
1437 * <p>Do not confuse this method with activity lifecycle callbacks such as
1438 * {@link #onPause}, which is always called when an activity is being placed
1439 * in the background or on its way to destruction, or {@link #onStop} which
1440 * is called before destruction. One example of when {@link #onPause} and
1441 * {@link #onStop} is called and not this method is when a user navigates back
1442 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1443 * on B because that particular instance will never be restored, so the
1444 * system avoids calling it. An example when {@link #onPause} is called and
1445 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1446 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1447 * killed during the lifetime of B since the state of the user interface of
1448 * A will stay intact.
1449 *
1450 * <p>The default implementation takes care of most of the UI per-instance
1451 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1452 * view in the hierarchy that has an id, and by saving the id of the currently
1453 * focused view (all of which is restored by the default implementation of
1454 * {@link #onRestoreInstanceState}). If you override this method to save additional
1455 * information not captured by each individual view, you will likely want to
1456 * call through to the default implementation, otherwise be prepared to save
1457 * all of the state of each view yourself.
1458 *
1459 * <p>If called, this method will occur before {@link #onStop}. There are
1460 * no guarantees about whether it will occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001461 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 * @see #onCreate
1465 * @see #onRestoreInstanceState
1466 * @see #onPause
1467 */
1468 protected void onSaveInstanceState(Bundle outState) {
1469 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001470 Parcelable p = mFragments.saveAllState();
1471 if (p != null) {
1472 outState.putParcelable(FRAGMENTS_TAG, p);
1473 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001474 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 }
1476
1477 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001478 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001479 * created with the attribute {@link android.R.attr#persistableMode} set to
1480 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1481 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1482 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001483 *
1484 * @param outState Bundle in which to place your saved state.
1485 * @param outPersistentState State which will be saved across reboots.
1486 *
1487 * @see #onSaveInstanceState(Bundle)
1488 * @see #onCreate
1489 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1490 * @see #onPause
1491 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001492 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001493 onSaveInstanceState(outState);
1494 }
1495
1496 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 * Save the state of any managed dialogs.
1498 *
1499 * @param outState place to store the saved state.
1500 */
1501 private void saveManagedDialogs(Bundle outState) {
1502 if (mManagedDialogs == null) {
1503 return;
1504 }
1505
1506 final int numDialogs = mManagedDialogs.size();
1507 if (numDialogs == 0) {
1508 return;
1509 }
1510
1511 Bundle dialogState = new Bundle();
1512
1513 int[] ids = new int[mManagedDialogs.size()];
1514
1515 // save each dialog's bundle, gather the ids
1516 for (int i = 0; i < numDialogs; i++) {
1517 final int key = mManagedDialogs.keyAt(i);
1518 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001519 final ManagedDialog md = mManagedDialogs.valueAt(i);
1520 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1521 if (md.mArgs != null) {
1522 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525
1526 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1527 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1528 }
1529
1530
1531 /**
1532 * Called as part of the activity lifecycle when an activity is going into
1533 * the background, but has not (yet) been killed. The counterpart to
1534 * {@link #onResume}.
1535 *
1536 * <p>When activity B is launched in front of activity A, this callback will
1537 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1538 * so be sure to not do anything lengthy here.
1539 *
1540 * <p>This callback is mostly used for saving any persistent state the
1541 * activity is editing, to present a "edit in place" model to the user and
1542 * making sure nothing is lost if there are not enough resources to start
1543 * the new activity without first killing this one. This is also a good
1544 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001545 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 * as fast as possible, or to close resources that are exclusive access
1547 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001548 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 * <p>In situations where the system needs more memory it may kill paused
1550 * processes to reclaim resources. Because of this, you should be sure
1551 * that all of your state is saved by the time you return from
1552 * this function. In general {@link #onSaveInstanceState} is used to save
1553 * per-instance state in the activity and this method is used to store
1554 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001555 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 * <p>After receiving this call you will usually receive a following call
1557 * to {@link #onStop} (after the next activity has been resumed and
1558 * displayed), however in some cases there will be a direct call back to
1559 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001560 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 * <p><em>Derived classes must call through to the super class's
1562 * implementation of this method. If they do not, an exception will be
1563 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001564 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 * @see #onResume
1566 * @see #onSaveInstanceState
1567 * @see #onStop
1568 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001569 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001571 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001572 getApplication().dispatchActivityPaused(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 mCalled = true;
1574 }
1575
1576 /**
1577 * Called as part of the activity lifecycle when an activity is about to go
1578 * into the background as the result of user choice. For example, when the
1579 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1580 * when an incoming phone call causes the in-call Activity to be automatically
1581 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1582 * the activity being interrupted. In cases when it is invoked, this method
1583 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001584 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 * <p>This callback and {@link #onUserInteraction} are intended to help
1586 * activities manage status bar notifications intelligently; specifically,
1587 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07001588 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 * @see #onUserInteraction()
1590 */
1591 protected void onUserLeaveHint() {
1592 }
RoboErik55011652014-07-09 15:05:53 -07001593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 /**
1595 * Generate a new thumbnail for this activity. This method is called before
1596 * pausing the activity, and should draw into <var>outBitmap</var> the
1597 * imagery for the desired thumbnail in the dimensions of that bitmap. It
1598 * can use the given <var>canvas</var>, which is configured to draw into the
1599 * bitmap, for rendering if desired.
RoboErik55011652014-07-09 15:05:53 -07001600 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001601 * <p>The default implementation returns fails and does not draw a thumbnail;
1602 * this will result in the platform creating its own thumbnail if needed.
RoboErik55011652014-07-09 15:05:53 -07001603 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 * @param outBitmap The bitmap to contain the thumbnail.
1605 * @param canvas Can be used to render into the bitmap.
RoboErik55011652014-07-09 15:05:53 -07001606 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 * @return Return true if you have drawn into the bitmap; otherwise after
1608 * you return it will be filled with a default thumbnail.
RoboErik55011652014-07-09 15:05:53 -07001609 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 * @see #onCreateDescription
1611 * @see #onSaveInstanceState
1612 * @see #onPause
1613 */
1614 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001615 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
1617
1618 /**
1619 * Generate a new description for this activity. This method is called
1620 * before pausing the activity and can, if desired, return some textual
1621 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001622 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 * <p>The default implementation returns null, which will cause you to
1624 * inherit the description from the previous activity. If all activities
1625 * return null, generally the label of the top activity will be used as the
1626 * description.
RoboErik55011652014-07-09 15:05:53 -07001627 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 * @return A description of what the user is doing. It should be short and
1629 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001630 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 * @see #onCreateThumbnail
1632 * @see #onSaveInstanceState
1633 * @see #onPause
1634 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001635 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 public CharSequence onCreateDescription() {
1637 return null;
1638 }
1639
1640 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001641 * This is called when the user is requesting an assist, to build a full
1642 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1643 * application. You can override this method to place into the bundle anything
1644 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001645 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001646 *
1647 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001648 * been registered with {@link Application#registerOnProvideAssistDataListener
1649 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001650 */
1651 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001652 }
1653
1654 /**
1655 * This is called when the user is requesting an assist, to provide references
1656 * to content related to the current activity. Before being called, the
1657 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
1658 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
1659 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
1660 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
1661 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
1662 *
1663 * <p>Custom implementation may adjust the content intent to better reflect the top-level
1664 * context of the activity, and fill in its ClipData with additional content of
1665 * interest that the user is currently viewing. For example, an image gallery application
1666 * that has launched in to an activity allowing the user to swipe through pictures should
1667 * modify the intent to reference the current image they are looking it; such an
1668 * application when showing a list of pictures should add a ClipData that has
1669 * references to all of the pictures currently visible on screen.</p>
1670 *
1671 * @param outContent The assist content to return.
1672 */
1673 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001674 }
1675
Clara Bayarri75e09792015-07-29 16:20:40 +01001676 @Override
1677 public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, Menu menu) {
1678 if (menu == null) {
1679 return;
1680 }
1681 KeyboardShortcutGroup group = null;
1682 int menuSize = menu.size();
1683 for (int i = 0; i < menuSize; ++i) {
1684 final MenuItem item = menu.getItem(i);
1685 final CharSequence title = item.getTitle();
1686 final char alphaShortcut = item.getAlphabeticShortcut();
1687 if (title != null && alphaShortcut != MIN_VALUE) {
1688 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00001689 final int resource = mApplication.getApplicationInfo().labelRes;
1690 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01001691 }
1692 group.addItem(new KeyboardShortcutInfo(
1693 title, alphaShortcut, KeyEvent.META_CTRL_ON));
1694 }
1695 }
1696 if (group != null) {
1697 data.add(group);
1698 }
1699 }
1700
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001701 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07001702 * Ask to have the current assistant shown to the user. This only works if the calling
1703 * activity is the current foreground activity. It is the same as calling
1704 * {@link android.service.voice.VoiceInteractionService#showSession
1705 * VoiceInteractionService.showSession} and requesting all of the possible context.
1706 * The receiver will always see
1707 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
1708 * @return Returns true if the assistant was successfully invoked, else false. For example
1709 * false will be returned if the caller is not the current top activity.
1710 */
1711 public boolean showAssist(Bundle args) {
1712 try {
1713 return ActivityManagerNative.getDefault().showAssistFromActivity(mToken, args);
1714 } catch (RemoteException e) {
1715 }
1716 return false;
1717 }
1718
1719 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 * Called when you are no longer visible to the user. You will next
1721 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1722 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001723 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 * <p>Note that this method may never be called, in low memory situations
1725 * where the system does not have enough memory to keep your activity's
1726 * process running after its {@link #onPause} method is called.
RoboErik55011652014-07-09 15:05:53 -07001727 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 * <p><em>Derived classes must call through to the super class's
1729 * implementation of this method. If they do not, an exception will be
1730 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001731 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 * @see #onRestart
1733 * @see #onResume
1734 * @see #onSaveInstanceState
1735 * @see #onDestroy
1736 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001737 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001739 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001740 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001741 mActivityTransitionState.onStop();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001742 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001743 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 mCalled = true;
1745 }
1746
1747 /**
1748 * Perform any final cleanup before an activity is destroyed. This can
1749 * happen either because the activity is finishing (someone called
1750 * {@link #finish} on it, or because the system is temporarily destroying
1751 * this instance of the activity to save space. You can distinguish
1752 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001753 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 * <p><em>Note: do not count on this method being called as a place for
1755 * saving data! For example, if an activity is editing data in a content
1756 * provider, those edits should be committed in either {@link #onPause} or
1757 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1758 * free resources like threads that are associated with an activity, so
1759 * that a destroyed activity does not leave such things around while the
1760 * rest of its application is still running. There are situations where
1761 * the system will simply kill the activity's hosting process without
1762 * calling this method (or any others) in it, so it should not be used to
1763 * do things that are intended to remain around after the process goes
1764 * away.
RoboErik55011652014-07-09 15:05:53 -07001765 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 * <p><em>Derived classes must call through to the super class's
1767 * implementation of this method. If they do not, an exception will be
1768 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001769 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 * @see #onPause
1771 * @see #onStop
1772 * @see #finish
1773 * @see #isFinishing
1774 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001775 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001777 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 mCalled = true;
1779
1780 // dismiss any dialogs we are managing.
1781 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 final int numDialogs = mManagedDialogs.size();
1783 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001784 final ManagedDialog md = mManagedDialogs.valueAt(i);
1785 if (md.mDialog.isShowing()) {
1786 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
1788 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001789 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791
1792 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001793 synchronized (mManagedCursors) {
1794 int numCursors = mManagedCursors.size();
1795 for (int i = 0; i < numCursors; i++) {
1796 ManagedCursor c = mManagedCursors.get(i);
1797 if (c != null) {
1798 c.mCursor.close();
1799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001801 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
Amith Yamasani49860442010-03-17 20:54:10 -07001803
1804 // Close any open search dialog
1805 if (mSearchManager != null) {
1806 mSearchManager.stopSearch();
1807 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001808
Chris Banes21b25772016-01-04 20:41:59 +00001809 if (mActionBar != null) {
1810 mActionBar.onDestroy();
1811 }
1812
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001813 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
1815
1816 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001817 * Report to the system that your app is now fully drawn, purely for diagnostic
1818 * purposes (calling it does not impact the visible behavior of the activity).
1819 * This is only used to help instrument application launch times, so that the
1820 * app can report when it is fully in a usable state; without this, the only thing
1821 * the system itself can determine is the point at which the activity's window
1822 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001823 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001824 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001825 * entirely drawn your UI and populated with all of the significant data. You
1826 * can safely call this method any time after first launch as well, in which case
1827 * it will simply be ignored.
1828 */
1829 public void reportFullyDrawn() {
1830 if (mDoReportFullyDrawn) {
1831 mDoReportFullyDrawn = false;
1832 try {
1833 ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken);
1834 } catch (RemoteException e) {
1835 }
1836 }
1837 }
1838
1839 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08001840 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
1841 * visa-versa.
1842 * @see android.R.attr#resizeableActivity
1843 *
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001844 * @param inMultiWindow True if the activity is in multi-window mode.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001845 */
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001846 @CallSuper
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001847 public void onMultiWindowChanged(boolean inMultiWindow) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001848 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001849 "onMultiWindowChanged " + this + ": " + inMultiWindow);
Wale Ogunwale7c796812016-01-29 21:13:50 -08001850 mFragments.dispatchMultiWindowChanged(inMultiWindow);
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001851 if (mWindow != null) {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001852 mWindow.onMultiWindowChanged();
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001853 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001854 }
1855
1856 /**
1857 * Returns true if the activity is currently in multi-window mode.
1858 * @see android.R.attr#resizeableActivity
1859 *
1860 * @return True if the activity is in multi-window mode.
1861 */
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001862 public boolean inMultiWindow() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001863 try {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001864 return ActivityManagerNative.getDefault().inMultiWindow(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001865 } catch (RemoteException e) {
1866 }
1867 return false;
1868 }
1869
1870 /**
1871 * Called by the system when the activity changes to and from picture-in-picture mode.
1872 * @see android.R.attr#supportsPictureInPicture
1873 *
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001874 * @param inPictureInPicture True if the activity is in picture-in-picture mode.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001875 */
Wale Ogunwale7c796812016-01-29 21:13:50 -08001876 @CallSuper
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001877 public void onPictureInPictureChanged(boolean inPictureInPicture) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001878 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001879 "onPictureInPictureChanged " + this + ": " + inPictureInPicture);
Wale Ogunwale7c796812016-01-29 21:13:50 -08001880 mFragments.dispatchPictureInPictureChanged(inPictureInPicture);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001881 }
1882
1883 /**
1884 * Returns true if the activity is currently in picture-in-picture mode.
1885 * @see android.R.attr#supportsPictureInPicture
1886 *
1887 * @return True if the activity is in picture-in-picture mode.
1888 */
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001889 public boolean inPictureInPicture() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001890 try {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001891 return ActivityManagerNative.getDefault().inPictureInPicture(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001892 } catch (RemoteException e) {
1893 }
1894 return false;
1895 }
1896
1897 /**
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001898 * Puts the activity in picture-in-picture mode.
1899 * @see android.R.attr#supportsPictureInPicture
1900 */
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001901 public void enterPictureInPicture() {
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001902 try {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001903 ActivityManagerNative.getDefault().enterPictureInPicture(mToken);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001904 } catch (RemoteException e) {
1905 }
1906 }
1907
1908 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 * Called by the system when the device configuration changes while your
1910 * activity is running. Note that this will <em>only</em> be called if
1911 * you have selected configurations you would like to handle with the
1912 * {@link android.R.attr#configChanges} attribute in your manifest. If
1913 * any configuration change occurs that is not selected to be reported
1914 * by that attribute, then instead of reporting it the system will stop
1915 * and restart the activity (to have it launched with the new
1916 * configuration).
RoboErik55011652014-07-09 15:05:53 -07001917 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 * <p>At the time that this function has been called, your Resources
1919 * object will have been updated to return resource values matching the
1920 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07001921 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 * @param newConfig The new device configuration.
1923 */
1924 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001925 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01001927
Dianne Hackborn9d071802010-12-08 14:49:15 -08001928 mFragments.dispatchConfigurationChanged(newConfig);
1929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 if (mWindow != null) {
1931 // Pass the configuration changed event to the window
1932 mWindow.onConfigurationChanged(newConfig);
1933 }
Adam Powell45c0b192011-07-28 15:11:57 -07001934
1935 if (mActionBar != null) {
1936 // Do this last; the action bar will need to access
1937 // view changes from above.
1938 mActionBar.onConfigurationChanged(newConfig);
1939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 }
RoboErik55011652014-07-09 15:05:53 -07001941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 /**
1943 * If this activity is being destroyed because it can not handle a
1944 * configuration parameter being changed (and thus its
1945 * {@link #onConfigurationChanged(Configuration)} method is
1946 * <em>not</em> being called), then you can use this method to discover
1947 * the set of changes that have occurred while in the process of being
1948 * destroyed. Note that there is no guarantee that these will be
1949 * accurate (other changes could have happened at any time), so you should
1950 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07001951 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 * @return Returns a bit field of the configuration parameters that are
1953 * changing, as defined by the {@link android.content.res.Configuration}
1954 * class.
1955 */
1956 public int getChangingConfigurations() {
1957 return mConfigChangeFlags;
1958 }
RoboErik55011652014-07-09 15:05:53 -07001959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 /**
1961 * Retrieve the non-configuration instance data that was previously
1962 * returned by {@link #onRetainNonConfigurationInstance()}. This will
1963 * be available from the initial {@link #onCreate} and
1964 * {@link #onStart} calls to the new instance, allowing you to extract
1965 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07001966 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 * <p>Note that the data you retrieve here should <em>only</em> be used
1968 * as an optimization for handling configuration changes. You should always
1969 * be able to handle getting a null pointer back, and an activity must
1970 * still be able to restore itself to its previous state (through the
1971 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
1972 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07001973 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 * @return Returns the object previously returned by
1975 * {@link #onRetainNonConfigurationInstance()}.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07001976 *
1977 * @deprecated Use the new {@link Fragment} API
1978 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
1979 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001981 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07001982 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001984 return mLastNonConfigurationInstances != null
1985 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 }
RoboErik55011652014-07-09 15:05:53 -07001987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 /**
1989 * Called by the system, as part of destroying an
1990 * activity due to a configuration change, when it is known that a new
1991 * instance will immediately be created for the new configuration. You
1992 * can return any object you like here, including the activity instance
1993 * itself, which can later be retrieved by calling
1994 * {@link #getLastNonConfigurationInstance()} in the new activity
1995 * instance.
RoboErik55011652014-07-09 15:05:53 -07001996 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001997 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1998 * or later, consider instead using a {@link Fragment} with
1999 * {@link Fragment#setRetainInstance(boolean)
2000 * Fragment.setRetainInstance(boolean}.</em>
2001 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 * <p>This function is called purely as an optimization, and you must
2003 * not rely on it being called. When it is called, a number of guarantees
2004 * will be made to help optimize configuration switching:
2005 * <ul>
2006 * <li> The function will be called between {@link #onStop} and
2007 * {@link #onDestroy}.
2008 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002009 * created after this one's {@link #onDestroy()} is called. In particular,
2010 * <em>no</em> messages will be dispatched during this time (when the returned
2011 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 * <li> The object you return here will <em>always</em> be available from
2013 * the {@link #getLastNonConfigurationInstance()} method of the following
2014 * activity instance as described there.
2015 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002016 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 * <p>These guarantees are designed so that an activity can use this API
2018 * to propagate extensive state from the old to new activity instance, from
2019 * loaded bitmaps, to network connections, to evenly actively running
2020 * threads. Note that you should <em>not</em> propagate any data that
2021 * may change based on the configuration, including any data loaded from
2022 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002023 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002024 * <p>The guarantee of no message handling during the switch to the next
2025 * activity simplifies use with active objects. For example if your retained
2026 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2027 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2028 * not be called from the call here until you execute the next instance's
2029 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2030 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2031 * running in a separate thread.)
2032 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 * @return Return any Object holding the desired state to propagate to the
2034 * next activity instance.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002035 *
2036 * @deprecated Use the new {@link Fragment} API
2037 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
2038 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 */
2040 public Object onRetainNonConfigurationInstance() {
2041 return null;
2042 }
RoboErik55011652014-07-09 15:05:53 -07002043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 /**
2045 * Retrieve the non-configuration instance data that was previously
2046 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2047 * be available from the initial {@link #onCreate} and
2048 * {@link #onStart} calls to the new instance, allowing you to extract
2049 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002050 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 * <p>Note that the data you retrieve here should <em>only</em> be used
2052 * as an optimization for handling configuration changes. You should always
2053 * be able to handle getting a null pointer back, and an activity must
2054 * still be able to restore itself to its previous state (through the
2055 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2056 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002057 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 * @return Returns the object previously returned by
2059 * {@link #onRetainNonConfigurationChildInstances()}
2060 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002061 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002062 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2063 return mLastNonConfigurationInstances != null
2064 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 }
RoboErik55011652014-07-09 15:05:53 -07002066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 /**
2068 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2069 * it should return either a mapping from child activity id strings to arbitrary objects,
2070 * or null. This method is intended to be used by Activity framework subclasses that control a
2071 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2072 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2073 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002074 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2076 return null;
2077 }
RoboErik55011652014-07-09 15:05:53 -07002078
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002079 NonConfigurationInstances retainNonConfigurationInstances() {
2080 Object activity = onRetainNonConfigurationInstance();
2081 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002082 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002083 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
2084 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002085 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002086 return null;
2087 }
RoboErik55011652014-07-09 15:05:53 -07002088
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002089 NonConfigurationInstances nci = new NonConfigurationInstances();
2090 nci.activity = activity;
2091 nci.children = children;
2092 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002093 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002094 if (mVoiceInteractor != null) {
2095 mVoiceInteractor.retainInstance();
2096 nci.voiceInteractor = mVoiceInteractor;
2097 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002098 return nci;
2099 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002102 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002104 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002106
2107 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002108 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002109 mCalled = true;
2110 mFragments.dispatchTrimMemory(level);
2111 }
2112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002114 * Return the FragmentManager for interacting with fragments associated
2115 * with this activity.
2116 */
2117 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002118 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002119 }
RoboErik55011652014-07-09 15:05:53 -07002120
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002121 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002122 * Called when a Fragment is being attached to this activity, immediately
2123 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2124 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
2125 */
2126 public void onAttachFragment(Fragment fragment) {
2127 }
RoboErik55011652014-07-09 15:05:53 -07002128
Dianne Hackbornc8017682010-07-06 13:34:38 -07002129 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 * Wrapper around
2131 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2132 * that gives the resulting {@link Cursor} to call
2133 * {@link #startManagingCursor} so that the activity will manage its
2134 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002135 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002136 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2137 * or later, consider instead using {@link LoaderManager} instead, available
2138 * via {@link #getLoaderManager()}.</em>
2139 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002140 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2141 * this method, because the activity will do that for you at the appropriate time. However, if
2142 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2143 * not</em> automatically close the cursor and, in that case, you must call
2144 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 * @param uri The URI of the content provider to query.
2147 * @param projection List of columns to return.
2148 * @param selection SQL WHERE clause.
2149 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2154 * @see #startManagingCursor
2155 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002156 *
2157 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 */
Jason parks6ed50de2010-08-25 10:18:50 -05002159 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002160 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2161 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2163 if (c != null) {
2164 startManagingCursor(c);
2165 }
2166 return c;
2167 }
2168
2169 /**
2170 * Wrapper around
2171 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2172 * that gives the resulting {@link Cursor} to call
2173 * {@link #startManagingCursor} so that the activity will manage its
2174 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002175 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002176 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2177 * or later, consider instead using {@link LoaderManager} instead, available
2178 * via {@link #getLoaderManager()}.</em>
2179 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002180 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2181 * this method, because the activity will do that for you at the appropriate time. However, if
2182 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2183 * not</em> automatically close the cursor and, in that case, you must call
2184 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 * @param uri The URI of the content provider to query.
2187 * @param projection List of columns to return.
2188 * @param selection SQL WHERE clause.
2189 * @param selectionArgs The arguments to selection, if any ?s are pesent
2190 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002191 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002193 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2195 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002196 *
2197 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 */
Jason parks6ed50de2010-08-25 10:18:50 -05002199 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002200 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2201 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2203 if (c != null) {
2204 startManagingCursor(c);
2205 }
2206 return c;
2207 }
2208
2209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 * This method allows the activity to take care of managing the given
2211 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2212 * That is, when the activity is stopped it will automatically call
2213 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2214 * it will call {@link Cursor#requery} for you. When the activity is
2215 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002216 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002217 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2218 * or later, consider instead using {@link LoaderManager} instead, available
2219 * via {@link #getLoaderManager()}.</em>
2220 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002221 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2222 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2223 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2224 * <em>will not</em> automatically close the cursor and, in that case, you must call
2225 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002226 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002228 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2230 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002231 *
2232 * @deprecated Use the new {@link android.content.CursorLoader} class with
2233 * {@link LoaderManager} instead; this is also
2234 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 */
Jason parks6ed50de2010-08-25 10:18:50 -05002236 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 public void startManagingCursor(Cursor c) {
2238 synchronized (mManagedCursors) {
2239 mManagedCursors.add(new ManagedCursor(c));
2240 }
2241 }
2242
2243 /**
2244 * Given a Cursor that was previously given to
2245 * {@link #startManagingCursor}, stop the activity's management of that
2246 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002247 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002248 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002249 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002250 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002255 *
2256 * @deprecated Use the new {@link android.content.CursorLoader} class with
2257 * {@link LoaderManager} instead; this is also
2258 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 */
Jason parks6ed50de2010-08-25 10:18:50 -05002260 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 public void stopManagingCursor(Cursor c) {
2262 synchronized (mManagedCursors) {
2263 final int N = mManagedCursors.size();
2264 for (int i=0; i<N; i++) {
2265 ManagedCursor mc = mManagedCursors.get(i);
2266 if (mc.mCursor == c) {
2267 mManagedCursors.remove(i);
2268 break;
2269 }
2270 }
2271 }
2272 }
2273
2274 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002275 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2276 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002277 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002279 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 }
2282
2283 /**
2284 * Finds a view that was identified by the id attribute from the XML that
2285 * was processed in {@link #onCreate}.
2286 *
2287 * @return The view if found or null otherwise.
2288 */
Scott Kennedyc0519552015-02-11 15:33:10 -08002289 @Nullable
Tor Norbye7b9c9122013-05-30 16:48:33 -07002290 public View findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 return getWindow().findViewById(id);
2292 }
RoboErik55011652014-07-09 15:05:53 -07002293
Adam Powell33b97432010-04-20 10:01:14 -07002294 /**
2295 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002296 *
Adam Powell33b97432010-04-20 10:01:14 -07002297 * @return The Activity's ActionBar, or null if it does not have one.
2298 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002299 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002300 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002301 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002302 return mActionBar;
2303 }
Adam Powelle43340c2014-03-17 19:10:43 -07002304
2305 /**
2306 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2307 * Activity window.
2308 *
2309 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2310 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2311 * a traditional window decor action bar. The toolbar's menu will be populated with the
2312 * Activity's options menu and the navigation button will be wired through the standard
2313 * {@link android.R.id#home home} menu select action.</p>
2314 *
2315 * <p>In order to use a Toolbar within the Activity's window content the application
2316 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2317 *
Chris Banesc7d6c322016-01-27 14:09:16 +00002318 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07002319 */
Adam Powell37780142014-06-01 13:31:00 -07002320 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00002321 final ActionBar ab = getActionBar();
2322 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002323 throw new IllegalStateException("This Activity already has an action bar supplied " +
2324 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2325 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2326 }
Chris Banes21b25772016-01-04 20:41:59 +00002327
2328 // If we reach here then we're setting a new action bar
2329 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01002330 mMenuInflater = null;
2331
Chris Banes21b25772016-01-04 20:41:59 +00002332 // If we have an action bar currently, destroy it
2333 if (ab != null) {
2334 ab.onDestroy();
2335 }
2336
Chris Banesc7d6c322016-01-27 14:09:16 +00002337 if (toolbar != null) {
2338 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2339 mActionBar = tbab;
2340 mWindow.setCallback(tbab.getWrappedWindowCallback());
2341 } else {
2342 mActionBar = null;
2343 // Re-set the original window callback since we may have already set a Toolbar wrapper
2344 mWindow.setCallback(this);
2345 }
2346
2347 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002348 }
RoboErik55011652014-07-09 15:05:53 -07002349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 /**
Adam Powell33b97432010-04-20 10:01:14 -07002351 * Creates a new ActionBar, locates the inflated ActionBarView,
2352 * initializes the ActionBar with the view, and sets mActionBar.
2353 */
Adam Powelle43340c2014-03-17 19:10:43 -07002354 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002355 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002356
2357 // Initializing the window decor can change window feature flags.
2358 // Make sure that we have the correct set before performing the test below.
2359 window.getDecorView();
2360
Adam Powell9b4c8042010-08-10 15:36:44 -07002361 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002362 return;
2363 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002364
Adam Powelle43340c2014-03-17 19:10:43 -07002365 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002366 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002367
2368 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2369 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002370 }
RoboErik55011652014-07-09 15:05:53 -07002371
Adam Powell33b97432010-04-20 10:01:14 -07002372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 * Set the activity content from a layout resource. The resource will be
2374 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08002375 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002377 *
Romain Guy482b34a62011-01-20 10:59:28 -08002378 * @see #setContentView(android.view.View)
2379 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002381 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002383 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
2385
2386 /**
2387 * Set the activity content to an explicit view. This view is placed
2388 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002389 * view hierarchy. When calling this method, the layout parameters of the
2390 * specified view are ignored. Both the width and the height of the view are
2391 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2392 * your own layout parameters, invoke
2393 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2394 * instead.
RoboErik55011652014-07-09 15:05:53 -07002395 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08002397 *
2398 * @see #setContentView(int)
2399 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 */
2401 public void setContentView(View view) {
2402 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002403 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
2405
2406 /**
2407 * Set the activity content to an explicit view. This view is placed
2408 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002409 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002410 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 * @param view The desired content to display.
2412 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08002413 *
2414 * @see #setContentView(android.view.View)
2415 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 */
2417 public void setContentView(View view, ViewGroup.LayoutParams params) {
2418 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002419 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 }
2421
2422 /**
2423 * Add an additional content view to the activity. Added after any existing
2424 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002425 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 * @param view The desired content to display.
2427 * @param params Layout parameters for the view.
2428 */
2429 public void addContentView(View view, ViewGroup.LayoutParams params) {
2430 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002431 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 }
2433
2434 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002435 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2436 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2437 *
2438 * <p>This method will return non-null after content has been initialized (e.g. by using
2439 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2440 *
2441 * @return This window's content TransitionManager or null if none is set.
2442 */
2443 public TransitionManager getContentTransitionManager() {
2444 return getWindow().getTransitionManager();
2445 }
2446
2447 /**
2448 * Set the {@link TransitionManager} to use for default transitions in this window.
2449 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2450 *
2451 * @param tm The TransitionManager to use for scene changes.
2452 */
2453 public void setContentTransitionManager(TransitionManager tm) {
2454 getWindow().setTransitionManager(tm);
2455 }
2456
2457 /**
2458 * Retrieve the {@link Scene} representing this window's current content.
2459 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2460 *
2461 * <p>This method will return null if the current content is not represented by a Scene.</p>
2462 *
2463 * @return Current Scene being shown or null
2464 */
2465 public Scene getContentScene() {
2466 return getWindow().getContentScene();
2467 }
2468
2469 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002470 * Sets whether this activity is finished when touched outside its window's
2471 * bounds.
2472 */
2473 public void setFinishOnTouchOutside(boolean finish) {
2474 mWindow.setCloseOnTouchOutside(finish);
2475 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002476
2477 /** @hide */
2478 @IntDef({
2479 DEFAULT_KEYS_DISABLE,
2480 DEFAULT_KEYS_DIALER,
2481 DEFAULT_KEYS_SHORTCUT,
2482 DEFAULT_KEYS_SEARCH_LOCAL,
2483 DEFAULT_KEYS_SEARCH_GLOBAL})
2484 @Retention(RetentionPolicy.SOURCE)
2485 @interface DefaultKeyMode {}
2486
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2489 * keys.
RoboErik55011652014-07-09 15:05:53 -07002490 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 * @see #setDefaultKeyMode
2492 */
2493 static public final int DEFAULT_KEYS_DISABLE = 0;
2494 /**
2495 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2496 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002497 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 * @see #setDefaultKeyMode
2499 */
2500 static public final int DEFAULT_KEYS_DIALER = 1;
2501 /**
2502 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2503 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002504 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002506 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 * @see #setDefaultKeyMode
2508 */
2509 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2510 /**
2511 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2512 * will start an application-defined search. (If the application or activity does not
2513 * actually define a search, the the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002514 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002516 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 * @see #setDefaultKeyMode
2518 */
2519 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2520
2521 /**
2522 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2523 * will start a global search (typically web search, but some platforms may define alternate
2524 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002525 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002527 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 * @see #setDefaultKeyMode
2529 */
2530 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2531
2532 /**
2533 * Select the default key handling for this activity. This controls what
2534 * will happen to key events that are not otherwise handled. The default
2535 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2536 * floor. Other modes allow you to launch the dialer
2537 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2538 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002539 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002541 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 * <p>Note that the mode selected here does not impact the default
2543 * handling of system keys, such as the "back" and "menu" keys, and your
2544 * activity and its views always get a first chance to receive and handle
2545 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002546 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002548 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 * @see #DEFAULT_KEYS_DISABLE
2550 * @see #DEFAULT_KEYS_DIALER
2551 * @see #DEFAULT_KEYS_SHORTCUT
2552 * @see #DEFAULT_KEYS_SEARCH_LOCAL
2553 * @see #DEFAULT_KEYS_SEARCH_GLOBAL
2554 * @see #onKeyDown
2555 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002556 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 // Some modes use a SpannableStringBuilder to track & dispatch input events
2560 // This list must remain in sync with the switch in onKeyDown()
2561 switch (mode) {
2562 case DEFAULT_KEYS_DISABLE:
2563 case DEFAULT_KEYS_SHORTCUT:
2564 mDefaultKeySsb = null; // not used in these modes
2565 break;
2566 case DEFAULT_KEYS_DIALER:
2567 case DEFAULT_KEYS_SEARCH_LOCAL:
2568 case DEFAULT_KEYS_SEARCH_GLOBAL:
2569 mDefaultKeySsb = new SpannableStringBuilder();
2570 Selection.setSelection(mDefaultKeySsb,0);
2571 break;
2572 default:
2573 throw new IllegalArgumentException();
2574 }
2575 }
2576
2577 /**
2578 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002579 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 * is inside a TextView will not trigger the event (unless it is a navigation
2581 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002582 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 * <p>If the focused view didn't want this event, this method is called.
2584 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002585 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
2586 * by calling {@link #onBackPressed()}, though the behavior varies based
2587 * on the application compatibility mode: for
2588 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
2589 * it will set up the dispatch to call {@link #onKeyUp} where the action
2590 * will be performed; for earlier applications, it will perform the
2591 * action immediately in on-down, as those versions of the platform
2592 * behaved.
RoboErik55011652014-07-09 15:05:53 -07002593 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002594 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002595 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07002596 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002598 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 * this event and it should continue to be propagated.
2600 * @see #onKeyUp
2601 * @see android.view.KeyEvent
2602 */
2603 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002604 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002605 if (getApplicationInfo().targetSdkVersion
2606 >= Build.VERSION_CODES.ECLAIR) {
2607 event.startTracking();
2608 } else {
2609 onBackPressed();
2610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 return true;
2612 }
RoboErik55011652014-07-09 15:05:53 -07002613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
2615 return false;
2616 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08002617 Window w = getWindow();
2618 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
2619 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
2620 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002621 return true;
2622 }
2623 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 } else {
2625 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
2626 boolean clearSpannable = false;
2627 boolean handled;
2628 if ((event.getRepeatCount() != 0) || event.isSystem()) {
2629 clearSpannable = true;
2630 handled = false;
2631 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002632 handled = TextKeyListener.getInstance().onKeyDown(
2633 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 if (handled && mDefaultKeySsb.length() > 0) {
2635 // something useable has been typed - dispatch it now.
2636
2637 final String str = mDefaultKeySsb.toString();
2638 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07002639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 switch (mDefaultKeyMode) {
2641 case DEFAULT_KEYS_DIALER:
2642 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
2643 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07002644 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 break;
2646 case DEFAULT_KEYS_SEARCH_LOCAL:
2647 startSearch(str, false, null, false);
2648 break;
2649 case DEFAULT_KEYS_SEARCH_GLOBAL:
2650 startSearch(str, false, null, true);
2651 break;
2652 }
2653 }
2654 }
2655 if (clearSpannable) {
2656 mDefaultKeySsb.clear();
2657 mDefaultKeySsb.clearSpans();
2658 Selection.setSelection(mDefaultKeySsb,0);
2659 }
2660 return handled;
2661 }
2662 }
2663
2664 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002665 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
2666 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
2667 * the event).
2668 */
2669 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2670 return false;
2671 }
2672
2673 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002675 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 * is inside a TextView will not trigger the event (unless it is a navigation
2677 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002678 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002679 * <p>The default implementation handles KEYCODE_BACK to stop the activity
2680 * and go back.
RoboErik55011652014-07-09 15:05:53 -07002681 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002683 * further, or <code>false</code> to indicate that you have not handled
2684 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 * @see #onKeyDown
2686 * @see KeyEvent
2687 */
2688 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002689 if (getApplicationInfo().targetSdkVersion
2690 >= Build.VERSION_CODES.ECLAIR) {
2691 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
2692 && !event.isCanceled()) {
2693 onBackPressed();
2694 return true;
2695 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 return false;
2698 }
2699
2700 /**
2701 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
2702 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
2703 * the event).
2704 */
2705 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2706 return false;
2707 }
RoboErik55011652014-07-09 15:05:53 -07002708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002710 * Called when the activity has detected the user's press of the back
2711 * key. The default implementation simply finishes the current activity,
2712 * but you can override this to do whatever you want.
2713 */
2714 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07002715 if (mActionBar != null && mActionBar.collapseActionView()) {
2716 return;
2717 }
2718
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002719 if (!mFragments.getFragmentManager().popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07002720 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07002721 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002722 }
Jeff Brown64da12a2011-01-04 19:57:47 -08002723
2724 /**
2725 * Called when a key shortcut event is not handled by any of the views in the Activity.
2726 * Override this method to implement global key shortcuts for the Activity.
2727 * Key shortcuts can also be implemented by setting the
2728 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
2729 *
2730 * @param keyCode The value in event.getKeyCode().
2731 * @param event Description of the key event.
2732 * @return True if the key shortcut was handled.
2733 */
2734 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07002735 // Let the Action Bar have a chance at handling the shortcut.
2736 ActionBar actionBar = getActionBar();
2737 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08002738 }
2739
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 * Called when a touch screen event was not handled by any of the views
2742 * under it. This is most useful to process touch events that happen
2743 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07002744 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07002746 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 * @return Return true if you have consumed the event, false if you haven't.
2748 * The default implementation always returns false.
2749 */
2750 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002751 if (mWindow.shouldCloseOnTouch(this, event)) {
2752 finish();
2753 return true;
2754 }
RoboErik55011652014-07-09 15:05:53 -07002755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 return false;
2757 }
RoboErik55011652014-07-09 15:05:53 -07002758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 /**
2760 * Called when the trackball was moved and not handled by any of the
2761 * views inside of the activity. So, for example, if the trackball moves
2762 * while focus is on a button, you will receive a call here because
2763 * buttons do not normally do anything with trackball events. The call
2764 * here happens <em>before</em> trackball movements are converted to
2765 * DPAD key events, which then get sent back to the view hierarchy, and
2766 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07002767 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07002769 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 * @return Return true if you have consumed the event, false if you haven't.
2771 * The default implementation always returns false.
2772 */
2773 public boolean onTrackballEvent(MotionEvent event) {
2774 return false;
2775 }
Jeff Browncb1404e2011-01-15 18:14:15 -08002776
2777 /**
2778 * Called when a generic motion event was not handled by any of the
2779 * views inside of the activity.
2780 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08002781 * Generic motion events describe joystick movements, mouse hovers, track pad
2782 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08002783 * {@link MotionEvent#getSource() source} of the motion event specifies
2784 * the class of input that was received. Implementations of this method
2785 * must examine the bits in the source before processing the event.
2786 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08002787 * </p><p>
2788 * Generic motion events with source class
2789 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
2790 * are delivered to the view under the pointer. All other generic motion events are
2791 * delivered to the focused view.
2792 * </p><p>
2793 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
2794 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08002795 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08002796 *
2797 * @param event The generic motion event being processed.
2798 *
2799 * @return Return true if you have consumed the event, false if you haven't.
2800 * The default implementation always returns false.
2801 */
2802 public boolean onGenericMotionEvent(MotionEvent event) {
2803 return false;
2804 }
2805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 /**
2807 * Called whenever a key, touch, or trackball event is dispatched to the
2808 * activity. Implement this method if you wish to know that the user has
2809 * interacted with the device in some way while your activity is running.
2810 * This callback and {@link #onUserLeaveHint} are intended to help
2811 * activities manage status bar notifications intelligently; specifically,
2812 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07002813 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
2815 * be accompanied by calls to {@link #onUserInteraction}. This
2816 * ensures that your activity will be told of relevant user activity such
2817 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07002818 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 * <p>Note that this callback will be invoked for the touch down action
2820 * that begins a touch gesture, but may not be invoked for the touch-moved
2821 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07002822 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 * @see #onUserLeaveHint()
2824 */
2825 public void onUserInteraction() {
2826 }
RoboErik55011652014-07-09 15:05:53 -07002827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
2829 // Update window manager if: we have a view, that view is
2830 // attached to its parent (which will be a RootView), and
2831 // this activity is not embedded.
2832 if (mParent == null) {
2833 View decor = mDecor;
2834 if (decor != null && decor.getParent() != null) {
2835 getWindowManager().updateViewLayout(decor, params);
2836 }
2837 }
2838 }
2839
2840 public void onContentChanged() {
2841 }
2842
2843 /**
2844 * Called when the current {@link Window} of the activity gains or loses
2845 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002846 * to the user. The default implementation clears the key tracking
2847 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07002848 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002849 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 * is managed independently of activity lifecycles. As such, while focus
2851 * changes will generally have some relation to lifecycle changes (an
2852 * activity that is stopped will not generally get window focus), you
2853 * should not rely on any particular order between the callbacks here and
2854 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07002855 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 * <p>As a general rule, however, a resumed activity will have window
2857 * focus... unless it has displayed other dialogs or popups that take
2858 * input focus, in which case the activity itself will not have focus
2859 * when the other windows have it. Likewise, the system may display
2860 * system-level windows (such as the status bar notification panel or
2861 * a system alert) which will temporarily take window input focus without
2862 * pausing the foreground activity.
2863 *
2864 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07002865 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 * @see #hasWindowFocus()
2867 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002868 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 */
2870 public void onWindowFocusChanged(boolean hasFocus) {
2871 }
RoboErik55011652014-07-09 15:05:53 -07002872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002874 * Called when the main window associated with the activity has been
2875 * attached to the window manager.
2876 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
2877 * for more information.
2878 * @see View#onAttachedToWindow
2879 */
2880 public void onAttachedToWindow() {
2881 }
RoboErik55011652014-07-09 15:05:53 -07002882
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002883 /**
2884 * Called when the main window associated with the activity has been
2885 * detached from the window manager.
2886 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
2887 * for more information.
2888 * @see View#onDetachedFromWindow
2889 */
2890 public void onDetachedFromWindow() {
2891 }
RoboErik55011652014-07-09 15:05:53 -07002892
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 * Returns true if this activity's <em>main</em> window currently has window focus.
2895 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07002896 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07002898 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
2900 */
2901 public boolean hasWindowFocus() {
2902 Window w = getWindow();
2903 if (w != null) {
2904 View d = w.getDecorView();
2905 if (d != null) {
2906 return d.hasWindowFocus();
2907 }
2908 }
2909 return false;
2910 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002911
2912 /**
2913 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07002914 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002915 */
Adam Powell117b6952014-05-05 18:14:56 -07002916 @Override
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002917 public void onWindowDismissed(boolean finishTask) {
2918 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002919 }
RoboErik55011652014-07-09 15:05:53 -07002920
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002921
Skuhnece2faa52015-08-11 10:36:38 -07002922 /**
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08002923 * Moves the activity from
2924 * {@link android.app.ActivityManager.StackId#FREEFORM_WORKSPACE_STACK_ID} to
2925 * {@link android.app.ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} stack.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002926 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002927 * @hide
2928 */
2929 @Override
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08002930 public void exitFreeformMode() throws RemoteException {
2931 ActivityManagerNative.getDefault().exitFreeformMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002932 }
2933
2934 /** Returns the current stack Id for the window.
2935 * @hide
2936 */
2937 @Override
2938 public int getWindowStackId() throws RemoteException {
2939 return ActivityManagerNative.getDefault().getActivityStackId(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 }
2941
2942 /**
RoboErik55011652014-07-09 15:05:53 -07002943 * Called to process key events. You can override this to intercept all
2944 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07002946 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07002948 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 * @return boolean Return true if this event was consumed.
2950 */
2951 public boolean dispatchKeyEvent(KeyEvent event) {
2952 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07002953
2954 // Let action bars open menus in response to the menu key prioritized over
2955 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08002956 final int keyCode = event.getKeyCode();
2957 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07002958 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
2959 return true;
George Mountcbe28352016-01-06 16:24:26 -08002960 } else if (event.isCtrlPressed() &&
2961 event.getUnicodeChar(event.getMetaState() & ~KeyEvent.META_CTRL_MASK) == '<') {
2962 // Capture the Control-< and send focus to the ActionBar
George Mount5beb26172015-12-15 13:36:01 -08002963 final int action = event.getAction();
2964 if (action == KeyEvent.ACTION_DOWN) {
George Mountcbe28352016-01-06 16:24:26 -08002965 final ActionBar actionBar = getActionBar();
2966 if (actionBar != null && actionBar.isShowing() && actionBar.requestFocus()) {
2967 mEatKeyUpEvent = true;
2968 return true;
George Mount5beb26172015-12-15 13:36:01 -08002969 }
2970 } else if (action == KeyEvent.ACTION_UP && mEatKeyUpEvent) {
2971 mEatKeyUpEvent = false;
2972 return true;
2973 }
Adam Powell07a74542014-05-30 15:52:44 -07002974 }
2975
Dianne Hackborn8d374262009-09-14 21:21:52 -07002976 Window win = getWindow();
2977 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 return true;
2979 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002980 View decor = mDecor;
2981 if (decor == null) decor = win.getDecorView();
2982 return event.dispatch(this, decor != null
2983 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 }
2985
2986 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08002987 * Called to process a key shortcut event.
2988 * You can override this to intercept all key shortcut events before they are
2989 * dispatched to the window. Be sure to call this implementation for key shortcut
2990 * events that should be handled normally.
2991 *
2992 * @param event The key shortcut event.
2993 * @return True if this event was consumed.
2994 */
2995 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
2996 onUserInteraction();
2997 if (getWindow().superDispatchKeyShortcutEvent(event)) {
2998 return true;
2999 }
3000 return onKeyShortcut(event.getKeyCode(), event);
3001 }
3002
3003 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 * Called to process touch screen events. You can override this to
3005 * intercept all touch screen events before they are dispatched to the
3006 * window. Be sure to call this implementation for touch screen events
3007 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003010 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 * @return boolean Return true if this event was consumed.
3012 */
3013 public boolean dispatchTouchEvent(MotionEvent ev) {
3014 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3015 onUserInteraction();
3016 }
3017 if (getWindow().superDispatchTouchEvent(ev)) {
3018 return true;
3019 }
3020 return onTouchEvent(ev);
3021 }
RoboErik55011652014-07-09 15:05:53 -07003022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 /**
3024 * Called to process trackball events. You can override this to
3025 * intercept all trackball events before they are dispatched to the
3026 * window. Be sure to call this implementation for trackball events
3027 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003028 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003030 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 * @return boolean Return true if this event was consumed.
3032 */
3033 public boolean dispatchTrackballEvent(MotionEvent ev) {
3034 onUserInteraction();
3035 if (getWindow().superDispatchTrackballEvent(ev)) {
3036 return true;
3037 }
3038 return onTrackballEvent(ev);
3039 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003040
Jeff Browncb1404e2011-01-15 18:14:15 -08003041 /**
3042 * Called to process generic motion events. You can override this to
3043 * intercept all generic motion events before they are dispatched to the
3044 * window. Be sure to call this implementation for generic motion events
3045 * that should be handled normally.
3046 *
3047 * @param ev The generic motion event.
3048 *
3049 * @return boolean Return true if this event was consumed.
3050 */
3051 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3052 onUserInteraction();
3053 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3054 return true;
3055 }
3056 return onGenericMotionEvent(ev);
3057 }
3058
svetoslavganov75986cf2009-05-14 22:28:01 -07003059 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3060 event.setClassName(getClass().getName());
3061 event.setPackageName(getPackageName());
3062
3063 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003064 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3065 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003066 event.setFullScreen(isFullScreen);
3067
3068 CharSequence title = getTitle();
3069 if (!TextUtils.isEmpty(title)) {
3070 event.getText().add(title);
3071 }
3072
3073 return true;
3074 }
3075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 /**
3077 * Default implementation of
3078 * {@link android.view.Window.Callback#onCreatePanelView}
3079 * for activities. This
3080 * simply returns null so that all panel sub-windows will have the default
3081 * menu behavior.
3082 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003083 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 public View onCreatePanelView(int featureId) {
3085 return null;
3086 }
3087
3088 /**
3089 * Default implementation of
3090 * {@link android.view.Window.Callback#onCreatePanelMenu}
3091 * for activities. This calls through to the new
3092 * {@link #onCreateOptionsMenu} method for the
3093 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3094 * so that subclasses of Activity don't need to deal with feature codes.
3095 */
3096 public boolean onCreatePanelMenu(int featureId, Menu menu) {
3097 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003098 boolean show = onCreateOptionsMenu(menu);
3099 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3100 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 }
3102 return false;
3103 }
3104
3105 /**
3106 * Default implementation of
3107 * {@link android.view.Window.Callback#onPreparePanel}
3108 * for activities. This
3109 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3110 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3111 * panel, so that subclasses of
3112 * Activity don't need to deal with feature codes.
3113 */
3114 public boolean onPreparePanel(int featureId, View view, Menu menu) {
3115 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
3116 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003117 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003118 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 }
3120 return true;
3121 }
3122
3123 /**
3124 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003125 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 * @return The default implementation returns true.
3127 */
3128 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003129 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003130 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003131 if (mActionBar != null) {
3132 mActionBar.dispatchMenuVisibilityChanged(true);
3133 } else {
3134 Log.e(TAG, "Tried to open action bar menu with no action bar");
3135 }
Adam Powell8515ee82010-11-30 14:09:55 -08003136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 return true;
3138 }
3139
3140 /**
3141 * Default implementation of
3142 * {@link android.view.Window.Callback#onMenuItemSelected}
3143 * for activities. This calls through to the new
3144 * {@link #onOptionsItemSelected} method for the
3145 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3146 * panel, so that subclasses of
3147 * Activity don't need to deal with feature codes.
3148 */
3149 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09003150 CharSequence titleCondensed = item.getTitleCondensed();
3151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 switch (featureId) {
3153 case Window.FEATURE_OPTIONS_PANEL:
3154 // Put event logging here so it gets called even if subclass
3155 // doesn't call through to superclass's implmeentation of each
3156 // of these methods below
zobject686a8052012-12-14 21:11:08 +09003157 if(titleCondensed != null) {
3158 EventLog.writeEvent(50000, 0, titleCondensed.toString());
3159 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003160 if (onOptionsItemSelected(item)) {
3161 return true;
3162 }
Adam Powell04d58112012-04-09 10:22:12 -07003163 if (mFragments.dispatchOptionsItemSelected(item)) {
3164 return true;
3165 }
3166 if (item.getItemId() == android.R.id.home && mActionBar != null &&
3167 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
3168 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07003169 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07003170 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07003171 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07003172 }
Adam Powell04d58112012-04-09 10:22:12 -07003173 }
3174 return false;
RoboErik55011652014-07-09 15:05:53 -07003175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09003177 if(titleCondensed != null) {
3178 EventLog.writeEvent(50000, 1, titleCondensed.toString());
3179 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07003180 if (onContextItemSelected(item)) {
3181 return true;
3182 }
3183 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08003184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185 default:
3186 return false;
3187 }
3188 }
RoboErik55011652014-07-09 15:05:53 -07003189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 /**
3191 * Default implementation of
3192 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
3193 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
3194 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3195 * so that subclasses of Activity don't need to deal with feature codes.
3196 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
3197 * {@link #onContextMenuClosed(Menu)} will be called.
3198 */
3199 public void onPanelClosed(int featureId, Menu menu) {
3200 switch (featureId) {
3201 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003202 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 onOptionsMenuClosed(menu);
3204 break;
RoboErik55011652014-07-09 15:05:53 -07003205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 case Window.FEATURE_CONTEXT_MENU:
3207 onContextMenuClosed(menu);
3208 break;
Adam Powell8515ee82010-11-30 14:09:55 -08003209
3210 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07003211 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08003212 mActionBar.dispatchMenuVisibilityChanged(false);
3213 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 }
3215 }
3216
3217 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003218 * Declare that the options menu has changed, so should be recreated.
3219 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
3220 * time it needs to be displayed.
3221 */
3222 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003223 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3224 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003225 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
3226 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003227 }
RoboErik55011652014-07-09 15:05:53 -07003228
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 * Initialize the contents of the Activity's standard options menu. You
3231 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07003232 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 * <p>This is only called once, the first time the options menu is
3234 * displayed. To update the menu every time it is displayed, see
3235 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07003236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07003238 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
3239 * they will be correctly ordered with application-defined menu items.
3240 * Deriving classes should always call through to the base implementation.
3241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 * <p>You can safely hold on to <var>menu</var> (and any items created
3243 * from it), making modifications to it as desired, until the next
3244 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07003245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 * <p>When you add items to the menu, you can implement the Activity's
3247 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07003248 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07003250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 * @return You must return true for the menu to be displayed;
3252 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 * @see #onPrepareOptionsMenu
3255 * @see #onOptionsItemSelected
3256 */
3257 public boolean onCreateOptionsMenu(Menu menu) {
3258 if (mParent != null) {
3259 return mParent.onCreateOptionsMenu(menu);
3260 }
3261 return true;
3262 }
3263
3264 /**
3265 * Prepare the Screen's standard options menu to be displayed. This is
3266 * called right before the menu is shown, every time it is shown. You can
3267 * use this method to efficiently enable/disable items or otherwise
3268 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07003269 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 * <p>The default implementation updates the system menu items based on the
3271 * activity's state. Deriving classes should always call through to the
3272 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07003273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 * @param menu The options menu as last shown or first initialized by
3275 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07003276 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 * @return You must return true for the menu to be displayed;
3278 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003279 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 * @see #onCreateOptionsMenu
3281 */
3282 public boolean onPrepareOptionsMenu(Menu menu) {
3283 if (mParent != null) {
3284 return mParent.onPrepareOptionsMenu(menu);
3285 }
3286 return true;
3287 }
3288
3289 /**
3290 * This hook is called whenever an item in your options menu is selected.
3291 * The default implementation simply returns false to have the normal
3292 * processing happen (calling the item's Runnable or sending a message to
3293 * its Handler as appropriate). You can use this method for any items
3294 * for which you would like to do processing without those other
3295 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003296 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003298 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003301 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 * @return boolean Return false to allow normal menu processing to
3303 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003304 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 * @see #onCreateOptionsMenu
3306 */
3307 public boolean onOptionsItemSelected(MenuItem item) {
3308 if (mParent != null) {
3309 return mParent.onOptionsItemSelected(item);
3310 }
3311 return false;
3312 }
3313
3314 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003315 * This method is called whenever the user chooses to navigate Up within your application's
3316 * activity hierarchy from the action bar.
3317 *
3318 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3319 * was specified in the manifest for this activity or an activity-alias to it,
3320 * default Up navigation will be handled automatically. If any activity
3321 * along the parent chain requires extra Intent arguments, the Activity subclass
3322 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3323 * to supply those arguments.</p>
3324 *
3325 * <p>See <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
3326 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3327 * from the design guide for more information about navigating within your app.</p>
3328 *
3329 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3330 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3331 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3332 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3333 *
3334 * @return true if Up navigation completed successfully and this Activity was finished,
3335 * false otherwise.
3336 */
3337 public boolean onNavigateUp() {
3338 // Automatically handle hierarchical Up navigation if the proper
3339 // metadata is available.
3340 Intent upIntent = getParentActivityIntent();
3341 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003342 if (mActivityInfo.taskAffinity == null) {
3343 // Activities with a null affinity are special; they really shouldn't
3344 // specify a parent activity intent in the first place. Just finish
3345 // the current activity and call it a day.
3346 finish();
3347 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003348 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003349 onCreateNavigateUpTaskStack(b);
3350 onPrepareNavigateUpTaskStack(b);
3351 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003352
3353 // We can't finishAffinity if we have a result.
3354 // Fall back and simply finish the current activity instead.
3355 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3356 // Tell the developer what's going on to avoid hair-pulling.
3357 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3358 finish();
3359 } else {
3360 finishAffinity();
3361 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003362 } else {
3363 navigateUpTo(upIntent);
3364 }
3365 return true;
3366 }
3367 return false;
3368 }
3369
3370 /**
3371 * This is called when a child activity of this one attempts to navigate up.
3372 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3373 *
3374 * @param child The activity making the call.
3375 */
3376 public boolean onNavigateUpFromChild(Activity child) {
3377 return onNavigateUp();
3378 }
3379
3380 /**
3381 * Define the synthetic task stack that will be generated during Up navigation from
3382 * a different task.
3383 *
3384 * <p>The default implementation of this method adds the parent chain of this activity
3385 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3386 * may choose to override this method to construct the desired task stack in a different
3387 * way.</p>
3388 *
Adam Powellf0195952012-05-02 21:38:54 -07003389 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3390 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3391 * returned by {@link #getParentActivityIntent()}.</p>
3392 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003393 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3394 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3395 *
3396 * @param builder An empty TaskStackBuilder - the application should add intents representing
3397 * the desired task stack
3398 */
3399 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3400 builder.addParentStack(this);
3401 }
3402
3403 /**
3404 * Prepare the synthetic task stack that will be generated during Up navigation
3405 * from a different task.
3406 *
3407 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3408 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3409 * If any extra data should be added to these intents before launching the new task,
3410 * the application should override this method and add that data here.</p>
3411 *
3412 * @param builder A TaskStackBuilder that has been populated with Intents by
3413 * onCreateNavigateUpTaskStack.
3414 */
3415 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3416 }
3417
3418 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 * This hook is called whenever the options menu is being closed (either by the user canceling
3420 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 * @param menu The options menu as last shown or first initialized by
3423 * onCreateOptionsMenu().
3424 */
3425 public void onOptionsMenuClosed(Menu menu) {
3426 if (mParent != null) {
3427 mParent.onOptionsMenuClosed(menu);
3428 }
3429 }
RoboErik55011652014-07-09 15:05:53 -07003430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 /**
3432 * Programmatically opens the options menu. If the options menu is already
3433 * open, this method does nothing.
3434 */
3435 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003436 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3437 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003438 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 }
RoboErik55011652014-07-09 15:05:53 -07003441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 /**
3443 * Progammatically closes the options menu. If the options menu is already
3444 * closed, this method does nothing.
3445 */
3446 public void closeOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003447 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
3448 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 }
3451
3452 /**
3453 * Called when a context menu for the {@code view} is about to be shown.
3454 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3455 * time the context menu is about to be shown and should be populated for
3456 * the view (or item inside the view for {@link AdapterView} subclasses,
3457 * this can be found in the {@code menuInfo})).
3458 * <p>
3459 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3460 * item has been selected.
3461 * <p>
3462 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 */
3465 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3466 }
3467
3468 /**
3469 * Registers a context menu to be shown for the given view (multiple views
3470 * can show the context menu). This method will set the
3471 * {@link OnCreateContextMenuListener} on the view to this activity, so
3472 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3473 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003474 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 * @see #unregisterForContextMenu(View)
3476 * @param view The view that should show a context menu.
3477 */
3478 public void registerForContextMenu(View view) {
3479 view.setOnCreateContextMenuListener(this);
3480 }
RoboErik55011652014-07-09 15:05:53 -07003481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 /**
3483 * Prevents a context menu to be shown for the given view. This method will remove the
3484 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003485 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 * @see #registerForContextMenu(View)
3487 * @param view The view that should stop showing a context menu.
3488 */
3489 public void unregisterForContextMenu(View view) {
3490 view.setOnCreateContextMenuListener(null);
3491 }
RoboErik55011652014-07-09 15:05:53 -07003492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 /**
3494 * Programmatically opens the context menu for a particular {@code view}.
3495 * The {@code view} should have been added via
3496 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003497 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 * @param view The view to show the context menu for.
3499 */
3500 public void openContextMenu(View view) {
3501 view.showContextMenu();
3502 }
RoboErik55011652014-07-09 15:05:53 -07003503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 /**
3505 * Programmatically closes the most recently opened context menu, if showing.
3506 */
3507 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08003508 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
3509 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 }
RoboErik55011652014-07-09 15:05:53 -07003512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 /**
3514 * This hook is called whenever an item in a context menu is selected. The
3515 * default implementation simply returns false to have the normal processing
3516 * happen (calling the item's Runnable or sending a message to its Handler
3517 * as appropriate). You can use this method for any items for which you
3518 * would like to do processing without those other facilities.
3519 * <p>
3520 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3521 * View that added this menu item.
3522 * <p>
3523 * Derived classes should call through to the base class for it to perform
3524 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003525 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 * @param item The context menu item that was selected.
3527 * @return boolean Return false to allow normal context menu processing to
3528 * proceed, true to consume it here.
3529 */
3530 public boolean onContextItemSelected(MenuItem item) {
3531 if (mParent != null) {
3532 return mParent.onContextItemSelected(item);
3533 }
3534 return false;
3535 }
3536
3537 /**
3538 * This hook is called whenever the context menu is being closed (either by
3539 * the user canceling the menu with the back/menu button, or when an item is
3540 * selected).
RoboErik55011652014-07-09 15:05:53 -07003541 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 * @param menu The context menu that is being closed.
3543 */
3544 public void onContextMenuClosed(Menu menu) {
3545 if (mParent != null) {
3546 mParent.onContextMenuClosed(menu);
3547 }
3548 }
3549
3550 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003551 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003553 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 protected Dialog onCreateDialog(int id) {
3555 return null;
3556 }
3557
3558 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003559 * Callback for creating dialogs that are managed (saved and restored) for you
3560 * by the activity. The default implementation calls through to
3561 * {@link #onCreateDialog(int)} for compatibility.
3562 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003563 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3564 * or later, consider instead using a {@link DialogFragment} instead.</em>
3565 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003566 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3567 * this method the first time, and hang onto it thereafter. Any dialog
3568 * that is created by this method will automatically be saved and restored
3569 * for you, including whether it is showing.
3570 *
3571 * <p>If you would like the activity to manage saving and restoring dialogs
3572 * for you, you should override this method and handle any ids that are
3573 * passed to {@link #showDialog}.
3574 *
3575 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3576 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3577 *
3578 * @param id The id of the dialog.
3579 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3580 * @return The dialog. If you return null, the dialog will not be created.
3581 *
3582 * @see #onPrepareDialog(int, Dialog, Bundle)
3583 * @see #showDialog(int, Bundle)
3584 * @see #dismissDialog(int)
3585 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003586 *
3587 * @deprecated Use the new {@link DialogFragment} class with
3588 * {@link FragmentManager} instead; this is also
3589 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003590 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003591 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003592 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003593 protected Dialog onCreateDialog(int id, Bundle args) {
3594 return onCreateDialog(id);
3595 }
3596
3597 /**
3598 * @deprecated Old no-arguments version of
3599 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
3600 */
3601 @Deprecated
3602 protected void onPrepareDialog(int id, Dialog dialog) {
3603 dialog.setOwnerActivity(this);
3604 }
3605
3606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003608 * shown. The default implementation calls through to
3609 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07003610 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 * <p>
3612 * Override this if you need to update a managed dialog based on the state
3613 * of the application each time it is shown. For example, a time picker
3614 * dialog might want to be updated with the current time. You should call
3615 * through to the superclass's implementation. The default implementation
3616 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07003617 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 * @param id The id of the managed dialog.
3619 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003620 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3621 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 * @see #showDialog(int)
3623 * @see #dismissDialog(int)
3624 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003625 *
3626 * @deprecated Use the new {@link DialogFragment} class with
3627 * {@link FragmentManager} instead; this is also
3628 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003629 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003630 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003631 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
3632 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 }
3634
3635 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003636 * Simple version of {@link #showDialog(int, Bundle)} that does not
3637 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
3638 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003639 *
3640 * @deprecated Use the new {@link DialogFragment} class with
3641 * {@link FragmentManager} instead; this is also
3642 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003643 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003644 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003645 public final void showDialog(int id) {
3646 showDialog(id, null);
3647 }
3648
3649 /**
3650 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 * will be made with the same id the first time this is called for a given
3652 * id. From thereafter, the dialog will be automatically saved and restored.
3653 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003654 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3655 * or later, consider instead using a {@link DialogFragment} instead.</em>
3656 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003657 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 * be made to provide an opportunity to do any timely preparation.
3659 *
3660 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003661 * @param args Arguments to pass through to the dialog. These will be saved
3662 * and restored for you. Note that if the dialog is already created,
3663 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
3664 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08003665 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003666 * @return Returns true if the Dialog was created; false is returned if
3667 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07003668 *
Joe Onorato37296dc2009-07-31 17:58:55 -07003669 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003670 * @see #onCreateDialog(int, Bundle)
3671 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 * @see #dismissDialog(int)
3673 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003674 *
3675 * @deprecated Use the new {@link DialogFragment} class with
3676 * {@link FragmentManager} instead; this is also
3677 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003679 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003680 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003681 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003683 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003685 ManagedDialog md = mManagedDialogs.get(id);
3686 if (md == null) {
3687 md = new ManagedDialog();
3688 md.mDialog = createDialog(id, null, args);
3689 if (md.mDialog == null) {
3690 return false;
3691 }
3692 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 }
RoboErik55011652014-07-09 15:05:53 -07003694
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003695 md.mArgs = args;
3696 onPrepareDialog(id, md.mDialog, args);
3697 md.mDialog.show();
3698 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 }
3700
3701 /**
3702 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
3703 *
3704 * @param id The id of the managed dialog.
3705 *
3706 * @throws IllegalArgumentException if the id was not previously shown via
3707 * {@link #showDialog(int)}.
3708 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003709 * @see #onCreateDialog(int, Bundle)
3710 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 * @see #showDialog(int)
3712 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003713 *
3714 * @deprecated Use the new {@link DialogFragment} class with
3715 * {@link FragmentManager} instead; this is also
3716 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003718 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 public final void dismissDialog(int id) {
3720 if (mManagedDialogs == null) {
3721 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
RoboErik55011652014-07-09 15:05:53 -07003723
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003724 final ManagedDialog md = mManagedDialogs.get(id);
3725 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 throw missingDialog(id);
3727 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003728 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 }
3730
3731 /**
3732 * Creates an exception to throw if a user passed in a dialog id that is
3733 * unexpected.
3734 */
3735 private IllegalArgumentException missingDialog(int id) {
3736 return new IllegalArgumentException("no dialog with id " + id + " was ever "
3737 + "shown via Activity#showDialog");
3738 }
3739
3740 /**
3741 * Removes any internal references to a dialog managed by this Activity.
3742 * If the dialog is showing, it will dismiss it as part of the clean up.
3743 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003744 * <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 -08003745 * want to avoid the overhead of saving and restoring it in the future.
3746 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003747 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
3748 * will not throw an exception if you try to remove an ID that does not
3749 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07003750 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 * @param id The id of the managed dialog.
3752 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003753 * @see #onCreateDialog(int, Bundle)
3754 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 * @see #showDialog(int)
3756 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003757 *
3758 * @deprecated Use the new {@link DialogFragment} class with
3759 * {@link FragmentManager} instead; this is also
3760 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003762 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003764 if (mManagedDialogs != null) {
3765 final ManagedDialog md = mManagedDialogs.get(id);
3766 if (md != null) {
3767 md.mDialog.dismiss();
3768 mManagedDialogs.remove(id);
3769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 }
3772
3773 /**
3774 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07003775 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01003776 * <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 -07003777 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01003778 * calling this function is the same as calling
3779 * {@link #startSearch startSearch(null, false, null, false)}, which launches
3780 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07003781 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 * <p>You can override this function to force global search, e.g. in response to a dedicated
3783 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07003784 *
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003785 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default
3786 * implementation changes to simply return false and you must supply your own custom
3787 * implementation if you want to support search.</p>
3788 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003789 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003790 * @return Returns {@code true} if search launched, and {@code false} if the activity does
3791 * not respond to search. The default implementation always returns {@code true}, except
3792 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07003793 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 * @see android.app.SearchManager
3795 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003796 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
3797 mSearchEvent = searchEvent;
3798 boolean result = onSearchRequested();
3799 mSearchEvent = null;
3800 return result;
3801 }
3802
3803 /**
3804 * @see #onSearchRequested(SearchEvent)
3805 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 public boolean onSearchRequested() {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003807 if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK)
3808 != Configuration.UI_MODE_TYPE_TELEVISION) {
3809 startSearch(null, false, null, false);
3810 return true;
3811 } else {
3812 return false;
3813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 }
RoboErik55011652014-07-09 15:05:53 -07003815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003817 * During the onSearchRequested() callbacks, this function will return the
3818 * {@link SearchEvent} that triggered the callback, if it exists.
3819 *
3820 * @return SearchEvent The SearchEvent that triggered the {@link
3821 * #onSearchRequested} callback.
3822 */
3823 public final SearchEvent getSearchEvent() {
3824 return mSearchEvent;
3825 }
3826
3827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07003829 *
3830 * <p>It is typically called from onSearchRequested(), either directly from
3831 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07003833 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 * is to inject specific data such as context data, it is preferred to <i>override</i>
3835 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07003836 *
3837 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07003839 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 * any further typing will replace it. This is useful for cases where an entire pre-formed
3841 * query is being inserted. If false, the selection point will be placed at the end of the
3842 * inserted query. This is useful when the inserted query is text that the user entered,
3843 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
3844 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07003845 * @param appSearchData An application can insert application-specific
3846 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 * searches. This data will be returned with SEARCH intent(s). Null if
3848 * no extra data is required.
3849 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07003850 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07003851 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07003853 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854 * @see android.app.SearchManager
3855 * @see #onSearchRequested
3856 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003857 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
3858 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003859 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01003860 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07003861 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 }
3863
3864 /**
krosaend2d60142009-08-17 08:56:48 -07003865 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
3866 * the search dialog. Made available for testing purposes.
3867 *
3868 * @param query The query to trigger. If empty, the request will be ignored.
3869 * @param appSearchData An application can insert application-specific
3870 * context here, in order to improve quality or specificity of its own
3871 * searches. This data will be returned with SEARCH intent(s). Null if
3872 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07003873 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003874 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07003875 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01003876 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07003877 }
3878
3879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 * Request that key events come to this activity. Use this if your
3881 * activity has no views with focus, but the activity still wants
3882 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07003883 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 * @see android.view.Window#takeKeyEvents
3885 */
3886 public void takeKeyEvents(boolean get) {
3887 getWindow().takeKeyEvents(get);
3888 }
3889
3890 /**
3891 * Enable extended window features. This is a convenience for calling
3892 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07003893 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 * @param featureId The desired feature as defined in
3895 * {@link android.view.Window}.
3896 * @return Returns true if the requested feature is supported and now
3897 * enabled.
RoboErik55011652014-07-09 15:05:53 -07003898 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 * @see android.view.Window#requestFeature
3900 */
3901 public final boolean requestWindowFeature(int featureId) {
3902 return getWindow().requestFeature(featureId);
3903 }
3904
3905 /**
3906 * Convenience for calling
3907 * {@link android.view.Window#setFeatureDrawableResource}.
3908 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003909 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 getWindow().setFeatureDrawableResource(featureId, resId);
3911 }
3912
3913 /**
3914 * Convenience for calling
3915 * {@link android.view.Window#setFeatureDrawableUri}.
3916 */
3917 public final void setFeatureDrawableUri(int featureId, Uri uri) {
3918 getWindow().setFeatureDrawableUri(featureId, uri);
3919 }
3920
3921 /**
3922 * Convenience for calling
3923 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
3924 */
3925 public final void setFeatureDrawable(int featureId, Drawable drawable) {
3926 getWindow().setFeatureDrawable(featureId, drawable);
3927 }
3928
3929 /**
3930 * Convenience for calling
3931 * {@link android.view.Window#setFeatureDrawableAlpha}.
3932 */
3933 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
3934 getWindow().setFeatureDrawableAlpha(featureId, alpha);
3935 }
3936
3937 /**
3938 * Convenience for calling
3939 * {@link android.view.Window#getLayoutInflater}.
3940 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003941 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 public LayoutInflater getLayoutInflater() {
3943 return getWindow().getLayoutInflater();
3944 }
3945
3946 /**
3947 * Returns a {@link MenuInflater} with this context.
3948 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003949 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07003951 // Make sure that action views can get an appropriate theme.
3952 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07003953 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07003954 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07003955 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07003956 } else {
3957 mMenuInflater = new MenuInflater(this);
3958 }
3959 }
3960 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 }
3962
3963 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07003964 public void setTheme(int resid) {
3965 super.setTheme(resid);
3966 mWindow.setTheme(resid);
3967 }
3968
3969 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07003970 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003971 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 if (mParent == null) {
3973 super.onApplyThemeResource(theme, resid, first);
3974 } else {
3975 try {
3976 theme.setTo(mParent.getTheme());
3977 } catch (Exception e) {
3978 // Empty
3979 }
3980 theme.applyStyle(resid, false);
3981 }
Winson073a5262016-02-29 16:04:40 -08003982
3983 // Get the primary color and update the TaskDescription for this activity
3984 TypedArray a = theme.obtainStyledAttributes(
3985 com.android.internal.R.styleable.ActivityTaskDescription);
3986 if (mTaskDescription.getPrimaryColor() == 0) {
3987 int colorPrimary = a.getColor(
3988 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
3989 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
3990 mTaskDescription.setPrimaryColor(colorPrimary);
3991 }
3992 }
3993 // For dev-preview only.
3994 if (mTaskDescription.getBackgroundColor() == 0) {
3995 int colorBackground = a.getColor(
3996 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
3997 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
3998 mTaskDescription.setBackgroundColor(colorBackground);
3999 }
4000 }
4001 a.recycle();
4002 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004003 }
4004
4005 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004006 * Requests permissions to be granted to this application. These permissions
4007 * must be requested in your manifest, they should not be granted to your app,
4008 * and they should have protection level {@link android.content.pm.PermissionInfo
4009 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
4010 * the platform or a third-party app.
4011 * <p>
4012 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4013 * are granted at install time if requested in the manifest. Signature permissions
4014 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4015 * install time if requested in the manifest and the signature of your app matches
4016 * the signature of the app declaring the permissions.
4017 * </p>
4018 * <p>
4019 * If your app does not have the requested permissions the user will be presented
4020 * with UI for accepting them. After the user has accepted or rejected the
4021 * requested permissions you will receive a callback on {@link
4022 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4023 * permissions were granted or not.
4024 * </p>
4025 * <p>
4026 * Note that requesting a permission does not guarantee it will be granted and
4027 * your app should be able to run without having this permission.
4028 * </p>
4029 * <p>
4030 * This method may start an activity allowing the user to choose which permissions
4031 * to grant and which to reject. Hence, you should be prepared that your activity
4032 * may be paused and resumed. Further, granting some permissions may require
4033 * a restart of you application. In such a case, the system will recreate the
4034 * activity stack before delivering the result to {@link
4035 * #onRequestPermissionsResult(int, String[], int[])}.
4036 * </p>
4037 * <p>
4038 * When checking whether you have a permission you should use {@link
4039 * #checkSelfPermission(String)}.
4040 * </p>
4041 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004042 * Calling this API for permissions already granted to your app would show UI
4043 * to the user to decide whether the app can still hold these permissions. This
4044 * can be useful if the way your app uses data guarded by the permissions
4045 * changes significantly.
4046 * </p>
4047 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004048 * You cannot request a permission if your activity sets {@link
4049 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4050 * <code>true</code> because in this case the activity would not receive
4051 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4052 * </p>
4053 * <p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004054 * A sample permissions request looks like this:
4055 * </p>
4056 * <code><pre><p>
4057 * private void showContacts() {
4058 * if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
4059 * != PackageManager.PERMISSION_GRANTED) {
4060 * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
4061 * PERMISSIONS_REQUEST_READ_CONTACTS);
4062 * } else {
4063 * doShowContacts();
4064 * }
4065 * }
4066 *
4067 * {@literal @}Override
4068 * public void onRequestPermissionsResult(int requestCode, String[] permissions,
4069 * int[] grantResults) {
4070 * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS
4071 * && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
4072 * showContacts();
4073 * }
4074 * }
4075 * </code></pre></p>
4076 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004077 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004078 * @param requestCode Application specific request code to match with a result
4079 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004080 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004081 *
4082 * @see #onRequestPermissionsResult(int, String[], int[])
4083 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004084 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004085 */
4086 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Svetoslavffb32b12015-10-15 16:54:00 -07004087 if (mHasCurrentPermissionsRequest) {
4088 Log.w(TAG, "Can reqeust only one set of permissions at a time");
4089 // Dispatch the callback with empty arrays which means a cancellation.
4090 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4091 return;
4092 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004093 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004094 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004095 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004096 }
4097
4098 /**
4099 * Callback for the result from requesting permissions. This method
4100 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004101 * <p>
4102 * <strong>Note:</strong> It is possible that the permissions request interaction
4103 * with the user is interrupted. In this case you will receive empty permissions
4104 * and results arrays which should be treated as a cancellation.
4105 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004106 *
4107 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4108 * @param permissions The requested permissions. Never null.
4109 * @param grantResults The grant results for the corresponding permissions
4110 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4111 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4112 *
4113 * @see #requestPermissions(String[], int)
4114 */
4115 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4116 @NonNull int[] grantResults) {
4117 /* callback - no nothing */
4118 }
4119
4120 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004121 * Gets whether you should show UI with rationale for requesting a permission.
4122 * You should do this only if you do not have the permission and the context in
4123 * which the permission is requested does not clearly communicate to the user
4124 * what would be the benefit from granting this permission.
4125 * <p>
4126 * For example, if you write a camera app, requesting the camera permission
4127 * would be expected by the user and no rationale for why it is requested is
4128 * needed. If however, the app needs location for tagging photos then a non-tech
4129 * savvy user may wonder how location is related to taking photos. In this case
4130 * you may choose to show UI with rationale of requesting this permission.
4131 * </p>
4132 *
4133 * @param permission A permission your app wants to request.
4134 * @return Whether you can show permission rationale UI.
4135 *
4136 * @see #checkSelfPermission(String)
4137 * @see #requestPermissions(String[], int)
4138 * @see #onRequestPermissionsResult(int, String[], int[])
4139 */
Svetoslav9ce9a152015-06-02 17:59:31 -07004140 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07004141 return getPackageManager().shouldShowRequestPermissionRationale(permission);
4142 }
4143
4144 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004145 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
4146 * with no options.
4147 *
4148 * @param intent The intent to start.
4149 * @param requestCode If >= 0, this code will be returned in
4150 * onActivityResult() when the activity exits.
4151 *
4152 * @throws android.content.ActivityNotFoundException
4153 *
George Mount0a778ed2013-12-13 13:35:36 -08004154 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07004155 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004156 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004157 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004158 }
4159
4160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 * Launch an activity for which you would like a result when it finished.
4162 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08004163 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07004164 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07004166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 * <p>Note that this method should only be used with Intent protocols
4168 * that are defined to return a result. In other protocols (such as
4169 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
4170 * not get the result when you expect. For example, if the activity you
4171 * are launching uses the singleTask launch mode, it will not run in your
4172 * task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004173 *
RoboErik55011652014-07-09 15:05:53 -07004174 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08004176 * activity, then your window will not be displayed until a result is
4177 * returned back from the started activity. This is to avoid visible
4178 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 * <p>This method throws {@link android.content.ActivityNotFoundException}
4181 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004182 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 * @param intent The intent to start.
4184 * @param requestCode If >= 0, this code will be returned in
4185 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004186 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004187 * See {@link android.content.Context#startActivity(Intent, Bundle)
4188 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004189 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004191 *
George Mount0a778ed2013-12-13 13:35:36 -08004192 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004194 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
4195 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 if (mParent == null) {
4197 Instrumentation.ActivityResult ar =
4198 mInstrumentation.execStartActivity(
4199 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004200 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 if (ar != null) {
4202 mMainThread.sendActivityResult(
4203 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
4204 ar.getResultData());
4205 }
4206 if (requestCode >= 0) {
4207 // If this start is requesting a result, we can avoid making
4208 // the activity visible until the result is received. Setting
4209 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4210 // activity hidden during this time, to avoid flickering.
4211 // This can only be done when a result is requested because
4212 // that guarantees we will get information back when the
4213 // activity is finished, no matter what happens to it.
4214 mStartedActivity = true;
4215 }
Adam Powell14874662013-07-18 19:42:41 -07004216
George Mount41725de2015-04-09 08:23:05 -07004217 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07004218 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004220 if (options != null) {
4221 mParent.startActivityFromChild(this, intent, requestCode, options);
4222 } else {
4223 // Note we want to go through this method for compatibility with
4224 // existing applications that may have overridden it.
4225 mParent.startActivityFromChild(this, intent, requestCode);
4226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
George Mount41725de2015-04-09 08:23:05 -07004228 }
4229
4230 /**
4231 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
4232 *
4233 * @param options The ActivityOptions bundle used to start an Activity.
4234 */
4235 private void cancelInputsAndStartExitTransition(Bundle options) {
4236 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
4237 if (decor != null) {
4238 decor.cancelPendingInputEvents();
4239 }
George Mountff243282014-07-07 16:12:07 -07004240 if (options != null && !isTopOfTask()) {
4241 mActivityTransitionState.startExitOutTransition(this, options);
4242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 }
4244
4245 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004246 * @hide Implement to provide correct calling token.
4247 */
Alexandra Gherghina83231452014-07-04 12:05:20 +01004248 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
4249 startActivityForResultAsUser(intent, requestCode, null, user);
4250 }
4251
4252 /**
4253 * @hide Implement to provide correct calling token.
4254 */
4255 public void startActivityForResultAsUser(Intent intent, int requestCode,
4256 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004257 if (mParent != null) {
4258 throw new RuntimeException("Can't be called from a child");
4259 }
4260 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
4261 this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode,
4262 options, user);
4263 if (ar != null) {
4264 mMainThread.sendActivityResult(
4265 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
4266 }
4267 if (requestCode >= 0) {
4268 // If this start is requesting a result, we can avoid making
4269 // the activity visible until the result is received. Setting
4270 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4271 // activity hidden during this time, to avoid flickering.
4272 // This can only be done when a result is requested because
4273 // that guarantees we will get information back when the
4274 // activity is finished, no matter what happens to it.
4275 mStartedActivity = true;
4276 }
4277
George Mount41725de2015-04-09 08:23:05 -07004278 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004279 }
4280
4281 /**
4282 * @hide Implement to provide correct calling token.
4283 */
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004284 public void startActivityAsUser(Intent intent, UserHandle user) {
4285 startActivityAsUser(intent, null, user);
4286 }
4287
4288 /**
4289 * @hide Implement to provide correct calling token.
4290 */
4291 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
4292 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004293 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004294 }
4295 Instrumentation.ActivityResult ar =
4296 mInstrumentation.execStartActivity(
4297 this, mMainThread.getApplicationThread(), mToken, this,
4298 intent, -1, options, user);
4299 if (ar != null) {
4300 mMainThread.sendActivityResult(
4301 mToken, mEmbeddedID, -1, ar.getResultCode(),
4302 ar.getResultData());
4303 }
George Mount41725de2015-04-09 08:23:05 -07004304 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004305 }
4306
4307 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004308 * Start a new activity as if it was started by the activity that started our
4309 * current activity. This is for the resolver and chooser activities, which operate
4310 * as intermediaries that dispatch their intent to the target the user selects -- to
4311 * do this, they must perform all security checks including permission grants as if
4312 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004313 * @param intent The Intent to start.
4314 * @param options ActivityOptions or null.
4315 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
4316 * caller it is doing the start is, is actually allowed to start the target activity.
4317 * If you set this to true, you must set an explicit component in the Intent and do any
4318 * appropriate security checks yourself.
4319 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004320 * @hide
4321 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004322 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
4323 boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004324 if (mParent != null) {
4325 throw new RuntimeException("Can't be called from a child");
4326 }
4327 Instrumentation.ActivityResult ar =
4328 mInstrumentation.execStartActivityAsCaller(
4329 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004330 intent, -1, options, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004331 if (ar != null) {
4332 mMainThread.sendActivityResult(
4333 mToken, mEmbeddedID, -1, ar.getResultCode(),
4334 ar.getResultData());
4335 }
George Mount41725de2015-04-09 08:23:05 -07004336 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004337 }
4338
4339 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004340 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
4341 * Intent, int, int, int, Bundle)} with no options.
4342 *
4343 * @param intent The IntentSender to launch.
4344 * @param requestCode If >= 0, this code will be returned in
4345 * onActivityResult() when the activity exits.
4346 * @param fillInIntent If non-null, this will be provided as the
4347 * intent parameter to {@link IntentSender#sendIntent}.
4348 * @param flagsMask Intent flags in the original IntentSender that you
4349 * would like to change.
4350 * @param flagsValues Desired values for any bits set in
4351 * <var>flagsMask</var>
4352 * @param extraFlags Always set to 0.
4353 */
4354 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004355 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004356 throws IntentSender.SendIntentException {
4357 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
4358 flagsValues, extraFlags, null);
4359 }
4360
4361 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004362 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004363 * to use a IntentSender to describe the activity to be started. If
4364 * the IntentSender is for an activity, that activity will be started
4365 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
4366 * here; otherwise, its associated action will be executed (such as
4367 * sending a broadcast) as if you had called
4368 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07004369 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004370 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004371 * @param requestCode If >= 0, this code will be returned in
4372 * onActivityResult() when the activity exits.
4373 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004374 * intent parameter to {@link IntentSender#sendIntent}.
4375 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004376 * would like to change.
4377 * @param flagsValues Desired values for any bits set in
4378 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004379 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004380 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004381 * See {@link android.content.Context#startActivity(Intent, Bundle)
4382 * Context.startActivity(Intent, Bundle)} for more details. If options
4383 * have also been supplied by the IntentSender, options given here will
4384 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004385 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004386 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004387 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004388 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004389 if (mParent == null) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004390 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004391 flagsMask, flagsValues, this, options);
4392 } else if (options != null) {
4393 mParent.startIntentSenderFromChild(this, intent, requestCode,
4394 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004395 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004396 // Note we want to go through this call for compatibility with
4397 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004398 mParent.startIntentSenderFromChild(this, intent, requestCode,
4399 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004400 }
4401 }
4402
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004403 private void startIntentSenderForResultInner(IntentSender intent, int requestCode,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004404 Intent fillInIntent, int flagsMask, int flagsValues, Activity activity,
4405 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004406 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004407 try {
4408 String resolvedType = null;
4409 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004410 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004411 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004412 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
4413 }
4414 int result = ActivityManagerNative.getDefault()
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004415 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004416 fillInIntent, resolvedType, mToken, activity.mEmbeddedID,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004417 requestCode, flagsMask, flagsValues, options);
4418 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004419 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004420 }
4421 Instrumentation.checkStartActivityResult(result, null);
4422 } catch (RemoteException e) {
4423 }
4424 if (requestCode >= 0) {
4425 // If this start is requesting a result, we can avoid making
4426 // the activity visible until the result is received. Setting
4427 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4428 // activity hidden during this time, to avoid flickering.
4429 // This can only be done when a result is requested because
4430 // that guarantees we will get information back when the
4431 // activity is finished, no matter what happens to it.
4432 mStartedActivity = true;
4433 }
4434 }
4435
4436 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004437 * Same as {@link #startActivity(Intent, Bundle)} with no options
4438 * specified.
4439 *
4440 * @param intent The intent to start.
4441 *
4442 * @throws android.content.ActivityNotFoundException
4443 *
4444 * @see {@link #startActivity(Intent, Bundle)}
4445 * @see #startActivityForResult
4446 */
4447 @Override
4448 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00004449 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004450 }
4451
4452 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 * Launch a new activity. You will not receive any information about when
4454 * the activity exits. This implementation overrides the base version,
4455 * providing information about
4456 * the activity performing the launch. Because of this additional
4457 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4458 * required; if not specified, the new activity will be added to the
4459 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07004460 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 * <p>This method throws {@link android.content.ActivityNotFoundException}
4462 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004463 *
4464 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004465 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004466 * See {@link android.content.Context#startActivity(Intent, Bundle)
4467 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004468 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004470 *
4471 * @see {@link #startActivity(Intent)}
RoboErik55011652014-07-09 15:05:53 -07004472 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 */
4474 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004475 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004476 if (options != null) {
4477 startActivityForResult(intent, -1, options);
4478 } else {
4479 // Note we want to go through this call for compatibility with
4480 // applications that may have overridden the method.
4481 startActivityForResult(intent, -1);
4482 }
4483 }
4484
4485 /**
4486 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4487 * specified.
4488 *
4489 * @param intents The intents to start.
4490 *
4491 * @throws android.content.ActivityNotFoundException
4492 *
4493 * @see {@link #startActivities(Intent[], Bundle)}
4494 * @see #startActivityForResult
4495 */
4496 @Override
4497 public void startActivities(Intent[] intents) {
4498 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 }
4500
4501 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004502 * Launch a new activity. You will not receive any information about when
4503 * the activity exits. This implementation overrides the base version,
4504 * providing information about
4505 * the activity performing the launch. Because of this additional
4506 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4507 * required; if not specified, the new activity will be added to the
4508 * task of the caller.
4509 *
4510 * <p>This method throws {@link android.content.ActivityNotFoundException}
4511 * if there was no Activity found to run the given Intent.
4512 *
4513 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004514 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004515 * See {@link android.content.Context#startActivity(Intent, Bundle)
4516 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004517 *
4518 * @throws android.content.ActivityNotFoundException
4519 *
Dianne Hackborna4972e92012-03-14 10:38:05 -07004520 * @see {@link #startActivities(Intent[])}
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004521 * @see #startActivityForResult
4522 */
4523 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004524 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004525 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004526 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004527 }
4528
4529 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004530 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4531 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004532 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004533 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004534 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004535 * intent parameter to {@link IntentSender#sendIntent}.
4536 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004537 * would like to change.
4538 * @param flagsValues Desired values for any bits set in
4539 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004540 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004541 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004542 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004543 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004544 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004545 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
4546 extraFlags, null);
4547 }
4548
4549 /**
4550 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
4551 * to start; see
4552 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
4553 * for more information.
4554 *
4555 * @param intent The IntentSender to launch.
4556 * @param fillInIntent If non-null, this will be provided as the
4557 * intent parameter to {@link IntentSender#sendIntent}.
4558 * @param flagsMask Intent flags in the original IntentSender that you
4559 * would like to change.
4560 * @param flagsValues Desired values for any bits set in
4561 * <var>flagsMask</var>
4562 * @param extraFlags Always set to 0.
4563 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004564 * See {@link android.content.Context#startActivity(Intent, Bundle)
4565 * Context.startActivity(Intent, Bundle)} for more details. If options
4566 * have also been supplied by the IntentSender, options given here will
4567 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004568 */
4569 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004570 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004571 Bundle options) throws IntentSender.SendIntentException {
4572 if (options != null) {
4573 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4574 flagsValues, extraFlags, options);
4575 } else {
4576 // Note we want to go through this call for compatibility with
4577 // applications that may have overridden the method.
4578 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4579 flagsValues, extraFlags);
4580 }
4581 }
4582
4583 /**
4584 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
4585 * with no options.
4586 *
4587 * @param intent The intent to start.
4588 * @param requestCode If >= 0, this code will be returned in
4589 * onActivityResult() when the activity exits, as described in
4590 * {@link #startActivityForResult}.
4591 *
4592 * @return If a new activity was launched then true is returned; otherwise
4593 * false is returned and you must handle the Intent yourself.
4594 *
4595 * @see #startActivity
4596 * @see #startActivityForResult
4597 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004598 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4599 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004600 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004601 }
4602
4603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 * A special variation to launch an activity only if a new activity
4605 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07004606 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07004608 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004609 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07004610 * and the activity
4611 * that handles <var>intent</var> is the same as your currently running
4612 * activity, then a new instance is not needed. In this case, instead of
4613 * the normal behavior of calling {@link #onNewIntent} this function will
4614 * return and you can handle the Intent yourself.
4615 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 * <p>This function can only be called from a top-level activity; if it is
4617 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07004618 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619 * @param intent The intent to start.
4620 * @param requestCode If >= 0, this code will be returned in
4621 * onActivityResult() when the activity exits, as described in
4622 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004623 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004624 * See {@link android.content.Context#startActivity(Intent, Bundle)
4625 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004626 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 * @return If a new activity was launched then true is returned; otherwise
4628 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07004629 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 * @see #startActivity
4631 * @see #startActivityForResult
4632 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004633 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4634 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004636 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004638 Uri referrer = onProvideReferrer();
4639 if (referrer != null) {
4640 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4641 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004642 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004643 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 result = ActivityManagerNative.getDefault()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08004645 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07004646 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
4647 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
4648 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004649 } catch (RemoteException e) {
4650 // Empty
4651 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08004652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 if (requestCode >= 0) {
4656 // If this start is requesting a result, we can avoid making
4657 // the activity visible until the result is received. Setting
4658 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4659 // activity hidden during this time, to avoid flickering.
4660 // This can only be done when a result is requested because
4661 // that guarantees we will get information back when the
4662 // activity is finished, no matter what happens to it.
4663 mStartedActivity = true;
4664 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004665 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 }
4667
4668 throw new UnsupportedOperationException(
4669 "startActivityIfNeeded can only be called from a top-level activity");
4670 }
4671
4672 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004673 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
4674 * no options.
4675 *
4676 * @param intent The intent to dispatch to the next activity. For
4677 * correct behavior, this must be the same as the Intent that started
4678 * your own activity; the only changes you can make are to the extras
4679 * inside of it.
4680 *
4681 * @return Returns a boolean indicating whether there was another Activity
4682 * to start: true if there was a next activity to start, false if there
4683 * wasn't. In general, if true is returned you will then want to call
4684 * finish() on yourself.
4685 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004686 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004687 return startNextMatchingActivity(intent, null);
4688 }
4689
4690 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 * Special version of starting an activity, for use when you are replacing
4692 * other activity components. You can use this to hand the Intent off
4693 * to the next Activity that can handle it. You typically call this in
4694 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07004695 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696 * @param intent The intent to dispatch to the next activity. For
4697 * correct behavior, this must be the same as the Intent that started
4698 * your own activity; the only changes you can make are to the extras
4699 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004700 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004701 * See {@link android.content.Context#startActivity(Intent, Bundle)
4702 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004703 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 * @return Returns a boolean indicating whether there was another Activity
4705 * to start: true if there was a next activity to start, false if there
4706 * wasn't. In general, if true is returned you will then want to call
4707 * finish() on yourself.
4708 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004709 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
4710 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 if (mParent == null) {
4712 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004713 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004714 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 return ActivityManagerNative.getDefault()
Dianne Hackborna4972e92012-03-14 10:38:05 -07004716 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 } catch (RemoteException e) {
4718 // Empty
4719 }
4720 return false;
4721 }
4722
4723 throw new UnsupportedOperationException(
4724 "startNextMatchingActivity can only be called from a top-level activity");
4725 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004726
4727 /**
4728 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
4729 * with no options.
4730 *
4731 * @param child The activity making the call.
4732 * @param intent The intent to start.
4733 * @param requestCode Reply request code. < 0 if reply is not requested.
4734 *
4735 * @throws android.content.ActivityNotFoundException
4736 *
4737 * @see #startActivity
4738 * @see #startActivityForResult
4739 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004740 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004741 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004742 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004743 }
4744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 /**
RoboErik55011652014-07-09 15:05:53 -07004746 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07004748 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004749 * <p>This method throws {@link android.content.ActivityNotFoundException}
4750 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004751 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004752 * @param child The activity making the call.
4753 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004754 * @param requestCode Reply request code. < 0 if reply is not requested.
4755 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004756 * See {@link android.content.Context#startActivity(Intent, Bundle)
4757 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004758 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004760 *
4761 * @see #startActivity
4762 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004763 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004764 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004765 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004766 Instrumentation.ActivityResult ar =
4767 mInstrumentation.execStartActivity(
4768 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004769 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 if (ar != null) {
4771 mMainThread.sendActivityResult(
4772 mToken, child.mEmbeddedID, requestCode,
4773 ar.getResultCode(), ar.getResultData());
4774 }
George Mount41725de2015-04-09 08:23:05 -07004775 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004776 }
4777
4778 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004779 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
4780 * with no options.
4781 *
4782 * @param fragment The fragment making the call.
4783 * @param intent The intent to start.
4784 * @param requestCode Reply request code. < 0 if reply is not requested.
4785 *
4786 * @throws android.content.ActivityNotFoundException
4787 *
4788 * @see Fragment#startActivity
4789 * @see Fragment#startActivityForResult
4790 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004791 public void startActivityFromFragment(@NonNull Fragment fragment,
4792 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004793 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004794 }
4795
4796 /**
RoboErik55011652014-07-09 15:05:53 -07004797 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004798 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
4799 * method.
RoboErik55011652014-07-09 15:05:53 -07004800 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004801 * <p>This method throws {@link android.content.ActivityNotFoundException}
4802 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004803 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004804 * @param fragment The fragment making the call.
4805 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07004806 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004807 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004808 * See {@link android.content.Context#startActivity(Intent, Bundle)
4809 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004810 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004811 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004812 *
4813 * @see Fragment#startActivity
4814 * @see Fragment#startActivityForResult
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004815 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004816 public void startActivityFromFragment(@NonNull Fragment fragment,
4817 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004818 startActivityForResult(fragment.mWho, intent, requestCode, options);
4819 }
4820
4821 /**
4822 * @hide
4823 */
4824 @Override
4825 public void startActivityForResult(
4826 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004827 Uri referrer = onProvideReferrer();
4828 if (referrer != null) {
4829 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4830 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004831 Instrumentation.ActivityResult ar =
4832 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004833 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004834 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004835 if (ar != null) {
4836 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004837 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004838 ar.getResultCode(), ar.getResultData());
4839 }
George Mount41725de2015-04-09 08:23:05 -07004840 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004841 }
4842
4843 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004844 * @hide
4845 */
4846 @Override
4847 public boolean canStartActivityForResult() {
4848 return true;
4849 }
4850
4851 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004852 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
4853 * int, Intent, int, int, int, Bundle)} with no options.
4854 */
4855 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4856 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
4857 int extraFlags)
4858 throws IntentSender.SendIntentException {
4859 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
4860 flagsMask, flagsValues, extraFlags, null);
4861 }
4862
4863 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004864 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004865 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07004866 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004867 * for more information.
4868 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004869 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4870 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07004871 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004872 throws IntentSender.SendIntentException {
4873 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004874 flagsMask, flagsValues, child, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004875 }
4876
4877 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004878 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
4879 * or {@link #finish} to specify an explicit transition animation to
4880 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004881 *
4882 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
4883 * to using this with starting activities is to supply the desired animation
4884 * information through a {@link ActivityOptions} bundle to
4885 * {@link #startActivity(Intent, Bundle) or a related function. This allows
4886 * you to specify a custom animation even when starting an activity from
4887 * outside the context of the current top activity.
4888 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004889 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004890 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004891 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004892 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004893 */
4894 public void overridePendingTransition(int enterAnim, int exitAnim) {
4895 try {
4896 ActivityManagerNative.getDefault().overridePendingTransition(
4897 mToken, getPackageName(), enterAnim, exitAnim);
4898 } catch (RemoteException e) {
4899 }
4900 }
RoboErik55011652014-07-09 15:05:53 -07004901
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004902 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 * Call this to set the result that your activity will return to its
4904 * caller.
RoboErik55011652014-07-09 15:05:53 -07004905 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004906 * @param resultCode The result code to propagate back to the originating
4907 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07004908 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 * @see #RESULT_CANCELED
4910 * @see #RESULT_OK
4911 * @see #RESULT_FIRST_USER
4912 * @see #setResult(int, Intent)
4913 */
4914 public final void setResult(int resultCode) {
4915 synchronized (this) {
4916 mResultCode = resultCode;
4917 mResultData = null;
4918 }
4919 }
4920
4921 /**
4922 * Call this to set the result that your activity will return to its
4923 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004924 *
4925 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
4926 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
4927 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
4928 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
4929 * Activity receiving the result access to the specific URIs in the Intent.
4930 * Access will remain until the Activity has finished (it will remain across the hosting
4931 * process being killed and other temporary destruction) and will be added
4932 * to any existing set of URI permissions it already holds.
4933 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 * @param resultCode The result code to propagate back to the originating
4935 * activity, often RESULT_CANCELED or RESULT_OK
4936 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07004937 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004938 * @see #RESULT_CANCELED
4939 * @see #RESULT_OK
4940 * @see #RESULT_FIRST_USER
4941 * @see #setResult(int)
4942 */
4943 public final void setResult(int resultCode, Intent data) {
4944 synchronized (this) {
4945 mResultCode = resultCode;
4946 mResultData = data;
4947 }
4948 }
4949
4950 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004951 * Return information about who launched this activity. If the launching Intent
4952 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
4953 * that will be returned as-is; otherwise, if known, an
4954 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
4955 * package name that started the Intent will be returned. This may return null if no
4956 * referrer can be identified -- it is neither explicitly specified, nor is it known which
4957 * application package was involved.
4958 *
4959 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
4960 * return the referrer that submitted that new intent to the activity. Otherwise, it
4961 * always returns the referrer of the original Intent.</p>
4962 *
4963 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
4964 * referrer information, applications can spoof it.</p>
4965 */
4966 @Nullable
4967 public Uri getReferrer() {
4968 Intent intent = getIntent();
4969 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
4970 if (referrer != null) {
4971 return referrer;
4972 }
4973 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
4974 if (referrerName != null) {
4975 return Uri.parse(referrerName);
4976 }
4977 if (mReferrer != null) {
4978 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
4979 }
4980 return null;
4981 }
4982
4983 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004984 * Override to generate the desired referrer for the content currently being shown
4985 * by the app. The default implementation returns null, meaning the referrer will simply
4986 * be the android-app: of the package name of this activity. Return a non-null Uri to
4987 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
4988 */
4989 public Uri onProvideReferrer() {
4990 return null;
4991 }
4992
4993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 * Return the name of the package that invoked this activity. This is who
4995 * the data in {@link #setResult setResult()} will be sent to. You can
4996 * use this information to validate that the recipient is allowed to
4997 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07004998 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07004999 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005000 * did not use the {@link #startActivityForResult}
5001 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005002 * null.</p>
5003 *
5004 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5005 * the result from this method was unstable. If the process hosting the calling
5006 * package was no longer running, it would return null instead of the proper package
5007 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5008 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005010 * @return The package of the activity that will receive your
5011 * reply, or null if none.
5012 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005013 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 public String getCallingPackage() {
5015 try {
5016 return ActivityManagerNative.getDefault().getCallingPackage(mToken);
5017 } catch (RemoteException e) {
5018 return null;
5019 }
5020 }
5021
5022 /**
5023 * Return the name of the activity that invoked this activity. This is
5024 * who the data in {@link #setResult setResult()} will be sent to. You
5025 * can use this information to validate that the recipient is allowed to
5026 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005027 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005028 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005029 * did not use the {@link #startActivityForResult}
5030 * form that includes a request code), then the calling package will be
5031 * null.
5032 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005033 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034 * reply, or null if none.
5035 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005036 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 public ComponentName getCallingActivity() {
5038 try {
5039 return ActivityManagerNative.getDefault().getCallingActivity(mToken);
5040 } catch (RemoteException e) {
5041 return null;
5042 }
5043 }
5044
5045 /**
5046 * Control whether this activity's main window is visible. This is intended
5047 * only for the special case of an activity that is not going to show a
5048 * UI itself, but can't just finish prior to onResume() because it needs
5049 * to wait for a service binding or such. Setting this to false allows
5050 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005051 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 * <p>The default value for this is taken from the
5053 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
5054 */
5055 public void setVisible(boolean visible) {
5056 if (mVisibleFromClient != visible) {
5057 mVisibleFromClient = visible;
5058 if (mVisibleFromServer) {
5059 if (visible) makeVisible();
5060 else mDecor.setVisibility(View.INVISIBLE);
5061 }
5062 }
5063 }
RoboErik55011652014-07-09 15:05:53 -07005064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 void makeVisible() {
5066 if (!mWindowAdded) {
5067 ViewManager wm = getWindowManager();
5068 wm.addView(mDecor, getWindow().getAttributes());
5069 mWindowAdded = true;
5070 }
5071 mDecor.setVisibility(View.VISIBLE);
5072 }
RoboErik55011652014-07-09 15:05:53 -07005073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 /**
5075 * Check to see whether this activity is in the process of finishing,
5076 * either because you called {@link #finish} on it or someone else
5077 * has requested that it finished. This is often used in
5078 * {@link #onPause} to determine whether the activity is simply pausing or
5079 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07005080 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07005082 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 * @see #finish
5084 */
5085 public boolean isFinishing() {
5086 return mFinished;
5087 }
5088
5089 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07005090 * Returns true if the final {@link #onDestroy()} call has been made
5091 * on the Activity, so this instance is now dead.
5092 */
5093 public boolean isDestroyed() {
5094 return mDestroyed;
5095 }
5096
5097 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005098 * Check to see whether this activity is in the process of being destroyed in order to be
5099 * recreated with a new configuration. This is often used in
5100 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
5101 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07005102 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005103 * @return If the activity is being torn down in order to be recreated with a new configuration,
5104 * returns true; else returns false.
5105 */
5106 public boolean isChangingConfigurations() {
5107 return mChangingConfigurations;
5108 }
5109
5110 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005111 * Cause this Activity to be recreated with a new instance. This results
5112 * in essentially the same flow as when the Activity is created due to
5113 * a configuration change -- the current instance will go through its
5114 * lifecycle to {@link #onDestroy} and a new instance then created after it.
5115 */
5116 public void recreate() {
5117 if (mParent != null) {
5118 throw new IllegalStateException("Can only be called on top-level activity");
5119 }
5120 if (Looper.myLooper() != mMainThread.getLooper()) {
5121 throw new IllegalStateException("Must be called from main thread");
5122 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07005123 mMainThread.requestRelaunchActivity(mToken, null, null, 0, false, null, null, false,
5124 false /* preserveWindow */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005125 }
5126
5127 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005128 * Finishes the current activity and specifies whether to remove the task associated with this
5129 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005131 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 if (mParent == null) {
5133 int resultCode;
5134 Intent resultData;
5135 synchronized (this) {
5136 resultCode = mResultCode;
5137 resultData = mResultData;
5138 }
Joe Onorato43a17652011-04-06 19:22:23 -07005139 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005140 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005141 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005142 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 if (ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07005145 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 mFinished = true;
5147 }
5148 } catch (RemoteException e) {
5149 // Empty
5150 }
5151 } else {
5152 mParent.finishFromChild(this);
5153 }
5154 }
5155
5156 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005157 * Call this when your activity is done and should be closed. The
5158 * ActivityResult is propagated back to whoever launched you via
5159 * onActivityResult().
5160 */
5161 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005162 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005163 }
5164
5165 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005166 * Finish this activity as well as all activities immediately below it
5167 * in the current task that have the same affinity. This is typically
5168 * used when an application can be launched on to another task (such as
5169 * from an ACTION_VIEW of a content type it understands) and the user
5170 * has used the up navigation to switch out of the current task and in
5171 * to its own task. In this case, if the user has navigated down into
5172 * any other activities of the second application, all of those should
5173 * be removed from the original task as part of the task switch.
5174 *
5175 * <p>Note that this finish does <em>not</em> allow you to deliver results
5176 * to the previous activity, and an exception will be thrown if you are trying
5177 * to do so.</p>
5178 */
5179 public void finishAffinity() {
5180 if (mParent != null) {
5181 throw new IllegalStateException("Can not be called from an embedded activity");
5182 }
5183 if (mResultCode != RESULT_CANCELED || mResultData != null) {
5184 throw new IllegalStateException("Can not be called to deliver a result");
5185 }
5186 try {
5187 if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) {
5188 mFinished = true;
5189 }
5190 } catch (RemoteException e) {
5191 // Empty
5192 }
5193 }
5194
5195 /**
RoboErik55011652014-07-09 15:05:53 -07005196 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 * {@link #finish} method. The default implementation simply calls
5198 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07005199 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07005201 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202 * @see #finish
5203 */
5204 public void finishFromChild(Activity child) {
5205 finish();
5206 }
5207
5208 /**
George Mountcb4b7d92014-02-25 10:47:55 -08005209 * Reverses the Activity Scene entry Transition and triggers the calling Activity
5210 * to reverse its exit Transition. When the exit Transition completes,
5211 * {@link #finish()} is called. If no entry Transition was used, finish() is called
5212 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07005213 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08005214 */
Craig Mautner73f843d2014-05-19 09:42:28 -07005215 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07005216 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07005217 finish();
5218 }
George Mountcb4b7d92014-02-25 10:47:55 -08005219 }
5220
5221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 * Force finish another activity that you had previously started with
5223 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07005224 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 * @param requestCode The request code of the activity that you had
5226 * given to startActivityForResult(). If there are multiple
5227 * activities started with this request code, they
5228 * will all be finished.
5229 */
5230 public void finishActivity(int requestCode) {
5231 if (mParent == null) {
5232 try {
5233 ActivityManagerNative.getDefault()
5234 .finishSubActivity(mToken, mEmbeddedID, requestCode);
5235 } catch (RemoteException e) {
5236 // Empty
5237 }
5238 } else {
5239 mParent.finishActivityFromChild(this, requestCode);
5240 }
5241 }
5242
5243 /**
5244 * This is called when a child activity of this one calls its
5245 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07005246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 * @param child The activity making the call.
5248 * @param requestCode Request code that had been used to start the
5249 * activity.
5250 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005251 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 try {
5253 ActivityManagerNative.getDefault()
5254 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
5255 } catch (RemoteException e) {
5256 // Empty
5257 }
5258 }
5259
5260 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005261 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005262 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07005263 */
5264 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005265 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005266 }
5267
5268 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005269 * Ask that the local app instance of this activity be released to free up its memory.
5270 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
5271 * a new instance of the activity will later be re-created if needed due to the user
5272 * navigating back to it.
5273 *
5274 * @return Returns true if the activity was in a state that it has started the process
5275 * of destroying its current instance; returns false if for any reason this could not
5276 * be done: it is currently visible to the user, it is already being destroyed, it is
5277 * being finished, it hasn't yet saved its state, etc.
5278 */
5279 public boolean releaseInstance() {
5280 try {
5281 return ActivityManagerNative.getDefault().releaseActivityInstance(mToken);
5282 } catch (RemoteException e) {
5283 // Empty
5284 }
5285 return false;
5286 }
5287
5288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 * Called when an activity you launched exits, giving you the requestCode
5290 * you started it with, the resultCode it returned, and any additional
5291 * data from it. The <var>resultCode</var> will be
5292 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
5293 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07005294 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 * <p>You will receive this call immediately before onResume() when your
5296 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07005297 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07005298 * <p>This method is never invoked if your activity sets
5299 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5300 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08005301 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005302 * @param requestCode The integer request code originally supplied to
5303 * startActivityForResult(), allowing you to identify who this
5304 * result came from.
5305 * @param resultCode The integer result code returned by the child activity
5306 * through its setResult().
5307 * @param data An Intent, which can return result data to the caller
5308 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07005309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 * @see #startActivityForResult
5311 * @see #createPendingResult
5312 * @see #setResult(int)
5313 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005314 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005315 }
5316
5317 /**
George Mount62ab9b72014-05-02 13:51:17 -07005318 * Called when an activity you launched with an activity transition exposes this
5319 * Activity through a returning activity transition, giving you the resultCode
5320 * and any additional data from it. This method will only be called if the activity
5321 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07005322 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07005323 *
5324 * <p>The purpose of this function is to let the called Activity send a hint about
5325 * its state so that this underlying Activity can prepare to be exposed. A call to
5326 * this method does not guarantee that the called Activity has or will be exiting soon.
5327 * It only indicates that it will expose this Activity's Window and it has
5328 * some data to pass to prepare it.</p>
5329 *
5330 * @param resultCode The integer result code returned by the child activity
5331 * through its setResult().
5332 * @param data An Intent, which can return result data to the caller
5333 * (various data can be attached to Intent "extras").
5334 */
Craig Mautner7a629c22014-09-04 14:57:04 -07005335 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07005336 }
5337
5338 /**
RoboErik55011652014-07-09 15:05:53 -07005339 * Create a new PendingIntent object which you can hand to others
5340 * for them to use to send result data back to your
5341 * {@link #onActivityResult} callback. The created object will be either
5342 * one-shot (becoming invalid after a result is sent back) or multiple
5343 * (allowing any number of results to be sent through it).
5344 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005345 * @param requestCode Private request code for the sender that will be
5346 * associated with the result data when it is returned. The sender can not
5347 * modify this value, allowing you to identify incoming results.
5348 * @param data Default data to supply in the result, which may be modified
5349 * by the sender.
5350 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
5351 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
5352 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
5353 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
5354 * or any of the flags as supported by
5355 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
5356 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07005357 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358 * @return Returns an existing or new PendingIntent matching the given
5359 * parameters. May return null only if
5360 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
5361 * supplied.
RoboErik55011652014-07-09 15:05:53 -07005362 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 * @see PendingIntent
5364 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005365 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
5366 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005367 String packageName = getPackageName();
5368 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005369 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 IIntentSender target =
5371 ActivityManagerNative.getDefault().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07005372 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07005374 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
5375 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 return target != null ? new PendingIntent(target) : null;
5377 } catch (RemoteException e) {
5378 // Empty
5379 }
5380 return null;
5381 }
5382
5383 /**
5384 * Change the desired orientation of this activity. If the activity
5385 * is currently in the foreground or otherwise impacting the screen
5386 * orientation, the screen will immediately be changed (possibly causing
5387 * the activity to be restarted). Otherwise, this will be used the next
5388 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07005389 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 * @param requestedOrientation An orientation constant as used in
5391 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5392 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005393 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 if (mParent == null) {
5395 try {
5396 ActivityManagerNative.getDefault().setRequestedOrientation(
5397 mToken, requestedOrientation);
5398 } catch (RemoteException e) {
5399 // Empty
5400 }
5401 } else {
5402 mParent.setRequestedOrientation(requestedOrientation);
5403 }
5404 }
RoboErik55011652014-07-09 15:05:53 -07005405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 /**
5407 * Return the current requested orientation of the activity. This will
5408 * either be the orientation requested in its component's manifest, or
5409 * the last requested orientation given to
5410 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07005411 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 * @return Returns an orientation constant as used in
5413 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5414 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005415 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 public int getRequestedOrientation() {
5417 if (mParent == null) {
5418 try {
5419 return ActivityManagerNative.getDefault()
5420 .getRequestedOrientation(mToken);
5421 } catch (RemoteException e) {
5422 // Empty
5423 }
5424 } else {
5425 return mParent.getRequestedOrientation();
5426 }
5427 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
5428 }
RoboErik55011652014-07-09 15:05:53 -07005429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 /**
5431 * Return the identifier of the task this activity is in. This identifier
5432 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07005433 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 * @return Task identifier, an opaque integer.
5435 */
5436 public int getTaskId() {
5437 try {
5438 return ActivityManagerNative.getDefault()
5439 .getTaskForActivity(mToken, false);
5440 } catch (RemoteException e) {
5441 return -1;
5442 }
5443 }
5444
5445 /**
5446 * Return whether this activity is the root of a task. The root is the
5447 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07005448 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 * @return True if this is the root activity, else false.
5450 */
5451 public boolean isTaskRoot() {
5452 try {
Filip Gruszczynski3d026712015-12-16 13:46:38 -08005453 return ActivityManagerNative.getDefault().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 } catch (RemoteException e) {
5455 return false;
5456 }
5457 }
5458
5459 /**
5460 * Move the task containing this activity to the back of the activity
5461 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07005462 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 * @param nonRoot If false then this only works if the activity is the root
5464 * of a task; if true it will work for any activity in
5465 * a task.
RoboErik55011652014-07-09 15:05:53 -07005466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 * @return If the task was moved (or it was already at the
5468 * back) true is returned, else false.
5469 */
5470 public boolean moveTaskToBack(boolean nonRoot) {
5471 try {
5472 return ActivityManagerNative.getDefault().moveActivityTaskToBack(
5473 mToken, nonRoot);
5474 } catch (RemoteException e) {
5475 // Empty
5476 }
5477 return false;
5478 }
5479
5480 /**
5481 * Returns class name for this activity with the package prefix removed.
5482 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07005483 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005484 * @return The local class name.
5485 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005486 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 public String getLocalClassName() {
5488 final String pkg = getPackageName();
5489 final String cls = mComponent.getClassName();
5490 int packageLen = pkg.length();
5491 if (!cls.startsWith(pkg) || cls.length() <= packageLen
5492 || cls.charAt(packageLen) != '.') {
5493 return cls;
5494 }
5495 return cls.substring(packageLen+1);
5496 }
RoboErik55011652014-07-09 15:05:53 -07005497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 /**
5499 * Returns complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07005500 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501 * @return Returns the complete component name for this activity
5502 */
5503 public ComponentName getComponentName()
5504 {
5505 return mComponent;
5506 }
5507
5508 /**
5509 * Retrieve a {@link SharedPreferences} object for accessing preferences
5510 * that are private to this activity. This simply calls the underlying
5511 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
5512 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07005513 *
5514 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07005515 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005516 *
5517 * @return Returns the single SharedPreferences instance that can be used
5518 * to retrieve and modify the preference values.
5519 */
5520 public SharedPreferences getPreferences(int mode) {
5521 return getSharedPreferences(getLocalClassName(), mode);
5522 }
RoboErik55011652014-07-09 15:05:53 -07005523
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005524 private void ensureSearchManager() {
5525 if (mSearchManager != null) {
5526 return;
5527 }
RoboErik55011652014-07-09 15:05:53 -07005528
Amith Yamasanie9ce3f02010-01-25 09:15:50 -08005529 mSearchManager = new SearchManager(this, null);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005530 }
Tor Norbyed9273d62013-05-30 15:59:53 -07005531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005533 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 if (getBaseContext() == null) {
5535 throw new IllegalStateException(
5536 "System services not available to Activities before onCreate()");
5537 }
5538
5539 if (WINDOW_SERVICE.equals(name)) {
5540 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005541 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005542 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005543 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 }
5545 return super.getSystemService(name);
5546 }
5547
5548 /**
5549 * Change the title associated with this activity. If this is a
5550 * top-level activity, the title for its window will change. If it
5551 * is an embedded activity, the parent can do whatever it wants
5552 * with it.
5553 */
5554 public void setTitle(CharSequence title) {
5555 mTitle = title;
5556 onTitleChanged(title, mTitleColor);
5557
5558 if (mParent != null) {
5559 mParent.onChildTitleChanged(this, title);
5560 }
5561 }
5562
5563 /**
5564 * Change the title associated with this activity. If this is a
5565 * top-level activity, the title for its window will change. If it
5566 * is an embedded activity, the parent can do whatever it wants
5567 * with it.
5568 */
5569 public void setTitle(int titleId) {
5570 setTitle(getText(titleId));
5571 }
5572
Alan Viverette2525d9c2013-11-15 14:42:19 -08005573 /**
5574 * Change the color of the title associated with this activity.
5575 * <p>
5576 * This method is deprecated starting in API Level 11 and replaced by action
5577 * bar styles. For information on styling the Action Bar, read the <a
5578 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
5579 * guide.
5580 *
5581 * @deprecated Use action bar styles instead.
5582 */
5583 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584 public void setTitleColor(int textColor) {
5585 mTitleColor = textColor;
5586 onTitleChanged(mTitle, textColor);
5587 }
5588
5589 public final CharSequence getTitle() {
5590 return mTitle;
5591 }
5592
5593 public final int getTitleColor() {
5594 return mTitleColor;
5595 }
5596
5597 protected void onTitleChanged(CharSequence title, int color) {
5598 if (mTitleReady) {
5599 final Window win = getWindow();
5600 if (win != null) {
5601 win.setTitle(title);
5602 if (color != 0) {
5603 win.setTitleColor(color);
5604 }
5605 }
Adam Powellaf2d8592014-08-26 18:06:40 -07005606 if (mActionBar != null) {
5607 mActionBar.setWindowTitle(title);
5608 }
Adam Powella557fdc2014-08-21 18:05:53 -07005609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005610 }
5611
5612 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
5613 }
5614
5615 /**
Winson Chunga449dc02014-05-16 11:15:04 -07005616 * Sets information describing the task with this activity for presentation inside the Recents
5617 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
5618 * are traversed in order from the topmost activity to the bottommost. The traversal continues
5619 * for each property until a suitable value is found. For each task the taskDescription will be
5620 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07005621 *
5622 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07005623 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07005624 *
Winson Chunga449dc02014-05-16 11:15:04 -07005625 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07005626 */
Winson Chunga449dc02014-05-16 11:15:04 -07005627 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08005628 if (mTaskDescription != taskDescription) {
5629 mTaskDescription.copyFrom(taskDescription);
5630 // Scale the icon down to something reasonable if it is provided
5631 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
5632 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
5633 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
5634 true);
5635 mTaskDescription.setIcon(icon);
5636 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07005637 }
5638 try {
Winsonb6403152016-02-23 13:32:09 -08005639 ActivityManagerNative.getDefault().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07005640 } catch (RemoteException e) {
5641 }
5642 }
5643
5644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 * Sets the visibility of the progress bar in the title.
5646 * <p>
5647 * In order for the progress bar to be shown, the feature must be requested
5648 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005649 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005651 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005653 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 public final void setProgressBarVisibility(boolean visible) {
5655 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
5656 Window.PROGRESS_VISIBILITY_OFF);
5657 }
5658
5659 /**
5660 * Sets the visibility of the indeterminate progress bar in the title.
5661 * <p>
5662 * In order for the progress bar to be shown, the feature must be requested
5663 * via {@link #requestWindowFeature(int)}.
5664 *
5665 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005666 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005668 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 public final void setProgressBarIndeterminateVisibility(boolean visible) {
5670 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
5671 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
5672 }
RoboErik55011652014-07-09 15:05:53 -07005673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 /**
5675 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
5676 * is always indeterminate).
5677 * <p>
5678 * In order for the progress bar to be shown, the feature must be requested
5679 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005680 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005682 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005683 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005684 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 public final void setProgressBarIndeterminate(boolean indeterminate) {
5686 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00005687 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
5688 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 }
RoboErik55011652014-07-09 15:05:53 -07005690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 /**
5692 * Sets the progress for the progress bars in the title.
5693 * <p>
5694 * In order for the progress bar to be shown, the feature must be requested
5695 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005696 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697 * @param progress The progress for the progress bar. Valid ranges are from
5698 * 0 to 10000 (both inclusive). If 10000 is given, the progress
5699 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005700 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005702 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 public final void setProgress(int progress) {
5704 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
5705 }
RoboErik55011652014-07-09 15:05:53 -07005706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 /**
5708 * Sets the secondary progress for the progress bar in the title. This
5709 * progress is drawn between the primary progress (set via
5710 * {@link #setProgress(int)} and the background. It can be ideal for media
5711 * scenarios such as showing the buffering progress while the default
5712 * progress shows the play progress.
5713 * <p>
5714 * In order for the progress bar to be shown, the feature must be requested
5715 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005716 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005717 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
5718 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04005719 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005721 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 public final void setSecondaryProgress(int secondaryProgress) {
5723 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
5724 secondaryProgress + Window.PROGRESS_SECONDARY_START);
5725 }
5726
5727 /**
5728 * Suggests an audio stream whose volume should be changed by the hardware
5729 * volume controls.
5730 * <p>
5731 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07005732 * Volume requests which are received while the Activity is in the
5733 * foreground will affect this stream.
5734 * <p>
5735 * It is not guaranteed that the hardware volume controls will always change
5736 * this stream's volume (for example, if a call is in progress, its stream's
5737 * volume may be changed instead). To reset back to the default, use
5738 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
5739 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07005741 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005742 */
5743 public final void setVolumeControlStream(int streamType) {
5744 getWindow().setVolumeControlStream(streamType);
5745 }
5746
5747 /**
5748 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07005749 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07005750 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 * @return The suggested audio stream type whose volume should be changed by
5752 * the hardware volume controls.
5753 * @see #setVolumeControlStream(int)
5754 */
5755 public final int getVolumeControlStream() {
5756 return getWindow().getVolumeControlStream();
5757 }
RoboErik55011652014-07-09 15:05:53 -07005758
5759 /**
5760 * Sets a {@link MediaController} to send media keys and volume changes to.
5761 * <p>
5762 * The controller will be tied to the window of this Activity. Media key and
5763 * volume events which are received while the Activity is in the foreground
5764 * will be forwarded to the controller and used to invoke transport controls
5765 * or adjust the volume. This may be used instead of or in addition to
5766 * {@link #setVolumeControlStream} to affect a specific session instead of a
5767 * specific stream.
5768 * <p>
5769 * It is not guaranteed that the hardware volume controls will always change
5770 * this session's volume (for example, if a call is in progress, its
5771 * stream's volume may be changed instead). To reset back to the default use
5772 * null as the controller.
5773 *
5774 * @param controller The controller for the session which should receive
5775 * media keys and volume changes.
5776 */
5777 public final void setMediaController(MediaController controller) {
5778 getWindow().setMediaController(controller);
5779 }
5780
5781 /**
5782 * Gets the controller which should be receiving media key and volume events
5783 * while this activity is in the foreground.
5784 *
5785 * @return The controller which should receive events.
5786 * @see #setMediaController(android.media.session.MediaController)
5787 */
5788 public final MediaController getMediaController() {
5789 return getWindow().getMediaController();
5790 }
5791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 /**
5793 * Runs the specified action on the UI thread. If the current thread is the UI
5794 * thread, then the action is executed immediately. If the current thread is
5795 * not the UI thread, the action is posted to the event queue of the UI thread.
5796 *
5797 * @param action the action to run on the UI thread
5798 */
5799 public final void runOnUiThread(Runnable action) {
5800 if (Thread.currentThread() != mUiThread) {
5801 mHandler.post(action);
5802 } else {
5803 action.run();
5804 }
5805 }
5806
5807 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005808 * Standard implementation of
5809 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
5810 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005811 * This implementation does nothing and is for
5812 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
5813 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
5814 *
5815 * @see android.view.LayoutInflater#createView
5816 * @see android.view.Window#getLayoutInflater
5817 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005818 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07005819 public View onCreateView(String name, Context context, AttributeSet attrs) {
5820 return null;
5821 }
5822
5823 /**
5824 * Standard implementation of
5825 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
5826 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005827 * This implementation handles <fragment> tags to embed fragments inside
5828 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005829 *
5830 * @see android.view.LayoutInflater#createView
5831 * @see android.view.Window#getLayoutInflater
5832 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07005833 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005834 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07005835 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005836 }
RoboErik55011652014-07-09 15:05:53 -07005837
Adam Powell371a8092014-06-20 12:51:12 -07005838 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 }
5840
Daniel Sandler69a48172010-06-23 16:29:36 -04005841 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07005842 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07005843 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07005844 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08005845 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005846 * @param fd The raw file descriptor that the dump is being sent to.
5847 * @param writer The PrintWriter to which you should dump your state. This will be
5848 * closed for you after you return.
5849 * @param args additional arguments to the dump request.
5850 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08005851 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07005852 dumpInner(prefix, fd, writer, args);
5853 }
5854
5855 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08005856 writer.print(prefix); writer.print("Local Activity ");
5857 writer.print(Integer.toHexString(System.identityHashCode(this)));
5858 writer.println(" State:");
5859 String innerPrefix = prefix + " ";
5860 writer.print(innerPrefix); writer.print("mResumed=");
5861 writer.print(mResumed); writer.print(" mStopped=");
5862 writer.print(mStopped); writer.print(" mFinished=");
5863 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08005864 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
5865 writer.println(mChangingConfigurations);
5866 writer.print(innerPrefix); writer.print("mCurrentConfig=");
5867 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07005868
Todd Kennedya5fc6f02015-04-14 18:22:54 -07005869 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
5870 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07005871 if (mVoiceInteractor != null) {
5872 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
5873 }
Jeff Brown5182c782013-10-15 20:31:52 -07005874
Michael Wright5f48dc72013-11-01 12:42:49 -07005875 if (getWindow() != null &&
5876 getWindow().peekDecorView() != null &&
5877 getWindow().peekDecorView().getViewRootImpl() != null) {
5878 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
5879 }
Jeff Brown5182c782013-10-15 20:31:52 -07005880
5881 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07005882 }
5883
5884 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04005885 * Bit indicating that this activity is "immersive" and should not be
5886 * interrupted by notifications if possible.
5887 *
5888 * This value is initially set by the manifest property
5889 * <code>android:immersive</code> but may be changed at runtime by
5890 * {@link #setImmersive}.
5891 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07005892 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04005893 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5894 */
5895 public boolean isImmersive() {
5896 try {
5897 return ActivityManagerNative.getDefault().isImmersive(mToken);
5898 } catch (RemoteException e) {
5899 return false;
5900 }
5901 }
5902
5903 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07005904 * Indication of whether this is the highest level activity in this task. Can be used to
5905 * determine whether an activity launched by this activity was placed in the same task or
5906 * another task.
5907 *
5908 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07005909 */
George Mountff243282014-07-07 16:12:07 -07005910 private boolean isTopOfTask() {
Craig Mautnerd61dc202014-07-07 11:09:11 -07005911 try {
5912 return ActivityManagerNative.getDefault().isTopOfTask(mToken);
5913 } catch (RemoteException e) {
5914 return false;
5915 }
5916 }
5917
5918 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07005919 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
5920 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005921 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005922 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005923 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
5924 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005925 * This call has no effect on non-translucent activities or on activities with the
5926 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005927 *
George Mount62ab9b72014-05-02 13:51:17 -07005928 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
5929 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07005930 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005931 *
5932 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07005933 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005934 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07005935 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07005936 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07005937 mTranslucentCallback = null;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005938 if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) {
5939 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
5940 }
Craig Mautner4addfc52013-06-25 08:05:45 -07005941 } catch (RemoteException e) {
5942 // pass
5943 }
5944 }
5945
5946 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07005947 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
5948 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
5949 * <p>
5950 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
5951 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
5952 * be called indicating that it is safe to make this activity translucent again. Until
5953 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
5954 * behind the frontmost Activity will be indeterminate.
5955 * <p>
5956 * This call has no effect on non-translucent activities or on activities with the
5957 * {@link android.R.attr#windowIsFloating} attribute.
5958 *
5959 * @param callback the method to call when all visible Activities behind this one have been
5960 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07005961 * @param options activity options delivered to the activity below this one. The options
5962 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07005963 * @return <code>true</code> if Window was opaque and will become translucent or
5964 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005965 *
5966 * @see #convertFromTranslucent()
5967 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005968 *
5969 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07005970 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005971 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07005972 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07005973 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07005974 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005975 try {
5976 mTranslucentCallback = callback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005977 mChangeCanvasToTranslucent =
Craig Mautner233ceee2014-05-09 17:05:11 -07005978 ActivityManagerNative.getDefault().convertToTranslucent(mToken, options);
George Mount9e183972014-09-03 12:35:09 -07005979 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07005980 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005981 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07005982 // Make callback return as though it timed out.
5983 mChangeCanvasToTranslucent = false;
5984 drawComplete = false;
5985 }
5986 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
5987 // Window is already translucent.
5988 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07005989 }
George Mount3cc716c2014-06-12 16:35:35 -07005990 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005991 }
5992
5993 /** @hide */
5994 void onTranslucentConversionComplete(boolean drawComplete) {
5995 if (mTranslucentCallback != null) {
5996 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
5997 mTranslucentCallback = null;
5998 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005999 if (mChangeCanvasToTranslucent) {
6000 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
6001 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006002 }
6003
Craig Mautnereb8abf72014-07-02 15:04:09 -07006004 /** @hide */
6005 public void onNewActivityOptions(ActivityOptions options) {
6006 mActivityTransitionState.setEnterActivityOptions(this, options);
6007 if (!mStopped) {
6008 mActivityTransitionState.enterReady(this);
6009 }
6010 }
6011
Craig Mautner5eda9b32013-07-02 11:58:16 -07006012 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07006013 * Retrieve the ActivityOptions passed in from the launching activity or passed back
6014 * from an activity launched by this activity in its call to {@link
6015 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
6016 *
6017 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
6018 * @hide
6019 */
6020 ActivityOptions getActivityOptions() {
6021 try {
6022 return ActivityManagerNative.getDefault().getActivityOptions(mToken);
6023 } catch (RemoteException e) {
6024 }
6025 return null;
6026 }
6027
6028 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006029 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07006030 * this method anytime between the start of {@link #onResume()} and the return from
6031 * {@link #onPause()}. If this call is successful then the activity will remain visible after
6032 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
6033 *
6034 * <p>The actions of this call are reset each time that this activity is brought to the
6035 * front. That is, every time {@link #onResume()} is called the activity will be assumed
6036 * to not have requested visible behind. Therefore, if you want this activity to continue to
6037 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006038 *
6039 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
6040 * for dialog and translucent activities.
6041 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006042 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
6043 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
6044 *
6045 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07006046 * the next call to onResume.
6047 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006048 * @param visible true to notify the system that the activity wishes to be visible behind other
6049 * translucent activities, false to indicate otherwise. Resources must be
6050 * released when passing false to this method.
Craig Mautner64ccb702014-10-01 09:38:40 -07006051 * @return the resulting visibiity state. If true the activity will remain visible beyond
6052 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
6053 * then the activity may not count on being visible behind other translucent activities,
6054 * and must stop any media playback and release resources.
6055 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
6056 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006057 *
Jose Limafcf70832014-08-27 23:09:05 -07006058 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006059 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006060 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006061 public boolean requestVisibleBehind(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006062 if (!mResumed) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006063 // Do not permit paused or stopped activities to do this.
6064 visible = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006065 }
6066 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07006067 mVisibleBehind = ActivityManagerNative.getDefault()
6068 .requestVisibleBehind(mToken, visible) && visible;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006069 } catch (RemoteException e) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006070 mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006071 }
Jose Lima4b6c6692014-08-12 17:41:12 -07006072 return mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006073 }
6074
6075 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006076 * Called when a translucent activity over this activity is becoming opaque or another
6077 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07006078 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006079 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006080 * <p>When this method is called the activity has 500 msec to release any resources it may be
6081 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006082 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07006083 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07006084 * process. Otherwise {@link #onStop()} will be called following return.
6085 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006086 * @see #requestVisibleBehind(boolean)
6087 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006088 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006089 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07006090 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006091 mCalled = true;
6092 }
6093
6094 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006095 * Translucent activities may call this to determine if there is an activity below them that
6096 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006097 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006098 * @return true if an activity below is set to visible according to the most recent call to
6099 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006100 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006101 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006102 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006103 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006104 * @hide
6105 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006106 @SystemApi
6107 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006108 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07006109 return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07006110 } catch (RemoteException e) {
6111 }
6112 return false;
6113 }
6114
6115 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006116 * The topmost foreground activity will receive this call when the background visibility state
6117 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006118 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006119 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07006120 * due to a background activity finishing itself.
6121 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006122 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006123 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006124 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006125 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006126 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07006127 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006128 @SystemApi
6129 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006130 }
6131
6132 /**
Craig Mautner8746a472014-07-24 15:12:54 -07006133 * Activities cannot draw during the period that their windows are animating in. In order
6134 * to know when it is safe to begin drawing they can override this method which will be
6135 * called when the entering animation has completed.
6136 */
6137 public void onEnterAnimationComplete() {
6138 }
6139
6140 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08006141 * @hide
6142 */
6143 public void dispatchEnterAnimationComplete() {
6144 onEnterAnimationComplete();
6145 if (getWindow() != null && getWindow().getDecorView() != null) {
6146 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
6147 }
6148 }
6149
6150 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006151 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07006152 *
Daniel Sandler69a48172010-06-23 16:29:36 -04006153 * Note that changing this value will have no effect on the activity's
6154 * {@link android.content.pm.ActivityInfo} structure; that is, if
6155 * <code>android:immersive</code> is set to <code>true</code>
6156 * in the application's manifest entry for this activity, the {@link
6157 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
6158 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6159 * FLAG_IMMERSIVE} bit set.
6160 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006161 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04006162 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6163 */
6164 public void setImmersive(boolean i) {
6165 try {
6166 ActivityManagerNative.getDefault().setImmersive(mToken, i);
6167 } catch (RemoteException e) {
6168 // pass
6169 }
6170 }
6171
Adam Powell6e346362010-07-23 10:18:23 -07006172 /**
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006173 * Enable or disable virtual reality (VR) mode.
6174 *
6175 * <p>VR mode is a hint to Android system services to switch to modes optimized for
6176 * high-performance stereoscopic rendering.</p>
6177 *
6178 * @param enabled {@code true} to enable this mode.
6179 */
6180 public void setVrMode(boolean enabled) {
6181 try {
6182 ActivityManagerNative.getDefault().setVrMode(mToken, enabled);
6183 } catch (RemoteException e) {
6184 // pass
6185 }
6186 }
6187
6188 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006189 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07006190 *
Clara Bayarri4423d912015-03-02 19:42:48 +00006191 * @param callback Callback that will manage lifecycle events for this action mode
6192 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07006193 *
6194 * @see ActionMode
6195 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006196 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07006197 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07006198 return mWindow.getDecorView().startActionMode(callback);
6199 }
6200
Adam Powelldebf3be2010-11-15 18:58:48 -08006201 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006202 * Start an action mode of the given type.
6203 *
6204 * @param callback Callback that will manage lifecycle events for this action mode
6205 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
6206 * @return The ActionMode that was started, or null if it was canceled
6207 *
6208 * @see ActionMode
6209 */
6210 @Nullable
6211 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
6212 return mWindow.getDecorView().startActionMode(callback, type);
6213 }
6214
6215 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006216 * Give the Activity a chance to control the UI for an action mode requested
6217 * by the system.
6218 *
6219 * <p>Note: If you are looking for a notification callback that an action mode
6220 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
6221 *
6222 * @param callback The callback that should control the new action mode
6223 * @return The new action mode, or <code>null</code> if the activity does not want to
6224 * provide special handling for this action mode. (It will be handled by the system.)
6225 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006226 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07006227 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006228 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00006229 // Only Primary ActionModes are represented in the ActionBar.
6230 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
6231 initWindowDecorActionBar();
6232 if (mActionBar != null) {
6233 return mActionBar.startActionMode(callback);
6234 }
Adam Powell6e346362010-07-23 10:18:23 -07006235 }
6236 return null;
6237 }
6238
Adam Powelldebf3be2010-11-15 18:58:48 -08006239 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006240 * {@inheritDoc}
6241 */
6242 @Nullable
6243 @Override
6244 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
6245 try {
6246 mActionModeTypeStarting = type;
6247 return onWindowStartingActionMode(callback);
6248 } finally {
6249 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
6250 }
6251 }
6252
6253 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006254 * Notifies the Activity that an action mode has been started.
6255 * Activity subclasses overriding this method should call the superclass implementation.
6256 *
6257 * @param mode The new action mode.
6258 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006259 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006260 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006261 public void onActionModeStarted(ActionMode mode) {
6262 }
6263
6264 /**
6265 * Notifies the activity that an action mode has finished.
6266 * Activity subclasses overriding this method should call the superclass implementation.
6267 *
6268 * @param mode The action mode that just finished.
6269 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006270 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006271 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006272 public void onActionModeFinished(ActionMode mode) {
6273 }
6274
Adam Powelldd8fab22012-03-22 17:47:27 -07006275 /**
6276 * Returns true if the app should recreate the task when navigating 'up' from this activity
6277 * by using targetIntent.
6278 *
6279 * <p>If this method returns false the app can trivially call
6280 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
6281 * up navigation. If this method returns false, the app should synthesize a new task stack
6282 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
6283 *
6284 * @param targetIntent An intent representing the target destination for up navigation
6285 * @return true if navigating up should recreate a new task stack, false if the same task
6286 * should be used for the destination
6287 */
6288 public boolean shouldUpRecreateTask(Intent targetIntent) {
6289 try {
6290 PackageManager pm = getPackageManager();
6291 ComponentName cn = targetIntent.getComponent();
6292 if (cn == null) {
6293 cn = targetIntent.resolveActivity(pm);
6294 }
6295 ActivityInfo info = pm.getActivityInfo(cn, 0);
6296 if (info.taskAffinity == null) {
6297 return false;
6298 }
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07006299 return ActivityManagerNative.getDefault()
6300 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07006301 } catch (RemoteException e) {
6302 return false;
6303 } catch (NameNotFoundException e) {
6304 return false;
6305 }
6306 }
6307
6308 /**
6309 * Navigate from this activity to the activity specified by upIntent, finishing this activity
6310 * in the process. If the activity indicated by upIntent already exists in the task's history,
6311 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07006312 * finished.
6313 *
6314 * <p>If the indicated activity does not appear in the history stack, this will finish
6315 * each activity in this task until the root activity of the task is reached, resulting in
6316 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
6317 * when an activity may be reached by a path not passing through a canonical parent
6318 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07006319 *
6320 * <p>This method should be used when performing up navigation from within the same task
6321 * as the destination. If up navigation should cross tasks in some cases, see
6322 * {@link #shouldUpRecreateTask(Intent)}.</p>
6323 *
6324 * @param upIntent An intent representing the target destination for up navigation
6325 *
6326 * @return true if up navigation successfully reached the activity indicated by upIntent and
6327 * upIntent was delivered to it. false if an instance of the indicated activity could
6328 * not be found and this activity was simply finished normally.
6329 */
6330 public boolean navigateUpTo(Intent upIntent) {
6331 if (mParent == null) {
6332 ComponentName destInfo = upIntent.getComponent();
6333 if (destInfo == null) {
6334 destInfo = upIntent.resolveActivity(getPackageManager());
6335 if (destInfo == null) {
6336 return false;
6337 }
6338 upIntent = new Intent(upIntent);
6339 upIntent.setComponent(destInfo);
6340 }
6341 int resultCode;
6342 Intent resultData;
6343 synchronized (this) {
6344 resultCode = mResultCode;
6345 resultData = mResultData;
6346 }
6347 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006348 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006349 }
6350 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006351 upIntent.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006352 return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent,
6353 resultCode, resultData);
6354 } catch (RemoteException e) {
6355 return false;
6356 }
6357 } else {
6358 return mParent.navigateUpToFromChild(this, upIntent);
6359 }
6360 }
6361
6362 /**
6363 * This is called when a child activity of this one calls its
6364 * {@link #navigateUpTo} method. The default implementation simply calls
6365 * navigateUpTo(upIntent) on this activity (the parent).
6366 *
6367 * @param child The activity making the call.
6368 * @param upIntent An intent representing the target destination for up navigation
6369 *
6370 * @return true if up navigation successfully reached the activity indicated by upIntent and
6371 * upIntent was delivered to it. false if an instance of the indicated activity could
6372 * not be found and this activity was simply finished normally.
6373 */
6374 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
6375 return navigateUpTo(upIntent);
6376 }
6377
6378 /**
6379 * Obtain an {@link Intent} that will launch an explicit target activity specified by
6380 * this activity's logical parent. The logical parent is named in the application's manifest
6381 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07006382 * Activity subclasses may override this method to modify the Intent returned by
6383 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
6384 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07006385 *
Adam Powell04d58112012-04-09 10:22:12 -07006386 * @return a new Intent targeting the defined parent of this activity or null if
6387 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07006388 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006389 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07006390 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07006391 final String parentName = mActivityInfo.parentActivityName;
6392 if (TextUtils.isEmpty(parentName)) {
6393 return null;
6394 }
Adam Powell5a4010c2012-09-16 15:14:05 -07006395
6396 // If the parent itself has no parent, generate a main activity intent.
6397 final ComponentName target = new ComponentName(this, parentName);
6398 try {
6399 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
6400 final String parentActivity = parentInfo.parentActivityName;
6401 final Intent parentIntent = parentActivity == null
6402 ? Intent.makeMainActivity(target)
6403 : new Intent().setComponent(target);
6404 return parentIntent;
6405 } catch (NameNotFoundException e) {
6406 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
6407 "' in manifest");
6408 return null;
6409 }
Adam Powelldd8fab22012-03-22 17:47:27 -07006410 }
6411
George Mount31a21722014-03-24 17:44:36 -07006412 /**
George Mount62ab9b72014-05-02 13:51:17 -07006413 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006414 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006415 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07006416 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07006417 *
George Mount65580562014-08-29 08:15:48 -07006418 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07006419 */
George Mount65580562014-08-29 08:15:48 -07006420 public void setEnterSharedElementCallback(SharedElementCallback callback) {
6421 if (callback == null) {
6422 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07006423 }
George Mount65580562014-08-29 08:15:48 -07006424 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07006425 }
6426
6427 /**
6428 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006429 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006430 * will be called to handle shared elements on the <i>launching</i> Activity. Most
6431 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07006432 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07006433 *
George Mount65580562014-08-29 08:15:48 -07006434 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07006435 */
George Mount65580562014-08-29 08:15:48 -07006436 public void setExitSharedElementCallback(SharedElementCallback callback) {
6437 if (callback == null) {
6438 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07006439 }
George Mount65580562014-08-29 08:15:48 -07006440 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07006441 }
6442
George Mount8c2614c2014-06-10 11:12:01 -07006443 /**
6444 * Postpone the entering activity transition when Activity was started with
6445 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6446 * android.util.Pair[])}.
6447 * <p>This method gives the Activity the ability to delay starting the entering and
6448 * shared element transitions until all data is loaded. Until then, the Activity won't
6449 * draw into its window, leaving the window transparent. This may also cause the
6450 * returning animation to be delayed until data is ready. This method should be
6451 * called in {@link #onCreate(android.os.Bundle)} or in
6452 * {@link #onActivityReenter(int, android.content.Intent)}.
6453 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
6454 * start the transitions. If the Activity did not use
6455 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6456 * android.util.Pair[])}, then this method does nothing.</p>
6457 */
6458 public void postponeEnterTransition() {
6459 mActivityTransitionState.postponeEnterTransition();
6460 }
6461
6462 /**
6463 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
6464 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
6465 * to have your Activity start drawing.
6466 */
6467 public void startPostponedEnterTransition() {
6468 mActivityTransitionState.startPostponedEnterTransition();
6469 }
6470
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006471 /**
6472 * Create {@link DropPermissions} object bound to this activity and controlling the access
6473 * permissions for content URIs associated with the {@link DragEvent}.
6474 * @param event Drag event
6475 * @return The DropPermissions object used to control access to the content URIs. Null if
6476 * no content URIs are associated with the event or if permissions could not be granted.
6477 */
6478 public DropPermissions requestDropPermissions(DragEvent event) {
6479 DropPermissions dropPermissions = DropPermissions.obtain(event);
6480 if (dropPermissions != null && dropPermissions.take(getActivityToken())) {
6481 return dropPermissions;
6482 }
6483 return null;
6484 }
6485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006486 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07006487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006488 final void setParent(Activity parent) {
6489 mParent = parent;
6490 }
6491
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006492 final void attach(Context context, ActivityThread aThread,
6493 Instrumentation instr, IBinder token, int ident,
6494 Application application, Intent intent, ActivityInfo info,
6495 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006496 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006497 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
6498 Window window) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 attachBaseContext(context);
6500
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006501 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07006502
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006503 mWindow = new PhoneWindow(this, window);
Skuhnece2faa52015-08-11 10:36:38 -07006504 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07006506 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08006507 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
6509 mWindow.setSoftInputMode(info.softInputMode);
6510 }
Adam Powell269248d2011-08-02 10:26:54 -07006511 if (info.uiOptions != 0) {
6512 mWindow.setUiOptions(info.uiOptions);
6513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006514 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08006515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006516 mMainThread = aThread;
6517 mInstrumentation = instr;
6518 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006519 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006520 mApplication = application;
6521 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006522 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006523 mComponent = intent.getComponent();
6524 mActivityInfo = info;
6525 mTitle = title;
6526 mParent = parent;
6527 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006528 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07006529 if (voiceInteractor != null) {
6530 if (lastNonConfigurationInstances != null) {
6531 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
6532 } else {
6533 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
6534 Looper.myLooper());
6535 }
6536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006537
Jeff Brown98365d72012-08-19 20:30:52 -07006538 mWindow.setWindowManager(
6539 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
6540 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07006541 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006542 if (mParent != null) {
6543 mWindow.setContainer(mParent.getWindow());
6544 }
6545 mWindowManager = mWindow.getWindowManager();
6546 mCurrentConfig = config;
6547 }
6548
Dianne Hackborn5320eb82012-05-18 12:05:04 -07006549 /** @hide */
6550 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006551 return mParent != null ? mParent.getActivityToken() : mToken;
6552 }
6553
Craig Mautnera0026042014-04-23 11:45:37 -07006554 final void performCreateCommon() {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006555 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
6556 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07006557 mFragments.dispatchActivityCreated();
George Mount62ab9b72014-05-02 13:51:17 -07006558 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006559 }
George Mount0a778ed2013-12-13 13:35:36 -08006560
Craig Mautnera0026042014-04-23 11:45:37 -07006561 final void performCreate(Bundle icicle) {
Svetoslavffb32b12015-10-15 16:54:00 -07006562 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006563 onCreate(icicle);
George Mount62ab9b72014-05-02 13:51:17 -07006564 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006565 performCreateCommon();
6566 }
6567
6568 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Svetoslavffb32b12015-10-15 16:54:00 -07006569 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006570 onCreate(icicle, persistentState);
George Mount62ab9b72014-05-02 13:51:17 -07006571 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006572 performCreateCommon();
6573 }
6574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006575 final void performStart() {
George Mount62ab9b72014-05-02 13:51:17 -07006576 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006577 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07006579 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 mInstrumentation.callActivityOnStart(this);
6581 if (!mCalled) {
6582 throw new SuperNotCalledException(
6583 "Activity " + mComponent.toShortString() +
6584 " did not call through to super.onStart()");
6585 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006586 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006587 mFragments.reportLoaderStart();
George Mount62ab9b72014-05-02 13:51:17 -07006588 mActivityTransitionState.enterReady(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006589 }
RoboErik55011652014-07-09 15:05:53 -07006590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 final void performRestart() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006592 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07006593
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006594 if (mToken != null && mParent == null) {
6595 // We might have view roots that were preserved during a relaunch, we need to start them
6596 // again. We don't need to check mStopped, the roots will check if they were actually
6597 // stopped.
6598 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
6599 }
6600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601 if (mStopped) {
6602 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006603
6604 synchronized (mManagedCursors) {
6605 final int N = mManagedCursors.size();
6606 for (int i=0; i<N; i++) {
6607 ManagedCursor mc = mManagedCursors.get(i);
6608 if (mc.mReleased || mc.mUpdated) {
6609 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07006610 if (getApplicationInfo().targetSdkVersion
6611 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
6612 throw new IllegalStateException(
6613 "trying to requery an already closed cursor "
6614 + mc.mCursor);
6615 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006616 }
6617 mc.mReleased = false;
6618 mc.mUpdated = false;
6619 }
6620 }
6621 }
6622
6623 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 mInstrumentation.callActivityOnRestart(this);
6625 if (!mCalled) {
6626 throw new SuperNotCalledException(
6627 "Activity " + mComponent.toShortString() +
6628 " did not call through to super.onRestart()");
6629 }
6630 performStart();
6631 }
6632 }
RoboErik55011652014-07-09 15:05:53 -07006633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 final void performResume() {
6635 performRestart();
RoboErik55011652014-07-09 15:05:53 -07006636
Dianne Hackborn445646c2010-06-25 15:52:59 -07006637 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006638
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006639 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07006640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06006642 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006643 mInstrumentation.callActivityOnResume(this);
6644 if (!mCalled) {
6645 throw new SuperNotCalledException(
6646 "Activity " + mComponent.toShortString() +
6647 " did not call through to super.onResume()");
6648 }
6649
Todd Kennedyee8c9c62014-12-10 14:22:59 -08006650 // invisible activities must be finished before onResume() completes
6651 if (!mVisibleFromClient && !mFinished) {
6652 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
6653 if (getApplicationInfo().targetSdkVersion
6654 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
6655 throw new IllegalStateException(
6656 "Activity " + mComponent.toShortString() +
6657 " did not call finish() prior to onResume() completing");
6658 }
6659 }
6660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07006663
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006664 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07006665 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006667 onPostResume();
6668 if (!mCalled) {
6669 throw new SuperNotCalledException(
6670 "Activity " + mComponent.toShortString() +
6671 " did not call through to super.onPostResume()");
6672 }
6673 }
6674
6675 final void performPause() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006676 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006677 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006678 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 onPause();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006680 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006681 if (!mCalled && getApplicationInfo().targetSdkVersion
6682 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
6683 throw new SuperNotCalledException(
6684 "Activity " + mComponent.toShortString() +
6685 " did not call through to super.onPause()");
6686 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06006687 mResumed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688 }
RoboErik55011652014-07-09 15:05:53 -07006689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 final void performUserLeaving() {
6691 onUserInteraction();
6692 onUserLeaveHint();
6693 }
RoboErik55011652014-07-09 15:05:53 -07006694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006695 final void performStop() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006696 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006697 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
RoboErik55011652014-07-09 15:05:53 -07006698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006699 if (!mStopped) {
6700 if (mWindow != null) {
6701 mWindow.closeAllPanels();
6702 }
6703
Dianne Hackbornce418e62011-03-01 14:31:38 -08006704 if (mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07006705 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08006706 }
RoboErik55011652014-07-09 15:05:53 -07006707
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006708 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07006709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006710 mCalled = false;
6711 mInstrumentation.callActivityOnStop(this);
6712 if (!mCalled) {
6713 throw new SuperNotCalledException(
6714 "Activity " + mComponent.toShortString() +
6715 " did not call through to super.onStop()");
6716 }
RoboErik55011652014-07-09 15:05:53 -07006717
Makoto Onuki2f6a0182010-02-22 13:26:59 -08006718 synchronized (mManagedCursors) {
6719 final int N = mManagedCursors.size();
6720 for (int i=0; i<N; i++) {
6721 ManagedCursor mc = mManagedCursors.get(i);
6722 if (!mc.mReleased) {
6723 mc.mCursor.deactivate();
6724 mc.mReleased = true;
6725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006726 }
6727 }
George Mount0a778ed2013-12-13 13:35:36 -08006728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 mStopped = true;
6730 }
6731 mResumed = false;
6732 }
6733
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006734 final void performDestroy() {
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006735 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07006736 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006737 mFragments.dispatchDestroy();
6738 onDestroy();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006739 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07006740 if (mVoiceInteractor != null) {
6741 mVoiceInteractor.detachActivity();
6742 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006743 }
George Mount0a778ed2013-12-13 13:35:36 -08006744
6745 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06006746 * @hide
6747 */
6748 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006749 return mResumed;
6750 }
6751
Svetoslavffb32b12015-10-15 16:54:00 -07006752 private void storeHasCurrentPermissionRequest(Bundle bundle) {
6753 if (bundle != null && mHasCurrentPermissionsRequest) {
6754 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
6755 }
6756 }
6757
6758 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
6759 if (bundle != null) {
6760 mHasCurrentPermissionsRequest = bundle.getBoolean(
6761 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
6762 }
6763 }
6764
George Mount0a778ed2013-12-13 13:35:36 -08006765 void dispatchActivityResult(String who, int requestCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766 int resultCode, Intent data) {
Joe Onorato43a17652011-04-06 19:22:23 -07006767 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
6769 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006770 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07006772 onActivityResult(requestCode, resultCode, data);
6773 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
6774 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
6775 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08006776 dispatchRequestPermissionsResult(requestCode, data);
6777 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006778 Fragment frag = mFragments.findFragmentByWho(who);
6779 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07006780 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08006781 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006782 }
Svetoslav970b59c2015-06-09 16:05:21 -07006783 } else if (who.startsWith("@android:view:")) {
6784 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
6785 getActivityToken());
6786 for (ViewRootImpl viewRoot : views) {
6787 if (viewRoot.getView() != null
6788 && viewRoot.getView().dispatchActivityResult(
6789 who, requestCode, resultCode, data)) {
6790 return;
6791 }
6792 }
6793 } else {
6794 Fragment frag = mFragments.findFragmentByWho(who);
6795 if (frag != null) {
6796 frag.onActivityResult(requestCode, resultCode, data);
6797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006798 }
6799 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006800
Jason Monka57e5e02014-05-07 10:06:48 -04006801 /**
Jason Monk62515be2014-05-21 16:06:19 -04006802 * Request to put this Activity in a mode where the user is locked to the
Jason Monka57e5e02014-05-07 10:06:48 -04006803 * current task.
6804 *
Craig Mautner15df08a2015-04-01 12:17:18 -07006805 * This will prevent the user from launching other apps, going to settings, or reaching the
6806 * home screen. This does not include those apps whose {@link android.R.attr#lockTaskMode}
6807 * values permit launching while locked.
Jason Monka57e5e02014-05-07 10:06:48 -04006808 *
Craig Mautner15df08a2015-04-01 12:17:18 -07006809 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true or
6810 * lockTaskMode=lockTaskModeAlways for this component then the app will go directly into
6811 * Lock Task mode. The user will not be able to exit this mode until
6812 * {@link Activity#stopLockTask()} is called.
Jason Monk62515be2014-05-21 16:06:19 -04006813 *
6814 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false
6815 * then the system will prompt the user with a dialog requesting permission to enter
Jason Monk8863b572014-07-29 13:49:32 -04006816 * this mode. When entered through this method the user can exit at any time through
6817 * an action described by the request dialog. Calling stopLockTask will also exit the
6818 * mode.
Craig Mautner15df08a2015-04-01 12:17:18 -07006819 *
6820 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04006821 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006822 public void startLockTask() {
6823 try {
6824 ActivityManagerNative.getDefault().startLockTaskMode(mToken);
6825 } catch (RemoteException e) {
6826 }
6827 }
6828
Jason Monka57e5e02014-05-07 10:06:48 -04006829 /**
6830 * Allow the user to switch away from the current task.
6831 *
6832 * Called to end the mode started by {@link Activity#startLockTask}. This
6833 * can only be called by activities that have successfully called
6834 * startLockTask previously.
6835 *
6836 * This will allow the user to exit this app and move onto other activities.
Craig Mautner15df08a2015-04-01 12:17:18 -07006837 * <p>Note: This method should only be called when the activity is user-facing. That is,
6838 * between onResume() and onPause().
6839 * <p>Note: If there are other tasks below this one that are also locked then calling this
6840 * method will immediately finish this task and resume the previous locked one, remaining in
6841 * lockTask mode.
6842 *
6843 * @see android.R.attr#lockTaskMode
6844 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04006845 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006846 public void stopLockTask() {
6847 try {
6848 ActivityManagerNative.getDefault().stopLockTaskMode();
6849 } catch (RemoteException e) {
6850 }
6851 }
6852
Craig Mautner5eda9b32013-07-02 11:58:16 -07006853 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07006854 * Shows the user the system defined message for telling the user how to exit
6855 * lock task mode. The task containing this activity must be in lock task mode at the time
6856 * of this call for the message to be displayed.
6857 */
6858 public void showLockTaskEscapeMessage() {
6859 try {
6860 ActivityManagerNative.getDefault().showLockTaskEscapeMessage(mToken);
6861 } catch (RemoteException e) {
6862 }
6863 }
6864
6865 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08006866 * Set whether the caption should displayed directly on the content rather than push it down.
6867 *
6868 * This affects only freeform windows since they display the caption and only the main
6869 * window of the activity. The caption is used to drag the window around and also shows
6870 * maximize and close action buttons.
6871 */
6872 public void overlayWithDecorCaption(boolean overlay) {
6873 mWindow.setOverlayDecorCaption(overlay);
6874 }
6875
6876 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006877 * Interface for informing a translucent {@link Activity} once all visible activities below it
6878 * have completed drawing. This is necessary only after an {@link Activity} has been made
6879 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
6880 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07006881 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6882 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07006883 *
6884 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006885 */
Jose Lima14914852014-08-14 09:14:12 -07006886 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006887 public interface TranslucentConversionListener {
6888 /**
6889 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
6890 * below the top one have been redrawn. Following this callback it is safe to make the top
6891 * Activity translucent because the underlying Activity has been drawn.
6892 *
6893 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
6894 * occurred waiting for the Activity to complete drawing.
6895 *
6896 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07006897 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006898 */
6899 public void onTranslucentConversionComplete(boolean drawComplete);
6900 }
Svetoslavc6d1c342015-02-26 14:44:43 -08006901
6902 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07006903 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07006904 // If the package installer crashed we may have not data - best effort.
6905 String[] permissions = (data != null) ? data.getStringArrayExtra(
6906 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
6907 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
6908 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08006909 onRequestPermissionsResult(requestCode, permissions, grantResults);
6910 }
6911
6912 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07006913 Fragment fragment) {
6914 // If the package installer crashed we may have not data - best effort.
6915 String[] permissions = (data != null) ? data.getStringArrayExtra(
6916 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
6917 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
6918 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
6919 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08006920 }
6921
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006922 class HostCallbacks extends FragmentHostCallback<Activity> {
6923 public HostCallbacks() {
6924 super(Activity.this /*activity*/);
6925 }
6926
6927 @Override
6928 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
6929 Activity.this.dump(prefix, fd, writer, args);
6930 }
6931
6932 @Override
6933 public boolean onShouldSaveFragmentState(Fragment fragment) {
6934 return !isFinishing();
6935 }
6936
6937 @Override
6938 public LayoutInflater onGetLayoutInflater() {
6939 final LayoutInflater result = Activity.this.getLayoutInflater();
6940 if (onUseFragmentManagerInflaterFactory()) {
6941 return result.cloneInContext(Activity.this);
6942 }
6943 return result;
6944 }
6945
6946 @Override
6947 public boolean onUseFragmentManagerInflaterFactory() {
6948 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
6949 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
6950 }
6951
6952 @Override
6953 public Activity onGetHost() {
6954 return Activity.this;
6955 }
6956
6957 @Override
6958 public void onInvalidateOptionsMenu() {
6959 Activity.this.invalidateOptionsMenu();
6960 }
6961
6962 @Override
6963 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
6964 Bundle options) {
6965 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
6966 }
6967
6968 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07006969 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
6970 int requestCode) {
6971 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
6972 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
6973 startActivityForResult(who, intent, requestCode, null);
6974 }
6975
6976 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006977 public boolean onHasWindowAnimations() {
6978 return getWindow() != null;
6979 }
6980
6981 @Override
6982 public int onGetWindowAnimations() {
6983 final Window w = getWindow();
6984 return (w == null) ? 0 : w.getAttributes().windowAnimations;
6985 }
6986
Todd Kennedy434bd652015-05-04 12:29:50 -07006987 @Override
6988 public void onAttachFragment(Fragment fragment) {
6989 Activity.this.onAttachFragment(fragment);
6990 }
6991
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006992 @Nullable
6993 @Override
6994 public View onFindViewById(int id) {
6995 return Activity.this.findViewById(id);
6996 }
6997
6998 @Override
6999 public boolean onHasView() {
7000 final Window w = getWindow();
7001 return (w != null && w.peekDecorView() != null);
7002 }
7003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004}