blob: 4792dc98c0eeb7372b290762b9eb314bfbde064f [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 /**
Winsonb6403152016-02-23 13:32:09 -08001122 * Attempts to extract the color from a given drawable.
1123 *
1124 * @return the extracted color or 0 if no color could be extracted.
1125 */
1126 private int tryExtractColorFromDrawable(Drawable drawable) {
1127 if (drawable instanceof ColorDrawable) {
1128 return ((ColorDrawable) drawable).getColor();
1129 } else if (drawable instanceof InsetDrawable) {
1130 return tryExtractColorFromDrawable(((InsetDrawable) drawable).getDrawable());
1131 } else if (drawable instanceof ShapeDrawable) {
1132 Paint p = ((ShapeDrawable) drawable).getPaint();
1133 if (p != null) {
1134 return p.getColor();
1135 }
1136 } else if (drawable instanceof LayerDrawable) {
1137 LayerDrawable ld = (LayerDrawable) drawable;
1138 int numLayers = ld.getNumberOfLayers();
1139 for (int i = 0; i < numLayers; i++) {
1140 int color = tryExtractColorFromDrawable(ld.getDrawable(i));
1141 if (color != 0) {
1142 return color;
1143 }
1144 }
1145 }
1146 return 0;
1147 }
1148
1149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 * Called when activity start-up is complete (after {@link #onStart}
1151 * and {@link #onRestoreInstanceState} have been called). Applications will
1152 * generally not implement this method; it is intended for system
1153 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 * <p><em>Derived classes must call through to the super class's
1156 * implementation of this method. If they do not, an exception will be
1157 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 * @param savedInstanceState If the activity is being re-initialized after
1160 * previously being shut down then this Bundle contains the data it most
1161 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1162 * @see #onCreate
1163 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001164 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001165 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 if (!isChild()) {
1167 mTitleReady = true;
1168 onTitleChanged(getTitle(), getTitleColor());
1169 }
Winsonb6403152016-02-23 13:32:09 -08001170
1171 Resources.Theme theme = getTheme();
1172 if (theme != null) {
1173 // Get the primary color and update the TaskDescription for this activity
1174 TypedArray a = theme.obtainStyledAttributes(
1175 com.android.internal.R.styleable.ActivityTaskDescription);
1176 if (mTaskDescription.getPrimaryColor() == 0) {
1177 int colorPrimary = a.getColor(
1178 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
1179 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
1180 mTaskDescription.setPrimaryColor(colorPrimary);
1181 }
1182 }
1183 if (mTaskDescription.getBackgroundColor() == 0) {
1184 int windowBgResourceId = a.getResourceId(
1185 com.android.internal.R.styleable.ActivityTaskDescription_windowBackground,
1186 0);
1187 int windowBgFallbackResourceId = a.getResourceId(
1188 com.android.internal.R.styleable.ActivityTaskDescription_windowBackgroundFallback,
1189 0);
1190 int colorBg = tryExtractColorFromDrawable(DecorView.getResizingBackgroundDrawable(
1191 this, windowBgResourceId, windowBgFallbackResourceId));
1192 if (colorBg != 0 && Color.alpha(colorBg) == 0xFF) {
1193 mTaskDescription.setBackgroundColor(colorBg);
1194 }
1195 }
1196 a.recycle();
1197 setTaskDescription(mTaskDescription);
1198 }
1199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 mCalled = true;
1201 }
1202
1203 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001204 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001205 * created with the attribute {@link android.R.attr#persistableMode} set to
1206 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001207 *
1208 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1209 * @param persistentState The data caming from the PersistableBundle first
1210 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1211 *
1212 * @see #onCreate
1213 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001214 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001215 @Nullable PersistableBundle persistentState) {
1216 onPostCreate(savedInstanceState);
1217 }
1218
1219 /**
RoboErik55011652014-07-09 15:05:53 -07001220 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1221 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001222 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 *
1224 * <p><em>Derived classes must call through to the super class's
1225 * implementation of this method. If they do not, an exception will be
1226 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001227 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 * @see #onCreate
1229 * @see #onStop
1230 * @see #onResume
1231 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001232 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001234 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001236
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001237 mFragments.doLoaderStart();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001238
1239 getApplication().dispatchActivityStarted(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 }
1241
1242 /**
1243 * Called after {@link #onStop} when the current activity is being
1244 * re-displayed to the user (the user has navigated back to it). It will
1245 * be followed by {@link #onStart} and then {@link #onResume}.
1246 *
1247 * <p>For activities that are using raw {@link Cursor} objects (instead of
1248 * creating them through
1249 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1250 * this is usually the place
1251 * where the cursor should be requeried (because you had deactivated it in
1252 * {@link #onStop}.
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 #onStop
1259 * @see #onStart
1260 * @see #onResume
1261 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001262 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 protected void onRestart() {
1264 mCalled = true;
1265 }
1266
1267 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001268 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1269 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1270 * to give the activity a hint that its state is no longer saved -- it will generally
1271 * be called after {@link #onSaveInstanceState} and prior to the activity being
1272 * resumed/started again.
1273 */
1274 public void onStateNotSaved() {
1275 }
1276
1277 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1279 * {@link #onPause}, for your activity to start interacting with the user.
1280 * This is a good place to begin animations, open exclusive-access devices
1281 * (such as the camera), etc.
1282 *
1283 * <p>Keep in mind that onResume is not the best indicator that your activity
1284 * is visible to the user; a system window such as the keyguard may be in
1285 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1286 * activity is visible to the user (for example, to resume a game).
1287 *
1288 * <p><em>Derived classes must call through to the super class's
1289 * implementation of this method. If they do not, an exception will be
1290 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001291 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 * @see #onRestoreInstanceState
1293 * @see #onRestart
1294 * @see #onPostResume
1295 * @see #onPause
1296 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001297 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001299 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001300 getApplication().dispatchActivityResumed(this);
George Mount8cab50a2014-05-15 09:57:17 -07001301 mActivityTransitionState.onResume();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 mCalled = true;
1303 }
1304
1305 /**
1306 * Called when activity resume is complete (after {@link #onResume} has
1307 * been called). Applications will generally not implement this method;
1308 * it is intended for system classes to do final setup after application
1309 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001310 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 * <p><em>Derived classes must call through to the super class's
1312 * implementation of this method. If they do not, an exception will be
1313 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001314 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 * @see #onResume
1316 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001317 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 protected void onPostResume() {
1319 final Window win = getWindow();
1320 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001321 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 mCalled = true;
1323 }
1324
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001325 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
1326 if (voiceInteractor == null) {
1327 mVoiceInteractor = null;
1328 } else {
1329 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1330 Looper.myLooper());
1331 }
1332 }
1333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001335 * Check whether this activity is running as part of a voice interaction with the user.
1336 * If true, it should perform its interaction with the user through the
1337 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1338 */
1339 public boolean isVoiceInteraction() {
1340 return mVoiceInteractor != null;
1341 }
1342
1343 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001344 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1345 * of a voice interaction. That is, returns true if this activity was directly
1346 * started by the voice interaction service as the initiation of a voice interaction.
1347 * Otherwise, for example if it was started by another activity while under voice
1348 * interaction, returns false.
1349 */
1350 public boolean isVoiceInteractionRoot() {
1351 try {
1352 return mVoiceInteractor != null
1353 && ActivityManagerNative.getDefault().isRootVoiceInteraction(mToken);
1354 } catch (RemoteException e) {
1355 }
1356 return false;
1357 }
1358
1359 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001360 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1361 * interact with this activity.
1362 */
1363 public VoiceInteractor getVoiceInteractor() {
1364 return mVoiceInteractor;
1365 }
1366
1367 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001368 * Queries whether the currently enabled voice interaction service supports returning
1369 * a voice interactor for use by the activity. This is valid only for the duration of the
1370 * activity.
1371 *
1372 * @return whether the current voice interaction service supports local voice interaction
1373 */
1374 public boolean isLocalVoiceInteractionSupported() {
1375 try {
1376 return ActivityManagerNative.getDefault().supportsLocalVoiceInteraction();
1377 } catch (RemoteException re) {
1378 }
1379 return false;
1380 }
1381
1382 /**
1383 * Starts a local voice interaction session. When ready,
1384 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1385 * to the registered voice interaction service.
1386 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1387 */
1388 public void startLocalVoiceInteraction(Bundle privateOptions) {
1389 try {
1390 ActivityManagerNative.getDefault().startLocalVoiceInteraction(mToken, privateOptions);
1391 } catch (RemoteException re) {
1392 }
1393 }
1394
1395 /**
1396 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1397 * voice interaction session being started. You can now retrieve a voice interactor using
1398 * {@link #getVoiceInteractor()}.
1399 */
1400 public void onLocalVoiceInteractionStarted() {
1401 Log.i(TAG, "onLocalVoiceInteractionStarted! " + getVoiceInteractor());
1402 }
1403
1404 /**
1405 * Callback to indicate that the local voice interaction has stopped for some
1406 * reason.
1407 */
1408 public void onLocalVoiceInteractionStopped() {
1409 Log.i(TAG, "onLocalVoiceInteractionStopped :( " + getVoiceInteractor());
1410 }
1411
1412 /**
1413 * Request to terminate the current voice interaction that was previously started
1414 * using {@link #startLocalVoiceInteraction(Bundle)}.
1415 */
1416 public void stopLocalVoiceInteraction() {
1417 try {
1418 ActivityManagerNative.getDefault().stopLocalVoiceInteraction(mToken);
1419 } catch (RemoteException re) {
1420 }
1421 }
1422
1423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 * This is called for activities that set launchMode to "singleTop" in
1425 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1426 * flag when calling {@link #startActivity}. In either case, when the
1427 * activity is re-launched while at the top of the activity stack instead
1428 * of a new instance of the activity being started, onNewIntent() will be
1429 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001430 * re-launch it.
1431 *
1432 * <p>An activity will always be paused before receiving a new intent, so
1433 * you can count on {@link #onResume} being called after this method.
1434 *
1435 * <p>Note that {@link #getIntent} still returns the original Intent. You
1436 * can use {@link #setIntent} to update it to this new Intent.
1437 *
1438 * @param intent The new intent that was started for the activity.
1439 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001441 * @see #setIntent
1442 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 */
1444 protected void onNewIntent(Intent intent) {
1445 }
1446
1447 /**
1448 * The hook for {@link ActivityThread} to save the state of this activity.
1449 *
1450 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1451 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1452 *
1453 * @param outState The bundle to save the state to.
1454 */
1455 final void performSaveInstanceState(Bundle outState) {
1456 onSaveInstanceState(outState);
1457 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001458 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001459 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001460 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 }
1462
1463 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001464 * The hook for {@link ActivityThread} to save the state of this activity.
1465 *
1466 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1467 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1468 *
1469 * @param outState The bundle to save the state to.
1470 * @param outPersistentState The bundle to save persistent state to.
1471 */
1472 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1473 onSaveInstanceState(outState, outPersistentState);
1474 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001475 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001476 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1477 ", " + outPersistentState);
1478 }
1479
1480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 * Called to retrieve per-instance state from an activity before being killed
1482 * so that the state can be restored in {@link #onCreate} or
1483 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1484 * will be passed to both).
1485 *
1486 * <p>This method is called before an activity may be killed so that when it
1487 * comes back some time in the future it can restore its state. For example,
1488 * if activity B is launched in front of activity A, and at some point activity
1489 * A is killed to reclaim resources, activity A will have a chance to save the
1490 * current state of its user interface via this method so that when the user
1491 * returns to activity A, the state of the user interface can be restored
1492 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1493 *
1494 * <p>Do not confuse this method with activity lifecycle callbacks such as
1495 * {@link #onPause}, which is always called when an activity is being placed
1496 * in the background or on its way to destruction, or {@link #onStop} which
1497 * is called before destruction. One example of when {@link #onPause} and
1498 * {@link #onStop} is called and not this method is when a user navigates back
1499 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1500 * on B because that particular instance will never be restored, so the
1501 * system avoids calling it. An example when {@link #onPause} is called and
1502 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1503 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1504 * killed during the lifetime of B since the state of the user interface of
1505 * A will stay intact.
1506 *
1507 * <p>The default implementation takes care of most of the UI per-instance
1508 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1509 * view in the hierarchy that has an id, and by saving the id of the currently
1510 * focused view (all of which is restored by the default implementation of
1511 * {@link #onRestoreInstanceState}). If you override this method to save additional
1512 * information not captured by each individual view, you will likely want to
1513 * call through to the default implementation, otherwise be prepared to save
1514 * all of the state of each view yourself.
1515 *
1516 * <p>If called, this method will occur before {@link #onStop}. There are
1517 * no guarantees about whether it will occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001518 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001520 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 * @see #onCreate
1522 * @see #onRestoreInstanceState
1523 * @see #onPause
1524 */
1525 protected void onSaveInstanceState(Bundle outState) {
1526 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001527 Parcelable p = mFragments.saveAllState();
1528 if (p != null) {
1529 outState.putParcelable(FRAGMENTS_TAG, p);
1530 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001531 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 }
1533
1534 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001535 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001536 * created with the attribute {@link android.R.attr#persistableMode} set to
1537 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1538 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1539 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001540 *
1541 * @param outState Bundle in which to place your saved state.
1542 * @param outPersistentState State which will be saved across reboots.
1543 *
1544 * @see #onSaveInstanceState(Bundle)
1545 * @see #onCreate
1546 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1547 * @see #onPause
1548 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001549 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001550 onSaveInstanceState(outState);
1551 }
1552
1553 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 * Save the state of any managed dialogs.
1555 *
1556 * @param outState place to store the saved state.
1557 */
1558 private void saveManagedDialogs(Bundle outState) {
1559 if (mManagedDialogs == null) {
1560 return;
1561 }
1562
1563 final int numDialogs = mManagedDialogs.size();
1564 if (numDialogs == 0) {
1565 return;
1566 }
1567
1568 Bundle dialogState = new Bundle();
1569
1570 int[] ids = new int[mManagedDialogs.size()];
1571
1572 // save each dialog's bundle, gather the ids
1573 for (int i = 0; i < numDialogs; i++) {
1574 final int key = mManagedDialogs.keyAt(i);
1575 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001576 final ManagedDialog md = mManagedDialogs.valueAt(i);
1577 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1578 if (md.mArgs != null) {
1579 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
1582
1583 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1584 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1585 }
1586
1587
1588 /**
1589 * Called as part of the activity lifecycle when an activity is going into
1590 * the background, but has not (yet) been killed. The counterpart to
1591 * {@link #onResume}.
1592 *
1593 * <p>When activity B is launched in front of activity A, this callback will
1594 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1595 * so be sure to not do anything lengthy here.
1596 *
1597 * <p>This callback is mostly used for saving any persistent state the
1598 * activity is editing, to present a "edit in place" model to the user and
1599 * making sure nothing is lost if there are not enough resources to start
1600 * the new activity without first killing this one. This is also a good
1601 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001602 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 * as fast as possible, or to close resources that are exclusive access
1604 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001605 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 * <p>In situations where the system needs more memory it may kill paused
1607 * processes to reclaim resources. Because of this, you should be sure
1608 * that all of your state is saved by the time you return from
1609 * this function. In general {@link #onSaveInstanceState} is used to save
1610 * per-instance state in the activity and this method is used to store
1611 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001612 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 * <p>After receiving this call you will usually receive a following call
1614 * to {@link #onStop} (after the next activity has been resumed and
1615 * displayed), however in some cases there will be a direct call back to
1616 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001617 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 * <p><em>Derived classes must call through to the super class's
1619 * implementation of this method. If they do not, an exception will be
1620 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001621 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 * @see #onResume
1623 * @see #onSaveInstanceState
1624 * @see #onStop
1625 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001626 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001628 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001629 getApplication().dispatchActivityPaused(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 mCalled = true;
1631 }
1632
1633 /**
1634 * Called as part of the activity lifecycle when an activity is about to go
1635 * into the background as the result of user choice. For example, when the
1636 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1637 * when an incoming phone call causes the in-call Activity to be automatically
1638 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1639 * the activity being interrupted. In cases when it is invoked, this method
1640 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001641 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 * <p>This callback and {@link #onUserInteraction} are intended to help
1643 * activities manage status bar notifications intelligently; specifically,
1644 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07001645 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 * @see #onUserInteraction()
1647 */
1648 protected void onUserLeaveHint() {
1649 }
RoboErik55011652014-07-09 15:05:53 -07001650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 /**
1652 * Generate a new thumbnail for this activity. This method is called before
1653 * pausing the activity, and should draw into <var>outBitmap</var> the
1654 * imagery for the desired thumbnail in the dimensions of that bitmap. It
1655 * can use the given <var>canvas</var>, which is configured to draw into the
1656 * bitmap, for rendering if desired.
RoboErik55011652014-07-09 15:05:53 -07001657 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001658 * <p>The default implementation returns fails and does not draw a thumbnail;
1659 * this will result in the platform creating its own thumbnail if needed.
RoboErik55011652014-07-09 15:05:53 -07001660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 * @param outBitmap The bitmap to contain the thumbnail.
1662 * @param canvas Can be used to render into the bitmap.
RoboErik55011652014-07-09 15:05:53 -07001663 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 * @return Return true if you have drawn into the bitmap; otherwise after
1665 * you return it will be filled with a default thumbnail.
RoboErik55011652014-07-09 15:05:53 -07001666 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 * @see #onCreateDescription
1668 * @see #onSaveInstanceState
1669 * @see #onPause
1670 */
1671 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001672 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
1674
1675 /**
1676 * Generate a new description for this activity. This method is called
1677 * before pausing the activity and can, if desired, return some textual
1678 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001679 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 * <p>The default implementation returns null, which will cause you to
1681 * inherit the description from the previous activity. If all activities
1682 * return null, generally the label of the top activity will be used as the
1683 * description.
RoboErik55011652014-07-09 15:05:53 -07001684 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 * @return A description of what the user is doing. It should be short and
1686 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001687 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 * @see #onCreateThumbnail
1689 * @see #onSaveInstanceState
1690 * @see #onPause
1691 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001692 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 public CharSequence onCreateDescription() {
1694 return null;
1695 }
1696
1697 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001698 * This is called when the user is requesting an assist, to build a full
1699 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1700 * application. You can override this method to place into the bundle anything
1701 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001702 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001703 *
1704 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001705 * been registered with {@link Application#registerOnProvideAssistDataListener
1706 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001707 */
1708 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001709 }
1710
1711 /**
1712 * This is called when the user is requesting an assist, to provide references
1713 * to content related to the current activity. Before being called, the
1714 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
1715 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
1716 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
1717 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
1718 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
1719 *
1720 * <p>Custom implementation may adjust the content intent to better reflect the top-level
1721 * context of the activity, and fill in its ClipData with additional content of
1722 * interest that the user is currently viewing. For example, an image gallery application
1723 * that has launched in to an activity allowing the user to swipe through pictures should
1724 * modify the intent to reference the current image they are looking it; such an
1725 * application when showing a list of pictures should add a ClipData that has
1726 * references to all of the pictures currently visible on screen.</p>
1727 *
1728 * @param outContent The assist content to return.
1729 */
1730 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001731 }
1732
Clara Bayarri75e09792015-07-29 16:20:40 +01001733 @Override
1734 public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, Menu menu) {
1735 if (menu == null) {
1736 return;
1737 }
1738 KeyboardShortcutGroup group = null;
1739 int menuSize = menu.size();
1740 for (int i = 0; i < menuSize; ++i) {
1741 final MenuItem item = menu.getItem(i);
1742 final CharSequence title = item.getTitle();
1743 final char alphaShortcut = item.getAlphabeticShortcut();
1744 if (title != null && alphaShortcut != MIN_VALUE) {
1745 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00001746 final int resource = mApplication.getApplicationInfo().labelRes;
1747 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01001748 }
1749 group.addItem(new KeyboardShortcutInfo(
1750 title, alphaShortcut, KeyEvent.META_CTRL_ON));
1751 }
1752 }
1753 if (group != null) {
1754 data.add(group);
1755 }
1756 }
1757
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001758 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07001759 * Ask to have the current assistant shown to the user. This only works if the calling
1760 * activity is the current foreground activity. It is the same as calling
1761 * {@link android.service.voice.VoiceInteractionService#showSession
1762 * VoiceInteractionService.showSession} and requesting all of the possible context.
1763 * The receiver will always see
1764 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
1765 * @return Returns true if the assistant was successfully invoked, else false. For example
1766 * false will be returned if the caller is not the current top activity.
1767 */
1768 public boolean showAssist(Bundle args) {
1769 try {
1770 return ActivityManagerNative.getDefault().showAssistFromActivity(mToken, args);
1771 } catch (RemoteException e) {
1772 }
1773 return false;
1774 }
1775
1776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 * Called when you are no longer visible to the user. You will next
1778 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1779 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001780 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 * <p>Note that this method may never be called, in low memory situations
1782 * where the system does not have enough memory to keep your activity's
1783 * process running after its {@link #onPause} method is called.
RoboErik55011652014-07-09 15:05:53 -07001784 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 * <p><em>Derived classes must call through to the super class's
1786 * implementation of this method. If they do not, an exception will be
1787 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001788 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 * @see #onRestart
1790 * @see #onResume
1791 * @see #onSaveInstanceState
1792 * @see #onDestroy
1793 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001794 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001796 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001797 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001798 mActivityTransitionState.onStop();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001799 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001800 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 mCalled = true;
1802 }
1803
1804 /**
1805 * Perform any final cleanup before an activity is destroyed. This can
1806 * happen either because the activity is finishing (someone called
1807 * {@link #finish} on it, or because the system is temporarily destroying
1808 * this instance of the activity to save space. You can distinguish
1809 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001810 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 * <p><em>Note: do not count on this method being called as a place for
1812 * saving data! For example, if an activity is editing data in a content
1813 * provider, those edits should be committed in either {@link #onPause} or
1814 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1815 * free resources like threads that are associated with an activity, so
1816 * that a destroyed activity does not leave such things around while the
1817 * rest of its application is still running. There are situations where
1818 * the system will simply kill the activity's hosting process without
1819 * calling this method (or any others) in it, so it should not be used to
1820 * do things that are intended to remain around after the process goes
1821 * away.
RoboErik55011652014-07-09 15:05:53 -07001822 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 * <p><em>Derived classes must call through to the super class's
1824 * implementation of this method. If they do not, an exception will be
1825 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001826 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 * @see #onPause
1828 * @see #onStop
1829 * @see #finish
1830 * @see #isFinishing
1831 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001832 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001834 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 mCalled = true;
1836
1837 // dismiss any dialogs we are managing.
1838 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 final int numDialogs = mManagedDialogs.size();
1840 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001841 final ManagedDialog md = mManagedDialogs.valueAt(i);
1842 if (md.mDialog.isShowing()) {
1843 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
1845 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001846 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848
1849 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001850 synchronized (mManagedCursors) {
1851 int numCursors = mManagedCursors.size();
1852 for (int i = 0; i < numCursors; i++) {
1853 ManagedCursor c = mManagedCursors.get(i);
1854 if (c != null) {
1855 c.mCursor.close();
1856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001858 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 }
Amith Yamasani49860442010-03-17 20:54:10 -07001860
1861 // Close any open search dialog
1862 if (mSearchManager != null) {
1863 mSearchManager.stopSearch();
1864 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001865
Chris Banes21b25772016-01-04 20:41:59 +00001866 if (mActionBar != null) {
1867 mActionBar.onDestroy();
1868 }
1869
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001870 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 }
1872
1873 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001874 * Report to the system that your app is now fully drawn, purely for diagnostic
1875 * purposes (calling it does not impact the visible behavior of the activity).
1876 * This is only used to help instrument application launch times, so that the
1877 * app can report when it is fully in a usable state; without this, the only thing
1878 * the system itself can determine is the point at which the activity's window
1879 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001880 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001881 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001882 * entirely drawn your UI and populated with all of the significant data. You
1883 * can safely call this method any time after first launch as well, in which case
1884 * it will simply be ignored.
1885 */
1886 public void reportFullyDrawn() {
1887 if (mDoReportFullyDrawn) {
1888 mDoReportFullyDrawn = false;
1889 try {
1890 ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken);
1891 } catch (RemoteException e) {
1892 }
1893 }
1894 }
1895
1896 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08001897 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
1898 * visa-versa.
1899 * @see android.R.attr#resizeableActivity
1900 *
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001901 * @param inMultiWindow True if the activity is in multi-window mode.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001902 */
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001903 @CallSuper
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001904 public void onMultiWindowChanged(boolean inMultiWindow) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001905 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001906 "onMultiWindowChanged " + this + ": " + inMultiWindow);
Wale Ogunwale7c796812016-01-29 21:13:50 -08001907 mFragments.dispatchMultiWindowChanged(inMultiWindow);
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001908 if (mWindow != null) {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001909 mWindow.onMultiWindowChanged();
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001910 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001911 }
1912
1913 /**
1914 * Returns true if the activity is currently in multi-window mode.
1915 * @see android.R.attr#resizeableActivity
1916 *
1917 * @return True if the activity is in multi-window mode.
1918 */
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001919 public boolean inMultiWindow() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001920 try {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001921 return ActivityManagerNative.getDefault().inMultiWindow(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001922 } catch (RemoteException e) {
1923 }
1924 return false;
1925 }
1926
1927 /**
1928 * Called by the system when the activity changes to and from picture-in-picture mode.
1929 * @see android.R.attr#supportsPictureInPicture
1930 *
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001931 * @param inPictureInPicture True if the activity is in picture-in-picture mode.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001932 */
Wale Ogunwale7c796812016-01-29 21:13:50 -08001933 @CallSuper
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001934 public void onPictureInPictureChanged(boolean inPictureInPicture) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001935 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001936 "onPictureInPictureChanged " + this + ": " + inPictureInPicture);
Wale Ogunwale7c796812016-01-29 21:13:50 -08001937 mFragments.dispatchPictureInPictureChanged(inPictureInPicture);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001938 }
1939
1940 /**
1941 * Returns true if the activity is currently in picture-in-picture mode.
1942 * @see android.R.attr#supportsPictureInPicture
1943 *
1944 * @return True if the activity is in picture-in-picture mode.
1945 */
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001946 public boolean inPictureInPicture() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001947 try {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001948 return ActivityManagerNative.getDefault().inPictureInPicture(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001949 } catch (RemoteException e) {
1950 }
1951 return false;
1952 }
1953
1954 /**
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001955 * Puts the activity in picture-in-picture mode.
1956 * @see android.R.attr#supportsPictureInPicture
1957 */
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001958 public void enterPictureInPicture() {
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001959 try {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001960 ActivityManagerNative.getDefault().enterPictureInPicture(mToken);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001961 } catch (RemoteException e) {
1962 }
1963 }
1964
1965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 * Called by the system when the device configuration changes while your
1967 * activity is running. Note that this will <em>only</em> be called if
1968 * you have selected configurations you would like to handle with the
1969 * {@link android.R.attr#configChanges} attribute in your manifest. If
1970 * any configuration change occurs that is not selected to be reported
1971 * by that attribute, then instead of reporting it the system will stop
1972 * and restart the activity (to have it launched with the new
1973 * configuration).
RoboErik55011652014-07-09 15:05:53 -07001974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 * <p>At the time that this function has been called, your Resources
1976 * object will have been updated to return resource values matching the
1977 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07001978 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 * @param newConfig The new device configuration.
1980 */
1981 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001982 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01001984
Dianne Hackborn9d071802010-12-08 14:49:15 -08001985 mFragments.dispatchConfigurationChanged(newConfig);
1986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 if (mWindow != null) {
1988 // Pass the configuration changed event to the window
1989 mWindow.onConfigurationChanged(newConfig);
1990 }
Adam Powell45c0b192011-07-28 15:11:57 -07001991
1992 if (mActionBar != null) {
1993 // Do this last; the action bar will need to access
1994 // view changes from above.
1995 mActionBar.onConfigurationChanged(newConfig);
1996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 }
RoboErik55011652014-07-09 15:05:53 -07001998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 /**
2000 * If this activity is being destroyed because it can not handle a
2001 * configuration parameter being changed (and thus its
2002 * {@link #onConfigurationChanged(Configuration)} method is
2003 * <em>not</em> being called), then you can use this method to discover
2004 * the set of changes that have occurred while in the process of being
2005 * destroyed. Note that there is no guarantee that these will be
2006 * accurate (other changes could have happened at any time), so you should
2007 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07002008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 * @return Returns a bit field of the configuration parameters that are
2010 * changing, as defined by the {@link android.content.res.Configuration}
2011 * class.
2012 */
2013 public int getChangingConfigurations() {
2014 return mConfigChangeFlags;
2015 }
RoboErik55011652014-07-09 15:05:53 -07002016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 /**
2018 * Retrieve the non-configuration instance data that was previously
2019 * returned by {@link #onRetainNonConfigurationInstance()}. This will
2020 * be available from the initial {@link #onCreate} and
2021 * {@link #onStart} calls to the new instance, allowing you to extract
2022 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002023 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 * <p>Note that the data you retrieve here should <em>only</em> be used
2025 * as an optimization for handling configuration changes. You should always
2026 * be able to handle getting a null pointer back, and an activity must
2027 * still be able to restore itself to its previous state (through the
2028 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2029 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002030 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 * @return Returns the object previously returned by
2032 * {@link #onRetainNonConfigurationInstance()}.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002033 *
2034 * @deprecated Use the new {@link Fragment} API
2035 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
2036 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002038 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07002039 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002041 return mLastNonConfigurationInstances != null
2042 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 }
RoboErik55011652014-07-09 15:05:53 -07002044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 /**
2046 * Called by the system, as part of destroying an
2047 * activity due to a configuration change, when it is known that a new
2048 * instance will immediately be created for the new configuration. You
2049 * can return any object you like here, including the activity instance
2050 * itself, which can later be retrieved by calling
2051 * {@link #getLastNonConfigurationInstance()} in the new activity
2052 * instance.
RoboErik55011652014-07-09 15:05:53 -07002053 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002054 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2055 * or later, consider instead using a {@link Fragment} with
2056 * {@link Fragment#setRetainInstance(boolean)
2057 * Fragment.setRetainInstance(boolean}.</em>
2058 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 * <p>This function is called purely as an optimization, and you must
2060 * not rely on it being called. When it is called, a number of guarantees
2061 * will be made to help optimize configuration switching:
2062 * <ul>
2063 * <li> The function will be called between {@link #onStop} and
2064 * {@link #onDestroy}.
2065 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002066 * created after this one's {@link #onDestroy()} is called. In particular,
2067 * <em>no</em> messages will be dispatched during this time (when the returned
2068 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 * <li> The object you return here will <em>always</em> be available from
2070 * the {@link #getLastNonConfigurationInstance()} method of the following
2071 * activity instance as described there.
2072 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 * <p>These guarantees are designed so that an activity can use this API
2075 * to propagate extensive state from the old to new activity instance, from
2076 * loaded bitmaps, to network connections, to evenly actively running
2077 * threads. Note that you should <em>not</em> propagate any data that
2078 * may change based on the configuration, including any data loaded from
2079 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002080 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002081 * <p>The guarantee of no message handling during the switch to the next
2082 * activity simplifies use with active objects. For example if your retained
2083 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2084 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2085 * not be called from the call here until you execute the next instance's
2086 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2087 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2088 * running in a separate thread.)
2089 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 * @return Return any Object holding the desired state to propagate to the
2091 * next activity instance.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002092 *
2093 * @deprecated Use the new {@link Fragment} API
2094 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
2095 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 */
2097 public Object onRetainNonConfigurationInstance() {
2098 return null;
2099 }
RoboErik55011652014-07-09 15:05:53 -07002100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 /**
2102 * Retrieve the non-configuration instance data that was previously
2103 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2104 * be available from the initial {@link #onCreate} and
2105 * {@link #onStart} calls to the new instance, allowing you to extract
2106 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 * <p>Note that the data you retrieve here should <em>only</em> be used
2109 * as an optimization for handling configuration changes. You should always
2110 * be able to handle getting a null pointer back, and an activity must
2111 * still be able to restore itself to its previous state (through the
2112 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2113 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002114 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 * @return Returns the object previously returned by
2116 * {@link #onRetainNonConfigurationChildInstances()}
2117 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002118 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002119 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2120 return mLastNonConfigurationInstances != null
2121 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
RoboErik55011652014-07-09 15:05:53 -07002123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 /**
2125 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2126 * it should return either a mapping from child activity id strings to arbitrary objects,
2127 * or null. This method is intended to be used by Activity framework subclasses that control a
2128 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2129 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2130 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002131 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2133 return null;
2134 }
RoboErik55011652014-07-09 15:05:53 -07002135
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002136 NonConfigurationInstances retainNonConfigurationInstances() {
2137 Object activity = onRetainNonConfigurationInstance();
2138 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002139 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002140 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
2141 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002142 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002143 return null;
2144 }
RoboErik55011652014-07-09 15:05:53 -07002145
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002146 NonConfigurationInstances nci = new NonConfigurationInstances();
2147 nci.activity = activity;
2148 nci.children = children;
2149 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002150 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002151 if (mVoiceInteractor != null) {
2152 mVoiceInteractor.retainInstance();
2153 nci.voiceInteractor = mVoiceInteractor;
2154 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002155 return nci;
2156 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002159 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002161 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002163
2164 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002165 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002166 mCalled = true;
2167 mFragments.dispatchTrimMemory(level);
2168 }
2169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002171 * Return the FragmentManager for interacting with fragments associated
2172 * with this activity.
2173 */
2174 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002175 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002176 }
RoboErik55011652014-07-09 15:05:53 -07002177
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002178 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002179 * Called when a Fragment is being attached to this activity, immediately
2180 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2181 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
2182 */
2183 public void onAttachFragment(Fragment fragment) {
2184 }
RoboErik55011652014-07-09 15:05:53 -07002185
Dianne Hackbornc8017682010-07-06 13:34:38 -07002186 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 * Wrapper around
2188 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2189 * that gives the resulting {@link Cursor} to call
2190 * {@link #startManagingCursor} so that the activity will manage its
2191 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002192 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002193 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2194 * or later, consider instead using {@link LoaderManager} instead, available
2195 * via {@link #getLoaderManager()}.</em>
2196 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002197 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2198 * this method, because the activity will do that for you at the appropriate time. However, if
2199 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2200 * not</em> automatically close the cursor and, in that case, you must call
2201 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002202 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 * @param uri The URI of the content provider to query.
2204 * @param projection List of columns to return.
2205 * @param selection SQL WHERE clause.
2206 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002207 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002209 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2211 * @see #startManagingCursor
2212 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002213 *
2214 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 */
Jason parks6ed50de2010-08-25 10:18:50 -05002216 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002217 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2218 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2220 if (c != null) {
2221 startManagingCursor(c);
2222 }
2223 return c;
2224 }
2225
2226 /**
2227 * Wrapper around
2228 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2229 * that gives the resulting {@link Cursor} to call
2230 * {@link #startManagingCursor} so that the activity will manage its
2231 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002232 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002233 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2234 * or later, consider instead using {@link LoaderManager} instead, available
2235 * via {@link #getLoaderManager()}.</em>
2236 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002237 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2238 * this method, because the activity will do that for you at the appropriate time. However, if
2239 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2240 * not</em> automatically close the cursor and, in that case, you must call
2241 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002242 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 * @param uri The URI of the content provider to query.
2244 * @param projection List of columns to return.
2245 * @param selection SQL WHERE clause.
2246 * @param selectionArgs The arguments to selection, if any ?s are pesent
2247 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002248 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2252 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002253 *
2254 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 */
Jason parks6ed50de2010-08-25 10:18:50 -05002256 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002257 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2258 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2260 if (c != null) {
2261 startManagingCursor(c);
2262 }
2263 return c;
2264 }
2265
2266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 * This method allows the activity to take care of managing the given
2268 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2269 * That is, when the activity is stopped it will automatically call
2270 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2271 * it will call {@link Cursor#requery} for you. When the activity is
2272 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002273 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002274 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2275 * or later, consider instead using {@link LoaderManager} instead, available
2276 * via {@link #getLoaderManager()}.</em>
2277 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002278 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2279 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2280 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2281 * <em>will not</em> automatically close the cursor and, in that case, you must call
2282 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2287 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002288 *
2289 * @deprecated Use the new {@link android.content.CursorLoader} class with
2290 * {@link LoaderManager} instead; this is also
2291 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 */
Jason parks6ed50de2010-08-25 10:18:50 -05002293 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 public void startManagingCursor(Cursor c) {
2295 synchronized (mManagedCursors) {
2296 mManagedCursors.add(new ManagedCursor(c));
2297 }
2298 }
2299
2300 /**
2301 * Given a Cursor that was previously given to
2302 * {@link #startManagingCursor}, stop the activity's management of that
2303 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002304 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002305 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002306 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002307 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002308 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002310 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002312 *
2313 * @deprecated Use the new {@link android.content.CursorLoader} class with
2314 * {@link LoaderManager} instead; this is also
2315 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 */
Jason parks6ed50de2010-08-25 10:18:50 -05002317 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 public void stopManagingCursor(Cursor c) {
2319 synchronized (mManagedCursors) {
2320 final int N = mManagedCursors.size();
2321 for (int i=0; i<N; i++) {
2322 ManagedCursor mc = mManagedCursors.get(i);
2323 if (mc.mCursor == c) {
2324 mManagedCursors.remove(i);
2325 break;
2326 }
2327 }
2328 }
2329 }
2330
2331 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002332 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2333 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002334 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002336 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 }
2339
2340 /**
2341 * Finds a view that was identified by the id attribute from the XML that
2342 * was processed in {@link #onCreate}.
2343 *
2344 * @return The view if found or null otherwise.
2345 */
Scott Kennedyc0519552015-02-11 15:33:10 -08002346 @Nullable
Tor Norbye7b9c9122013-05-30 16:48:33 -07002347 public View findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 return getWindow().findViewById(id);
2349 }
RoboErik55011652014-07-09 15:05:53 -07002350
Adam Powell33b97432010-04-20 10:01:14 -07002351 /**
2352 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002353 *
Adam Powell33b97432010-04-20 10:01:14 -07002354 * @return The Activity's ActionBar, or null if it does not have one.
2355 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002356 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002357 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002358 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002359 return mActionBar;
2360 }
Adam Powelle43340c2014-03-17 19:10:43 -07002361
2362 /**
2363 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2364 * Activity window.
2365 *
2366 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2367 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2368 * a traditional window decor action bar. The toolbar's menu will be populated with the
2369 * Activity's options menu and the navigation button will be wired through the standard
2370 * {@link android.R.id#home home} menu select action.</p>
2371 *
2372 * <p>In order to use a Toolbar within the Activity's window content the application
2373 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2374 *
Chris Banesc7d6c322016-01-27 14:09:16 +00002375 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07002376 */
Adam Powell37780142014-06-01 13:31:00 -07002377 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00002378 final ActionBar ab = getActionBar();
2379 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002380 throw new IllegalStateException("This Activity already has an action bar supplied " +
2381 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2382 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2383 }
Chris Banes21b25772016-01-04 20:41:59 +00002384
2385 // If we reach here then we're setting a new action bar
2386 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01002387 mMenuInflater = null;
2388
Chris Banes21b25772016-01-04 20:41:59 +00002389 // If we have an action bar currently, destroy it
2390 if (ab != null) {
2391 ab.onDestroy();
2392 }
2393
Chris Banesc7d6c322016-01-27 14:09:16 +00002394 if (toolbar != null) {
2395 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2396 mActionBar = tbab;
2397 mWindow.setCallback(tbab.getWrappedWindowCallback());
2398 } else {
2399 mActionBar = null;
2400 // Re-set the original window callback since we may have already set a Toolbar wrapper
2401 mWindow.setCallback(this);
2402 }
2403
2404 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002405 }
RoboErik55011652014-07-09 15:05:53 -07002406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 /**
Adam Powell33b97432010-04-20 10:01:14 -07002408 * Creates a new ActionBar, locates the inflated ActionBarView,
2409 * initializes the ActionBar with the view, and sets mActionBar.
2410 */
Adam Powelle43340c2014-03-17 19:10:43 -07002411 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002412 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002413
2414 // Initializing the window decor can change window feature flags.
2415 // Make sure that we have the correct set before performing the test below.
2416 window.getDecorView();
2417
Adam Powell9b4c8042010-08-10 15:36:44 -07002418 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002419 return;
2420 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002421
Adam Powelle43340c2014-03-17 19:10:43 -07002422 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002423 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002424
2425 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2426 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002427 }
RoboErik55011652014-07-09 15:05:53 -07002428
Adam Powell33b97432010-04-20 10:01:14 -07002429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 * Set the activity content from a layout resource. The resource will be
2431 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08002432 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002434 *
Romain Guy482b34a62011-01-20 10:59:28 -08002435 * @see #setContentView(android.view.View)
2436 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002438 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002440 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 }
2442
2443 /**
2444 * Set the activity content to an explicit view. This view is placed
2445 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002446 * view hierarchy. When calling this method, the layout parameters of the
2447 * specified view are ignored. Both the width and the height of the view are
2448 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2449 * your own layout parameters, invoke
2450 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2451 * instead.
RoboErik55011652014-07-09 15:05:53 -07002452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08002454 *
2455 * @see #setContentView(int)
2456 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 */
2458 public void setContentView(View view) {
2459 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002460 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 }
2462
2463 /**
2464 * Set the activity content to an explicit view. This view is placed
2465 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002466 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002467 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 * @param view The desired content to display.
2469 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08002470 *
2471 * @see #setContentView(android.view.View)
2472 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 */
2474 public void setContentView(View view, ViewGroup.LayoutParams params) {
2475 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002476 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 }
2478
2479 /**
2480 * Add an additional content view to the activity. Added after any existing
2481 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002482 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 * @param view The desired content to display.
2484 * @param params Layout parameters for the view.
2485 */
2486 public void addContentView(View view, ViewGroup.LayoutParams params) {
2487 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002488 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 }
2490
2491 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002492 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2493 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2494 *
2495 * <p>This method will return non-null after content has been initialized (e.g. by using
2496 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2497 *
2498 * @return This window's content TransitionManager or null if none is set.
2499 */
2500 public TransitionManager getContentTransitionManager() {
2501 return getWindow().getTransitionManager();
2502 }
2503
2504 /**
2505 * Set the {@link TransitionManager} to use for default transitions in this window.
2506 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2507 *
2508 * @param tm The TransitionManager to use for scene changes.
2509 */
2510 public void setContentTransitionManager(TransitionManager tm) {
2511 getWindow().setTransitionManager(tm);
2512 }
2513
2514 /**
2515 * Retrieve the {@link Scene} representing this window's current content.
2516 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2517 *
2518 * <p>This method will return null if the current content is not represented by a Scene.</p>
2519 *
2520 * @return Current Scene being shown or null
2521 */
2522 public Scene getContentScene() {
2523 return getWindow().getContentScene();
2524 }
2525
2526 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002527 * Sets whether this activity is finished when touched outside its window's
2528 * bounds.
2529 */
2530 public void setFinishOnTouchOutside(boolean finish) {
2531 mWindow.setCloseOnTouchOutside(finish);
2532 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002533
2534 /** @hide */
2535 @IntDef({
2536 DEFAULT_KEYS_DISABLE,
2537 DEFAULT_KEYS_DIALER,
2538 DEFAULT_KEYS_SHORTCUT,
2539 DEFAULT_KEYS_SEARCH_LOCAL,
2540 DEFAULT_KEYS_SEARCH_GLOBAL})
2541 @Retention(RetentionPolicy.SOURCE)
2542 @interface DefaultKeyMode {}
2543
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002544 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2546 * keys.
RoboErik55011652014-07-09 15:05:53 -07002547 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 * @see #setDefaultKeyMode
2549 */
2550 static public final int DEFAULT_KEYS_DISABLE = 0;
2551 /**
2552 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2553 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 * @see #setDefaultKeyMode
2556 */
2557 static public final int DEFAULT_KEYS_DIALER = 1;
2558 /**
2559 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2560 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002561 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002563 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 * @see #setDefaultKeyMode
2565 */
2566 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2567 /**
2568 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2569 * will start an application-defined search. (If the application or activity does not
2570 * actually define a search, the the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002571 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002573 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 * @see #setDefaultKeyMode
2575 */
2576 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2577
2578 /**
2579 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2580 * will start a global search (typically web search, but some platforms may define alternate
2581 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002582 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002584 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 * @see #setDefaultKeyMode
2586 */
2587 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2588
2589 /**
2590 * Select the default key handling for this activity. This controls what
2591 * will happen to key events that are not otherwise handled. The default
2592 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2593 * floor. Other modes allow you to launch the dialer
2594 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2595 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002596 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002598 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 * <p>Note that the mode selected here does not impact the default
2600 * handling of system keys, such as the "back" and "menu" keys, and your
2601 * activity and its views always get a first chance to receive and handle
2602 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002603 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002605 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 * @see #DEFAULT_KEYS_DISABLE
2607 * @see #DEFAULT_KEYS_DIALER
2608 * @see #DEFAULT_KEYS_SHORTCUT
2609 * @see #DEFAULT_KEYS_SEARCH_LOCAL
2610 * @see #DEFAULT_KEYS_SEARCH_GLOBAL
2611 * @see #onKeyDown
2612 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002613 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 // Some modes use a SpannableStringBuilder to track & dispatch input events
2617 // This list must remain in sync with the switch in onKeyDown()
2618 switch (mode) {
2619 case DEFAULT_KEYS_DISABLE:
2620 case DEFAULT_KEYS_SHORTCUT:
2621 mDefaultKeySsb = null; // not used in these modes
2622 break;
2623 case DEFAULT_KEYS_DIALER:
2624 case DEFAULT_KEYS_SEARCH_LOCAL:
2625 case DEFAULT_KEYS_SEARCH_GLOBAL:
2626 mDefaultKeySsb = new SpannableStringBuilder();
2627 Selection.setSelection(mDefaultKeySsb,0);
2628 break;
2629 default:
2630 throw new IllegalArgumentException();
2631 }
2632 }
2633
2634 /**
2635 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002636 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 * is inside a TextView will not trigger the event (unless it is a navigation
2638 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002639 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 * <p>If the focused view didn't want this event, this method is called.
2641 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002642 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
2643 * by calling {@link #onBackPressed()}, though the behavior varies based
2644 * on the application compatibility mode: for
2645 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
2646 * it will set up the dispatch to call {@link #onKeyUp} where the action
2647 * will be performed; for earlier applications, it will perform the
2648 * action immediately in on-down, as those versions of the platform
2649 * behaved.
RoboErik55011652014-07-09 15:05:53 -07002650 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002651 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002652 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07002653 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002655 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 * this event and it should continue to be propagated.
2657 * @see #onKeyUp
2658 * @see android.view.KeyEvent
2659 */
2660 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002661 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002662 if (getApplicationInfo().targetSdkVersion
2663 >= Build.VERSION_CODES.ECLAIR) {
2664 event.startTracking();
2665 } else {
2666 onBackPressed();
2667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 return true;
2669 }
RoboErik55011652014-07-09 15:05:53 -07002670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
2672 return false;
2673 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08002674 Window w = getWindow();
2675 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
2676 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
2677 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002678 return true;
2679 }
2680 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 } else {
2682 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
2683 boolean clearSpannable = false;
2684 boolean handled;
2685 if ((event.getRepeatCount() != 0) || event.isSystem()) {
2686 clearSpannable = true;
2687 handled = false;
2688 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002689 handled = TextKeyListener.getInstance().onKeyDown(
2690 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 if (handled && mDefaultKeySsb.length() > 0) {
2692 // something useable has been typed - dispatch it now.
2693
2694 final String str = mDefaultKeySsb.toString();
2695 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07002696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 switch (mDefaultKeyMode) {
2698 case DEFAULT_KEYS_DIALER:
2699 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
2700 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07002701 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 break;
2703 case DEFAULT_KEYS_SEARCH_LOCAL:
2704 startSearch(str, false, null, false);
2705 break;
2706 case DEFAULT_KEYS_SEARCH_GLOBAL:
2707 startSearch(str, false, null, true);
2708 break;
2709 }
2710 }
2711 }
2712 if (clearSpannable) {
2713 mDefaultKeySsb.clear();
2714 mDefaultKeySsb.clearSpans();
2715 Selection.setSelection(mDefaultKeySsb,0);
2716 }
2717 return handled;
2718 }
2719 }
2720
2721 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002722 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
2723 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
2724 * the event).
2725 */
2726 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2727 return false;
2728 }
2729
2730 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002732 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 * is inside a TextView will not trigger the event (unless it is a navigation
2734 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002735 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002736 * <p>The default implementation handles KEYCODE_BACK to stop the activity
2737 * and go back.
RoboErik55011652014-07-09 15:05:53 -07002738 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002740 * further, or <code>false</code> to indicate that you have not handled
2741 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 * @see #onKeyDown
2743 * @see KeyEvent
2744 */
2745 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002746 if (getApplicationInfo().targetSdkVersion
2747 >= Build.VERSION_CODES.ECLAIR) {
2748 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
2749 && !event.isCanceled()) {
2750 onBackPressed();
2751 return true;
2752 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 return false;
2755 }
2756
2757 /**
2758 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
2759 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
2760 * the event).
2761 */
2762 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2763 return false;
2764 }
RoboErik55011652014-07-09 15:05:53 -07002765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002767 * Called when the activity has detected the user's press of the back
2768 * key. The default implementation simply finishes the current activity,
2769 * but you can override this to do whatever you want.
2770 */
2771 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07002772 if (mActionBar != null && mActionBar.collapseActionView()) {
2773 return;
2774 }
2775
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002776 if (!mFragments.getFragmentManager().popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07002777 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07002778 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002779 }
Jeff Brown64da12a2011-01-04 19:57:47 -08002780
2781 /**
2782 * Called when a key shortcut event is not handled by any of the views in the Activity.
2783 * Override this method to implement global key shortcuts for the Activity.
2784 * Key shortcuts can also be implemented by setting the
2785 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
2786 *
2787 * @param keyCode The value in event.getKeyCode().
2788 * @param event Description of the key event.
2789 * @return True if the key shortcut was handled.
2790 */
2791 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07002792 // Let the Action Bar have a chance at handling the shortcut.
2793 ActionBar actionBar = getActionBar();
2794 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08002795 }
2796
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002797 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 * Called when a touch screen event was not handled by any of the views
2799 * under it. This is most useful to process touch events that happen
2800 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07002801 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07002803 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 * @return Return true if you have consumed the event, false if you haven't.
2805 * The default implementation always returns false.
2806 */
2807 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002808 if (mWindow.shouldCloseOnTouch(this, event)) {
2809 finish();
2810 return true;
2811 }
RoboErik55011652014-07-09 15:05:53 -07002812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 return false;
2814 }
RoboErik55011652014-07-09 15:05:53 -07002815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 /**
2817 * Called when the trackball was moved and not handled by any of the
2818 * views inside of the activity. So, for example, if the trackball moves
2819 * while focus is on a button, you will receive a call here because
2820 * buttons do not normally do anything with trackball events. The call
2821 * here happens <em>before</em> trackball movements are converted to
2822 * DPAD key events, which then get sent back to the view hierarchy, and
2823 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07002824 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07002826 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 * @return Return true if you have consumed the event, false if you haven't.
2828 * The default implementation always returns false.
2829 */
2830 public boolean onTrackballEvent(MotionEvent event) {
2831 return false;
2832 }
Jeff Browncb1404e2011-01-15 18:14:15 -08002833
2834 /**
2835 * Called when a generic motion event was not handled by any of the
2836 * views inside of the activity.
2837 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08002838 * Generic motion events describe joystick movements, mouse hovers, track pad
2839 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08002840 * {@link MotionEvent#getSource() source} of the motion event specifies
2841 * the class of input that was received. Implementations of this method
2842 * must examine the bits in the source before processing the event.
2843 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08002844 * </p><p>
2845 * Generic motion events with source class
2846 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
2847 * are delivered to the view under the pointer. All other generic motion events are
2848 * delivered to the focused view.
2849 * </p><p>
2850 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
2851 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08002852 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08002853 *
2854 * @param event The generic motion event being processed.
2855 *
2856 * @return Return true if you have consumed the event, false if you haven't.
2857 * The default implementation always returns false.
2858 */
2859 public boolean onGenericMotionEvent(MotionEvent event) {
2860 return false;
2861 }
2862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 /**
2864 * Called whenever a key, touch, or trackball event is dispatched to the
2865 * activity. Implement this method if you wish to know that the user has
2866 * interacted with the device in some way while your activity is running.
2867 * This callback and {@link #onUserLeaveHint} are intended to help
2868 * activities manage status bar notifications intelligently; specifically,
2869 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07002870 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
2872 * be accompanied by calls to {@link #onUserInteraction}. This
2873 * ensures that your activity will be told of relevant user activity such
2874 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07002875 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 * <p>Note that this callback will be invoked for the touch down action
2877 * that begins a touch gesture, but may not be invoked for the touch-moved
2878 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07002879 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 * @see #onUserLeaveHint()
2881 */
2882 public void onUserInteraction() {
2883 }
RoboErik55011652014-07-09 15:05:53 -07002884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
2886 // Update window manager if: we have a view, that view is
2887 // attached to its parent (which will be a RootView), and
2888 // this activity is not embedded.
2889 if (mParent == null) {
2890 View decor = mDecor;
2891 if (decor != null && decor.getParent() != null) {
2892 getWindowManager().updateViewLayout(decor, params);
2893 }
2894 }
2895 }
2896
2897 public void onContentChanged() {
2898 }
2899
2900 /**
2901 * Called when the current {@link Window} of the activity gains or loses
2902 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002903 * to the user. The default implementation clears the key tracking
2904 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07002905 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002906 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 * is managed independently of activity lifecycles. As such, while focus
2908 * changes will generally have some relation to lifecycle changes (an
2909 * activity that is stopped will not generally get window focus), you
2910 * should not rely on any particular order between the callbacks here and
2911 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07002912 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 * <p>As a general rule, however, a resumed activity will have window
2914 * focus... unless it has displayed other dialogs or popups that take
2915 * input focus, in which case the activity itself will not have focus
2916 * when the other windows have it. Likewise, the system may display
2917 * system-level windows (such as the status bar notification panel or
2918 * a system alert) which will temporarily take window input focus without
2919 * pausing the foreground activity.
2920 *
2921 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07002922 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 * @see #hasWindowFocus()
2924 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002925 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 */
2927 public void onWindowFocusChanged(boolean hasFocus) {
2928 }
RoboErik55011652014-07-09 15:05:53 -07002929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002931 * Called when the main window associated with the activity has been
2932 * attached to the window manager.
2933 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
2934 * for more information.
2935 * @see View#onAttachedToWindow
2936 */
2937 public void onAttachedToWindow() {
2938 }
RoboErik55011652014-07-09 15:05:53 -07002939
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002940 /**
2941 * Called when the main window associated with the activity has been
2942 * detached from the window manager.
2943 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
2944 * for more information.
2945 * @see View#onDetachedFromWindow
2946 */
2947 public void onDetachedFromWindow() {
2948 }
RoboErik55011652014-07-09 15:05:53 -07002949
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002950 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 * Returns true if this activity's <em>main</em> window currently has window focus.
2952 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07002953 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07002955 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
2957 */
2958 public boolean hasWindowFocus() {
2959 Window w = getWindow();
2960 if (w != null) {
2961 View d = w.getDecorView();
2962 if (d != null) {
2963 return d.hasWindowFocus();
2964 }
2965 }
2966 return false;
2967 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002968
2969 /**
2970 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07002971 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002972 */
Adam Powell117b6952014-05-05 18:14:56 -07002973 @Override
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002974 public void onWindowDismissed(boolean finishTask) {
2975 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002976 }
RoboErik55011652014-07-09 15:05:53 -07002977
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002978
Skuhnece2faa52015-08-11 10:36:38 -07002979 /**
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08002980 * Moves the activity from
2981 * {@link android.app.ActivityManager.StackId#FREEFORM_WORKSPACE_STACK_ID} to
2982 * {@link android.app.ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} stack.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002983 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002984 * @hide
2985 */
2986 @Override
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08002987 public void exitFreeformMode() throws RemoteException {
2988 ActivityManagerNative.getDefault().exitFreeformMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002989 }
2990
2991 /** Returns the current stack Id for the window.
2992 * @hide
2993 */
2994 @Override
2995 public int getWindowStackId() throws RemoteException {
2996 return ActivityManagerNative.getDefault().getActivityStackId(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 }
2998
2999 /**
RoboErik55011652014-07-09 15:05:53 -07003000 * Called to process key events. You can override this to intercept all
3001 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07003005 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 * @return boolean Return true if this event was consumed.
3007 */
3008 public boolean dispatchKeyEvent(KeyEvent event) {
3009 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07003010
3011 // Let action bars open menus in response to the menu key prioritized over
3012 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08003013 final int keyCode = event.getKeyCode();
3014 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07003015 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
3016 return true;
George Mountcbe28352016-01-06 16:24:26 -08003017 } else if (event.isCtrlPressed() &&
3018 event.getUnicodeChar(event.getMetaState() & ~KeyEvent.META_CTRL_MASK) == '<') {
3019 // Capture the Control-< and send focus to the ActionBar
George Mount5beb26172015-12-15 13:36:01 -08003020 final int action = event.getAction();
3021 if (action == KeyEvent.ACTION_DOWN) {
George Mountcbe28352016-01-06 16:24:26 -08003022 final ActionBar actionBar = getActionBar();
3023 if (actionBar != null && actionBar.isShowing() && actionBar.requestFocus()) {
3024 mEatKeyUpEvent = true;
3025 return true;
George Mount5beb26172015-12-15 13:36:01 -08003026 }
3027 } else if (action == KeyEvent.ACTION_UP && mEatKeyUpEvent) {
3028 mEatKeyUpEvent = false;
3029 return true;
3030 }
Adam Powell07a74542014-05-30 15:52:44 -07003031 }
3032
Dianne Hackborn8d374262009-09-14 21:21:52 -07003033 Window win = getWindow();
3034 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 return true;
3036 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07003037 View decor = mDecor;
3038 if (decor == null) decor = win.getDecorView();
3039 return event.dispatch(this, decor != null
3040 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 }
3042
3043 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08003044 * Called to process a key shortcut event.
3045 * You can override this to intercept all key shortcut events before they are
3046 * dispatched to the window. Be sure to call this implementation for key shortcut
3047 * events that should be handled normally.
3048 *
3049 * @param event The key shortcut event.
3050 * @return True if this event was consumed.
3051 */
3052 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3053 onUserInteraction();
3054 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3055 return true;
3056 }
3057 return onKeyShortcut(event.getKeyCode(), event);
3058 }
3059
3060 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 * Called to process touch screen events. You can override this to
3062 * intercept all touch screen events before they are dispatched to the
3063 * window. Be sure to call this implementation for touch screen events
3064 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003065 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003067 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 * @return boolean Return true if this event was consumed.
3069 */
3070 public boolean dispatchTouchEvent(MotionEvent ev) {
3071 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3072 onUserInteraction();
3073 }
3074 if (getWindow().superDispatchTouchEvent(ev)) {
3075 return true;
3076 }
3077 return onTouchEvent(ev);
3078 }
RoboErik55011652014-07-09 15:05:53 -07003079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 /**
3081 * Called to process trackball events. You can override this to
3082 * intercept all trackball events before they are dispatched to the
3083 * window. Be sure to call this implementation for trackball events
3084 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003085 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003087 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 * @return boolean Return true if this event was consumed.
3089 */
3090 public boolean dispatchTrackballEvent(MotionEvent ev) {
3091 onUserInteraction();
3092 if (getWindow().superDispatchTrackballEvent(ev)) {
3093 return true;
3094 }
3095 return onTrackballEvent(ev);
3096 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003097
Jeff Browncb1404e2011-01-15 18:14:15 -08003098 /**
3099 * Called to process generic motion events. You can override this to
3100 * intercept all generic motion events before they are dispatched to the
3101 * window. Be sure to call this implementation for generic motion events
3102 * that should be handled normally.
3103 *
3104 * @param ev The generic motion event.
3105 *
3106 * @return boolean Return true if this event was consumed.
3107 */
3108 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3109 onUserInteraction();
3110 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3111 return true;
3112 }
3113 return onGenericMotionEvent(ev);
3114 }
3115
svetoslavganov75986cf2009-05-14 22:28:01 -07003116 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3117 event.setClassName(getClass().getName());
3118 event.setPackageName(getPackageName());
3119
3120 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003121 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3122 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003123 event.setFullScreen(isFullScreen);
3124
3125 CharSequence title = getTitle();
3126 if (!TextUtils.isEmpty(title)) {
3127 event.getText().add(title);
3128 }
3129
3130 return true;
3131 }
3132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 /**
3134 * Default implementation of
3135 * {@link android.view.Window.Callback#onCreatePanelView}
3136 * for activities. This
3137 * simply returns null so that all panel sub-windows will have the default
3138 * menu behavior.
3139 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003140 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 public View onCreatePanelView(int featureId) {
3142 return null;
3143 }
3144
3145 /**
3146 * Default implementation of
3147 * {@link android.view.Window.Callback#onCreatePanelMenu}
3148 * for activities. This calls through to the new
3149 * {@link #onCreateOptionsMenu} method for the
3150 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3151 * so that subclasses of Activity don't need to deal with feature codes.
3152 */
3153 public boolean onCreatePanelMenu(int featureId, Menu menu) {
3154 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003155 boolean show = onCreateOptionsMenu(menu);
3156 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3157 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 }
3159 return false;
3160 }
3161
3162 /**
3163 * Default implementation of
3164 * {@link android.view.Window.Callback#onPreparePanel}
3165 * for activities. This
3166 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3167 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3168 * panel, so that subclasses of
3169 * Activity don't need to deal with feature codes.
3170 */
3171 public boolean onPreparePanel(int featureId, View view, Menu menu) {
3172 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
3173 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003174 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003175 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 }
3177 return true;
3178 }
3179
3180 /**
3181 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003182 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 * @return The default implementation returns true.
3184 */
3185 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003186 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003187 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003188 if (mActionBar != null) {
3189 mActionBar.dispatchMenuVisibilityChanged(true);
3190 } else {
3191 Log.e(TAG, "Tried to open action bar menu with no action bar");
3192 }
Adam Powell8515ee82010-11-30 14:09:55 -08003193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 return true;
3195 }
3196
3197 /**
3198 * Default implementation of
3199 * {@link android.view.Window.Callback#onMenuItemSelected}
3200 * for activities. This calls through to the new
3201 * {@link #onOptionsItemSelected} method for the
3202 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3203 * panel, so that subclasses of
3204 * Activity don't need to deal with feature codes.
3205 */
3206 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09003207 CharSequence titleCondensed = item.getTitleCondensed();
3208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 switch (featureId) {
3210 case Window.FEATURE_OPTIONS_PANEL:
3211 // Put event logging here so it gets called even if subclass
3212 // doesn't call through to superclass's implmeentation of each
3213 // of these methods below
zobject686a8052012-12-14 21:11:08 +09003214 if(titleCondensed != null) {
3215 EventLog.writeEvent(50000, 0, titleCondensed.toString());
3216 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003217 if (onOptionsItemSelected(item)) {
3218 return true;
3219 }
Adam Powell04d58112012-04-09 10:22:12 -07003220 if (mFragments.dispatchOptionsItemSelected(item)) {
3221 return true;
3222 }
3223 if (item.getItemId() == android.R.id.home && mActionBar != null &&
3224 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
3225 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07003226 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07003227 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07003228 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07003229 }
Adam Powell04d58112012-04-09 10:22:12 -07003230 }
3231 return false;
RoboErik55011652014-07-09 15:05:53 -07003232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09003234 if(titleCondensed != null) {
3235 EventLog.writeEvent(50000, 1, titleCondensed.toString());
3236 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07003237 if (onContextItemSelected(item)) {
3238 return true;
3239 }
3240 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08003241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 default:
3243 return false;
3244 }
3245 }
RoboErik55011652014-07-09 15:05:53 -07003246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 /**
3248 * Default implementation of
3249 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
3250 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
3251 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3252 * so that subclasses of Activity don't need to deal with feature codes.
3253 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
3254 * {@link #onContextMenuClosed(Menu)} will be called.
3255 */
3256 public void onPanelClosed(int featureId, Menu menu) {
3257 switch (featureId) {
3258 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003259 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 onOptionsMenuClosed(menu);
3261 break;
RoboErik55011652014-07-09 15:05:53 -07003262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 case Window.FEATURE_CONTEXT_MENU:
3264 onContextMenuClosed(menu);
3265 break;
Adam Powell8515ee82010-11-30 14:09:55 -08003266
3267 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07003268 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08003269 mActionBar.dispatchMenuVisibilityChanged(false);
3270 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 }
3272 }
3273
3274 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003275 * Declare that the options menu has changed, so should be recreated.
3276 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
3277 * time it needs to be displayed.
3278 */
3279 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003280 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3281 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003282 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
3283 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003284 }
RoboErik55011652014-07-09 15:05:53 -07003285
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 * Initialize the contents of the Activity's standard options menu. You
3288 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07003289 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 * <p>This is only called once, the first time the options menu is
3291 * displayed. To update the menu every time it is displayed, see
3292 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07003293 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07003295 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
3296 * they will be correctly ordered with application-defined menu items.
3297 * Deriving classes should always call through to the base implementation.
3298 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 * <p>You can safely hold on to <var>menu</var> (and any items created
3300 * from it), making modifications to it as desired, until the next
3301 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07003302 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 * <p>When you add items to the menu, you can implement the Activity's
3304 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07003305 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07003307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 * @return You must return true for the menu to be displayed;
3309 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003310 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 * @see #onPrepareOptionsMenu
3312 * @see #onOptionsItemSelected
3313 */
3314 public boolean onCreateOptionsMenu(Menu menu) {
3315 if (mParent != null) {
3316 return mParent.onCreateOptionsMenu(menu);
3317 }
3318 return true;
3319 }
3320
3321 /**
3322 * Prepare the Screen's standard options menu to be displayed. This is
3323 * called right before the menu is shown, every time it is shown. You can
3324 * use this method to efficiently enable/disable items or otherwise
3325 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07003326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 * <p>The default implementation updates the system menu items based on the
3328 * activity's state. Deriving classes should always call through to the
3329 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07003330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 * @param menu The options menu as last shown or first initialized by
3332 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07003333 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 * @return You must return true for the menu to be displayed;
3335 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003336 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 * @see #onCreateOptionsMenu
3338 */
3339 public boolean onPrepareOptionsMenu(Menu menu) {
3340 if (mParent != null) {
3341 return mParent.onPrepareOptionsMenu(menu);
3342 }
3343 return true;
3344 }
3345
3346 /**
3347 * This hook is called whenever an item in your options menu is selected.
3348 * The default implementation simply returns false to have the normal
3349 * processing happen (calling the item's Runnable or sending a message to
3350 * its Handler as appropriate). You can use this method for any items
3351 * for which you would like to do processing without those other
3352 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003353 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003355 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003356 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003358 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 * @return boolean Return false to allow normal menu processing to
3360 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003361 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 * @see #onCreateOptionsMenu
3363 */
3364 public boolean onOptionsItemSelected(MenuItem item) {
3365 if (mParent != null) {
3366 return mParent.onOptionsItemSelected(item);
3367 }
3368 return false;
3369 }
3370
3371 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003372 * This method is called whenever the user chooses to navigate Up within your application's
3373 * activity hierarchy from the action bar.
3374 *
3375 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3376 * was specified in the manifest for this activity or an activity-alias to it,
3377 * default Up navigation will be handled automatically. If any activity
3378 * along the parent chain requires extra Intent arguments, the Activity subclass
3379 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3380 * to supply those arguments.</p>
3381 *
3382 * <p>See <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
3383 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3384 * from the design guide for more information about navigating within your app.</p>
3385 *
3386 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3387 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3388 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3389 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3390 *
3391 * @return true if Up navigation completed successfully and this Activity was finished,
3392 * false otherwise.
3393 */
3394 public boolean onNavigateUp() {
3395 // Automatically handle hierarchical Up navigation if the proper
3396 // metadata is available.
3397 Intent upIntent = getParentActivityIntent();
3398 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003399 if (mActivityInfo.taskAffinity == null) {
3400 // Activities with a null affinity are special; they really shouldn't
3401 // specify a parent activity intent in the first place. Just finish
3402 // the current activity and call it a day.
3403 finish();
3404 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003405 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003406 onCreateNavigateUpTaskStack(b);
3407 onPrepareNavigateUpTaskStack(b);
3408 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003409
3410 // We can't finishAffinity if we have a result.
3411 // Fall back and simply finish the current activity instead.
3412 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3413 // Tell the developer what's going on to avoid hair-pulling.
3414 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3415 finish();
3416 } else {
3417 finishAffinity();
3418 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003419 } else {
3420 navigateUpTo(upIntent);
3421 }
3422 return true;
3423 }
3424 return false;
3425 }
3426
3427 /**
3428 * This is called when a child activity of this one attempts to navigate up.
3429 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3430 *
3431 * @param child The activity making the call.
3432 */
3433 public boolean onNavigateUpFromChild(Activity child) {
3434 return onNavigateUp();
3435 }
3436
3437 /**
3438 * Define the synthetic task stack that will be generated during Up navigation from
3439 * a different task.
3440 *
3441 * <p>The default implementation of this method adds the parent chain of this activity
3442 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3443 * may choose to override this method to construct the desired task stack in a different
3444 * way.</p>
3445 *
Adam Powellf0195952012-05-02 21:38:54 -07003446 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3447 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3448 * returned by {@link #getParentActivityIntent()}.</p>
3449 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003450 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3451 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3452 *
3453 * @param builder An empty TaskStackBuilder - the application should add intents representing
3454 * the desired task stack
3455 */
3456 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3457 builder.addParentStack(this);
3458 }
3459
3460 /**
3461 * Prepare the synthetic task stack that will be generated during Up navigation
3462 * from a different task.
3463 *
3464 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3465 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3466 * If any extra data should be added to these intents before launching the new task,
3467 * the application should override this method and add that data here.</p>
3468 *
3469 * @param builder A TaskStackBuilder that has been populated with Intents by
3470 * onCreateNavigateUpTaskStack.
3471 */
3472 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3473 }
3474
3475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 * This hook is called whenever the options menu is being closed (either by the user canceling
3477 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003478 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 * @param menu The options menu as last shown or first initialized by
3480 * onCreateOptionsMenu().
3481 */
3482 public void onOptionsMenuClosed(Menu menu) {
3483 if (mParent != null) {
3484 mParent.onOptionsMenuClosed(menu);
3485 }
3486 }
RoboErik55011652014-07-09 15:05:53 -07003487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 /**
3489 * Programmatically opens the options menu. If the options menu is already
3490 * open, this method does nothing.
3491 */
3492 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003493 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3494 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003495 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 }
RoboErik55011652014-07-09 15:05:53 -07003498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 /**
3500 * Progammatically closes the options menu. If the options menu is already
3501 * closed, this method does nothing.
3502 */
3503 public void closeOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003504 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
3505 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 }
3508
3509 /**
3510 * Called when a context menu for the {@code view} is about to be shown.
3511 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3512 * time the context menu is about to be shown and should be populated for
3513 * the view (or item inside the view for {@link AdapterView} subclasses,
3514 * this can be found in the {@code menuInfo})).
3515 * <p>
3516 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3517 * item has been selected.
3518 * <p>
3519 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003520 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 */
3522 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3523 }
3524
3525 /**
3526 * Registers a context menu to be shown for the given view (multiple views
3527 * can show the context menu). This method will set the
3528 * {@link OnCreateContextMenuListener} on the view to this activity, so
3529 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3530 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003531 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 * @see #unregisterForContextMenu(View)
3533 * @param view The view that should show a context menu.
3534 */
3535 public void registerForContextMenu(View view) {
3536 view.setOnCreateContextMenuListener(this);
3537 }
RoboErik55011652014-07-09 15:05:53 -07003538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 /**
3540 * Prevents a context menu to be shown for the given view. This method will remove the
3541 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003542 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 * @see #registerForContextMenu(View)
3544 * @param view The view that should stop showing a context menu.
3545 */
3546 public void unregisterForContextMenu(View view) {
3547 view.setOnCreateContextMenuListener(null);
3548 }
RoboErik55011652014-07-09 15:05:53 -07003549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 /**
3551 * Programmatically opens the context menu for a particular {@code view}.
3552 * The {@code view} should have been added via
3553 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 * @param view The view to show the context menu for.
3556 */
3557 public void openContextMenu(View view) {
3558 view.showContextMenu();
3559 }
RoboErik55011652014-07-09 15:05:53 -07003560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 /**
3562 * Programmatically closes the most recently opened context menu, if showing.
3563 */
3564 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08003565 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
3566 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 }
RoboErik55011652014-07-09 15:05:53 -07003569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 /**
3571 * This hook is called whenever an item in a context menu is selected. The
3572 * default implementation simply returns false to have the normal processing
3573 * happen (calling the item's Runnable or sending a message to its Handler
3574 * as appropriate). You can use this method for any items for which you
3575 * would like to do processing without those other facilities.
3576 * <p>
3577 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3578 * View that added this menu item.
3579 * <p>
3580 * Derived classes should call through to the base class for it to perform
3581 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003582 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 * @param item The context menu item that was selected.
3584 * @return boolean Return false to allow normal context menu processing to
3585 * proceed, true to consume it here.
3586 */
3587 public boolean onContextItemSelected(MenuItem item) {
3588 if (mParent != null) {
3589 return mParent.onContextItemSelected(item);
3590 }
3591 return false;
3592 }
3593
3594 /**
3595 * This hook is called whenever the context menu is being closed (either by
3596 * the user canceling the menu with the back/menu button, or when an item is
3597 * selected).
RoboErik55011652014-07-09 15:05:53 -07003598 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 * @param menu The context menu that is being closed.
3600 */
3601 public void onContextMenuClosed(Menu menu) {
3602 if (mParent != null) {
3603 mParent.onContextMenuClosed(menu);
3604 }
3605 }
3606
3607 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003608 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003610 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 protected Dialog onCreateDialog(int id) {
3612 return null;
3613 }
3614
3615 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003616 * Callback for creating dialogs that are managed (saved and restored) for you
3617 * by the activity. The default implementation calls through to
3618 * {@link #onCreateDialog(int)} for compatibility.
3619 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003620 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3621 * or later, consider instead using a {@link DialogFragment} instead.</em>
3622 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003623 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3624 * this method the first time, and hang onto it thereafter. Any dialog
3625 * that is created by this method will automatically be saved and restored
3626 * for you, including whether it is showing.
3627 *
3628 * <p>If you would like the activity to manage saving and restoring dialogs
3629 * for you, you should override this method and handle any ids that are
3630 * passed to {@link #showDialog}.
3631 *
3632 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3633 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3634 *
3635 * @param id The id of the dialog.
3636 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3637 * @return The dialog. If you return null, the dialog will not be created.
3638 *
3639 * @see #onPrepareDialog(int, Dialog, Bundle)
3640 * @see #showDialog(int, Bundle)
3641 * @see #dismissDialog(int)
3642 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003643 *
3644 * @deprecated Use the new {@link DialogFragment} class with
3645 * {@link FragmentManager} instead; this is also
3646 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003647 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003648 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003649 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003650 protected Dialog onCreateDialog(int id, Bundle args) {
3651 return onCreateDialog(id);
3652 }
3653
3654 /**
3655 * @deprecated Old no-arguments version of
3656 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
3657 */
3658 @Deprecated
3659 protected void onPrepareDialog(int id, Dialog dialog) {
3660 dialog.setOwnerActivity(this);
3661 }
3662
3663 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003665 * shown. The default implementation calls through to
3666 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07003667 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 * <p>
3669 * Override this if you need to update a managed dialog based on the state
3670 * of the application each time it is shown. For example, a time picker
3671 * dialog might want to be updated with the current time. You should call
3672 * through to the superclass's implementation. The default implementation
3673 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07003674 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 * @param id The id of the managed dialog.
3676 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003677 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3678 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 * @see #showDialog(int)
3680 * @see #dismissDialog(int)
3681 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003682 *
3683 * @deprecated Use the new {@link DialogFragment} class with
3684 * {@link FragmentManager} instead; this is also
3685 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003687 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003688 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
3689 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 }
3691
3692 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003693 * Simple version of {@link #showDialog(int, Bundle)} that does not
3694 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
3695 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003696 *
3697 * @deprecated Use the new {@link DialogFragment} class with
3698 * {@link FragmentManager} instead; this is also
3699 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003700 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003701 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003702 public final void showDialog(int id) {
3703 showDialog(id, null);
3704 }
3705
3706 /**
3707 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 * will be made with the same id the first time this is called for a given
3709 * id. From thereafter, the dialog will be automatically saved and restored.
3710 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003711 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3712 * or later, consider instead using a {@link DialogFragment} instead.</em>
3713 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003714 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 * be made to provide an opportunity to do any timely preparation.
3716 *
3717 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003718 * @param args Arguments to pass through to the dialog. These will be saved
3719 * and restored for you. Note that if the dialog is already created,
3720 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
3721 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08003722 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003723 * @return Returns true if the Dialog was created; false is returned if
3724 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07003725 *
Joe Onorato37296dc2009-07-31 17:58:55 -07003726 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003727 * @see #onCreateDialog(int, Bundle)
3728 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 * @see #dismissDialog(int)
3730 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003731 *
3732 * @deprecated Use the new {@link DialogFragment} class with
3733 * {@link FragmentManager} instead; this is also
3734 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003736 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003737 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003738 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003740 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003742 ManagedDialog md = mManagedDialogs.get(id);
3743 if (md == null) {
3744 md = new ManagedDialog();
3745 md.mDialog = createDialog(id, null, args);
3746 if (md.mDialog == null) {
3747 return false;
3748 }
3749 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 }
RoboErik55011652014-07-09 15:05:53 -07003751
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003752 md.mArgs = args;
3753 onPrepareDialog(id, md.mDialog, args);
3754 md.mDialog.show();
3755 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 }
3757
3758 /**
3759 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
3760 *
3761 * @param id The id of the managed dialog.
3762 *
3763 * @throws IllegalArgumentException if the id was not previously shown via
3764 * {@link #showDialog(int)}.
3765 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003766 * @see #onCreateDialog(int, Bundle)
3767 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 * @see #showDialog(int)
3769 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003770 *
3771 * @deprecated Use the new {@link DialogFragment} class with
3772 * {@link FragmentManager} instead; this is also
3773 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003775 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 public final void dismissDialog(int id) {
3777 if (mManagedDialogs == null) {
3778 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 }
RoboErik55011652014-07-09 15:05:53 -07003780
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003781 final ManagedDialog md = mManagedDialogs.get(id);
3782 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 throw missingDialog(id);
3784 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003785 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 }
3787
3788 /**
3789 * Creates an exception to throw if a user passed in a dialog id that is
3790 * unexpected.
3791 */
3792 private IllegalArgumentException missingDialog(int id) {
3793 return new IllegalArgumentException("no dialog with id " + id + " was ever "
3794 + "shown via Activity#showDialog");
3795 }
3796
3797 /**
3798 * Removes any internal references to a dialog managed by this Activity.
3799 * If the dialog is showing, it will dismiss it as part of the clean up.
3800 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003801 * <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 -08003802 * want to avoid the overhead of saving and restoring it in the future.
3803 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003804 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
3805 * will not throw an exception if you try to remove an ID that does not
3806 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07003807 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 * @param id The id of the managed dialog.
3809 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003810 * @see #onCreateDialog(int, Bundle)
3811 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 * @see #showDialog(int)
3813 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003814 *
3815 * @deprecated Use the new {@link DialogFragment} class with
3816 * {@link FragmentManager} instead; this is also
3817 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003819 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003821 if (mManagedDialogs != null) {
3822 final ManagedDialog md = mManagedDialogs.get(id);
3823 if (md != null) {
3824 md.mDialog.dismiss();
3825 mManagedDialogs.remove(id);
3826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 }
3829
3830 /**
3831 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07003832 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01003833 * <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 -07003834 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01003835 * calling this function is the same as calling
3836 * {@link #startSearch startSearch(null, false, null, false)}, which launches
3837 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07003838 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 * <p>You can override this function to force global search, e.g. in response to a dedicated
3840 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07003841 *
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003842 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default
3843 * implementation changes to simply return false and you must supply your own custom
3844 * implementation if you want to support search.</p>
3845 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003846 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003847 * @return Returns {@code true} if search launched, and {@code false} if the activity does
3848 * not respond to search. The default implementation always returns {@code true}, except
3849 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07003850 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 * @see android.app.SearchManager
3852 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003853 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
3854 mSearchEvent = searchEvent;
3855 boolean result = onSearchRequested();
3856 mSearchEvent = null;
3857 return result;
3858 }
3859
3860 /**
3861 * @see #onSearchRequested(SearchEvent)
3862 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 public boolean onSearchRequested() {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003864 if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK)
3865 != Configuration.UI_MODE_TYPE_TELEVISION) {
3866 startSearch(null, false, null, false);
3867 return true;
3868 } else {
3869 return false;
3870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 }
RoboErik55011652014-07-09 15:05:53 -07003872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003874 * During the onSearchRequested() callbacks, this function will return the
3875 * {@link SearchEvent} that triggered the callback, if it exists.
3876 *
3877 * @return SearchEvent The SearchEvent that triggered the {@link
3878 * #onSearchRequested} callback.
3879 */
3880 public final SearchEvent getSearchEvent() {
3881 return mSearchEvent;
3882 }
3883
3884 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07003886 *
3887 * <p>It is typically called from onSearchRequested(), either directly from
3888 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07003890 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 * is to inject specific data such as context data, it is preferred to <i>override</i>
3892 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07003893 *
3894 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07003896 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 * any further typing will replace it. This is useful for cases where an entire pre-formed
3898 * query is being inserted. If false, the selection point will be placed at the end of the
3899 * inserted query. This is useful when the inserted query is text that the user entered,
3900 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
3901 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07003902 * @param appSearchData An application can insert application-specific
3903 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 * searches. This data will be returned with SEARCH intent(s). Null if
3905 * no extra data is required.
3906 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07003907 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07003908 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07003910 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 * @see android.app.SearchManager
3912 * @see #onSearchRequested
3913 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003914 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
3915 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003916 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01003917 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07003918 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 }
3920
3921 /**
krosaend2d60142009-08-17 08:56:48 -07003922 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
3923 * the search dialog. Made available for testing purposes.
3924 *
3925 * @param query The query to trigger. If empty, the request will be ignored.
3926 * @param appSearchData An application can insert application-specific
3927 * context here, in order to improve quality or specificity of its own
3928 * searches. This data will be returned with SEARCH intent(s). Null if
3929 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07003930 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003931 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07003932 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01003933 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07003934 }
3935
3936 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 * Request that key events come to this activity. Use this if your
3938 * activity has no views with focus, but the activity still wants
3939 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07003940 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 * @see android.view.Window#takeKeyEvents
3942 */
3943 public void takeKeyEvents(boolean get) {
3944 getWindow().takeKeyEvents(get);
3945 }
3946
3947 /**
3948 * Enable extended window features. This is a convenience for calling
3949 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07003950 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 * @param featureId The desired feature as defined in
3952 * {@link android.view.Window}.
3953 * @return Returns true if the requested feature is supported and now
3954 * enabled.
RoboErik55011652014-07-09 15:05:53 -07003955 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 * @see android.view.Window#requestFeature
3957 */
3958 public final boolean requestWindowFeature(int featureId) {
3959 return getWindow().requestFeature(featureId);
3960 }
3961
3962 /**
3963 * Convenience for calling
3964 * {@link android.view.Window#setFeatureDrawableResource}.
3965 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003966 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003967 getWindow().setFeatureDrawableResource(featureId, resId);
3968 }
3969
3970 /**
3971 * Convenience for calling
3972 * {@link android.view.Window#setFeatureDrawableUri}.
3973 */
3974 public final void setFeatureDrawableUri(int featureId, Uri uri) {
3975 getWindow().setFeatureDrawableUri(featureId, uri);
3976 }
3977
3978 /**
3979 * Convenience for calling
3980 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
3981 */
3982 public final void setFeatureDrawable(int featureId, Drawable drawable) {
3983 getWindow().setFeatureDrawable(featureId, drawable);
3984 }
3985
3986 /**
3987 * Convenience for calling
3988 * {@link android.view.Window#setFeatureDrawableAlpha}.
3989 */
3990 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
3991 getWindow().setFeatureDrawableAlpha(featureId, alpha);
3992 }
3993
3994 /**
3995 * Convenience for calling
3996 * {@link android.view.Window#getLayoutInflater}.
3997 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003998 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 public LayoutInflater getLayoutInflater() {
4000 return getWindow().getLayoutInflater();
4001 }
4002
4003 /**
4004 * Returns a {@link MenuInflater} with this context.
4005 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004006 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07004008 // Make sure that action views can get an appropriate theme.
4009 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07004010 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07004011 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07004012 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07004013 } else {
4014 mMenuInflater = new MenuInflater(this);
4015 }
4016 }
4017 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 }
4019
4020 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07004021 public void setTheme(int resid) {
4022 super.setTheme(resid);
4023 mWindow.setTheme(resid);
4024 }
4025
4026 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07004027 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004028 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 if (mParent == null) {
4030 super.onApplyThemeResource(theme, resid, first);
4031 } else {
4032 try {
4033 theme.setTo(mParent.getTheme());
4034 } catch (Exception e) {
4035 // Empty
4036 }
4037 theme.applyStyle(resid, false);
4038 }
Winson Chung1af8eda2016-02-05 17:55:56 +00004039 }
4040
4041 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004042 * Requests permissions to be granted to this application. These permissions
4043 * must be requested in your manifest, they should not be granted to your app,
4044 * and they should have protection level {@link android.content.pm.PermissionInfo
4045 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
4046 * the platform or a third-party app.
4047 * <p>
4048 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4049 * are granted at install time if requested in the manifest. Signature permissions
4050 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4051 * install time if requested in the manifest and the signature of your app matches
4052 * the signature of the app declaring the permissions.
4053 * </p>
4054 * <p>
4055 * If your app does not have the requested permissions the user will be presented
4056 * with UI for accepting them. After the user has accepted or rejected the
4057 * requested permissions you will receive a callback on {@link
4058 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4059 * permissions were granted or not.
4060 * </p>
4061 * <p>
4062 * Note that requesting a permission does not guarantee it will be granted and
4063 * your app should be able to run without having this permission.
4064 * </p>
4065 * <p>
4066 * This method may start an activity allowing the user to choose which permissions
4067 * to grant and which to reject. Hence, you should be prepared that your activity
4068 * may be paused and resumed. Further, granting some permissions may require
4069 * a restart of you application. In such a case, the system will recreate the
4070 * activity stack before delivering the result to {@link
4071 * #onRequestPermissionsResult(int, String[], int[])}.
4072 * </p>
4073 * <p>
4074 * When checking whether you have a permission you should use {@link
4075 * #checkSelfPermission(String)}.
4076 * </p>
4077 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004078 * Calling this API for permissions already granted to your app would show UI
4079 * to the user to decide whether the app can still hold these permissions. This
4080 * can be useful if the way your app uses data guarded by the permissions
4081 * changes significantly.
4082 * </p>
4083 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004084 * You cannot request a permission if your activity sets {@link
4085 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4086 * <code>true</code> because in this case the activity would not receive
4087 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4088 * </p>
4089 * <p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004090 * A sample permissions request looks like this:
4091 * </p>
4092 * <code><pre><p>
4093 * private void showContacts() {
4094 * if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
4095 * != PackageManager.PERMISSION_GRANTED) {
4096 * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
4097 * PERMISSIONS_REQUEST_READ_CONTACTS);
4098 * } else {
4099 * doShowContacts();
4100 * }
4101 * }
4102 *
4103 * {@literal @}Override
4104 * public void onRequestPermissionsResult(int requestCode, String[] permissions,
4105 * int[] grantResults) {
4106 * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS
4107 * && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
4108 * showContacts();
4109 * }
4110 * }
4111 * </code></pre></p>
4112 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004113 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004114 * @param requestCode Application specific request code to match with a result
4115 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004116 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004117 *
4118 * @see #onRequestPermissionsResult(int, String[], int[])
4119 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004120 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004121 */
4122 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Svetoslavffb32b12015-10-15 16:54:00 -07004123 if (mHasCurrentPermissionsRequest) {
4124 Log.w(TAG, "Can reqeust only one set of permissions at a time");
4125 // Dispatch the callback with empty arrays which means a cancellation.
4126 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4127 return;
4128 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004129 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004130 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004131 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004132 }
4133
4134 /**
4135 * Callback for the result from requesting permissions. This method
4136 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004137 * <p>
4138 * <strong>Note:</strong> It is possible that the permissions request interaction
4139 * with the user is interrupted. In this case you will receive empty permissions
4140 * and results arrays which should be treated as a cancellation.
4141 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004142 *
4143 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4144 * @param permissions The requested permissions. Never null.
4145 * @param grantResults The grant results for the corresponding permissions
4146 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4147 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4148 *
4149 * @see #requestPermissions(String[], int)
4150 */
4151 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4152 @NonNull int[] grantResults) {
4153 /* callback - no nothing */
4154 }
4155
4156 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004157 * Gets whether you should show UI with rationale for requesting a permission.
4158 * You should do this only if you do not have the permission and the context in
4159 * which the permission is requested does not clearly communicate to the user
4160 * what would be the benefit from granting this permission.
4161 * <p>
4162 * For example, if you write a camera app, requesting the camera permission
4163 * would be expected by the user and no rationale for why it is requested is
4164 * needed. If however, the app needs location for tagging photos then a non-tech
4165 * savvy user may wonder how location is related to taking photos. In this case
4166 * you may choose to show UI with rationale of requesting this permission.
4167 * </p>
4168 *
4169 * @param permission A permission your app wants to request.
4170 * @return Whether you can show permission rationale UI.
4171 *
4172 * @see #checkSelfPermission(String)
4173 * @see #requestPermissions(String[], int)
4174 * @see #onRequestPermissionsResult(int, String[], int[])
4175 */
Svetoslav9ce9a152015-06-02 17:59:31 -07004176 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07004177 return getPackageManager().shouldShowRequestPermissionRationale(permission);
4178 }
4179
4180 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004181 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
4182 * with no options.
4183 *
4184 * @param intent The intent to start.
4185 * @param requestCode If >= 0, this code will be returned in
4186 * onActivityResult() when the activity exits.
4187 *
4188 * @throws android.content.ActivityNotFoundException
4189 *
George Mount0a778ed2013-12-13 13:35:36 -08004190 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07004191 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004192 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004193 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004194 }
4195
4196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 * Launch an activity for which you would like a result when it finished.
4198 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08004199 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07004200 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07004202 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203 * <p>Note that this method should only be used with Intent protocols
4204 * that are defined to return a result. In other protocols (such as
4205 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
4206 * not get the result when you expect. For example, if the activity you
4207 * are launching uses the singleTask launch mode, it will not run in your
4208 * task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004209 *
RoboErik55011652014-07-09 15:05:53 -07004210 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08004212 * activity, then your window will not be displayed until a result is
4213 * returned back from the started activity. This is to avoid visible
4214 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004215 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 * <p>This method throws {@link android.content.ActivityNotFoundException}
4217 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004218 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 * @param intent The intent to start.
4220 * @param requestCode If >= 0, this code will be returned in
4221 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004222 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004223 * See {@link android.content.Context#startActivity(Intent, Bundle)
4224 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004225 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004227 *
George Mount0a778ed2013-12-13 13:35:36 -08004228 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004230 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
4231 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 if (mParent == null) {
4233 Instrumentation.ActivityResult ar =
4234 mInstrumentation.execStartActivity(
4235 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004236 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 if (ar != null) {
4238 mMainThread.sendActivityResult(
4239 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
4240 ar.getResultData());
4241 }
4242 if (requestCode >= 0) {
4243 // If this start is requesting a result, we can avoid making
4244 // the activity visible until the result is received. Setting
4245 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4246 // activity hidden during this time, to avoid flickering.
4247 // This can only be done when a result is requested because
4248 // that guarantees we will get information back when the
4249 // activity is finished, no matter what happens to it.
4250 mStartedActivity = true;
4251 }
Adam Powell14874662013-07-18 19:42:41 -07004252
George Mount41725de2015-04-09 08:23:05 -07004253 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07004254 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004256 if (options != null) {
4257 mParent.startActivityFromChild(this, intent, requestCode, options);
4258 } else {
4259 // Note we want to go through this method for compatibility with
4260 // existing applications that may have overridden it.
4261 mParent.startActivityFromChild(this, intent, requestCode);
4262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 }
George Mount41725de2015-04-09 08:23:05 -07004264 }
4265
4266 /**
4267 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
4268 *
4269 * @param options The ActivityOptions bundle used to start an Activity.
4270 */
4271 private void cancelInputsAndStartExitTransition(Bundle options) {
4272 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
4273 if (decor != null) {
4274 decor.cancelPendingInputEvents();
4275 }
George Mountff243282014-07-07 16:12:07 -07004276 if (options != null && !isTopOfTask()) {
4277 mActivityTransitionState.startExitOutTransition(this, options);
4278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 }
4280
4281 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004282 * @hide Implement to provide correct calling token.
4283 */
Alexandra Gherghina83231452014-07-04 12:05:20 +01004284 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
4285 startActivityForResultAsUser(intent, requestCode, null, user);
4286 }
4287
4288 /**
4289 * @hide Implement to provide correct calling token.
4290 */
4291 public void startActivityForResultAsUser(Intent intent, int requestCode,
4292 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004293 if (mParent != null) {
4294 throw new RuntimeException("Can't be called from a child");
4295 }
4296 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
4297 this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode,
4298 options, user);
4299 if (ar != null) {
4300 mMainThread.sendActivityResult(
4301 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
4302 }
4303 if (requestCode >= 0) {
4304 // If this start is requesting a result, we can avoid making
4305 // the activity visible until the result is received. Setting
4306 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4307 // activity hidden during this time, to avoid flickering.
4308 // This can only be done when a result is requested because
4309 // that guarantees we will get information back when the
4310 // activity is finished, no matter what happens to it.
4311 mStartedActivity = true;
4312 }
4313
George Mount41725de2015-04-09 08:23:05 -07004314 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004315 }
4316
4317 /**
4318 * @hide Implement to provide correct calling token.
4319 */
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004320 public void startActivityAsUser(Intent intent, UserHandle user) {
4321 startActivityAsUser(intent, null, user);
4322 }
4323
4324 /**
4325 * @hide Implement to provide correct calling token.
4326 */
4327 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
4328 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004329 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004330 }
4331 Instrumentation.ActivityResult ar =
4332 mInstrumentation.execStartActivity(
4333 this, mMainThread.getApplicationThread(), mToken, this,
4334 intent, -1, options, user);
4335 if (ar != null) {
4336 mMainThread.sendActivityResult(
4337 mToken, mEmbeddedID, -1, ar.getResultCode(),
4338 ar.getResultData());
4339 }
George Mount41725de2015-04-09 08:23:05 -07004340 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004341 }
4342
4343 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004344 * Start a new activity as if it was started by the activity that started our
4345 * current activity. This is for the resolver and chooser activities, which operate
4346 * as intermediaries that dispatch their intent to the target the user selects -- to
4347 * do this, they must perform all security checks including permission grants as if
4348 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004349 * @param intent The Intent to start.
4350 * @param options ActivityOptions or null.
4351 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
4352 * caller it is doing the start is, is actually allowed to start the target activity.
4353 * If you set this to true, you must set an explicit component in the Intent and do any
4354 * appropriate security checks yourself.
4355 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004356 * @hide
4357 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004358 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
4359 boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004360 if (mParent != null) {
4361 throw new RuntimeException("Can't be called from a child");
4362 }
4363 Instrumentation.ActivityResult ar =
4364 mInstrumentation.execStartActivityAsCaller(
4365 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004366 intent, -1, options, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004367 if (ar != null) {
4368 mMainThread.sendActivityResult(
4369 mToken, mEmbeddedID, -1, ar.getResultCode(),
4370 ar.getResultData());
4371 }
George Mount41725de2015-04-09 08:23:05 -07004372 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004373 }
4374
4375 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004376 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
4377 * Intent, int, int, int, Bundle)} with no options.
4378 *
4379 * @param intent The IntentSender to launch.
4380 * @param requestCode If >= 0, this code will be returned in
4381 * onActivityResult() when the activity exits.
4382 * @param fillInIntent If non-null, this will be provided as the
4383 * intent parameter to {@link IntentSender#sendIntent}.
4384 * @param flagsMask Intent flags in the original IntentSender that you
4385 * would like to change.
4386 * @param flagsValues Desired values for any bits set in
4387 * <var>flagsMask</var>
4388 * @param extraFlags Always set to 0.
4389 */
4390 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004391 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004392 throws IntentSender.SendIntentException {
4393 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
4394 flagsValues, extraFlags, null);
4395 }
4396
4397 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004398 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004399 * to use a IntentSender to describe the activity to be started. If
4400 * the IntentSender is for an activity, that activity will be started
4401 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
4402 * here; otherwise, its associated action will be executed (such as
4403 * sending a broadcast) as if you had called
4404 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07004405 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004406 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004407 * @param requestCode If >= 0, this code will be returned in
4408 * onActivityResult() when the activity exits.
4409 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004410 * intent parameter to {@link IntentSender#sendIntent}.
4411 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004412 * would like to change.
4413 * @param flagsValues Desired values for any bits set in
4414 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004415 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004416 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004417 * See {@link android.content.Context#startActivity(Intent, Bundle)
4418 * Context.startActivity(Intent, Bundle)} for more details. If options
4419 * have also been supplied by the IntentSender, options given here will
4420 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004421 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004422 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004423 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004424 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004425 if (mParent == null) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004426 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004427 flagsMask, flagsValues, this, options);
4428 } else if (options != null) {
4429 mParent.startIntentSenderFromChild(this, intent, requestCode,
4430 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004431 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004432 // Note we want to go through this call for compatibility with
4433 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004434 mParent.startIntentSenderFromChild(this, intent, requestCode,
4435 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004436 }
4437 }
4438
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004439 private void startIntentSenderForResultInner(IntentSender intent, int requestCode,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004440 Intent fillInIntent, int flagsMask, int flagsValues, Activity activity,
4441 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004442 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004443 try {
4444 String resolvedType = null;
4445 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004446 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004447 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004448 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
4449 }
4450 int result = ActivityManagerNative.getDefault()
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004451 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004452 fillInIntent, resolvedType, mToken, activity.mEmbeddedID,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004453 requestCode, flagsMask, flagsValues, options);
4454 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004455 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004456 }
4457 Instrumentation.checkStartActivityResult(result, null);
4458 } catch (RemoteException e) {
4459 }
4460 if (requestCode >= 0) {
4461 // If this start is requesting a result, we can avoid making
4462 // the activity visible until the result is received. Setting
4463 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4464 // activity hidden during this time, to avoid flickering.
4465 // This can only be done when a result is requested because
4466 // that guarantees we will get information back when the
4467 // activity is finished, no matter what happens to it.
4468 mStartedActivity = true;
4469 }
4470 }
4471
4472 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004473 * Same as {@link #startActivity(Intent, Bundle)} with no options
4474 * specified.
4475 *
4476 * @param intent The intent to start.
4477 *
4478 * @throws android.content.ActivityNotFoundException
4479 *
4480 * @see {@link #startActivity(Intent, Bundle)}
4481 * @see #startActivityForResult
4482 */
4483 @Override
4484 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00004485 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004486 }
4487
4488 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 * Launch a new activity. You will not receive any information about when
4490 * the activity exits. This implementation overrides the base version,
4491 * providing information about
4492 * the activity performing the launch. Because of this additional
4493 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4494 * required; if not specified, the new activity will be added to the
4495 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07004496 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004497 * <p>This method throws {@link android.content.ActivityNotFoundException}
4498 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004499 *
4500 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004501 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004502 * See {@link android.content.Context#startActivity(Intent, Bundle)
4503 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004504 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004506 *
4507 * @see {@link #startActivity(Intent)}
RoboErik55011652014-07-09 15:05:53 -07004508 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 */
4510 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004511 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004512 if (options != null) {
4513 startActivityForResult(intent, -1, options);
4514 } else {
4515 // Note we want to go through this call for compatibility with
4516 // applications that may have overridden the method.
4517 startActivityForResult(intent, -1);
4518 }
4519 }
4520
4521 /**
4522 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4523 * specified.
4524 *
4525 * @param intents The intents to start.
4526 *
4527 * @throws android.content.ActivityNotFoundException
4528 *
4529 * @see {@link #startActivities(Intent[], Bundle)}
4530 * @see #startActivityForResult
4531 */
4532 @Override
4533 public void startActivities(Intent[] intents) {
4534 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 }
4536
4537 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004538 * Launch a new activity. You will not receive any information about when
4539 * the activity exits. This implementation overrides the base version,
4540 * providing information about
4541 * the activity performing the launch. Because of this additional
4542 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4543 * required; if not specified, the new activity will be added to the
4544 * task of the caller.
4545 *
4546 * <p>This method throws {@link android.content.ActivityNotFoundException}
4547 * if there was no Activity found to run the given Intent.
4548 *
4549 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004550 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004551 * See {@link android.content.Context#startActivity(Intent, Bundle)
4552 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004553 *
4554 * @throws android.content.ActivityNotFoundException
4555 *
Dianne Hackborna4972e92012-03-14 10:38:05 -07004556 * @see {@link #startActivities(Intent[])}
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004557 * @see #startActivityForResult
4558 */
4559 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004560 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004561 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004562 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004563 }
4564
4565 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004566 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4567 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004568 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004569 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004570 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004571 * intent parameter to {@link IntentSender#sendIntent}.
4572 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004573 * would like to change.
4574 * @param flagsValues Desired values for any bits set in
4575 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004576 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004577 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004578 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004579 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004580 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004581 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
4582 extraFlags, null);
4583 }
4584
4585 /**
4586 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
4587 * to start; see
4588 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
4589 * for more information.
4590 *
4591 * @param intent The IntentSender to launch.
4592 * @param fillInIntent If non-null, this will be provided as the
4593 * intent parameter to {@link IntentSender#sendIntent}.
4594 * @param flagsMask Intent flags in the original IntentSender that you
4595 * would like to change.
4596 * @param flagsValues Desired values for any bits set in
4597 * <var>flagsMask</var>
4598 * @param extraFlags Always set to 0.
4599 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004600 * See {@link android.content.Context#startActivity(Intent, Bundle)
4601 * Context.startActivity(Intent, Bundle)} for more details. If options
4602 * have also been supplied by the IntentSender, options given here will
4603 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004604 */
4605 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004606 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004607 Bundle options) throws IntentSender.SendIntentException {
4608 if (options != null) {
4609 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4610 flagsValues, extraFlags, options);
4611 } else {
4612 // Note we want to go through this call for compatibility with
4613 // applications that may have overridden the method.
4614 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4615 flagsValues, extraFlags);
4616 }
4617 }
4618
4619 /**
4620 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
4621 * with no options.
4622 *
4623 * @param intent The intent to start.
4624 * @param requestCode If >= 0, this code will be returned in
4625 * onActivityResult() when the activity exits, as described in
4626 * {@link #startActivityForResult}.
4627 *
4628 * @return If a new activity was launched then true is returned; otherwise
4629 * false is returned and you must handle the Intent yourself.
4630 *
4631 * @see #startActivity
4632 * @see #startActivityForResult
4633 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004634 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4635 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004636 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004637 }
4638
4639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 * A special variation to launch an activity only if a new activity
4641 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07004642 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004643 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07004644 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004645 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07004646 * and the activity
4647 * that handles <var>intent</var> is the same as your currently running
4648 * activity, then a new instance is not needed. In this case, instead of
4649 * the normal behavior of calling {@link #onNewIntent} this function will
4650 * return and you can handle the Intent yourself.
4651 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 * <p>This function can only be called from a top-level activity; if it is
4653 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07004654 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 * @param intent The intent to start.
4656 * @param requestCode If >= 0, this code will be returned in
4657 * onActivityResult() when the activity exits, as described in
4658 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004659 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004660 * See {@link android.content.Context#startActivity(Intent, Bundle)
4661 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004662 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 * @return If a new activity was launched then true is returned; otherwise
4664 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07004665 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 * @see #startActivity
4667 * @see #startActivityForResult
4668 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004669 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4670 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004672 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004673 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004674 Uri referrer = onProvideReferrer();
4675 if (referrer != null) {
4676 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4677 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004678 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004679 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 result = ActivityManagerNative.getDefault()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08004681 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07004682 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
4683 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
4684 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004685 } catch (RemoteException e) {
4686 // Empty
4687 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08004688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004689 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 if (requestCode >= 0) {
4692 // If this start is requesting a result, we can avoid making
4693 // the activity visible until the result is received. Setting
4694 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4695 // activity hidden during this time, to avoid flickering.
4696 // This can only be done when a result is requested because
4697 // that guarantees we will get information back when the
4698 // activity is finished, no matter what happens to it.
4699 mStartedActivity = true;
4700 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004701 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004702 }
4703
4704 throw new UnsupportedOperationException(
4705 "startActivityIfNeeded can only be called from a top-level activity");
4706 }
4707
4708 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004709 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
4710 * no options.
4711 *
4712 * @param intent The intent to dispatch to the next activity. For
4713 * correct behavior, this must be the same as the Intent that started
4714 * your own activity; the only changes you can make are to the extras
4715 * inside of it.
4716 *
4717 * @return Returns a boolean indicating whether there was another Activity
4718 * to start: true if there was a next activity to start, false if there
4719 * wasn't. In general, if true is returned you will then want to call
4720 * finish() on yourself.
4721 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004722 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004723 return startNextMatchingActivity(intent, null);
4724 }
4725
4726 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004727 * Special version of starting an activity, for use when you are replacing
4728 * other activity components. You can use this to hand the Intent off
4729 * to the next Activity that can handle it. You typically call this in
4730 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07004731 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004732 * @param intent The intent to dispatch to the next activity. For
4733 * correct behavior, this must be the same as the Intent that started
4734 * your own activity; the only changes you can make are to the extras
4735 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004736 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004737 * See {@link android.content.Context#startActivity(Intent, Bundle)
4738 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004739 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004740 * @return Returns a boolean indicating whether there was another Activity
4741 * to start: true if there was a next activity to start, false if there
4742 * wasn't. In general, if true is returned you will then want to call
4743 * finish() on yourself.
4744 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004745 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
4746 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 if (mParent == null) {
4748 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004749 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004750 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004751 return ActivityManagerNative.getDefault()
Dianne Hackborna4972e92012-03-14 10:38:05 -07004752 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004753 } catch (RemoteException e) {
4754 // Empty
4755 }
4756 return false;
4757 }
4758
4759 throw new UnsupportedOperationException(
4760 "startNextMatchingActivity can only be called from a top-level activity");
4761 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004762
4763 /**
4764 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
4765 * with no options.
4766 *
4767 * @param child The activity making the call.
4768 * @param intent The intent to start.
4769 * @param requestCode Reply request code. < 0 if reply is not requested.
4770 *
4771 * @throws android.content.ActivityNotFoundException
4772 *
4773 * @see #startActivity
4774 * @see #startActivityForResult
4775 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004776 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004777 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004778 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004779 }
4780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 /**
RoboErik55011652014-07-09 15:05:53 -07004782 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07004784 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 * <p>This method throws {@link android.content.ActivityNotFoundException}
4786 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004787 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788 * @param child The activity making the call.
4789 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004790 * @param requestCode Reply request code. < 0 if reply is not requested.
4791 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004792 * See {@link android.content.Context#startActivity(Intent, Bundle)
4793 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004794 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004795 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004796 *
4797 * @see #startActivity
4798 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004799 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004800 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004801 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 Instrumentation.ActivityResult ar =
4803 mInstrumentation.execStartActivity(
4804 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004805 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004806 if (ar != null) {
4807 mMainThread.sendActivityResult(
4808 mToken, child.mEmbeddedID, requestCode,
4809 ar.getResultCode(), ar.getResultData());
4810 }
George Mount41725de2015-04-09 08:23:05 -07004811 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 }
4813
4814 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004815 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
4816 * with no options.
4817 *
4818 * @param fragment The fragment making the call.
4819 * @param intent The intent to start.
4820 * @param requestCode Reply request code. < 0 if reply is not requested.
4821 *
4822 * @throws android.content.ActivityNotFoundException
4823 *
4824 * @see Fragment#startActivity
4825 * @see Fragment#startActivityForResult
4826 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004827 public void startActivityFromFragment(@NonNull Fragment fragment,
4828 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004829 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004830 }
4831
4832 /**
RoboErik55011652014-07-09 15:05:53 -07004833 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004834 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
4835 * method.
RoboErik55011652014-07-09 15:05:53 -07004836 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004837 * <p>This method throws {@link android.content.ActivityNotFoundException}
4838 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004839 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004840 * @param fragment The fragment making the call.
4841 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07004842 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004843 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004844 * See {@link android.content.Context#startActivity(Intent, Bundle)
4845 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004846 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004847 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004848 *
4849 * @see Fragment#startActivity
4850 * @see Fragment#startActivityForResult
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004851 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004852 public void startActivityFromFragment(@NonNull Fragment fragment,
4853 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004854 startActivityForResult(fragment.mWho, intent, requestCode, options);
4855 }
4856
4857 /**
4858 * @hide
4859 */
4860 @Override
4861 public void startActivityForResult(
4862 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004863 Uri referrer = onProvideReferrer();
4864 if (referrer != null) {
4865 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4866 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004867 Instrumentation.ActivityResult ar =
4868 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004869 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004870 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004871 if (ar != null) {
4872 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004873 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004874 ar.getResultCode(), ar.getResultData());
4875 }
George Mount41725de2015-04-09 08:23:05 -07004876 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004877 }
4878
4879 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004880 * @hide
4881 */
4882 @Override
4883 public boolean canStartActivityForResult() {
4884 return true;
4885 }
4886
4887 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004888 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
4889 * int, Intent, int, int, int, Bundle)} with no options.
4890 */
4891 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4892 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
4893 int extraFlags)
4894 throws IntentSender.SendIntentException {
4895 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
4896 flagsMask, flagsValues, extraFlags, null);
4897 }
4898
4899 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004900 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004901 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07004902 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004903 * for more information.
4904 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004905 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4906 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07004907 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004908 throws IntentSender.SendIntentException {
4909 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004910 flagsMask, flagsValues, child, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004911 }
4912
4913 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004914 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
4915 * or {@link #finish} to specify an explicit transition animation to
4916 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004917 *
4918 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
4919 * to using this with starting activities is to supply the desired animation
4920 * information through a {@link ActivityOptions} bundle to
4921 * {@link #startActivity(Intent, Bundle) or a related function. This allows
4922 * you to specify a custom animation even when starting an activity from
4923 * outside the context of the current top activity.
4924 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004925 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004926 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004927 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004928 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004929 */
4930 public void overridePendingTransition(int enterAnim, int exitAnim) {
4931 try {
4932 ActivityManagerNative.getDefault().overridePendingTransition(
4933 mToken, getPackageName(), enterAnim, exitAnim);
4934 } catch (RemoteException e) {
4935 }
4936 }
RoboErik55011652014-07-09 15:05:53 -07004937
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 * Call this to set the result that your activity will return to its
4940 * caller.
RoboErik55011652014-07-09 15:05:53 -07004941 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942 * @param resultCode The result code to propagate back to the originating
4943 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07004944 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945 * @see #RESULT_CANCELED
4946 * @see #RESULT_OK
4947 * @see #RESULT_FIRST_USER
4948 * @see #setResult(int, Intent)
4949 */
4950 public final void setResult(int resultCode) {
4951 synchronized (this) {
4952 mResultCode = resultCode;
4953 mResultData = null;
4954 }
4955 }
4956
4957 /**
4958 * Call this to set the result that your activity will return to its
4959 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004960 *
4961 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
4962 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
4963 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
4964 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
4965 * Activity receiving the result access to the specific URIs in the Intent.
4966 * Access will remain until the Activity has finished (it will remain across the hosting
4967 * process being killed and other temporary destruction) and will be added
4968 * to any existing set of URI permissions it already holds.
4969 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 * @param resultCode The result code to propagate back to the originating
4971 * activity, often RESULT_CANCELED or RESULT_OK
4972 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07004973 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974 * @see #RESULT_CANCELED
4975 * @see #RESULT_OK
4976 * @see #RESULT_FIRST_USER
4977 * @see #setResult(int)
4978 */
4979 public final void setResult(int resultCode, Intent data) {
4980 synchronized (this) {
4981 mResultCode = resultCode;
4982 mResultData = data;
4983 }
4984 }
4985
4986 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004987 * Return information about who launched this activity. If the launching Intent
4988 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
4989 * that will be returned as-is; otherwise, if known, an
4990 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
4991 * package name that started the Intent will be returned. This may return null if no
4992 * referrer can be identified -- it is neither explicitly specified, nor is it known which
4993 * application package was involved.
4994 *
4995 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
4996 * return the referrer that submitted that new intent to the activity. Otherwise, it
4997 * always returns the referrer of the original Intent.</p>
4998 *
4999 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
5000 * referrer information, applications can spoof it.</p>
5001 */
5002 @Nullable
5003 public Uri getReferrer() {
5004 Intent intent = getIntent();
5005 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
5006 if (referrer != null) {
5007 return referrer;
5008 }
5009 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
5010 if (referrerName != null) {
5011 return Uri.parse(referrerName);
5012 }
5013 if (mReferrer != null) {
5014 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
5015 }
5016 return null;
5017 }
5018
5019 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07005020 * Override to generate the desired referrer for the content currently being shown
5021 * by the app. The default implementation returns null, meaning the referrer will simply
5022 * be the android-app: of the package name of this activity. Return a non-null Uri to
5023 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
5024 */
5025 public Uri onProvideReferrer() {
5026 return null;
5027 }
5028
5029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 * Return the name of the package that invoked this activity. This is who
5031 * the data in {@link #setResult setResult()} will be sent to. You can
5032 * use this information to validate that the recipient is allowed to
5033 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005034 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005035 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005036 * did not use the {@link #startActivityForResult}
5037 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005038 * null.</p>
5039 *
5040 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5041 * the result from this method was unstable. If the process hosting the calling
5042 * package was no longer running, it would return null instead of the proper package
5043 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5044 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005045 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005046 * @return The package of the activity that will receive your
5047 * reply, or null if none.
5048 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005049 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 public String getCallingPackage() {
5051 try {
5052 return ActivityManagerNative.getDefault().getCallingPackage(mToken);
5053 } catch (RemoteException e) {
5054 return null;
5055 }
5056 }
5057
5058 /**
5059 * Return the name of the activity that invoked this activity. This is
5060 * who the data in {@link #setResult setResult()} will be sent to. You
5061 * can use this information to validate that the recipient is allowed to
5062 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005063 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005064 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005065 * did not use the {@link #startActivityForResult}
5066 * form that includes a request code), then the calling package will be
5067 * null.
5068 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005069 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 * reply, or null if none.
5071 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005072 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 public ComponentName getCallingActivity() {
5074 try {
5075 return ActivityManagerNative.getDefault().getCallingActivity(mToken);
5076 } catch (RemoteException e) {
5077 return null;
5078 }
5079 }
5080
5081 /**
5082 * Control whether this activity's main window is visible. This is intended
5083 * only for the special case of an activity that is not going to show a
5084 * UI itself, but can't just finish prior to onResume() because it needs
5085 * to wait for a service binding or such. Setting this to false allows
5086 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005087 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 * <p>The default value for this is taken from the
5089 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
5090 */
5091 public void setVisible(boolean visible) {
5092 if (mVisibleFromClient != visible) {
5093 mVisibleFromClient = visible;
5094 if (mVisibleFromServer) {
5095 if (visible) makeVisible();
5096 else mDecor.setVisibility(View.INVISIBLE);
5097 }
5098 }
5099 }
RoboErik55011652014-07-09 15:05:53 -07005100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101 void makeVisible() {
5102 if (!mWindowAdded) {
5103 ViewManager wm = getWindowManager();
5104 wm.addView(mDecor, getWindow().getAttributes());
5105 mWindowAdded = true;
5106 }
5107 mDecor.setVisibility(View.VISIBLE);
5108 }
RoboErik55011652014-07-09 15:05:53 -07005109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 /**
5111 * Check to see whether this activity is in the process of finishing,
5112 * either because you called {@link #finish} on it or someone else
5113 * has requested that it finished. This is often used in
5114 * {@link #onPause} to determine whether the activity is simply pausing or
5115 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07005116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07005118 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005119 * @see #finish
5120 */
5121 public boolean isFinishing() {
5122 return mFinished;
5123 }
5124
5125 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07005126 * Returns true if the final {@link #onDestroy()} call has been made
5127 * on the Activity, so this instance is now dead.
5128 */
5129 public boolean isDestroyed() {
5130 return mDestroyed;
5131 }
5132
5133 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005134 * Check to see whether this activity is in the process of being destroyed in order to be
5135 * recreated with a new configuration. This is often used in
5136 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
5137 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07005138 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005139 * @return If the activity is being torn down in order to be recreated with a new configuration,
5140 * returns true; else returns false.
5141 */
5142 public boolean isChangingConfigurations() {
5143 return mChangingConfigurations;
5144 }
5145
5146 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005147 * Cause this Activity to be recreated with a new instance. This results
5148 * in essentially the same flow as when the Activity is created due to
5149 * a configuration change -- the current instance will go through its
5150 * lifecycle to {@link #onDestroy} and a new instance then created after it.
5151 */
5152 public void recreate() {
5153 if (mParent != null) {
5154 throw new IllegalStateException("Can only be called on top-level activity");
5155 }
5156 if (Looper.myLooper() != mMainThread.getLooper()) {
5157 throw new IllegalStateException("Must be called from main thread");
5158 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07005159 mMainThread.requestRelaunchActivity(mToken, null, null, 0, false, null, null, false,
5160 false /* preserveWindow */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005161 }
5162
5163 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005164 * Finishes the current activity and specifies whether to remove the task associated with this
5165 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005167 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 if (mParent == null) {
5169 int resultCode;
5170 Intent resultData;
5171 synchronized (this) {
5172 resultCode = mResultCode;
5173 resultData = mResultData;
5174 }
Joe Onorato43a17652011-04-06 19:22:23 -07005175 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005177 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005178 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 if (ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07005181 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 mFinished = true;
5183 }
5184 } catch (RemoteException e) {
5185 // Empty
5186 }
5187 } else {
5188 mParent.finishFromChild(this);
5189 }
5190 }
5191
5192 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005193 * Call this when your activity is done and should be closed. The
5194 * ActivityResult is propagated back to whoever launched you via
5195 * onActivityResult().
5196 */
5197 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005198 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005199 }
5200
5201 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005202 * Finish this activity as well as all activities immediately below it
5203 * in the current task that have the same affinity. This is typically
5204 * used when an application can be launched on to another task (such as
5205 * from an ACTION_VIEW of a content type it understands) and the user
5206 * has used the up navigation to switch out of the current task and in
5207 * to its own task. In this case, if the user has navigated down into
5208 * any other activities of the second application, all of those should
5209 * be removed from the original task as part of the task switch.
5210 *
5211 * <p>Note that this finish does <em>not</em> allow you to deliver results
5212 * to the previous activity, and an exception will be thrown if you are trying
5213 * to do so.</p>
5214 */
5215 public void finishAffinity() {
5216 if (mParent != null) {
5217 throw new IllegalStateException("Can not be called from an embedded activity");
5218 }
5219 if (mResultCode != RESULT_CANCELED || mResultData != null) {
5220 throw new IllegalStateException("Can not be called to deliver a result");
5221 }
5222 try {
5223 if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) {
5224 mFinished = true;
5225 }
5226 } catch (RemoteException e) {
5227 // Empty
5228 }
5229 }
5230
5231 /**
RoboErik55011652014-07-09 15:05:53 -07005232 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 * {@link #finish} method. The default implementation simply calls
5234 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07005235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07005237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 * @see #finish
5239 */
5240 public void finishFromChild(Activity child) {
5241 finish();
5242 }
5243
5244 /**
George Mountcb4b7d92014-02-25 10:47:55 -08005245 * Reverses the Activity Scene entry Transition and triggers the calling Activity
5246 * to reverse its exit Transition. When the exit Transition completes,
5247 * {@link #finish()} is called. If no entry Transition was used, finish() is called
5248 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07005249 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08005250 */
Craig Mautner73f843d2014-05-19 09:42:28 -07005251 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07005252 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07005253 finish();
5254 }
George Mountcb4b7d92014-02-25 10:47:55 -08005255 }
5256
5257 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 * Force finish another activity that you had previously started with
5259 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07005260 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 * @param requestCode The request code of the activity that you had
5262 * given to startActivityForResult(). If there are multiple
5263 * activities started with this request code, they
5264 * will all be finished.
5265 */
5266 public void finishActivity(int requestCode) {
5267 if (mParent == null) {
5268 try {
5269 ActivityManagerNative.getDefault()
5270 .finishSubActivity(mToken, mEmbeddedID, requestCode);
5271 } catch (RemoteException e) {
5272 // Empty
5273 }
5274 } else {
5275 mParent.finishActivityFromChild(this, requestCode);
5276 }
5277 }
5278
5279 /**
5280 * This is called when a child activity of this one calls its
5281 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07005282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 * @param child The activity making the call.
5284 * @param requestCode Request code that had been used to start the
5285 * activity.
5286 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005287 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 try {
5289 ActivityManagerNative.getDefault()
5290 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
5291 } catch (RemoteException e) {
5292 // Empty
5293 }
5294 }
5295
5296 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005297 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005298 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07005299 */
5300 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005301 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005302 }
5303
5304 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005305 * Ask that the local app instance of this activity be released to free up its memory.
5306 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
5307 * a new instance of the activity will later be re-created if needed due to the user
5308 * navigating back to it.
5309 *
5310 * @return Returns true if the activity was in a state that it has started the process
5311 * of destroying its current instance; returns false if for any reason this could not
5312 * be done: it is currently visible to the user, it is already being destroyed, it is
5313 * being finished, it hasn't yet saved its state, etc.
5314 */
5315 public boolean releaseInstance() {
5316 try {
5317 return ActivityManagerNative.getDefault().releaseActivityInstance(mToken);
5318 } catch (RemoteException e) {
5319 // Empty
5320 }
5321 return false;
5322 }
5323
5324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 * Called when an activity you launched exits, giving you the requestCode
5326 * you started it with, the resultCode it returned, and any additional
5327 * data from it. The <var>resultCode</var> will be
5328 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
5329 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07005330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 * <p>You will receive this call immediately before onResume() when your
5332 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07005333 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07005334 * <p>This method is never invoked if your activity sets
5335 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5336 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08005337 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 * @param requestCode The integer request code originally supplied to
5339 * startActivityForResult(), allowing you to identify who this
5340 * result came from.
5341 * @param resultCode The integer result code returned by the child activity
5342 * through its setResult().
5343 * @param data An Intent, which can return result data to the caller
5344 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07005345 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346 * @see #startActivityForResult
5347 * @see #createPendingResult
5348 * @see #setResult(int)
5349 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005350 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 }
5352
5353 /**
George Mount62ab9b72014-05-02 13:51:17 -07005354 * Called when an activity you launched with an activity transition exposes this
5355 * Activity through a returning activity transition, giving you the resultCode
5356 * and any additional data from it. This method will only be called if the activity
5357 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07005358 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07005359 *
5360 * <p>The purpose of this function is to let the called Activity send a hint about
5361 * its state so that this underlying Activity can prepare to be exposed. A call to
5362 * this method does not guarantee that the called Activity has or will be exiting soon.
5363 * It only indicates that it will expose this Activity's Window and it has
5364 * some data to pass to prepare it.</p>
5365 *
5366 * @param resultCode The integer result code returned by the child activity
5367 * through its setResult().
5368 * @param data An Intent, which can return result data to the caller
5369 * (various data can be attached to Intent "extras").
5370 */
Craig Mautner7a629c22014-09-04 14:57:04 -07005371 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07005372 }
5373
5374 /**
RoboErik55011652014-07-09 15:05:53 -07005375 * Create a new PendingIntent object which you can hand to others
5376 * for them to use to send result data back to your
5377 * {@link #onActivityResult} callback. The created object will be either
5378 * one-shot (becoming invalid after a result is sent back) or multiple
5379 * (allowing any number of results to be sent through it).
5380 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 * @param requestCode Private request code for the sender that will be
5382 * associated with the result data when it is returned. The sender can not
5383 * modify this value, allowing you to identify incoming results.
5384 * @param data Default data to supply in the result, which may be modified
5385 * by the sender.
5386 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
5387 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
5388 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
5389 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
5390 * or any of the flags as supported by
5391 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
5392 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07005393 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 * @return Returns an existing or new PendingIntent matching the given
5395 * parameters. May return null only if
5396 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
5397 * supplied.
RoboErik55011652014-07-09 15:05:53 -07005398 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 * @see PendingIntent
5400 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005401 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
5402 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 String packageName = getPackageName();
5404 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005405 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 IIntentSender target =
5407 ActivityManagerNative.getDefault().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07005408 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07005410 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
5411 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 return target != null ? new PendingIntent(target) : null;
5413 } catch (RemoteException e) {
5414 // Empty
5415 }
5416 return null;
5417 }
5418
5419 /**
5420 * Change the desired orientation of this activity. If the activity
5421 * is currently in the foreground or otherwise impacting the screen
5422 * orientation, the screen will immediately be changed (possibly causing
5423 * the activity to be restarted). Otherwise, this will be used the next
5424 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07005425 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 * @param requestedOrientation An orientation constant as used in
5427 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5428 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005429 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 if (mParent == null) {
5431 try {
5432 ActivityManagerNative.getDefault().setRequestedOrientation(
5433 mToken, requestedOrientation);
5434 } catch (RemoteException e) {
5435 // Empty
5436 }
5437 } else {
5438 mParent.setRequestedOrientation(requestedOrientation);
5439 }
5440 }
RoboErik55011652014-07-09 15:05:53 -07005441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442 /**
5443 * Return the current requested orientation of the activity. This will
5444 * either be the orientation requested in its component's manifest, or
5445 * the last requested orientation given to
5446 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07005447 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 * @return Returns an orientation constant as used in
5449 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5450 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005451 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 public int getRequestedOrientation() {
5453 if (mParent == null) {
5454 try {
5455 return ActivityManagerNative.getDefault()
5456 .getRequestedOrientation(mToken);
5457 } catch (RemoteException e) {
5458 // Empty
5459 }
5460 } else {
5461 return mParent.getRequestedOrientation();
5462 }
5463 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
5464 }
RoboErik55011652014-07-09 15:05:53 -07005465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 /**
5467 * Return the identifier of the task this activity is in. This identifier
5468 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07005469 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005470 * @return Task identifier, an opaque integer.
5471 */
5472 public int getTaskId() {
5473 try {
5474 return ActivityManagerNative.getDefault()
5475 .getTaskForActivity(mToken, false);
5476 } catch (RemoteException e) {
5477 return -1;
5478 }
5479 }
5480
5481 /**
5482 * Return whether this activity is the root of a task. The root is the
5483 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07005484 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 * @return True if this is the root activity, else false.
5486 */
5487 public boolean isTaskRoot() {
5488 try {
Filip Gruszczynski3d026712015-12-16 13:46:38 -08005489 return ActivityManagerNative.getDefault().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 } catch (RemoteException e) {
5491 return false;
5492 }
5493 }
5494
5495 /**
5496 * Move the task containing this activity to the back of the activity
5497 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07005498 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 * @param nonRoot If false then this only works if the activity is the root
5500 * of a task; if true it will work for any activity in
5501 * a task.
RoboErik55011652014-07-09 15:05:53 -07005502 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 * @return If the task was moved (or it was already at the
5504 * back) true is returned, else false.
5505 */
5506 public boolean moveTaskToBack(boolean nonRoot) {
5507 try {
5508 return ActivityManagerNative.getDefault().moveActivityTaskToBack(
5509 mToken, nonRoot);
5510 } catch (RemoteException e) {
5511 // Empty
5512 }
5513 return false;
5514 }
5515
5516 /**
5517 * Returns class name for this activity with the package prefix removed.
5518 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07005519 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 * @return The local class name.
5521 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005522 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005523 public String getLocalClassName() {
5524 final String pkg = getPackageName();
5525 final String cls = mComponent.getClassName();
5526 int packageLen = pkg.length();
5527 if (!cls.startsWith(pkg) || cls.length() <= packageLen
5528 || cls.charAt(packageLen) != '.') {
5529 return cls;
5530 }
5531 return cls.substring(packageLen+1);
5532 }
RoboErik55011652014-07-09 15:05:53 -07005533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 /**
5535 * Returns complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07005536 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 * @return Returns the complete component name for this activity
5538 */
5539 public ComponentName getComponentName()
5540 {
5541 return mComponent;
5542 }
5543
5544 /**
5545 * Retrieve a {@link SharedPreferences} object for accessing preferences
5546 * that are private to this activity. This simply calls the underlying
5547 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
5548 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07005549 *
5550 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07005551 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 *
5553 * @return Returns the single SharedPreferences instance that can be used
5554 * to retrieve and modify the preference values.
5555 */
5556 public SharedPreferences getPreferences(int mode) {
5557 return getSharedPreferences(getLocalClassName(), mode);
5558 }
RoboErik55011652014-07-09 15:05:53 -07005559
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005560 private void ensureSearchManager() {
5561 if (mSearchManager != null) {
5562 return;
5563 }
RoboErik55011652014-07-09 15:05:53 -07005564
Amith Yamasanie9ce3f02010-01-25 09:15:50 -08005565 mSearchManager = new SearchManager(this, null);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005566 }
Tor Norbyed9273d62013-05-30 15:59:53 -07005567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005569 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 if (getBaseContext() == null) {
5571 throw new IllegalStateException(
5572 "System services not available to Activities before onCreate()");
5573 }
5574
5575 if (WINDOW_SERVICE.equals(name)) {
5576 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005577 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005578 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005579 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 }
5581 return super.getSystemService(name);
5582 }
5583
5584 /**
5585 * Change the title associated with this activity. If this is a
5586 * top-level activity, the title for its window will change. If it
5587 * is an embedded activity, the parent can do whatever it wants
5588 * with it.
5589 */
5590 public void setTitle(CharSequence title) {
5591 mTitle = title;
5592 onTitleChanged(title, mTitleColor);
5593
5594 if (mParent != null) {
5595 mParent.onChildTitleChanged(this, title);
5596 }
5597 }
5598
5599 /**
5600 * Change the title associated with this activity. If this is a
5601 * top-level activity, the title for its window will change. If it
5602 * is an embedded activity, the parent can do whatever it wants
5603 * with it.
5604 */
5605 public void setTitle(int titleId) {
5606 setTitle(getText(titleId));
5607 }
5608
Alan Viverette2525d9c2013-11-15 14:42:19 -08005609 /**
5610 * Change the color of the title associated with this activity.
5611 * <p>
5612 * This method is deprecated starting in API Level 11 and replaced by action
5613 * bar styles. For information on styling the Action Bar, read the <a
5614 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
5615 * guide.
5616 *
5617 * @deprecated Use action bar styles instead.
5618 */
5619 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 public void setTitleColor(int textColor) {
5621 mTitleColor = textColor;
5622 onTitleChanged(mTitle, textColor);
5623 }
5624
5625 public final CharSequence getTitle() {
5626 return mTitle;
5627 }
5628
5629 public final int getTitleColor() {
5630 return mTitleColor;
5631 }
5632
5633 protected void onTitleChanged(CharSequence title, int color) {
5634 if (mTitleReady) {
5635 final Window win = getWindow();
5636 if (win != null) {
5637 win.setTitle(title);
5638 if (color != 0) {
5639 win.setTitleColor(color);
5640 }
5641 }
Adam Powellaf2d8592014-08-26 18:06:40 -07005642 if (mActionBar != null) {
5643 mActionBar.setWindowTitle(title);
5644 }
Adam Powella557fdc2014-08-21 18:05:53 -07005645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 }
5647
5648 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
5649 }
5650
5651 /**
Winson Chunga449dc02014-05-16 11:15:04 -07005652 * Sets information describing the task with this activity for presentation inside the Recents
5653 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
5654 * are traversed in order from the topmost activity to the bottommost. The traversal continues
5655 * for each property until a suitable value is found. For each task the taskDescription will be
5656 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07005657 *
5658 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07005659 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07005660 *
Winson Chunga449dc02014-05-16 11:15:04 -07005661 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07005662 */
Winson Chunga449dc02014-05-16 11:15:04 -07005663 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08005664 if (mTaskDescription != taskDescription) {
5665 mTaskDescription.copyFrom(taskDescription);
5666 // Scale the icon down to something reasonable if it is provided
5667 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
5668 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
5669 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
5670 true);
5671 mTaskDescription.setIcon(icon);
5672 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07005673 }
5674 try {
Winsonb6403152016-02-23 13:32:09 -08005675 ActivityManagerNative.getDefault().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07005676 } catch (RemoteException e) {
5677 }
5678 }
5679
5680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 * Sets the visibility of the progress bar in the title.
5682 * <p>
5683 * In order for the progress bar to be shown, the feature must be requested
5684 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005685 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005687 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005688 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005689 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005690 public final void setProgressBarVisibility(boolean visible) {
5691 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
5692 Window.PROGRESS_VISIBILITY_OFF);
5693 }
5694
5695 /**
5696 * Sets the visibility of the indeterminate progress bar in the title.
5697 * <p>
5698 * In order for the progress bar to be shown, the feature must be requested
5699 * via {@link #requestWindowFeature(int)}.
5700 *
5701 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005702 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005704 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 public final void setProgressBarIndeterminateVisibility(boolean visible) {
5706 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
5707 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
5708 }
RoboErik55011652014-07-09 15:05:53 -07005709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 /**
5711 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
5712 * is always indeterminate).
5713 * <p>
5714 * In order for the progress bar to be shown, the feature must be requested
5715 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005716 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005717 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005718 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005719 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005720 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005721 public final void setProgressBarIndeterminate(boolean indeterminate) {
5722 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00005723 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
5724 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 }
RoboErik55011652014-07-09 15:05:53 -07005726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 /**
5728 * Sets the progress for the progress bars in the title.
5729 * <p>
5730 * In order for the progress bar to be shown, the feature must be requested
5731 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005733 * @param progress The progress for the progress bar. Valid ranges are from
5734 * 0 to 10000 (both inclusive). If 10000 is given, the progress
5735 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005736 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005738 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 public final void setProgress(int progress) {
5740 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
5741 }
RoboErik55011652014-07-09 15:05:53 -07005742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 /**
5744 * Sets the secondary progress for the progress bar in the title. This
5745 * progress is drawn between the primary progress (set via
5746 * {@link #setProgress(int)} and the background. It can be ideal for media
5747 * scenarios such as showing the buffering progress while the default
5748 * progress shows the play progress.
5749 * <p>
5750 * In order for the progress bar to be shown, the feature must be requested
5751 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005752 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
5754 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04005755 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005757 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 public final void setSecondaryProgress(int secondaryProgress) {
5759 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
5760 secondaryProgress + Window.PROGRESS_SECONDARY_START);
5761 }
5762
5763 /**
5764 * Suggests an audio stream whose volume should be changed by the hardware
5765 * volume controls.
5766 * <p>
5767 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07005768 * Volume requests which are received while the Activity is in the
5769 * foreground will affect this stream.
5770 * <p>
5771 * It is not guaranteed that the hardware volume controls will always change
5772 * this stream's volume (for example, if a call is in progress, its stream's
5773 * volume may be changed instead). To reset back to the default, use
5774 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
5775 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07005777 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778 */
5779 public final void setVolumeControlStream(int streamType) {
5780 getWindow().setVolumeControlStream(streamType);
5781 }
5782
5783 /**
5784 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07005785 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07005786 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 * @return The suggested audio stream type whose volume should be changed by
5788 * the hardware volume controls.
5789 * @see #setVolumeControlStream(int)
5790 */
5791 public final int getVolumeControlStream() {
5792 return getWindow().getVolumeControlStream();
5793 }
RoboErik55011652014-07-09 15:05:53 -07005794
5795 /**
5796 * Sets a {@link MediaController} to send media keys and volume changes to.
5797 * <p>
5798 * The controller will be tied to the window of this Activity. Media key and
5799 * volume events which are received while the Activity is in the foreground
5800 * will be forwarded to the controller and used to invoke transport controls
5801 * or adjust the volume. This may be used instead of or in addition to
5802 * {@link #setVolumeControlStream} to affect a specific session instead of a
5803 * specific stream.
5804 * <p>
5805 * It is not guaranteed that the hardware volume controls will always change
5806 * this session's volume (for example, if a call is in progress, its
5807 * stream's volume may be changed instead). To reset back to the default use
5808 * null as the controller.
5809 *
5810 * @param controller The controller for the session which should receive
5811 * media keys and volume changes.
5812 */
5813 public final void setMediaController(MediaController controller) {
5814 getWindow().setMediaController(controller);
5815 }
5816
5817 /**
5818 * Gets the controller which should be receiving media key and volume events
5819 * while this activity is in the foreground.
5820 *
5821 * @return The controller which should receive events.
5822 * @see #setMediaController(android.media.session.MediaController)
5823 */
5824 public final MediaController getMediaController() {
5825 return getWindow().getMediaController();
5826 }
5827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 /**
5829 * Runs the specified action on the UI thread. If the current thread is the UI
5830 * thread, then the action is executed immediately. If the current thread is
5831 * not the UI thread, the action is posted to the event queue of the UI thread.
5832 *
5833 * @param action the action to run on the UI thread
5834 */
5835 public final void runOnUiThread(Runnable action) {
5836 if (Thread.currentThread() != mUiThread) {
5837 mHandler.post(action);
5838 } else {
5839 action.run();
5840 }
5841 }
5842
5843 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005844 * Standard implementation of
5845 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
5846 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005847 * This implementation does nothing and is for
5848 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
5849 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
5850 *
5851 * @see android.view.LayoutInflater#createView
5852 * @see android.view.Window#getLayoutInflater
5853 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005854 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07005855 public View onCreateView(String name, Context context, AttributeSet attrs) {
5856 return null;
5857 }
5858
5859 /**
5860 * Standard implementation of
5861 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
5862 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005863 * This implementation handles <fragment> tags to embed fragments inside
5864 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 *
5866 * @see android.view.LayoutInflater#createView
5867 * @see android.view.Window#getLayoutInflater
5868 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07005869 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005870 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07005871 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005872 }
RoboErik55011652014-07-09 15:05:53 -07005873
Adam Powell371a8092014-06-20 12:51:12 -07005874 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 }
5876
Daniel Sandler69a48172010-06-23 16:29:36 -04005877 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07005878 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07005879 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07005880 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08005881 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005882 * @param fd The raw file descriptor that the dump is being sent to.
5883 * @param writer The PrintWriter to which you should dump your state. This will be
5884 * closed for you after you return.
5885 * @param args additional arguments to the dump request.
5886 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08005887 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07005888 dumpInner(prefix, fd, writer, args);
5889 }
5890
5891 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08005892 writer.print(prefix); writer.print("Local Activity ");
5893 writer.print(Integer.toHexString(System.identityHashCode(this)));
5894 writer.println(" State:");
5895 String innerPrefix = prefix + " ";
5896 writer.print(innerPrefix); writer.print("mResumed=");
5897 writer.print(mResumed); writer.print(" mStopped=");
5898 writer.print(mStopped); writer.print(" mFinished=");
5899 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08005900 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
5901 writer.println(mChangingConfigurations);
5902 writer.print(innerPrefix); writer.print("mCurrentConfig=");
5903 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07005904
Todd Kennedya5fc6f02015-04-14 18:22:54 -07005905 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
5906 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07005907 if (mVoiceInteractor != null) {
5908 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
5909 }
Jeff Brown5182c782013-10-15 20:31:52 -07005910
Michael Wright5f48dc72013-11-01 12:42:49 -07005911 if (getWindow() != null &&
5912 getWindow().peekDecorView() != null &&
5913 getWindow().peekDecorView().getViewRootImpl() != null) {
5914 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
5915 }
Jeff Brown5182c782013-10-15 20:31:52 -07005916
5917 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07005918 }
5919
5920 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04005921 * Bit indicating that this activity is "immersive" and should not be
5922 * interrupted by notifications if possible.
5923 *
5924 * This value is initially set by the manifest property
5925 * <code>android:immersive</code> but may be changed at runtime by
5926 * {@link #setImmersive}.
5927 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07005928 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04005929 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5930 */
5931 public boolean isImmersive() {
5932 try {
5933 return ActivityManagerNative.getDefault().isImmersive(mToken);
5934 } catch (RemoteException e) {
5935 return false;
5936 }
5937 }
5938
5939 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07005940 * Indication of whether this is the highest level activity in this task. Can be used to
5941 * determine whether an activity launched by this activity was placed in the same task or
5942 * another task.
5943 *
5944 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07005945 */
George Mountff243282014-07-07 16:12:07 -07005946 private boolean isTopOfTask() {
Craig Mautnerd61dc202014-07-07 11:09:11 -07005947 try {
5948 return ActivityManagerNative.getDefault().isTopOfTask(mToken);
5949 } catch (RemoteException e) {
5950 return false;
5951 }
5952 }
5953
5954 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07005955 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
5956 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005957 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005958 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005959 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
5960 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005961 * This call has no effect on non-translucent activities or on activities with the
5962 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005963 *
George Mount62ab9b72014-05-02 13:51:17 -07005964 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
5965 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07005966 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005967 *
5968 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07005969 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005970 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07005971 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07005972 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07005973 mTranslucentCallback = null;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005974 if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) {
5975 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
5976 }
Craig Mautner4addfc52013-06-25 08:05:45 -07005977 } catch (RemoteException e) {
5978 // pass
5979 }
5980 }
5981
5982 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07005983 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
5984 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
5985 * <p>
5986 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
5987 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
5988 * be called indicating that it is safe to make this activity translucent again. Until
5989 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
5990 * behind the frontmost Activity will be indeterminate.
5991 * <p>
5992 * This call has no effect on non-translucent activities or on activities with the
5993 * {@link android.R.attr#windowIsFloating} attribute.
5994 *
5995 * @param callback the method to call when all visible Activities behind this one have been
5996 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07005997 * @param options activity options delivered to the activity below this one. The options
5998 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07005999 * @return <code>true</code> if Window was opaque and will become translucent or
6000 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07006001 *
6002 * @see #convertFromTranslucent()
6003 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07006004 *
6005 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006006 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006007 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07006008 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07006009 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006010 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006011 try {
6012 mTranslucentCallback = callback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006013 mChangeCanvasToTranslucent =
Craig Mautner233ceee2014-05-09 17:05:11 -07006014 ActivityManagerNative.getDefault().convertToTranslucent(mToken, options);
George Mount9e183972014-09-03 12:35:09 -07006015 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07006016 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006017 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07006018 // Make callback return as though it timed out.
6019 mChangeCanvasToTranslucent = false;
6020 drawComplete = false;
6021 }
6022 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
6023 // Window is already translucent.
6024 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07006025 }
George Mount3cc716c2014-06-12 16:35:35 -07006026 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07006027 }
6028
6029 /** @hide */
6030 void onTranslucentConversionComplete(boolean drawComplete) {
6031 if (mTranslucentCallback != null) {
6032 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
6033 mTranslucentCallback = null;
6034 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006035 if (mChangeCanvasToTranslucent) {
6036 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
6037 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006038 }
6039
Craig Mautnereb8abf72014-07-02 15:04:09 -07006040 /** @hide */
6041 public void onNewActivityOptions(ActivityOptions options) {
6042 mActivityTransitionState.setEnterActivityOptions(this, options);
6043 if (!mStopped) {
6044 mActivityTransitionState.enterReady(this);
6045 }
6046 }
6047
Craig Mautner5eda9b32013-07-02 11:58:16 -07006048 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07006049 * Retrieve the ActivityOptions passed in from the launching activity or passed back
6050 * from an activity launched by this activity in its call to {@link
6051 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
6052 *
6053 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
6054 * @hide
6055 */
6056 ActivityOptions getActivityOptions() {
6057 try {
6058 return ActivityManagerNative.getDefault().getActivityOptions(mToken);
6059 } catch (RemoteException e) {
6060 }
6061 return null;
6062 }
6063
6064 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006065 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07006066 * this method anytime between the start of {@link #onResume()} and the return from
6067 * {@link #onPause()}. If this call is successful then the activity will remain visible after
6068 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
6069 *
6070 * <p>The actions of this call are reset each time that this activity is brought to the
6071 * front. That is, every time {@link #onResume()} is called the activity will be assumed
6072 * to not have requested visible behind. Therefore, if you want this activity to continue to
6073 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006074 *
6075 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
6076 * for dialog and translucent activities.
6077 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006078 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
6079 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
6080 *
6081 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07006082 * the next call to onResume.
6083 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006084 * @param visible true to notify the system that the activity wishes to be visible behind other
6085 * translucent activities, false to indicate otherwise. Resources must be
6086 * released when passing false to this method.
Craig Mautner64ccb702014-10-01 09:38:40 -07006087 * @return the resulting visibiity state. If true the activity will remain visible beyond
6088 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
6089 * then the activity may not count on being visible behind other translucent activities,
6090 * and must stop any media playback and release resources.
6091 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
6092 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006093 *
Jose Limafcf70832014-08-27 23:09:05 -07006094 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006095 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006096 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006097 public boolean requestVisibleBehind(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006098 if (!mResumed) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006099 // Do not permit paused or stopped activities to do this.
6100 visible = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006101 }
6102 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07006103 mVisibleBehind = ActivityManagerNative.getDefault()
6104 .requestVisibleBehind(mToken, visible) && visible;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006105 } catch (RemoteException e) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006106 mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006107 }
Jose Lima4b6c6692014-08-12 17:41:12 -07006108 return mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006109 }
6110
6111 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006112 * Called when a translucent activity over this activity is becoming opaque or another
6113 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07006114 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006115 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006116 * <p>When this method is called the activity has 500 msec to release any resources it may be
6117 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006118 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07006119 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07006120 * process. Otherwise {@link #onStop()} will be called following return.
6121 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006122 * @see #requestVisibleBehind(boolean)
6123 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006124 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006125 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07006126 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006127 mCalled = true;
6128 }
6129
6130 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006131 * Translucent activities may call this to determine if there is an activity below them that
6132 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006133 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006134 * @return true if an activity below is set to visible according to the most recent call to
6135 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006136 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006137 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006138 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006139 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006140 * @hide
6141 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006142 @SystemApi
6143 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006144 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07006145 return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07006146 } catch (RemoteException e) {
6147 }
6148 return false;
6149 }
6150
6151 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006152 * The topmost foreground activity will receive this call when the background visibility state
6153 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006154 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006155 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07006156 * due to a background activity finishing itself.
6157 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006158 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006159 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006160 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006161 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006162 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07006163 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006164 @SystemApi
6165 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006166 }
6167
6168 /**
Craig Mautner8746a472014-07-24 15:12:54 -07006169 * Activities cannot draw during the period that their windows are animating in. In order
6170 * to know when it is safe to begin drawing they can override this method which will be
6171 * called when the entering animation has completed.
6172 */
6173 public void onEnterAnimationComplete() {
6174 }
6175
6176 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08006177 * @hide
6178 */
6179 public void dispatchEnterAnimationComplete() {
6180 onEnterAnimationComplete();
6181 if (getWindow() != null && getWindow().getDecorView() != null) {
6182 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
6183 }
6184 }
6185
6186 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006187 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07006188 *
Daniel Sandler69a48172010-06-23 16:29:36 -04006189 * Note that changing this value will have no effect on the activity's
6190 * {@link android.content.pm.ActivityInfo} structure; that is, if
6191 * <code>android:immersive</code> is set to <code>true</code>
6192 * in the application's manifest entry for this activity, the {@link
6193 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
6194 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6195 * FLAG_IMMERSIVE} bit set.
6196 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006197 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04006198 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6199 */
6200 public void setImmersive(boolean i) {
6201 try {
6202 ActivityManagerNative.getDefault().setImmersive(mToken, i);
6203 } catch (RemoteException e) {
6204 // pass
6205 }
6206 }
6207
Adam Powell6e346362010-07-23 10:18:23 -07006208 /**
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006209 * Enable or disable virtual reality (VR) mode.
6210 *
6211 * <p>VR mode is a hint to Android system services to switch to modes optimized for
6212 * high-performance stereoscopic rendering.</p>
6213 *
6214 * @param enabled {@code true} to enable this mode.
6215 */
6216 public void setVrMode(boolean enabled) {
6217 try {
6218 ActivityManagerNative.getDefault().setVrMode(mToken, enabled);
6219 } catch (RemoteException e) {
6220 // pass
6221 }
6222 }
6223
6224 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006225 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07006226 *
Clara Bayarri4423d912015-03-02 19:42:48 +00006227 * @param callback Callback that will manage lifecycle events for this action mode
6228 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07006229 *
6230 * @see ActionMode
6231 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006232 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07006233 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07006234 return mWindow.getDecorView().startActionMode(callback);
6235 }
6236
Adam Powelldebf3be2010-11-15 18:58:48 -08006237 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006238 * Start an action mode of the given type.
6239 *
6240 * @param callback Callback that will manage lifecycle events for this action mode
6241 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
6242 * @return The ActionMode that was started, or null if it was canceled
6243 *
6244 * @see ActionMode
6245 */
6246 @Nullable
6247 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
6248 return mWindow.getDecorView().startActionMode(callback, type);
6249 }
6250
6251 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006252 * Give the Activity a chance to control the UI for an action mode requested
6253 * by the system.
6254 *
6255 * <p>Note: If you are looking for a notification callback that an action mode
6256 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
6257 *
6258 * @param callback The callback that should control the new action mode
6259 * @return The new action mode, or <code>null</code> if the activity does not want to
6260 * provide special handling for this action mode. (It will be handled by the system.)
6261 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006262 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07006263 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006264 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00006265 // Only Primary ActionModes are represented in the ActionBar.
6266 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
6267 initWindowDecorActionBar();
6268 if (mActionBar != null) {
6269 return mActionBar.startActionMode(callback);
6270 }
Adam Powell6e346362010-07-23 10:18:23 -07006271 }
6272 return null;
6273 }
6274
Adam Powelldebf3be2010-11-15 18:58:48 -08006275 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006276 * {@inheritDoc}
6277 */
6278 @Nullable
6279 @Override
6280 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
6281 try {
6282 mActionModeTypeStarting = type;
6283 return onWindowStartingActionMode(callback);
6284 } finally {
6285 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
6286 }
6287 }
6288
6289 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006290 * Notifies the Activity that an action mode has been started.
6291 * Activity subclasses overriding this method should call the superclass implementation.
6292 *
6293 * @param mode The new action mode.
6294 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006295 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006296 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006297 public void onActionModeStarted(ActionMode mode) {
6298 }
6299
6300 /**
6301 * Notifies the activity that an action mode has finished.
6302 * Activity subclasses overriding this method should call the superclass implementation.
6303 *
6304 * @param mode The action mode that just finished.
6305 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006306 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006307 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006308 public void onActionModeFinished(ActionMode mode) {
6309 }
6310
Adam Powelldd8fab22012-03-22 17:47:27 -07006311 /**
6312 * Returns true if the app should recreate the task when navigating 'up' from this activity
6313 * by using targetIntent.
6314 *
6315 * <p>If this method returns false the app can trivially call
6316 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
6317 * up navigation. If this method returns false, the app should synthesize a new task stack
6318 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
6319 *
6320 * @param targetIntent An intent representing the target destination for up navigation
6321 * @return true if navigating up should recreate a new task stack, false if the same task
6322 * should be used for the destination
6323 */
6324 public boolean shouldUpRecreateTask(Intent targetIntent) {
6325 try {
6326 PackageManager pm = getPackageManager();
6327 ComponentName cn = targetIntent.getComponent();
6328 if (cn == null) {
6329 cn = targetIntent.resolveActivity(pm);
6330 }
6331 ActivityInfo info = pm.getActivityInfo(cn, 0);
6332 if (info.taskAffinity == null) {
6333 return false;
6334 }
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07006335 return ActivityManagerNative.getDefault()
6336 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07006337 } catch (RemoteException e) {
6338 return false;
6339 } catch (NameNotFoundException e) {
6340 return false;
6341 }
6342 }
6343
6344 /**
6345 * Navigate from this activity to the activity specified by upIntent, finishing this activity
6346 * in the process. If the activity indicated by upIntent already exists in the task's history,
6347 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07006348 * finished.
6349 *
6350 * <p>If the indicated activity does not appear in the history stack, this will finish
6351 * each activity in this task until the root activity of the task is reached, resulting in
6352 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
6353 * when an activity may be reached by a path not passing through a canonical parent
6354 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07006355 *
6356 * <p>This method should be used when performing up navigation from within the same task
6357 * as the destination. If up navigation should cross tasks in some cases, see
6358 * {@link #shouldUpRecreateTask(Intent)}.</p>
6359 *
6360 * @param upIntent An intent representing the target destination for up navigation
6361 *
6362 * @return true if up navigation successfully reached the activity indicated by upIntent and
6363 * upIntent was delivered to it. false if an instance of the indicated activity could
6364 * not be found and this activity was simply finished normally.
6365 */
6366 public boolean navigateUpTo(Intent upIntent) {
6367 if (mParent == null) {
6368 ComponentName destInfo = upIntent.getComponent();
6369 if (destInfo == null) {
6370 destInfo = upIntent.resolveActivity(getPackageManager());
6371 if (destInfo == null) {
6372 return false;
6373 }
6374 upIntent = new Intent(upIntent);
6375 upIntent.setComponent(destInfo);
6376 }
6377 int resultCode;
6378 Intent resultData;
6379 synchronized (this) {
6380 resultCode = mResultCode;
6381 resultData = mResultData;
6382 }
6383 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006384 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006385 }
6386 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006387 upIntent.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006388 return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent,
6389 resultCode, resultData);
6390 } catch (RemoteException e) {
6391 return false;
6392 }
6393 } else {
6394 return mParent.navigateUpToFromChild(this, upIntent);
6395 }
6396 }
6397
6398 /**
6399 * This is called when a child activity of this one calls its
6400 * {@link #navigateUpTo} method. The default implementation simply calls
6401 * navigateUpTo(upIntent) on this activity (the parent).
6402 *
6403 * @param child The activity making the call.
6404 * @param upIntent An intent representing the target destination for up navigation
6405 *
6406 * @return true if up navigation successfully reached the activity indicated by upIntent and
6407 * upIntent was delivered to it. false if an instance of the indicated activity could
6408 * not be found and this activity was simply finished normally.
6409 */
6410 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
6411 return navigateUpTo(upIntent);
6412 }
6413
6414 /**
6415 * Obtain an {@link Intent} that will launch an explicit target activity specified by
6416 * this activity's logical parent. The logical parent is named in the application's manifest
6417 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07006418 * Activity subclasses may override this method to modify the Intent returned by
6419 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
6420 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07006421 *
Adam Powell04d58112012-04-09 10:22:12 -07006422 * @return a new Intent targeting the defined parent of this activity or null if
6423 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07006424 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006425 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07006426 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07006427 final String parentName = mActivityInfo.parentActivityName;
6428 if (TextUtils.isEmpty(parentName)) {
6429 return null;
6430 }
Adam Powell5a4010c2012-09-16 15:14:05 -07006431
6432 // If the parent itself has no parent, generate a main activity intent.
6433 final ComponentName target = new ComponentName(this, parentName);
6434 try {
6435 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
6436 final String parentActivity = parentInfo.parentActivityName;
6437 final Intent parentIntent = parentActivity == null
6438 ? Intent.makeMainActivity(target)
6439 : new Intent().setComponent(target);
6440 return parentIntent;
6441 } catch (NameNotFoundException e) {
6442 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
6443 "' in manifest");
6444 return null;
6445 }
Adam Powelldd8fab22012-03-22 17:47:27 -07006446 }
6447
George Mount31a21722014-03-24 17:44:36 -07006448 /**
George Mount62ab9b72014-05-02 13:51:17 -07006449 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006450 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006451 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07006452 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07006453 *
George Mount65580562014-08-29 08:15:48 -07006454 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07006455 */
George Mount65580562014-08-29 08:15:48 -07006456 public void setEnterSharedElementCallback(SharedElementCallback callback) {
6457 if (callback == null) {
6458 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07006459 }
George Mount65580562014-08-29 08:15:48 -07006460 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07006461 }
6462
6463 /**
6464 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006465 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006466 * will be called to handle shared elements on the <i>launching</i> Activity. Most
6467 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07006468 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07006469 *
George Mount65580562014-08-29 08:15:48 -07006470 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07006471 */
George Mount65580562014-08-29 08:15:48 -07006472 public void setExitSharedElementCallback(SharedElementCallback callback) {
6473 if (callback == null) {
6474 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07006475 }
George Mount65580562014-08-29 08:15:48 -07006476 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07006477 }
6478
George Mount8c2614c2014-06-10 11:12:01 -07006479 /**
6480 * Postpone the entering activity transition when Activity was started with
6481 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6482 * android.util.Pair[])}.
6483 * <p>This method gives the Activity the ability to delay starting the entering and
6484 * shared element transitions until all data is loaded. Until then, the Activity won't
6485 * draw into its window, leaving the window transparent. This may also cause the
6486 * returning animation to be delayed until data is ready. This method should be
6487 * called in {@link #onCreate(android.os.Bundle)} or in
6488 * {@link #onActivityReenter(int, android.content.Intent)}.
6489 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
6490 * start the transitions. If the Activity did not use
6491 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6492 * android.util.Pair[])}, then this method does nothing.</p>
6493 */
6494 public void postponeEnterTransition() {
6495 mActivityTransitionState.postponeEnterTransition();
6496 }
6497
6498 /**
6499 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
6500 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
6501 * to have your Activity start drawing.
6502 */
6503 public void startPostponedEnterTransition() {
6504 mActivityTransitionState.startPostponedEnterTransition();
6505 }
6506
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006507 /**
6508 * Create {@link DropPermissions} object bound to this activity and controlling the access
6509 * permissions for content URIs associated with the {@link DragEvent}.
6510 * @param event Drag event
6511 * @return The DropPermissions object used to control access to the content URIs. Null if
6512 * no content URIs are associated with the event or if permissions could not be granted.
6513 */
6514 public DropPermissions requestDropPermissions(DragEvent event) {
6515 DropPermissions dropPermissions = DropPermissions.obtain(event);
6516 if (dropPermissions != null && dropPermissions.take(getActivityToken())) {
6517 return dropPermissions;
6518 }
6519 return null;
6520 }
6521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006522 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07006523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 final void setParent(Activity parent) {
6525 mParent = parent;
6526 }
6527
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006528 final void attach(Context context, ActivityThread aThread,
6529 Instrumentation instr, IBinder token, int ident,
6530 Application application, Intent intent, ActivityInfo info,
6531 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006532 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006533 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
6534 Window window) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 attachBaseContext(context);
6536
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006537 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07006538
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006539 mWindow = new PhoneWindow(this, window);
Skuhnece2faa52015-08-11 10:36:38 -07006540 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006541 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07006542 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08006543 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006544 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
6545 mWindow.setSoftInputMode(info.softInputMode);
6546 }
Adam Powell269248d2011-08-02 10:26:54 -07006547 if (info.uiOptions != 0) {
6548 mWindow.setUiOptions(info.uiOptions);
6549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08006551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006552 mMainThread = aThread;
6553 mInstrumentation = instr;
6554 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006555 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 mApplication = application;
6557 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006558 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 mComponent = intent.getComponent();
6560 mActivityInfo = info;
6561 mTitle = title;
6562 mParent = parent;
6563 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006564 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07006565 if (voiceInteractor != null) {
6566 if (lastNonConfigurationInstances != null) {
6567 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
6568 } else {
6569 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
6570 Looper.myLooper());
6571 }
6572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006573
Jeff Brown98365d72012-08-19 20:30:52 -07006574 mWindow.setWindowManager(
6575 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
6576 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07006577 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 if (mParent != null) {
6579 mWindow.setContainer(mParent.getWindow());
6580 }
6581 mWindowManager = mWindow.getWindowManager();
6582 mCurrentConfig = config;
6583 }
6584
Dianne Hackborn5320eb82012-05-18 12:05:04 -07006585 /** @hide */
6586 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006587 return mParent != null ? mParent.getActivityToken() : mToken;
6588 }
6589
Craig Mautnera0026042014-04-23 11:45:37 -07006590 final void performCreateCommon() {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006591 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
6592 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07006593 mFragments.dispatchActivityCreated();
George Mount62ab9b72014-05-02 13:51:17 -07006594 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006595 }
George Mount0a778ed2013-12-13 13:35:36 -08006596
Craig Mautnera0026042014-04-23 11:45:37 -07006597 final void performCreate(Bundle icicle) {
Svetoslavffb32b12015-10-15 16:54:00 -07006598 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006599 onCreate(icicle);
George Mount62ab9b72014-05-02 13:51:17 -07006600 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006601 performCreateCommon();
6602 }
6603
6604 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Svetoslavffb32b12015-10-15 16:54:00 -07006605 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006606 onCreate(icicle, persistentState);
George Mount62ab9b72014-05-02 13:51:17 -07006607 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006608 performCreateCommon();
6609 }
6610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006611 final void performStart() {
George Mount62ab9b72014-05-02 13:51:17 -07006612 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006613 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07006615 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 mInstrumentation.callActivityOnStart(this);
6617 if (!mCalled) {
6618 throw new SuperNotCalledException(
6619 "Activity " + mComponent.toShortString() +
6620 " did not call through to super.onStart()");
6621 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006622 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006623 mFragments.reportLoaderStart();
George Mount62ab9b72014-05-02 13:51:17 -07006624 mActivityTransitionState.enterReady(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 }
RoboErik55011652014-07-09 15:05:53 -07006626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006627 final void performRestart() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006628 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07006629
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006630 if (mToken != null && mParent == null) {
6631 // We might have view roots that were preserved during a relaunch, we need to start them
6632 // again. We don't need to check mStopped, the roots will check if they were actually
6633 // stopped.
6634 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
6635 }
6636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 if (mStopped) {
6638 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006639
6640 synchronized (mManagedCursors) {
6641 final int N = mManagedCursors.size();
6642 for (int i=0; i<N; i++) {
6643 ManagedCursor mc = mManagedCursors.get(i);
6644 if (mc.mReleased || mc.mUpdated) {
6645 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07006646 if (getApplicationInfo().targetSdkVersion
6647 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
6648 throw new IllegalStateException(
6649 "trying to requery an already closed cursor "
6650 + mc.mCursor);
6651 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006652 }
6653 mc.mReleased = false;
6654 mc.mUpdated = false;
6655 }
6656 }
6657 }
6658
6659 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006660 mInstrumentation.callActivityOnRestart(this);
6661 if (!mCalled) {
6662 throw new SuperNotCalledException(
6663 "Activity " + mComponent.toShortString() +
6664 " did not call through to super.onRestart()");
6665 }
6666 performStart();
6667 }
6668 }
RoboErik55011652014-07-09 15:05:53 -07006669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 final void performResume() {
6671 performRestart();
RoboErik55011652014-07-09 15:05:53 -07006672
Dianne Hackborn445646c2010-06-25 15:52:59 -07006673 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006674
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006675 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07006676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006677 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06006678 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 mInstrumentation.callActivityOnResume(this);
6680 if (!mCalled) {
6681 throw new SuperNotCalledException(
6682 "Activity " + mComponent.toShortString() +
6683 " did not call through to super.onResume()");
6684 }
6685
Todd Kennedyee8c9c62014-12-10 14:22:59 -08006686 // invisible activities must be finished before onResume() completes
6687 if (!mVisibleFromClient && !mFinished) {
6688 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
6689 if (getApplicationInfo().targetSdkVersion
6690 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
6691 throw new IllegalStateException(
6692 "Activity " + mComponent.toShortString() +
6693 " did not call finish() prior to onResume() completing");
6694 }
6695 }
6696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006697 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006698 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07006699
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006700 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07006701 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 onPostResume();
6704 if (!mCalled) {
6705 throw new SuperNotCalledException(
6706 "Activity " + mComponent.toShortString() +
6707 " did not call through to super.onPostResume()");
6708 }
6709 }
6710
6711 final void performPause() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006712 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006713 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006714 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 onPause();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006716 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006717 if (!mCalled && getApplicationInfo().targetSdkVersion
6718 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
6719 throw new SuperNotCalledException(
6720 "Activity " + mComponent.toShortString() +
6721 " did not call through to super.onPause()");
6722 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06006723 mResumed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006724 }
RoboErik55011652014-07-09 15:05:53 -07006725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006726 final void performUserLeaving() {
6727 onUserInteraction();
6728 onUserLeaveHint();
6729 }
RoboErik55011652014-07-09 15:05:53 -07006730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006731 final void performStop() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006732 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006733 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
RoboErik55011652014-07-09 15:05:53 -07006734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006735 if (!mStopped) {
6736 if (mWindow != null) {
6737 mWindow.closeAllPanels();
6738 }
6739
Dianne Hackbornce418e62011-03-01 14:31:38 -08006740 if (mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07006741 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08006742 }
RoboErik55011652014-07-09 15:05:53 -07006743
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006744 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07006745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 mCalled = false;
6747 mInstrumentation.callActivityOnStop(this);
6748 if (!mCalled) {
6749 throw new SuperNotCalledException(
6750 "Activity " + mComponent.toShortString() +
6751 " did not call through to super.onStop()");
6752 }
RoboErik55011652014-07-09 15:05:53 -07006753
Makoto Onuki2f6a0182010-02-22 13:26:59 -08006754 synchronized (mManagedCursors) {
6755 final int N = mManagedCursors.size();
6756 for (int i=0; i<N; i++) {
6757 ManagedCursor mc = mManagedCursors.get(i);
6758 if (!mc.mReleased) {
6759 mc.mCursor.deactivate();
6760 mc.mReleased = true;
6761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 }
6763 }
George Mount0a778ed2013-12-13 13:35:36 -08006764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 mStopped = true;
6766 }
6767 mResumed = false;
6768 }
6769
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006770 final void performDestroy() {
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006771 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07006772 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006773 mFragments.dispatchDestroy();
6774 onDestroy();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006775 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07006776 if (mVoiceInteractor != null) {
6777 mVoiceInteractor.detachActivity();
6778 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006779 }
George Mount0a778ed2013-12-13 13:35:36 -08006780
6781 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06006782 * @hide
6783 */
6784 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006785 return mResumed;
6786 }
6787
Svetoslavffb32b12015-10-15 16:54:00 -07006788 private void storeHasCurrentPermissionRequest(Bundle bundle) {
6789 if (bundle != null && mHasCurrentPermissionsRequest) {
6790 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
6791 }
6792 }
6793
6794 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
6795 if (bundle != null) {
6796 mHasCurrentPermissionsRequest = bundle.getBoolean(
6797 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
6798 }
6799 }
6800
George Mount0a778ed2013-12-13 13:35:36 -08006801 void dispatchActivityResult(String who, int requestCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 int resultCode, Intent data) {
Joe Onorato43a17652011-04-06 19:22:23 -07006803 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006804 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
6805 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006806 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006807 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07006808 onActivityResult(requestCode, resultCode, data);
6809 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
6810 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
6811 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08006812 dispatchRequestPermissionsResult(requestCode, data);
6813 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006814 Fragment frag = mFragments.findFragmentByWho(who);
6815 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07006816 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08006817 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006818 }
Svetoslav970b59c2015-06-09 16:05:21 -07006819 } else if (who.startsWith("@android:view:")) {
6820 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
6821 getActivityToken());
6822 for (ViewRootImpl viewRoot : views) {
6823 if (viewRoot.getView() != null
6824 && viewRoot.getView().dispatchActivityResult(
6825 who, requestCode, resultCode, data)) {
6826 return;
6827 }
6828 }
6829 } else {
6830 Fragment frag = mFragments.findFragmentByWho(who);
6831 if (frag != null) {
6832 frag.onActivityResult(requestCode, resultCode, data);
6833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 }
6835 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006836
Jason Monka57e5e02014-05-07 10:06:48 -04006837 /**
Jason Monk62515be2014-05-21 16:06:19 -04006838 * Request to put this Activity in a mode where the user is locked to the
Jason Monka57e5e02014-05-07 10:06:48 -04006839 * current task.
6840 *
Craig Mautner15df08a2015-04-01 12:17:18 -07006841 * This will prevent the user from launching other apps, going to settings, or reaching the
6842 * home screen. This does not include those apps whose {@link android.R.attr#lockTaskMode}
6843 * values permit launching while locked.
Jason Monka57e5e02014-05-07 10:06:48 -04006844 *
Craig Mautner15df08a2015-04-01 12:17:18 -07006845 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true or
6846 * lockTaskMode=lockTaskModeAlways for this component then the app will go directly into
6847 * Lock Task mode. The user will not be able to exit this mode until
6848 * {@link Activity#stopLockTask()} is called.
Jason Monk62515be2014-05-21 16:06:19 -04006849 *
6850 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false
6851 * then the system will prompt the user with a dialog requesting permission to enter
Jason Monk8863b572014-07-29 13:49:32 -04006852 * this mode. When entered through this method the user can exit at any time through
6853 * an action described by the request dialog. Calling stopLockTask will also exit the
6854 * mode.
Craig Mautner15df08a2015-04-01 12:17:18 -07006855 *
6856 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04006857 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006858 public void startLockTask() {
6859 try {
6860 ActivityManagerNative.getDefault().startLockTaskMode(mToken);
6861 } catch (RemoteException e) {
6862 }
6863 }
6864
Jason Monka57e5e02014-05-07 10:06:48 -04006865 /**
6866 * Allow the user to switch away from the current task.
6867 *
6868 * Called to end the mode started by {@link Activity#startLockTask}. This
6869 * can only be called by activities that have successfully called
6870 * startLockTask previously.
6871 *
6872 * This will allow the user to exit this app and move onto other activities.
Craig Mautner15df08a2015-04-01 12:17:18 -07006873 * <p>Note: This method should only be called when the activity is user-facing. That is,
6874 * between onResume() and onPause().
6875 * <p>Note: If there are other tasks below this one that are also locked then calling this
6876 * method will immediately finish this task and resume the previous locked one, remaining in
6877 * lockTask mode.
6878 *
6879 * @see android.R.attr#lockTaskMode
6880 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04006881 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006882 public void stopLockTask() {
6883 try {
6884 ActivityManagerNative.getDefault().stopLockTaskMode();
6885 } catch (RemoteException e) {
6886 }
6887 }
6888
Craig Mautner5eda9b32013-07-02 11:58:16 -07006889 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07006890 * Shows the user the system defined message for telling the user how to exit
6891 * lock task mode. The task containing this activity must be in lock task mode at the time
6892 * of this call for the message to be displayed.
6893 */
6894 public void showLockTaskEscapeMessage() {
6895 try {
6896 ActivityManagerNative.getDefault().showLockTaskEscapeMessage(mToken);
6897 } catch (RemoteException e) {
6898 }
6899 }
6900
6901 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08006902 * Set whether the caption should displayed directly on the content rather than push it down.
6903 *
6904 * This affects only freeform windows since they display the caption and only the main
6905 * window of the activity. The caption is used to drag the window around and also shows
6906 * maximize and close action buttons.
6907 */
6908 public void overlayWithDecorCaption(boolean overlay) {
6909 mWindow.setOverlayDecorCaption(overlay);
6910 }
6911
6912 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006913 * Interface for informing a translucent {@link Activity} once all visible activities below it
6914 * have completed drawing. This is necessary only after an {@link Activity} has been made
6915 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
6916 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07006917 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6918 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07006919 *
6920 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006921 */
Jose Lima14914852014-08-14 09:14:12 -07006922 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006923 public interface TranslucentConversionListener {
6924 /**
6925 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
6926 * below the top one have been redrawn. Following this callback it is safe to make the top
6927 * Activity translucent because the underlying Activity has been drawn.
6928 *
6929 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
6930 * occurred waiting for the Activity to complete drawing.
6931 *
6932 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07006933 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006934 */
6935 public void onTranslucentConversionComplete(boolean drawComplete);
6936 }
Svetoslavc6d1c342015-02-26 14:44:43 -08006937
6938 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07006939 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07006940 // If the package installer crashed we may have not data - best effort.
6941 String[] permissions = (data != null) ? data.getStringArrayExtra(
6942 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
6943 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
6944 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08006945 onRequestPermissionsResult(requestCode, permissions, grantResults);
6946 }
6947
6948 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07006949 Fragment fragment) {
6950 // If the package installer crashed we may have not data - best effort.
6951 String[] permissions = (data != null) ? data.getStringArrayExtra(
6952 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
6953 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
6954 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
6955 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08006956 }
6957
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006958 class HostCallbacks extends FragmentHostCallback<Activity> {
6959 public HostCallbacks() {
6960 super(Activity.this /*activity*/);
6961 }
6962
6963 @Override
6964 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
6965 Activity.this.dump(prefix, fd, writer, args);
6966 }
6967
6968 @Override
6969 public boolean onShouldSaveFragmentState(Fragment fragment) {
6970 return !isFinishing();
6971 }
6972
6973 @Override
6974 public LayoutInflater onGetLayoutInflater() {
6975 final LayoutInflater result = Activity.this.getLayoutInflater();
6976 if (onUseFragmentManagerInflaterFactory()) {
6977 return result.cloneInContext(Activity.this);
6978 }
6979 return result;
6980 }
6981
6982 @Override
6983 public boolean onUseFragmentManagerInflaterFactory() {
6984 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
6985 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
6986 }
6987
6988 @Override
6989 public Activity onGetHost() {
6990 return Activity.this;
6991 }
6992
6993 @Override
6994 public void onInvalidateOptionsMenu() {
6995 Activity.this.invalidateOptionsMenu();
6996 }
6997
6998 @Override
6999 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
7000 Bundle options) {
7001 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
7002 }
7003
7004 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07007005 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
7006 int requestCode) {
7007 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
7008 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
7009 startActivityForResult(who, intent, requestCode, null);
7010 }
7011
7012 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007013 public boolean onHasWindowAnimations() {
7014 return getWindow() != null;
7015 }
7016
7017 @Override
7018 public int onGetWindowAnimations() {
7019 final Window w = getWindow();
7020 return (w == null) ? 0 : w.getAttributes().windowAnimations;
7021 }
7022
Todd Kennedy434bd652015-05-04 12:29:50 -07007023 @Override
7024 public void onAttachFragment(Fragment fragment) {
7025 Activity.this.onAttachFragment(fragment);
7026 }
7027
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007028 @Nullable
7029 @Override
7030 public View onFindViewById(int id) {
7031 return Activity.this.findViewById(id);
7032 }
7033
7034 @Override
7035 public boolean onHasView() {
7036 final Window w = getWindow();
7037 return (w != null && w.peekDecorView() != null);
7038 }
7039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007040}