blob: be89b20c91c5ba215dbfb4429b78af8cb5579ffa [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Winson Chung1af8eda2016-02-05 17:55:56 +000019import static java.lang.Character.MIN_VALUE;
20
Tor Norbyec615c6f2015-03-02 10:11:44 -080021import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
23import android.annotation.IdRes;
24import android.annotation.IntDef;
25import android.annotation.LayoutRes;
Tor Norbye83c68962015-03-10 20:55:31 -070026import android.annotation.MainThread;
Tor Norbyed9273d62013-05-30 15:59:53 -070027import android.annotation.NonNull;
Tor Norbye7b9c9122013-05-30 16:48:33 -070028import android.annotation.Nullable;
Tor Norbye788fc2b2015-07-05 16:10:42 -070029import android.annotation.RequiresPermission;
Tor Norbye7b9c9122013-05-30 16:48:33 -070030import android.annotation.StyleRes;
Jose Lima4b6c6692014-08-12 17:41:12 -070031import android.annotation.SystemApi;
Jason Monk62515be2014-05-21 16:06:19 -040032import android.app.admin.DevicePolicyManager;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070033import android.app.assist.AssistContent;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070034import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.ComponentName;
36import android.content.ContentResolver;
37import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050038import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070039import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070040import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070041import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.SharedPreferences;
43import android.content.pm.ActivityInfo;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080044import android.content.pm.ApplicationInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070045import android.content.pm.PackageManager;
46import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.res.Configuration;
48import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070049import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.database.Cursor;
51import android.graphics.Bitmap;
52import android.graphics.Canvas;
Winson2d476832016-02-17 14:53:46 -080053import android.graphics.Color;
Winson Chung1af8eda2016-02-05 17:55:56 +000054import android.graphics.Paint;
55import android.graphics.drawable.ColorDrawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.graphics.drawable.Drawable;
Winson Chung1af8eda2016-02-05 17:55:56 +000057import android.graphics.drawable.InsetDrawable;
58import android.graphics.drawable.LayerDrawable;
59import android.graphics.drawable.ShapeDrawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070061import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.net.Uri;
Dianne Hackborn8d374262009-09-14 21:21:52 -070063import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Handler;
66import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080067import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070068import android.os.Parcelable;
Winson Chung1af8eda2016-02-05 17:55:56 +000069import android.os.PersistableBundle;
svetoslavganov75986cf2009-05-14 22:28:01 -070070import android.os.RemoteException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080071import android.os.StrictMode;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -080072import android.os.SystemProperties;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070073import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.text.Selection;
75import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070076import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.text.method.TextKeyListener;
Winson Chung1af8eda2016-02-05 17:55:56 +000078import android.transition.Scene;
79import android.transition.TransitionManager;
80import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.util.EventLog;
83import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070084import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070085import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.SparseArray;
Winson Chung1af8eda2016-02-05 17:55:56 +000087import android.util.SuperNotCalledException;
Adam Powell6e346362010-07-23 10:18:23 -070088import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070090import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.view.ContextThemeWrapper;
Winson Chung1af8eda2016-02-05 17:55:56 +000092import android.view.DragEvent;
93import android.view.DropPermissions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.KeyEvent;
Clara Bayarri75e09792015-07-29 16:20:40 +010095import android.view.KeyboardShortcutGroup;
96import android.view.KeyboardShortcutInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.view.LayoutInflater;
98import android.view.Menu;
99import android.view.MenuInflater;
100import android.view.MenuItem;
101import android.view.MotionEvent;
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700102import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -0700104import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -0700106import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.view.ViewManager;
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +0000108import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.view.Window;
Winson Chung1af8eda2016-02-05 17:55:56 +0000110import android.view.Window.WindowControllerCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700112import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -0700113import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.widget.AdapterView;
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800115import android.widget.Toast;
Winson Chung1af8eda2016-02-05 17:55:56 +0000116import android.widget.Toolbar;
Clara Bayarri75e09792015-07-29 16:20:40 +0100117
Winson Chung1af8eda2016-02-05 17:55:56 +0000118import com.android.internal.app.IVoiceInteractor;
119import com.android.internal.app.ToolbarActionBar;
120import com.android.internal.app.WindowDecorActionBar;
121import com.android.internal.policy.DecorView;
Clara Bayarri75e09792015-07-29 16:20:40 +0100122import com.android.internal.policy.PhoneWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
Dianne Hackborn625ac272010-09-17 18:29:22 -0700124import java.io.FileDescriptor;
125import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700126import java.lang.annotation.Retention;
127import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700128import java.util.ArrayList;
129import java.util.HashMap;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700130import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
132/**
133 * An activity is a single, focused thing that the user can do. Almost all
134 * activities interact with the user, so the Activity class takes care of
135 * creating a window for you in which you can place your UI with
136 * {@link #setContentView}. While activities are often presented to the user
137 * as full-screen windows, they can also be used in other ways: as floating
138 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
139 * or embedded inside of another activity (using {@link ActivityGroup}).
140 *
141 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * <ul>
144 * <li> {@link #onCreate} is where you initialize your activity. Most
145 * importantly, here you will usually call {@link #setContentView(int)}
146 * with a layout resource defining your UI, and using {@link #findViewById}
147 * to retrieve the widgets in that UI that you need to interact with
148 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700149 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 * <li> {@link #onPause} is where you deal with the user leaving your
151 * activity. Most importantly, any changes made by the user should at this
152 * point be committed (usually to the
153 * {@link android.content.ContentProvider} holding the data).
154 * </ul>
155 *
156 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
157 * activity classes must have a corresponding
158 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
159 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * <p>Topics covered here:
162 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700163 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
165 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
166 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
167 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
168 * <li><a href="#Permissions">Permissions</a>
169 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
170 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700171 *
172 * <div class="special reference">
173 * <h3>Developer Guides</h3>
174 * <p>The Activity class is an important part of an application's overall lifecycle,
175 * and the way activities are launched and put together is a fundamental
176 * part of the platform's application model. For a detailed perspective on the structure of an
177 * Android application and how activities behave, please read the
178 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
179 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
180 * developer guides.</p>
181 *
182 * <p>You can also find a detailed discussion about how to create activities in the
183 * <a href="{@docRoot}guide/topics/fundamentals/activities.html">Activities</a>
184 * developer guide.</p>
185 * </div>
186 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700187 * <a name="Fragments"></a>
188 * <h3>Fragments</h3>
189 *
190 * <p>Starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}, Activity
191 * implementations can make use of the {@link Fragment} class to better
192 * modularize their code, build more sophisticated user interfaces for larger
193 * screens, and help scale their application between small and large screens.
194 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <a name="ActivityLifecycle"></a>
196 * <h3>Activity Lifecycle</h3>
197 *
198 * <p>Activities in the system are managed as an <em>activity stack</em>.
199 * When a new activity is started, it is placed on the top of the stack
200 * and becomes the running activity -- the previous activity always remains
201 * below it in the stack, and will not come to the foreground again until
202 * the new activity exits.</p>
RoboErik55011652014-07-09 15:05:53 -0700203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * <p>An activity has essentially four states:</p>
205 * <ul>
206 * <li> If an activity in the foreground of the screen (at the top of
207 * the stack),
208 * it is <em>active</em> or <em>running</em>. </li>
209 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
RoboErik55011652014-07-09 15:05:53 -0700210 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * is <em>paused</em>. A paused activity is completely alive (it
212 * maintains all state and member information and remains attached to
213 * the window manager), but can be killed by the system in extreme
214 * low memory situations.
215 * <li>If an activity is completely obscured by another activity,
216 * it is <em>stopped</em>. It still retains all state and member information,
217 * however, it is no longer visible to the user so its window is hidden
218 * and it will often be killed by the system when memory is needed
219 * elsewhere.</li>
220 * <li>If an activity is paused or stopped, the system can drop the activity
221 * from memory by either asking it to finish, or simply killing its
222 * process. When it is displayed again to the user, it must be
223 * completely restarted and restored to its previous state.</li>
224 * </ul>
225 *
226 * <p>The following diagram shows the important state paths of an Activity.
227 * The square rectangles represent callback methods you can implement to
228 * perform operations when the Activity moves between states. The colored
229 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700230 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <p><img src="../../../images/activity_lifecycle.png"
232 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * <p>There are three key loops you may be interested in monitoring within your
235 * activity:
RoboErik55011652014-07-09 15:05:53 -0700236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * <ul>
238 * <li>The <b>entire lifetime</b> of an activity happens between the first call
239 * to {@link android.app.Activity#onCreate} through to a single final call
240 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
241 * of "global" state in onCreate(), and release all remaining resources in
242 * onDestroy(). For example, if it has a thread running in the background
243 * to download data from the network, it may create that thread in onCreate()
244 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * <li>The <b>visible lifetime</b> of an activity happens between a call to
247 * {@link android.app.Activity#onStart} until a corresponding call to
248 * {@link android.app.Activity#onStop}. During this time the user can see the
249 * activity on-screen, though it may not be in the foreground and interacting
250 * with the user. Between these two methods you can maintain resources that
251 * are needed to show the activity to the user. For example, you can register
252 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900253 * that impact your UI, and unregister it in onStop() when the user no
254 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * can be called multiple times, as the activity becomes visible and hidden
256 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
259 * {@link android.app.Activity#onResume} until a corresponding call to
260 * {@link android.app.Activity#onPause}. During this time the activity is
261 * in front of all other activities and interacting with the user. An activity
262 * can frequently go between the resumed and paused states -- for example when
263 * the device goes to sleep, when an activity result is delivered, when a new
264 * intent is delivered -- so the code in these methods should be fairly
265 * lightweight.
266 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700267 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * <p>The entire lifecycle of an activity is defined by the following
269 * Activity methods. All of these are hooks that you can override
270 * to do appropriate work when the activity changes state. All
271 * activities will implement {@link android.app.Activity#onCreate}
272 * to do their initial setup; many will also implement
273 * {@link android.app.Activity#onPause} to commit changes to data and
274 * otherwise prepare to stop interacting with the user. You should always
275 * call up to your superclass when implementing these methods.</p>
276 *
277 * </p>
278 * <pre class="prettyprint">
279 * public class Activity extends ApplicationContext {
280 * protected void onCreate(Bundle savedInstanceState);
281 *
282 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * protected void onRestart();
285 *
286 * protected void onResume();
287 *
288 * protected void onPause();
289 *
290 * protected void onStop();
291 *
292 * protected void onDestroy();
293 * }
294 * </pre>
295 *
296 * <p>In general the movement through an activity's lifecycle looks like
297 * this:</p>
298 *
299 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
300 * <colgroup align="left" span="3" />
301 * <colgroup align="left" />
302 * <colgroup align="center" />
303 * <colgroup align="center" />
304 *
305 * <thead>
306 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
307 * </thead>
308 *
309 * <tbody>
310 * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onCreate onCreate()}</th>
311 * <td>Called when the activity is first created.
312 * This is where you should do all of your normal static set up:
313 * create views, bind data to lists, etc. This method also
314 * provides you with a Bundle containing the activity's previously
315 * frozen state, if there was one.
316 * <p>Always followed by <code>onStart()</code>.</td>
317 * <td align="center">No</td>
318 * <td align="center"><code>onStart()</code></td>
319 * </tr>
320 *
321 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
322 * <th colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</th>
323 * <td>Called after your activity has been stopped, prior to it being
324 * started again.
325 * <p>Always followed by <code>onStart()</code></td>
326 * <td align="center">No</td>
327 * <td align="center"><code>onStart()</code></td>
328 * </tr>
329 *
330 * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStart onStart()}</th>
331 * <td>Called when the activity is becoming visible to the user.
332 * <p>Followed by <code>onResume()</code> if the activity comes
333 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
334 * <td align="center">No</td>
335 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
336 * </tr>
337 *
338 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
339 * <th align="left" border="0">{@link android.app.Activity#onResume onResume()}</th>
340 * <td>Called when the activity will start
341 * interacting with the user. At this point your activity is at
342 * the top of the activity stack, with user input going to it.
343 * <p>Always followed by <code>onPause()</code>.</td>
344 * <td align="center">No</td>
345 * <td align="center"><code>onPause()</code></td>
346 * </tr>
347 *
348 * <tr><th align="left" border="0">{@link android.app.Activity#onPause onPause()}</th>
349 * <td>Called when the system is about to start resuming a previous
350 * activity. This is typically used to commit unsaved changes to
351 * persistent data, stop animations and other things that may be consuming
352 * CPU, etc. Implementations of this method must be very quick because
353 * the next activity will not be resumed until this method returns.
354 * <p>Followed by either <code>onResume()</code> if the activity
355 * returns back to the front, or <code>onStop()</code> if it becomes
356 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800357 * <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 -0800358 * <td align="center"><code>onResume()</code> or<br>
359 * <code>onStop()</code></td>
360 * </tr>
361 *
362 * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</th>
363 * <td>Called when the activity is no longer visible to the user, because
364 * another activity has been resumed and is covering this one. This
365 * may happen either because a new activity is being started, an existing
366 * one is being brought in front of this one, or this one is being
367 * destroyed.
368 * <p>Followed by either <code>onRestart()</code> if
369 * this activity is coming back to interact with the user, or
370 * <code>onDestroy()</code> if this activity is going away.</td>
371 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
372 * <td align="center"><code>onRestart()</code> or<br>
373 * <code>onDestroy()</code></td>
374 * </tr>
375 *
376 * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onDestroy onDestroy()}</th>
377 * <td>The final call you receive before your
378 * activity is destroyed. This can happen either because the
379 * activity is finishing (someone called {@link Activity#finish} on
380 * it, or because the system is temporarily destroying this
381 * instance of the activity to save space. You can distinguish
382 * between these two scenarios with the {@link
383 * Activity#isFinishing} method.</td>
384 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
385 * <td align="center"><em>nothing</em></td>
386 * </tr>
387 * </tbody>
388 * </table>
389 *
390 * <p>Note the "Killable" column in the above table -- for those methods that
391 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800392 * 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 -0800393 * of its code being executed. Because of this, you should use the
394 * {@link #onPause} method to write any persistent data (such as user edits)
395 * to storage. In addition, the method
396 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
397 * in such a background state, allowing you to save away any dynamic instance
398 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700399 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
401 * section for more information on how the lifecycle of a process is tied
402 * to the activities it is hosting. Note that it is important to save
403 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800404 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 * be called in every situation as described in its documentation.</p>
406 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800407 * <p class="note">Be aware that these semantics will change slightly between
408 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
409 * vs. those targeting prior platforms. Starting with Honeycomb, an application
410 * is not in the killable state until its {@link #onStop} has returned. This
411 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
412 * safely called after {@link #onPause()} and allows and application to safely
413 * wait until {@link #onStop()} to save persistent state.</p>
414 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 * <p>For those methods that are not marked as being killable, the activity's
416 * process will not be killed by the system starting from the time the method
417 * is called and continuing after it returns. Thus an activity is in the killable
418 * state, for example, between after <code>onPause()</code> to the start of
419 * <code>onResume()</code>.</p>
420 *
421 * <a name="ConfigurationChanges"></a>
422 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700423 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 * <p>If the configuration of the device (as defined by the
425 * {@link Configuration Resources.Configuration} class) changes,
426 * then anything displaying a user interface will need to update to match that
427 * configuration. Because Activity is the primary mechanism for interacting
428 * with the user, it includes special support for handling configuration
429 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700430 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 * <p>Unless you specify otherwise, a configuration change (such as a change
432 * in screen orientation, language, input devices, etc) will cause your
433 * current activity to be <em>destroyed</em>, going through the normal activity
434 * lifecycle process of {@link #onPause},
435 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
436 * had been in the foreground or visible to the user, once {@link #onDestroy} is
437 * called in that instance then a new instance of the activity will be
438 * created, with whatever savedInstanceState the previous instance had generated
439 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700440 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 * <p>This is done because any application resource,
442 * including layout files, can change based on any configuration value. Thus
443 * the only safe way to handle a configuration change is to re-retrieve all
444 * resources, including layouts, drawables, and strings. Because activities
445 * must already know how to save their state and re-create themselves from
446 * that state, this is a convenient way to have an activity restart itself
447 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700448 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 * <p>In some special cases, you may want to bypass restarting of your
450 * activity based on one or more types of configuration changes. This is
451 * done with the {@link android.R.attr#configChanges android:configChanges}
452 * attribute in its manifest. For any types of configuration changes you say
453 * that you handle there, you will receive a call to your current activity's
454 * {@link #onConfigurationChanged} method instead of being restarted. If
455 * a configuration change involves any that you do not handle, however, the
456 * activity will still be restarted and {@link #onConfigurationChanged}
457 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700458 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 * <a name="StartingActivities"></a>
460 * <h3>Starting Activities and Getting Results</h3>
461 *
462 * <p>The {@link android.app.Activity#startActivity}
463 * method is used to start a
464 * new activity, which will be placed at the top of the activity stack. It
465 * takes a single argument, an {@link android.content.Intent Intent},
466 * which describes the activity
467 * to be executed.</p>
468 *
469 * <p>Sometimes you want to get a result back from an activity when it
470 * ends. For example, you may start an activity that lets the user pick
471 * a person in a list of contacts; when it ends, it returns the person
472 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700473 * {@link android.app.Activity#startActivityForResult(Intent, int)}
474 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700476 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 *
478 * <p>When an activity exits, it can call
479 * {@link android.app.Activity#setResult(int)}
480 * to return data back to its parent. It must always supply a result code,
481 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
482 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
483 * return back an Intent containing any additional data it wants. All of this
484 * information appears back on the
485 * parent's <code>Activity.onActivityResult()</code>, along with the integer
486 * identifier it originally supplied.</p>
487 *
488 * <p>If a child activity fails for any reason (such as crashing), the parent
489 * activity will receive a result with the code RESULT_CANCELED.</p>
490 *
491 * <pre class="prettyprint">
492 * public class MyActivity extends Activity {
493 * ...
494 *
495 * static final int PICK_CONTACT_REQUEST = 0;
496 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700497 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
499 * // When the user center presses, let them pick a contact.
500 * startActivityForResult(
501 * new Intent(Intent.ACTION_PICK,
502 * new Uri("content://contacts")),
503 * PICK_CONTACT_REQUEST);
504 * return true;
505 * }
506 * return false;
507 * }
508 *
509 * protected void onActivityResult(int requestCode, int resultCode,
510 * Intent data) {
511 * if (requestCode == PICK_CONTACT_REQUEST) {
512 * if (resultCode == RESULT_OK) {
513 * // A contact was picked. Here we will just display it
514 * // to the user.
515 * startActivity(new Intent(Intent.ACTION_VIEW, data));
516 * }
517 * }
518 * }
519 * }
520 * </pre>
521 *
522 * <a name="SavingPersistentState"></a>
523 * <h3>Saving Persistent State</h3>
524 *
525 * <p>There are generally two kinds of persistent state than an activity
526 * will deal with: shared document-like data (typically stored in a SQLite
527 * database using a {@linkplain android.content.ContentProvider content provider})
528 * and internal state such as user preferences.</p>
529 *
530 * <p>For content provider data, we suggest that activities use a
531 * "edit in place" user model. That is, any edits a user makes are effectively
532 * made immediately without requiring an additional confirmation step.
533 * Supporting this model is generally a simple matter of following two rules:</p>
534 *
535 * <ul>
536 * <li> <p>When creating a new document, the backing database entry or file for
537 * it is created immediately. For example, if the user chooses to write
538 * a new e-mail, a new entry for that e-mail is created as soon as they
539 * start entering data, so that if they go to any other activity after
540 * that point this e-mail will now appear in the list of drafts.</p>
541 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
542 * commit to the backing content provider or file any changes the user
543 * has made. This ensures that those changes will be seen by any other
544 * activity that is about to run. You will probably want to commit
545 * your data even more aggressively at key times during your
546 * activity's lifecycle: for example before starting a new
547 * activity, before finishing your own activity, when the user
548 * switches between input fields, etc.</p>
549 * </ul>
550 *
551 * <p>This model is designed to prevent data loss when a user is navigating
552 * between activities, and allows the system to safely kill an activity (because
553 * system resources are needed somewhere else) at any time after it has been
554 * paused. Note this implies
555 * that the user pressing BACK from your activity does <em>not</em>
556 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800557 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
559 *
560 * <p>See the {@linkplain android.content.ContentProvider content package} for
561 * more information about content providers. These are a key aspect of how
562 * different activities invoke and propagate data between themselves.</p>
563 *
564 * <p>The Activity class also provides an API for managing internal persistent state
565 * associated with an activity. This can be used, for example, to remember
566 * the user's preferred initial display in a calendar (day view or week view)
567 * or the user's default home page in a web browser.</p>
568 *
569 * <p>Activity persistent state is managed
570 * with the method {@link #getPreferences},
571 * allowing you to retrieve and
572 * modify a set of name/value pairs associated with the activity. To use
573 * preferences that are shared across multiple application components
574 * (activities, receivers, services, providers), you can use the underlying
575 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
576 * to retrieve a preferences
577 * object stored under a specific name.
578 * (Note that it is not possible to share settings data across application
579 * packages -- for that you will need a content provider.)</p>
580 *
581 * <p>Here is an excerpt from a calendar activity that stores the user's
582 * preferred view mode in its persistent settings:</p>
583 *
584 * <pre class="prettyprint">
585 * public class CalendarActivity extends Activity {
586 * ...
587 *
588 * static final int DAY_VIEW_MODE = 0;
589 * static final int WEEK_VIEW_MODE = 1;
590 *
591 * private SharedPreferences mPrefs;
592 * private int mCurViewMode;
593 *
594 * protected void onCreate(Bundle savedInstanceState) {
595 * super.onCreate(savedInstanceState);
596 *
597 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700598 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 * }
600 *
601 * protected void onPause() {
602 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700603 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 * SharedPreferences.Editor ed = mPrefs.edit();
605 * ed.putInt("view_mode", mCurViewMode);
606 * ed.commit();
607 * }
608 * }
609 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700610 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * <a name="Permissions"></a>
612 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700613 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * <p>The ability to start a particular Activity can be enforced when it is
615 * declared in its
616 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
617 * tag. By doing so, other applications will need to declare a corresponding
618 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
619 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800620 *
621 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
622 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
623 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
624 * Activity access to the specific URIs in the Intent. Access will remain
625 * until the Activity has finished (it will remain across the hosting
626 * process being killed and other temporary destruction). As of
627 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
628 * was already created and a new Intent is being delivered to
629 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
630 * to the existing ones it holds.
631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
633 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700634 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 * <a name="ProcessLifecycle"></a>
636 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700637 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 * <p>The Android system attempts to keep application process around for as
639 * long as possible, but eventually will need to remove old processes when
640 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
641 * Lifecycle</a>, the decision about which process to remove is intimately
642 * tied to the state of the user's interaction with it. In general, there
643 * are four states a process can be in based on the activities running in it,
644 * listed here in order of importance. The system will kill less important
645 * processes (the last ones) before it resorts to killing more important
646 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700647 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * <ol>
649 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
650 * that the user is currently interacting with) is considered the most important.
651 * Its process will only be killed as a last resort, if it uses more memory
652 * than is available on the device. Generally at this point the device has
653 * reached a memory paging state, so this is required in order to keep the user
654 * interface responsive.
655 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
656 * but not in the foreground, such as one sitting behind a foreground dialog)
657 * is considered extremely important and will not be killed unless that is
658 * required to keep the foreground activity running.
659 * <li> <p>A <b>background activity</b> (an activity that is not visible to
660 * the user and has been paused) is no longer critical, so the system may
661 * safely kill its process to reclaim memory for other foreground or
662 * visible processes. If its process needs to be killed, when the user navigates
663 * back to the activity (making it visible on the screen again), its
664 * {@link #onCreate} method will be called with the savedInstanceState it had previously
665 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
666 * state as the user last left it.
667 * <li> <p>An <b>empty process</b> is one hosting no activities or other
668 * application components (such as {@link Service} or
669 * {@link android.content.BroadcastReceiver} classes). These are killed very
670 * quickly by the system as memory becomes low. For this reason, any
671 * background operation you do outside of an activity must be executed in the
672 * context of an activity BroadcastReceiver or Service to ensure that the system
673 * knows it needs to keep your process around.
674 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700675 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * <p>Sometimes an Activity may need to do a long-running operation that exists
677 * independently of the activity lifecycle itself. An example may be a camera
678 * application that allows you to upload a picture to a web site. The upload
679 * may take a long time, and the application should allow the user to leave
680 * the application will it is executing. To accomplish this, your Activity
681 * should start a {@link Service} in which the upload takes place. This allows
682 * the system to properly prioritize your process (considering it to be more
683 * important than other non-visible applications) for the duration of the
684 * upload, independent of whether the original activity is paused, stopped,
685 * or finished.
686 */
687public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700688 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700690 OnCreateContextMenuListener, ComponentCallbacks2,
Skuhnece2faa52015-08-11 10:36:38 -0700691 Window.OnWindowDismissedCallback, WindowControllerCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700693 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694
695 /** Standard activity result: operation canceled. */
696 public static final int RESULT_CANCELED = 0;
697 /** Standard activity result: operation succeeded. */
698 public static final int RESULT_OK = -1;
699 /** Start of user-defined activity results. */
700 public static final int RESULT_FIRST_USER = 1;
701
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700702 /** @hide Task isn't finished when activity is finished */
703 public static final int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700704 /**
705 * @hide Task is finished if the finishing activity is the root of the task. To preserve the
706 * past behavior the task is also removed from recents.
707 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700708 public static final int FINISH_TASK_WITH_ROOT_ACTIVITY = 1;
Filip Gruszczynskic29ecc82015-10-08 11:08:44 -0700709 /**
710 * @hide Task is finished along with the finishing activity, but it is not removed from
711 * recents.
712 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700713 public static final int FINISH_TASK_WITH_ACTIVITY = 2;
714
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700715 static final String FRAGMENTS_TAG = "android:fragments";
716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
718 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
719 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
720 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800721 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
Svetoslavffb32b12015-10-15 16:54:00 -0700722 private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
723 "android:hasCurrentPermissionsRequest";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724
Svetoslav970b59c2015-06-09 16:05:21 -0700725 private static final String REQUEST_PERMISSIONS_WHO_PREFIX = "@android:requestPermissions:";
726
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800727 private static class ManagedDialog {
728 Dialog mDialog;
729 Bundle mArgs;
730 }
731 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732
733 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
734 private Instrumentation mInstrumentation;
735 private IBinder mToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700736 private int mIdent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 /*package*/ String mEmbeddedID;
738 private Application mApplication;
Christopher Tateb70f3df2009-04-07 16:07:59 -0700739 /*package*/ Intent mIntent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800740 /*package*/ String mReferrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 private ComponentName mComponent;
742 /*package*/ ActivityInfo mActivityInfo;
743 /*package*/ ActivityThread mMainThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 Activity mParent;
745 boolean mCalled;
Jeff Hamilton52d32032011-01-08 15:31:26 -0600746 /*package*/ boolean mResumed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 private boolean mStopped;
748 boolean mFinished;
749 boolean mStartedActivity;
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700750 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700751 private boolean mDoReportFullyDrawn = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700752 /** true if the activity is going through a transient pause */
753 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500754 /** true if the activity is being destroyed in order to recreate it with a new configuration */
755 /*package*/ boolean mChangingConfigurations = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 /*package*/ int mConfigChangeFlags;
757 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100758 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700759 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700761 static final class NonConfigurationInstances {
762 Object activity;
763 HashMap<String, Object> children;
Adam Powell44ba79e2016-02-04 16:20:37 -0800764 FragmentManagerNonConfig fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700765 ArrayMap<String, LoaderManager> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700766 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700767 }
768 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 private Window mWindow;
771
772 private WindowManager mWindowManager;
773 /*package*/ View mDecor = null;
774 /*package*/ boolean mWindowAdded = false;
775 /*package*/ boolean mVisibleFromServer = false;
776 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700777 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700778 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779
Dianne Hackborn91097de2014-04-04 18:02:06 -0700780 private VoiceInteractor mVoiceInteractor;
781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 private CharSequence mTitle;
783 private int mTitleColor = 0;
784
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700785 // we must have a handler before the FragmentController is constructed
786 final Handler mHandler = new Handler();
787 final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
Craig Mautneree2e45a2014-06-27 12:10:03 -0700788
Jose Lima4b6c6692014-08-12 17:41:12 -0700789 // Most recent call to requestVisibleBehind().
790 boolean mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 private static final class ManagedCursor {
793 ManagedCursor(Cursor cursor) {
794 mCursor = cursor;
795 mReleased = false;
796 mUpdated = false;
797 }
798
799 private final Cursor mCursor;
800 private boolean mReleased;
801 private boolean mUpdated;
802 }
803 private final ArrayList<ManagedCursor> mManagedCursors =
804 new ArrayList<ManagedCursor>();
805
RoboErik55011652014-07-09 15:05:53 -0700806 // protected by synchronized (this)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 int mResultCode = RESULT_CANCELED;
808 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700809
Craig Mautner5eda9b32013-07-02 11:58:16 -0700810 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700811 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700813 private SearchEvent mSearchEvent;
814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000816 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817
818 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
819 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700820
Winsonb6403152016-02-23 13:32:09 -0800821 private ActivityManager.TaskDescription mTaskDescription =
822 new ActivityManager.TaskDescription();
823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
825
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700826 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700827 private final Object mInstanceTracker = StrictMode.trackActivity(this);
828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 private Thread mUiThread;
George Mount62ab9b72014-05-02 13:51:17 -0700830
George Mount1fecfb22014-06-18 14:55:55 -0700831 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700832 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
833 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834
Svetoslavffb32b12015-10-15 16:54:00 -0700835 private boolean mHasCurrentPermissionsRequest;
George Mount5beb26172015-12-15 13:36:01 -0800836 private boolean mEatKeyUpEvent;
Svetoslavffb32b12015-10-15 16:54:00 -0700837
Dimitry Ivanov4449ef52016-02-25 17:41:13 -0800838 private static native String getDlWarning();
839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 /** Return the intent that started this activity. */
841 public Intent getIntent() {
842 return mIntent;
843 }
844
RoboErik55011652014-07-09 15:05:53 -0700845 /**
846 * Change the intent returned by {@link #getIntent}. This holds a
847 * reference to the given intent; it does not copy it. Often used in
848 * conjunction with {@link #onNewIntent}.
849 *
850 * @param newIntent The new Intent object to return from getIntent
851 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 * @see #getIntent
853 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700854 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 public void setIntent(Intent newIntent) {
856 mIntent = newIntent;
857 }
858
859 /** Return the application that owns this activity. */
860 public final Application getApplication() {
861 return mApplication;
862 }
863
864 /** Is this activity embedded inside of another activity? */
865 public final boolean isChild() {
866 return mParent != null;
867 }
RoboErik55011652014-07-09 15:05:53 -0700868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 /** Return the parent activity if this view is an embedded child. */
870 public final Activity getParent() {
871 return mParent;
872 }
873
874 /** Retrieve the window manager for showing custom windows. */
875 public WindowManager getWindowManager() {
876 return mWindowManager;
877 }
878
879 /**
880 * Retrieve the current {@link android.view.Window} for the activity.
881 * This can be used to directly access parts of the Window API that
882 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700883 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 * @return Window The current window, or null if the activity is not
885 * visual.
886 */
887 public Window getWindow() {
888 return mWindow;
889 }
890
891 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800892 * Return the LoaderManager for this activity, creating it if needed.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700893 */
894 public LoaderManager getLoaderManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700895 return mFragments.getLoaderManager();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700896 }
RoboErik55011652014-07-09 15:05:53 -0700897
Dianne Hackbornc8017682010-07-06 13:34:38 -0700898 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 * Calls {@link android.view.Window#getCurrentFocus} on the
900 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -0700901 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -0700903 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 * @see #getWindow
905 * @see android.view.Window#getCurrentFocus
906 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700907 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 public View getCurrentFocus() {
909 return mWindow != null ? mWindow.getCurrentFocus() : null;
910 }
911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 /**
913 * Called when the activity is starting. This is where most initialization
914 * should go: calling {@link #setContentView(int)} to inflate the
915 * activity's UI, using {@link #findViewById} to programmatically interact
916 * with widgets in the UI, calling
917 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
918 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -0700919 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 * <p>You can call {@link #finish} from within this function, in
921 * which case onDestroy() will be immediately called without any of the rest
922 * of the activity lifecycle ({@link #onStart}, {@link #onResume},
923 * {@link #onPause}, etc) executing.
RoboErik55011652014-07-09 15:05:53 -0700924 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 * <p><em>Derived classes must call through to the super class's
926 * implementation of this method. If they do not, an exception will be
927 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -0700928 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 * @param savedInstanceState If the activity is being re-initialized after
930 * previously being shut down then this Bundle contains the data it most
931 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -0700932 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 * @see #onStart
934 * @see #onSaveInstanceState
935 * @see #onRestoreInstanceState
936 * @see #onPostCreate
937 */
Tor Norbye83c68962015-03-10 20:55:31 -0700938 @MainThread
Tor Norbyec615c6f2015-03-02 10:11:44 -0800939 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -0700940 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700941 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700942 if (mLastNonConfigurationInstances != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700943 mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700944 }
Adam Powelldd8fab22012-03-22 17:47:27 -0700945 if (mActivityInfo.parentActivityName != null) {
946 if (mActionBar == null) {
947 mEnableDefaultActionBarUp = true;
948 } else {
949 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
950 }
951 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700952 if (savedInstanceState != null) {
953 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
954 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
955 ? mLastNonConfigurationInstances.fragments : null);
956 }
957 mFragments.dispatchCreate();
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700958 getApplication().dispatchActivityCreated(this, savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -0700959 if (mVoiceInteractor != null) {
960 mVoiceInteractor.attachActivity(this);
961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 mCalled = true;
963 }
964
965 /**
Craig Mautnera0026042014-04-23 11:45:37 -0700966 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -0700967 * the attribute {@link android.R.attr#persistableMode} set to
968 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -0700969 *
970 * @param savedInstanceState if the activity is being re-initialized after
971 * previously being shut down then this Bundle contains the data it most
972 * recently supplied in {@link #onSaveInstanceState}.
973 * <b><i>Note: Otherwise it is null.</i></b>
974 * @param persistentState if the activity is being re-initialized after
975 * previously being shut down or powered off then this Bundle contains the data it most
976 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
977 * <b><i>Note: Otherwise it is null.</i></b>
978 *
979 * @see #onCreate(android.os.Bundle)
980 * @see #onStart
981 * @see #onSaveInstanceState
982 * @see #onRestoreInstanceState
983 * @see #onPostCreate
984 */
Craig Mautner7a629c22014-09-04 14:57:04 -0700985 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -0700986 @Nullable PersistableBundle persistentState) {
987 onCreate(savedInstanceState);
988 }
989
990 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 * The hook for {@link ActivityThread} to restore the state of this activity.
992 *
993 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
994 * {@link #restoreManagedDialogs(android.os.Bundle)}.
995 *
996 * @param savedInstanceState contains the saved state
997 */
998 final void performRestoreInstanceState(Bundle savedInstanceState) {
999 onRestoreInstanceState(savedInstanceState);
1000 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
1002
1003 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001004 * The hook for {@link ActivityThread} to restore the state of this activity.
1005 *
1006 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
1007 * {@link #restoreManagedDialogs(android.os.Bundle)}.
1008 *
1009 * @param savedInstanceState contains the saved state
1010 * @param persistentState contains the persistable saved state
1011 */
1012 final void performRestoreInstanceState(Bundle savedInstanceState,
1013 PersistableBundle persistentState) {
1014 onRestoreInstanceState(savedInstanceState, persistentState);
1015 if (savedInstanceState != null) {
1016 restoreManagedDialogs(savedInstanceState);
1017 }
1018 }
1019
1020 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * This method is called after {@link #onStart} when the activity is
1022 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001023 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 * to restore their state, but it is sometimes convenient to do it here
1025 * after all of the initialization has been done or to allow subclasses to
1026 * decide whether to use your default implementation. The default
1027 * implementation of this method performs a restore of any view state that
1028 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001029 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 * <p>This method is called between {@link #onStart} and
1031 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001032 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 * @see #onCreate
1036 * @see #onPostCreate
1037 * @see #onResume
1038 * @see #onSaveInstanceState
1039 */
1040 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1041 if (mWindow != null) {
1042 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1043 if (windowState != null) {
1044 mWindow.restoreHierarchyState(windowState);
1045 }
1046 }
1047 }
Craig Mautnera0026042014-04-23 11:45:37 -07001048
1049 /**
1050 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001051 * created with the attribute {@link android.R.attr#persistableMode} set to
1052 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1053 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001054 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1055 *
1056 * <p>This method is called between {@link #onStart} and
1057 * {@link #onPostCreate}.
1058 *
1059 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1060 *
1061 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
1062 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}.
1063 *
1064 * @see #onRestoreInstanceState(Bundle)
1065 * @see #onCreate
1066 * @see #onPostCreate
1067 * @see #onResume
1068 * @see #onSaveInstanceState
1069 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001070 public void onRestoreInstanceState(Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001071 PersistableBundle persistentState) {
1072 if (savedInstanceState != null) {
1073 onRestoreInstanceState(savedInstanceState);
1074 }
1075 }
1076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 /**
1078 * Restore the state of any saved managed dialogs.
1079 *
1080 * @param savedInstanceState The bundle to restore from.
1081 */
1082 private void restoreManagedDialogs(Bundle savedInstanceState) {
1083 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1084 if (b == null) {
1085 return;
1086 }
1087
1088 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1089 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001090 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 for (int i = 0; i < numDialogs; i++) {
1092 final Integer dialogId = ids[i];
1093 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1094 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001095 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1096 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001097 final ManagedDialog md = new ManagedDialog();
1098 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1099 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1100 if (md.mDialog != null) {
1101 mManagedDialogs.put(dialogId, md);
1102 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1103 md.mDialog.onRestoreInstanceState(dialogState);
1104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106 }
1107 }
1108
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001109 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1110 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001111 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001112 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001113 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001114 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001115 return dialog;
1116 }
1117
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001118 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 return SAVED_DIALOG_KEY_PREFIX + key;
1120 }
1121
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001122 private static String savedDialogArgsKeyFor(int key) {
1123 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125
1126 /**
1127 * Called when activity start-up is complete (after {@link #onStart}
1128 * and {@link #onRestoreInstanceState} have been called). Applications will
1129 * generally not implement this method; it is intended for system
1130 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001131 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 * <p><em>Derived classes must call through to the super class's
1133 * implementation of this method. If they do not, an exception will be
1134 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001135 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 * @param savedInstanceState If the activity is being re-initialized after
1137 * previously being shut down then this Bundle contains the data it most
1138 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1139 * @see #onCreate
1140 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001141 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001142 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 if (!isChild()) {
1144 mTitleReady = true;
1145 onTitleChanged(getTitle(), getTitleColor());
1146 }
Winsonb6403152016-02-23 13:32:09 -08001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 mCalled = true;
1149 }
1150
1151 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001152 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001153 * created with the attribute {@link android.R.attr#persistableMode} set to
1154 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001155 *
1156 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1157 * @param persistentState The data caming from the PersistableBundle first
1158 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1159 *
1160 * @see #onCreate
1161 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001162 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001163 @Nullable PersistableBundle persistentState) {
1164 onPostCreate(savedInstanceState);
1165 }
1166
1167 /**
RoboErik55011652014-07-09 15:05:53 -07001168 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1169 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001170 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 *
1172 * <p><em>Derived classes must call through to the super class's
1173 * implementation of this method. If they do not, an exception will be
1174 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001175 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 * @see #onCreate
1177 * @see #onStop
1178 * @see #onResume
1179 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001180 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001182 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001184
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001185 mFragments.doLoaderStart();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001186
1187 getApplication().dispatchActivityStarted(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189
1190 /**
1191 * Called after {@link #onStop} when the current activity is being
1192 * re-displayed to the user (the user has navigated back to it). It will
1193 * be followed by {@link #onStart} and then {@link #onResume}.
1194 *
1195 * <p>For activities that are using raw {@link Cursor} objects (instead of
1196 * creating them through
1197 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1198 * this is usually the place
1199 * where the cursor should be requeried (because you had deactivated it in
1200 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001201 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 * <p><em>Derived classes must call through to the super class's
1203 * implementation of this method. If they do not, an exception will be
1204 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001205 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 * @see #onStop
1207 * @see #onStart
1208 * @see #onResume
1209 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001210 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 protected void onRestart() {
1212 mCalled = true;
1213 }
1214
1215 /**
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07001216 * Called when an {@link #onResume} is coming up, prior to other pre-resume callbacks
1217 * such as {@link #onNewIntent} and {@link #onActivityResult}. This is primarily intended
1218 * to give the activity a hint that its state is no longer saved -- it will generally
1219 * be called after {@link #onSaveInstanceState} and prior to the activity being
1220 * resumed/started again.
1221 */
1222 public void onStateNotSaved() {
1223 }
1224
1225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1227 * {@link #onPause}, for your activity to start interacting with the user.
1228 * This is a good place to begin animations, open exclusive-access devices
1229 * (such as the camera), etc.
1230 *
1231 * <p>Keep in mind that onResume is not the best indicator that your activity
1232 * is visible to the user; a system window such as the keyguard may be in
1233 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1234 * activity is visible to the user (for example, to resume a game).
1235 *
1236 * <p><em>Derived classes must call through to the super class's
1237 * implementation of this method. If they do not, an exception will be
1238 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001239 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 * @see #onRestoreInstanceState
1241 * @see #onRestart
1242 * @see #onPostResume
1243 * @see #onPause
1244 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001245 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001247 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001248 getApplication().dispatchActivityResumed(this);
George Mount8cab50a2014-05-15 09:57:17 -07001249 mActivityTransitionState.onResume();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 mCalled = true;
1251 }
1252
1253 /**
1254 * Called when activity resume is complete (after {@link #onResume} has
1255 * been called). Applications will generally not implement this method;
1256 * it is intended for system classes to do final setup after application
1257 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 * <p><em>Derived classes must call through to the super class's
1260 * implementation of this method. If they do not, an exception will be
1261 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001262 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 * @see #onResume
1264 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001265 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 protected void onPostResume() {
1267 final Window win = getWindow();
1268 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001269 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 mCalled = true;
1271 }
1272
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001273 void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
1274 if (voiceInteractor == null) {
1275 mVoiceInteractor = null;
1276 } else {
1277 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
1278 Looper.myLooper());
1279 }
1280 }
1281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001283 * Check whether this activity is running as part of a voice interaction with the user.
1284 * If true, it should perform its interaction with the user through the
1285 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1286 */
1287 public boolean isVoiceInteraction() {
1288 return mVoiceInteractor != null;
1289 }
1290
1291 /**
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001292 * Like {@link #isVoiceInteraction}, but only returns true if this is also the root
1293 * of a voice interaction. That is, returns true if this activity was directly
1294 * started by the voice interaction service as the initiation of a voice interaction.
1295 * Otherwise, for example if it was started by another activity while under voice
1296 * interaction, returns false.
1297 */
1298 public boolean isVoiceInteractionRoot() {
1299 try {
1300 return mVoiceInteractor != null
1301 && ActivityManagerNative.getDefault().isRootVoiceInteraction(mToken);
1302 } catch (RemoteException e) {
1303 }
1304 return false;
1305 }
1306
1307 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001308 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1309 * interact with this activity.
1310 */
1311 public VoiceInteractor getVoiceInteractor() {
1312 return mVoiceInteractor;
1313 }
1314
1315 /**
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001316 * Queries whether the currently enabled voice interaction service supports returning
1317 * a voice interactor for use by the activity. This is valid only for the duration of the
1318 * activity.
1319 *
1320 * @return whether the current voice interaction service supports local voice interaction
1321 */
1322 public boolean isLocalVoiceInteractionSupported() {
1323 try {
1324 return ActivityManagerNative.getDefault().supportsLocalVoiceInteraction();
1325 } catch (RemoteException re) {
1326 }
1327 return false;
1328 }
1329
1330 /**
1331 * Starts a local voice interaction session. When ready,
1332 * {@link #onLocalVoiceInteractionStarted()} is called. You can pass a bundle of private options
1333 * to the registered voice interaction service.
1334 * @param privateOptions a Bundle of private arguments to the current voice interaction service
1335 */
1336 public void startLocalVoiceInteraction(Bundle privateOptions) {
1337 try {
1338 ActivityManagerNative.getDefault().startLocalVoiceInteraction(mToken, privateOptions);
1339 } catch (RemoteException re) {
1340 }
1341 }
1342
1343 /**
1344 * Callback to indicate that {@link #startLocalVoiceInteraction(Bundle)} has resulted in a
1345 * voice interaction session being started. You can now retrieve a voice interactor using
1346 * {@link #getVoiceInteractor()}.
1347 */
1348 public void onLocalVoiceInteractionStarted() {
1349 Log.i(TAG, "onLocalVoiceInteractionStarted! " + getVoiceInteractor());
1350 }
1351
1352 /**
1353 * Callback to indicate that the local voice interaction has stopped for some
1354 * reason.
1355 */
1356 public void onLocalVoiceInteractionStopped() {
1357 Log.i(TAG, "onLocalVoiceInteractionStopped :( " + getVoiceInteractor());
1358 }
1359
1360 /**
1361 * Request to terminate the current voice interaction that was previously started
1362 * using {@link #startLocalVoiceInteraction(Bundle)}.
1363 */
1364 public void stopLocalVoiceInteraction() {
1365 try {
1366 ActivityManagerNative.getDefault().stopLocalVoiceInteraction(mToken);
1367 } catch (RemoteException re) {
1368 }
1369 }
1370
1371 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 * This is called for activities that set launchMode to "singleTop" in
1373 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1374 * flag when calling {@link #startActivity}. In either case, when the
1375 * activity is re-launched while at the top of the activity stack instead
1376 * of a new instance of the activity being started, onNewIntent() will be
1377 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001378 * re-launch it.
1379 *
1380 * <p>An activity will always be paused before receiving a new intent, so
1381 * you can count on {@link #onResume} being called after this method.
1382 *
1383 * <p>Note that {@link #getIntent} still returns the original Intent. You
1384 * can use {@link #setIntent} to update it to this new Intent.
1385 *
1386 * @param intent The new intent that was started for the activity.
1387 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001389 * @see #setIntent
1390 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 */
1392 protected void onNewIntent(Intent intent) {
1393 }
1394
1395 /**
1396 * The hook for {@link ActivityThread} to save the state of this activity.
1397 *
1398 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1399 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1400 *
1401 * @param outState The bundle to save the state to.
1402 */
1403 final void performSaveInstanceState(Bundle outState) {
1404 onSaveInstanceState(outState);
1405 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001406 mActivityTransitionState.saveState(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001407 storeHasCurrentPermissionRequest(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001408 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
1410
1411 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001412 * The hook for {@link ActivityThread} to save the state of this activity.
1413 *
1414 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1415 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1416 *
1417 * @param outState The bundle to save the state to.
1418 * @param outPersistentState The bundle to save persistent state to.
1419 */
1420 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1421 onSaveInstanceState(outState, outPersistentState);
1422 saveManagedDialogs(outState);
Svetoslavffb32b12015-10-15 16:54:00 -07001423 storeHasCurrentPermissionRequest(outState);
Craig Mautnera0026042014-04-23 11:45:37 -07001424 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1425 ", " + outPersistentState);
1426 }
1427
1428 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 * Called to retrieve per-instance state from an activity before being killed
1430 * so that the state can be restored in {@link #onCreate} or
1431 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1432 * will be passed to both).
1433 *
1434 * <p>This method is called before an activity may be killed so that when it
1435 * comes back some time in the future it can restore its state. For example,
1436 * if activity B is launched in front of activity A, and at some point activity
1437 * A is killed to reclaim resources, activity A will have a chance to save the
1438 * current state of its user interface via this method so that when the user
1439 * returns to activity A, the state of the user interface can be restored
1440 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1441 *
1442 * <p>Do not confuse this method with activity lifecycle callbacks such as
1443 * {@link #onPause}, which is always called when an activity is being placed
1444 * in the background or on its way to destruction, or {@link #onStop} which
1445 * is called before destruction. One example of when {@link #onPause} and
1446 * {@link #onStop} is called and not this method is when a user navigates back
1447 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1448 * on B because that particular instance will never be restored, so the
1449 * system avoids calling it. An example when {@link #onPause} is called and
1450 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1451 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1452 * killed during the lifetime of B since the state of the user interface of
1453 * A will stay intact.
1454 *
1455 * <p>The default implementation takes care of most of the UI per-instance
1456 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1457 * view in the hierarchy that has an id, and by saving the id of the currently
1458 * focused view (all of which is restored by the default implementation of
1459 * {@link #onRestoreInstanceState}). If you override this method to save additional
1460 * information not captured by each individual view, you will likely want to
1461 * call through to the default implementation, otherwise be prepared to save
1462 * all of the state of each view yourself.
1463 *
1464 * <p>If called, this method will occur before {@link #onStop}. There are
1465 * no guarantees about whether it will occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001468 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 * @see #onCreate
1470 * @see #onRestoreInstanceState
1471 * @see #onPause
1472 */
1473 protected void onSaveInstanceState(Bundle outState) {
1474 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001475 Parcelable p = mFragments.saveAllState();
1476 if (p != null) {
1477 outState.putParcelable(FRAGMENTS_TAG, p);
1478 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001479 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
1481
1482 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001483 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001484 * created with the attribute {@link android.R.attr#persistableMode} set to
1485 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1486 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1487 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001488 *
1489 * @param outState Bundle in which to place your saved state.
1490 * @param outPersistentState State which will be saved across reboots.
1491 *
1492 * @see #onSaveInstanceState(Bundle)
1493 * @see #onCreate
1494 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1495 * @see #onPause
1496 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001497 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001498 onSaveInstanceState(outState);
1499 }
1500
1501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 * Save the state of any managed dialogs.
1503 *
1504 * @param outState place to store the saved state.
1505 */
1506 private void saveManagedDialogs(Bundle outState) {
1507 if (mManagedDialogs == null) {
1508 return;
1509 }
1510
1511 final int numDialogs = mManagedDialogs.size();
1512 if (numDialogs == 0) {
1513 return;
1514 }
1515
1516 Bundle dialogState = new Bundle();
1517
1518 int[] ids = new int[mManagedDialogs.size()];
1519
1520 // save each dialog's bundle, gather the ids
1521 for (int i = 0; i < numDialogs; i++) {
1522 final int key = mManagedDialogs.keyAt(i);
1523 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001524 final ManagedDialog md = mManagedDialogs.valueAt(i);
1525 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1526 if (md.mArgs != null) {
1527 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 }
1530
1531 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1532 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1533 }
1534
1535
1536 /**
1537 * Called as part of the activity lifecycle when an activity is going into
1538 * the background, but has not (yet) been killed. The counterpart to
1539 * {@link #onResume}.
1540 *
1541 * <p>When activity B is launched in front of activity A, this callback will
1542 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1543 * so be sure to not do anything lengthy here.
1544 *
1545 * <p>This callback is mostly used for saving any persistent state the
1546 * activity is editing, to present a "edit in place" model to the user and
1547 * making sure nothing is lost if there are not enough resources to start
1548 * the new activity without first killing this one. This is also a good
1549 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001550 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * as fast as possible, or to close resources that are exclusive access
1552 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001553 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 * <p>In situations where the system needs more memory it may kill paused
1555 * processes to reclaim resources. Because of this, you should be sure
1556 * that all of your state is saved by the time you return from
1557 * this function. In general {@link #onSaveInstanceState} is used to save
1558 * per-instance state in the activity and this method is used to store
1559 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001560 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 * <p>After receiving this call you will usually receive a following call
1562 * to {@link #onStop} (after the next activity has been resumed and
1563 * displayed), however in some cases there will be a direct call back to
1564 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001565 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 * <p><em>Derived classes must call through to the super class's
1567 * implementation of this method. If they do not, an exception will be
1568 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001569 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 * @see #onResume
1571 * @see #onSaveInstanceState
1572 * @see #onStop
1573 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001574 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001576 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001577 getApplication().dispatchActivityPaused(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 mCalled = true;
1579 }
1580
1581 /**
1582 * Called as part of the activity lifecycle when an activity is about to go
1583 * into the background as the result of user choice. For example, when the
1584 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1585 * when an incoming phone call causes the in-call Activity to be automatically
1586 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1587 * the activity being interrupted. In cases when it is invoked, this method
1588 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001589 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 * <p>This callback and {@link #onUserInteraction} are intended to help
1591 * activities manage status bar notifications intelligently; specifically,
1592 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07001593 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 * @see #onUserInteraction()
1595 */
1596 protected void onUserLeaveHint() {
1597 }
RoboErik55011652014-07-09 15:05:53 -07001598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 /**
1600 * Generate a new thumbnail for this activity. This method is called before
1601 * pausing the activity, and should draw into <var>outBitmap</var> the
1602 * imagery for the desired thumbnail in the dimensions of that bitmap. It
1603 * can use the given <var>canvas</var>, which is configured to draw into the
1604 * bitmap, for rendering if desired.
RoboErik55011652014-07-09 15:05:53 -07001605 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001606 * <p>The default implementation returns fails and does not draw a thumbnail;
1607 * this will result in the platform creating its own thumbnail if needed.
RoboErik55011652014-07-09 15:05:53 -07001608 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 * @param outBitmap The bitmap to contain the thumbnail.
1610 * @param canvas Can be used to render into the bitmap.
RoboErik55011652014-07-09 15:05:53 -07001611 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 * @return Return true if you have drawn into the bitmap; otherwise after
1613 * you return it will be filled with a default thumbnail.
RoboErik55011652014-07-09 15:05:53 -07001614 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 * @see #onCreateDescription
1616 * @see #onSaveInstanceState
1617 * @see #onPause
1618 */
1619 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001620 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 }
1622
1623 /**
1624 * Generate a new description for this activity. This method is called
1625 * before pausing the activity and can, if desired, return some textual
1626 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001627 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 * <p>The default implementation returns null, which will cause you to
1629 * inherit the description from the previous activity. If all activities
1630 * return null, generally the label of the top activity will be used as the
1631 * description.
RoboErik55011652014-07-09 15:05:53 -07001632 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 * @return A description of what the user is doing. It should be short and
1634 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001635 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 * @see #onCreateThumbnail
1637 * @see #onSaveInstanceState
1638 * @see #onPause
1639 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001640 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 public CharSequence onCreateDescription() {
1642 return null;
1643 }
1644
1645 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001646 * This is called when the user is requesting an assist, to build a full
1647 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1648 * application. You can override this method to place into the bundle anything
1649 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001650 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001651 *
1652 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001653 * been registered with {@link Application#registerOnProvideAssistDataListener
1654 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001655 */
1656 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001657 }
1658
1659 /**
1660 * This is called when the user is requesting an assist, to provide references
1661 * to content related to the current activity. Before being called, the
1662 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
1663 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
1664 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
1665 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
1666 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
1667 *
1668 * <p>Custom implementation may adjust the content intent to better reflect the top-level
1669 * context of the activity, and fill in its ClipData with additional content of
1670 * interest that the user is currently viewing. For example, an image gallery application
1671 * that has launched in to an activity allowing the user to swipe through pictures should
1672 * modify the intent to reference the current image they are looking it; such an
1673 * application when showing a list of pictures should add a ClipData that has
1674 * references to all of the pictures currently visible on screen.</p>
1675 *
1676 * @param outContent The assist content to return.
1677 */
1678 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001679 }
1680
Clara Bayarri75e09792015-07-29 16:20:40 +01001681 @Override
1682 public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, Menu menu) {
1683 if (menu == null) {
1684 return;
1685 }
1686 KeyboardShortcutGroup group = null;
1687 int menuSize = menu.size();
1688 for (int i = 0; i < menuSize; ++i) {
1689 final MenuItem item = menu.getItem(i);
1690 final CharSequence title = item.getTitle();
1691 final char alphaShortcut = item.getAlphabeticShortcut();
1692 if (title != null && alphaShortcut != MIN_VALUE) {
1693 if (group == null) {
Clara Bayarriada88b32016-02-02 18:08:01 +00001694 final int resource = mApplication.getApplicationInfo().labelRes;
1695 group = new KeyboardShortcutGroup(resource != 0 ? getString(resource) : null);
Clara Bayarri75e09792015-07-29 16:20:40 +01001696 }
1697 group.addItem(new KeyboardShortcutInfo(
1698 title, alphaShortcut, KeyEvent.META_CTRL_ON));
1699 }
1700 }
1701 if (group != null) {
1702 data.add(group);
1703 }
1704 }
1705
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001706 /**
Dianne Hackborn17f69352015-07-17 18:04:14 -07001707 * Ask to have the current assistant shown to the user. This only works if the calling
1708 * activity is the current foreground activity. It is the same as calling
1709 * {@link android.service.voice.VoiceInteractionService#showSession
1710 * VoiceInteractionService.showSession} and requesting all of the possible context.
1711 * The receiver will always see
1712 * {@link android.service.voice.VoiceInteractionSession#SHOW_SOURCE_APPLICATION} set.
1713 * @return Returns true if the assistant was successfully invoked, else false. For example
1714 * false will be returned if the caller is not the current top activity.
1715 */
1716 public boolean showAssist(Bundle args) {
1717 try {
1718 return ActivityManagerNative.getDefault().showAssistFromActivity(mToken, args);
1719 } catch (RemoteException e) {
1720 }
1721 return false;
1722 }
1723
1724 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 * Called when you are no longer visible to the user. You will next
1726 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1727 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001728 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 * <p>Note that this method may never be called, in low memory situations
1730 * where the system does not have enough memory to keep your activity's
1731 * process running after its {@link #onPause} method is called.
RoboErik55011652014-07-09 15:05:53 -07001732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 * <p><em>Derived classes must call through to the super class's
1734 * implementation of this method. If they do not, an exception will be
1735 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001736 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 * @see #onRestart
1738 * @see #onResume
1739 * @see #onSaveInstanceState
1740 * @see #onDestroy
1741 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001742 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001744 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001745 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001746 mActivityTransitionState.onStop();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001747 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001748 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 mCalled = true;
1750 }
1751
1752 /**
1753 * Perform any final cleanup before an activity is destroyed. This can
1754 * happen either because the activity is finishing (someone called
1755 * {@link #finish} on it, or because the system is temporarily destroying
1756 * this instance of the activity to save space. You can distinguish
1757 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001758 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 * <p><em>Note: do not count on this method being called as a place for
1760 * saving data! For example, if an activity is editing data in a content
1761 * provider, those edits should be committed in either {@link #onPause} or
1762 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1763 * free resources like threads that are associated with an activity, so
1764 * that a destroyed activity does not leave such things around while the
1765 * rest of its application is still running. There are situations where
1766 * the system will simply kill the activity's hosting process without
1767 * calling this method (or any others) in it, so it should not be used to
1768 * do things that are intended to remain around after the process goes
1769 * away.
RoboErik55011652014-07-09 15:05:53 -07001770 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 * <p><em>Derived classes must call through to the super class's
1772 * implementation of this method. If they do not, an exception will be
1773 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001774 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 * @see #onPause
1776 * @see #onStop
1777 * @see #finish
1778 * @see #isFinishing
1779 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001780 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001782 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 mCalled = true;
1784
1785 // dismiss any dialogs we are managing.
1786 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 final int numDialogs = mManagedDialogs.size();
1788 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001789 final ManagedDialog md = mManagedDialogs.valueAt(i);
1790 if (md.mDialog.isShowing()) {
1791 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 }
1793 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001794 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796
1797 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001798 synchronized (mManagedCursors) {
1799 int numCursors = mManagedCursors.size();
1800 for (int i = 0; i < numCursors; i++) {
1801 ManagedCursor c = mManagedCursors.get(i);
1802 if (c != null) {
1803 c.mCursor.close();
1804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001806 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
Amith Yamasani49860442010-03-17 20:54:10 -07001808
1809 // Close any open search dialog
1810 if (mSearchManager != null) {
1811 mSearchManager.stopSearch();
1812 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001813
Chris Banes21b25772016-01-04 20:41:59 +00001814 if (mActionBar != null) {
1815 mActionBar.onDestroy();
1816 }
1817
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001818 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
1820
1821 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001822 * Report to the system that your app is now fully drawn, purely for diagnostic
1823 * purposes (calling it does not impact the visible behavior of the activity).
1824 * This is only used to help instrument application launch times, so that the
1825 * app can report when it is fully in a usable state; without this, the only thing
1826 * the system itself can determine is the point at which the activity's window
1827 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001828 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001829 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001830 * entirely drawn your UI and populated with all of the significant data. You
1831 * can safely call this method any time after first launch as well, in which case
1832 * it will simply be ignored.
1833 */
1834 public void reportFullyDrawn() {
1835 if (mDoReportFullyDrawn) {
1836 mDoReportFullyDrawn = false;
1837 try {
1838 ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken);
1839 } catch (RemoteException e) {
1840 }
1841 }
1842 }
1843
1844 /**
Wale Ogunwale5f986092015-12-04 15:35:38 -08001845 * Called by the system when the activity changes from fullscreen mode to multi-window mode and
1846 * visa-versa.
1847 * @see android.R.attr#resizeableActivity
1848 *
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001849 * @param inMultiWindow True if the activity is in multi-window mode.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001850 */
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001851 @CallSuper
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001852 public void onMultiWindowChanged(boolean inMultiWindow) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001853 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001854 "onMultiWindowChanged " + this + ": " + inMultiWindow);
Wale Ogunwale7c796812016-01-29 21:13:50 -08001855 mFragments.dispatchMultiWindowChanged(inMultiWindow);
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001856 if (mWindow != null) {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001857 mWindow.onMultiWindowChanged();
Wale Ogunwaleeb6722c2015-12-08 11:43:43 -08001858 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001859 }
1860
1861 /**
1862 * Returns true if the activity is currently in multi-window mode.
1863 * @see android.R.attr#resizeableActivity
1864 *
1865 * @return True if the activity is in multi-window mode.
1866 */
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001867 public boolean inMultiWindow() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001868 try {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001869 return ActivityManagerNative.getDefault().inMultiWindow(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001870 } catch (RemoteException e) {
1871 }
1872 return false;
1873 }
1874
1875 /**
1876 * Called by the system when the activity changes to and from picture-in-picture mode.
1877 * @see android.R.attr#supportsPictureInPicture
1878 *
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001879 * @param inPictureInPicture True if the activity is in picture-in-picture mode.
Wale Ogunwale5f986092015-12-04 15:35:38 -08001880 */
Wale Ogunwale7c796812016-01-29 21:13:50 -08001881 @CallSuper
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001882 public void onPictureInPictureChanged(boolean inPictureInPicture) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001883 if (DEBUG_LIFECYCLE) Slog.v(TAG,
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001884 "onPictureInPictureChanged " + this + ": " + inPictureInPicture);
Wale Ogunwale7c796812016-01-29 21:13:50 -08001885 mFragments.dispatchPictureInPictureChanged(inPictureInPicture);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001886 }
1887
1888 /**
1889 * Returns true if the activity is currently in picture-in-picture mode.
1890 * @see android.R.attr#supportsPictureInPicture
1891 *
1892 * @return True if the activity is in picture-in-picture mode.
1893 */
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001894 public boolean inPictureInPicture() {
Wale Ogunwale5f986092015-12-04 15:35:38 -08001895 try {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001896 return ActivityManagerNative.getDefault().inPictureInPicture(mToken);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001897 } catch (RemoteException e) {
1898 }
1899 return false;
1900 }
1901
1902 /**
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001903 * Puts the activity in picture-in-picture mode.
1904 * @see android.R.attr#supportsPictureInPicture
1905 */
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001906 public void enterPictureInPicture() {
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001907 try {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001908 ActivityManagerNative.getDefault().enterPictureInPicture(mToken);
Wale Ogunwale9c604c72015-12-06 18:42:57 -08001909 } catch (RemoteException e) {
1910 }
1911 }
1912
1913 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 * Called by the system when the device configuration changes while your
1915 * activity is running. Note that this will <em>only</em> be called if
1916 * you have selected configurations you would like to handle with the
1917 * {@link android.R.attr#configChanges} attribute in your manifest. If
1918 * any configuration change occurs that is not selected to be reported
1919 * by that attribute, then instead of reporting it the system will stop
1920 * and restart the activity (to have it launched with the new
1921 * configuration).
RoboErik55011652014-07-09 15:05:53 -07001922 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 * <p>At the time that this function has been called, your Resources
1924 * object will have been updated to return resource values matching the
1925 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07001926 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 * @param newConfig The new device configuration.
1928 */
1929 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001930 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01001932
Dianne Hackborn9d071802010-12-08 14:49:15 -08001933 mFragments.dispatchConfigurationChanged(newConfig);
1934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 if (mWindow != null) {
1936 // Pass the configuration changed event to the window
1937 mWindow.onConfigurationChanged(newConfig);
1938 }
Adam Powell45c0b192011-07-28 15:11:57 -07001939
1940 if (mActionBar != null) {
1941 // Do this last; the action bar will need to access
1942 // view changes from above.
1943 mActionBar.onConfigurationChanged(newConfig);
1944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
RoboErik55011652014-07-09 15:05:53 -07001946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 /**
1948 * If this activity is being destroyed because it can not handle a
1949 * configuration parameter being changed (and thus its
1950 * {@link #onConfigurationChanged(Configuration)} method is
1951 * <em>not</em> being called), then you can use this method to discover
1952 * the set of changes that have occurred while in the process of being
1953 * destroyed. Note that there is no guarantee that these will be
1954 * accurate (other changes could have happened at any time), so you should
1955 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07001956 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 * @return Returns a bit field of the configuration parameters that are
1958 * changing, as defined by the {@link android.content.res.Configuration}
1959 * class.
1960 */
1961 public int getChangingConfigurations() {
1962 return mConfigChangeFlags;
1963 }
RoboErik55011652014-07-09 15:05:53 -07001964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 /**
1966 * Retrieve the non-configuration instance data that was previously
1967 * returned by {@link #onRetainNonConfigurationInstance()}. This will
1968 * be available from the initial {@link #onCreate} and
1969 * {@link #onStart} calls to the new instance, allowing you to extract
1970 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07001971 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 * <p>Note that the data you retrieve here should <em>only</em> be used
1973 * as an optimization for handling configuration changes. You should always
1974 * be able to handle getting a null pointer back, and an activity must
1975 * still be able to restore itself to its previous state (through the
1976 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
1977 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07001978 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 * @return Returns the object previously returned by
1980 * {@link #onRetainNonConfigurationInstance()}.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07001981 *
1982 * @deprecated Use the new {@link Fragment} API
1983 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
1984 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001986 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07001987 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001989 return mLastNonConfigurationInstances != null
1990 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 }
RoboErik55011652014-07-09 15:05:53 -07001992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 /**
1994 * Called by the system, as part of destroying an
1995 * activity due to a configuration change, when it is known that a new
1996 * instance will immediately be created for the new configuration. You
1997 * can return any object you like here, including the activity instance
1998 * itself, which can later be retrieved by calling
1999 * {@link #getLastNonConfigurationInstance()} in the new activity
2000 * instance.
RoboErik55011652014-07-09 15:05:53 -07002001 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002002 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2003 * or later, consider instead using a {@link Fragment} with
2004 * {@link Fragment#setRetainInstance(boolean)
2005 * Fragment.setRetainInstance(boolean}.</em>
2006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 * <p>This function is called purely as an optimization, and you must
2008 * not rely on it being called. When it is called, a number of guarantees
2009 * will be made to help optimize configuration switching:
2010 * <ul>
2011 * <li> The function will be called between {@link #onStop} and
2012 * {@link #onDestroy}.
2013 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002014 * created after this one's {@link #onDestroy()} is called. In particular,
2015 * <em>no</em> messages will be dispatched during this time (when the returned
2016 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 * <li> The object you return here will <em>always</em> be available from
2018 * the {@link #getLastNonConfigurationInstance()} method of the following
2019 * activity instance as described there.
2020 * </ul>
RoboErik55011652014-07-09 15:05:53 -07002021 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 * <p>These guarantees are designed so that an activity can use this API
2023 * to propagate extensive state from the old to new activity instance, from
2024 * loaded bitmaps, to network connections, to evenly actively running
2025 * threads. Note that you should <em>not</em> propagate any data that
2026 * may change based on the configuration, including any data loaded from
2027 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07002028 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002029 * <p>The guarantee of no message handling during the switch to the next
2030 * activity simplifies use with active objects. For example if your retained
2031 * state is an {@link android.os.AsyncTask} you are guaranteed that its
2032 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
2033 * not be called from the call here until you execute the next instance's
2034 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
2035 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
2036 * running in a separate thread.)
2037 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 * @return Return any Object holding the desired state to propagate to the
2039 * next activity instance.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002040 *
2041 * @deprecated Use the new {@link Fragment} API
2042 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
2043 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 */
2045 public Object onRetainNonConfigurationInstance() {
2046 return null;
2047 }
RoboErik55011652014-07-09 15:05:53 -07002048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 /**
2050 * Retrieve the non-configuration instance data that was previously
2051 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
2052 * be available from the initial {@link #onCreate} and
2053 * {@link #onStart} calls to the new instance, allowing you to extract
2054 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07002055 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 * <p>Note that the data you retrieve here should <em>only</em> be used
2057 * as an optimization for handling configuration changes. You should always
2058 * be able to handle getting a null pointer back, and an activity must
2059 * still be able to restore itself to its previous state (through the
2060 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
2061 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07002062 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 * @return Returns the object previously returned by
2064 * {@link #onRetainNonConfigurationChildInstances()}
2065 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002066 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002067 HashMap<String, Object> getLastNonConfigurationChildInstances() {
2068 return mLastNonConfigurationInstances != null
2069 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 }
RoboErik55011652014-07-09 15:05:53 -07002071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 /**
2073 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
2074 * it should return either a mapping from child activity id strings to arbitrary objects,
2075 * or null. This method is intended to be used by Activity framework subclasses that control a
2076 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
2077 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
2078 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002079 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
2081 return null;
2082 }
RoboErik55011652014-07-09 15:05:53 -07002083
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002084 NonConfigurationInstances retainNonConfigurationInstances() {
2085 Object activity = onRetainNonConfigurationInstance();
2086 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
Adam Powell44ba79e2016-02-04 16:20:37 -08002087 FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002088 ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
2089 if (activity == null && children == null && fragments == null && loaders == null
Dianne Hackborn20d94742014-05-29 18:35:45 -07002090 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002091 return null;
2092 }
RoboErik55011652014-07-09 15:05:53 -07002093
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002094 NonConfigurationInstances nci = new NonConfigurationInstances();
2095 nci.activity = activity;
2096 nci.children = children;
2097 nci.fragments = fragments;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002098 nci.loaders = loaders;
Dianne Hackborn57dd7372015-07-27 18:11:14 -07002099 if (mVoiceInteractor != null) {
2100 mVoiceInteractor.retainInstance();
2101 nci.voiceInteractor = mVoiceInteractor;
2102 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002103 return nci;
2104 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002107 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08002109 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002111
2112 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002113 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002114 mCalled = true;
2115 mFragments.dispatchTrimMemory(level);
2116 }
2117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002119 * Return the FragmentManager for interacting with fragments associated
2120 * with this activity.
2121 */
2122 public FragmentManager getFragmentManager() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002123 return mFragments.getFragmentManager();
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07002124 }
RoboErik55011652014-07-09 15:05:53 -07002125
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002126 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07002127 * Called when a Fragment is being attached to this activity, immediately
2128 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
2129 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
2130 */
2131 public void onAttachFragment(Fragment fragment) {
2132 }
RoboErik55011652014-07-09 15:05:53 -07002133
Dianne Hackbornc8017682010-07-06 13:34:38 -07002134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 * Wrapper around
2136 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2137 * that gives the resulting {@link Cursor} to call
2138 * {@link #startManagingCursor} so that the activity will manage its
2139 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002140 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002141 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2142 * or later, consider instead using {@link LoaderManager} instead, available
2143 * via {@link #getLoaderManager()}.</em>
2144 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002145 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2146 * this method, because the activity will do that for you at the appropriate time. However, if
2147 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2148 * not</em> automatically close the cursor and, in that case, you must call
2149 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 * @param uri The URI of the content provider to query.
2152 * @param projection List of columns to return.
2153 * @param selection SQL WHERE clause.
2154 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002157 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2159 * @see #startManagingCursor
2160 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05002161 *
2162 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 */
Jason parks6ed50de2010-08-25 10:18:50 -05002164 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002165 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2166 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
2168 if (c != null) {
2169 startManagingCursor(c);
2170 }
2171 return c;
2172 }
2173
2174 /**
2175 * Wrapper around
2176 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
2177 * that gives the resulting {@link Cursor} to call
2178 * {@link #startManagingCursor} so that the activity will manage its
2179 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07002180 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002181 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2182 * or later, consider instead using {@link LoaderManager} instead, available
2183 * via {@link #getLoaderManager()}.</em>
2184 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002185 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
2186 * this method, because the activity will do that for you at the appropriate time. However, if
2187 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
2188 * not</em> automatically close the cursor and, in that case, you must call
2189 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002190 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 * @param uri The URI of the content provider to query.
2192 * @param projection List of columns to return.
2193 * @param selection SQL WHERE clause.
2194 * @param selectionArgs The arguments to selection, if any ?s are pesent
2195 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002196 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002198 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2200 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002201 *
2202 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 */
Jason parks6ed50de2010-08-25 10:18:50 -05002204 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002205 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2206 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2208 if (c != null) {
2209 startManagingCursor(c);
2210 }
2211 return c;
2212 }
2213
2214 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 * This method allows the activity to take care of managing the given
2216 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2217 * That is, when the activity is stopped it will automatically call
2218 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2219 * it will call {@link Cursor#requery} for you. When the activity is
2220 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002221 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002222 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2223 * or later, consider instead using {@link LoaderManager} instead, available
2224 * via {@link #getLoaderManager()}.</em>
2225 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002226 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2227 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2228 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2229 * <em>will not</em> automatically close the cursor and, in that case, you must call
2230 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002231 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2235 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002236 *
2237 * @deprecated Use the new {@link android.content.CursorLoader} class with
2238 * {@link LoaderManager} instead; this is also
2239 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 */
Jason parks6ed50de2010-08-25 10:18:50 -05002241 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 public void startManagingCursor(Cursor c) {
2243 synchronized (mManagedCursors) {
2244 mManagedCursors.add(new ManagedCursor(c));
2245 }
2246 }
2247
2248 /**
2249 * Given a Cursor that was previously given to
2250 * {@link #startManagingCursor}, stop the activity's management of that
2251 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002252 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002253 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002254 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002255 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002256 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002260 *
2261 * @deprecated Use the new {@link android.content.CursorLoader} class with
2262 * {@link LoaderManager} instead; this is also
2263 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 */
Jason parks6ed50de2010-08-25 10:18:50 -05002265 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 public void stopManagingCursor(Cursor c) {
2267 synchronized (mManagedCursors) {
2268 final int N = mManagedCursors.size();
2269 for (int i=0; i<N; i++) {
2270 ManagedCursor mc = mManagedCursors.get(i);
2271 if (mc.mCursor == c) {
2272 mManagedCursors.remove(i);
2273 break;
2274 }
2275 }
2276 }
2277 }
2278
2279 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002280 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2281 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002282 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002284 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 }
2287
2288 /**
2289 * Finds a view that was identified by the id attribute from the XML that
2290 * was processed in {@link #onCreate}.
2291 *
2292 * @return The view if found or null otherwise.
2293 */
Scott Kennedyc0519552015-02-11 15:33:10 -08002294 @Nullable
Tor Norbye7b9c9122013-05-30 16:48:33 -07002295 public View findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 return getWindow().findViewById(id);
2297 }
RoboErik55011652014-07-09 15:05:53 -07002298
Adam Powell33b97432010-04-20 10:01:14 -07002299 /**
2300 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002301 *
Adam Powell33b97432010-04-20 10:01:14 -07002302 * @return The Activity's ActionBar, or null if it does not have one.
2303 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002304 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002305 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002306 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002307 return mActionBar;
2308 }
Adam Powelle43340c2014-03-17 19:10:43 -07002309
2310 /**
2311 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2312 * Activity window.
2313 *
2314 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2315 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2316 * a traditional window decor action bar. The toolbar's menu will be populated with the
2317 * Activity's options menu and the navigation button will be wired through the standard
2318 * {@link android.R.id#home home} menu select action.</p>
2319 *
2320 * <p>In order to use a Toolbar within the Activity's window content the application
2321 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2322 *
Chris Banesc7d6c322016-01-27 14:09:16 +00002323 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
Adam Powelle43340c2014-03-17 19:10:43 -07002324 */
Adam Powell37780142014-06-01 13:31:00 -07002325 public void setActionBar(@Nullable Toolbar toolbar) {
Chris Banes21b25772016-01-04 20:41:59 +00002326 final ActionBar ab = getActionBar();
2327 if (ab instanceof WindowDecorActionBar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002328 throw new IllegalStateException("This Activity already has an action bar supplied " +
2329 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2330 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2331 }
Chris Banes21b25772016-01-04 20:41:59 +00002332
2333 // If we reach here then we're setting a new action bar
2334 // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
Chris Banes601bb992015-06-22 10:05:38 +01002335 mMenuInflater = null;
2336
Chris Banes21b25772016-01-04 20:41:59 +00002337 // If we have an action bar currently, destroy it
2338 if (ab != null) {
2339 ab.onDestroy();
2340 }
2341
Chris Banesc7d6c322016-01-27 14:09:16 +00002342 if (toolbar != null) {
2343 final ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2344 mActionBar = tbab;
2345 mWindow.setCallback(tbab.getWrappedWindowCallback());
2346 } else {
2347 mActionBar = null;
2348 // Re-set the original window callback since we may have already set a Toolbar wrapper
2349 mWindow.setCallback(this);
2350 }
2351
2352 invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002353 }
RoboErik55011652014-07-09 15:05:53 -07002354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 /**
Adam Powell33b97432010-04-20 10:01:14 -07002356 * Creates a new ActionBar, locates the inflated ActionBarView,
2357 * initializes the ActionBar with the view, and sets mActionBar.
2358 */
Adam Powelle43340c2014-03-17 19:10:43 -07002359 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002360 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002361
2362 // Initializing the window decor can change window feature flags.
2363 // Make sure that we have the correct set before performing the test below.
2364 window.getDecorView();
2365
Adam Powell9b4c8042010-08-10 15:36:44 -07002366 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002367 return;
2368 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002369
Adam Powelle43340c2014-03-17 19:10:43 -07002370 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002371 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002372
2373 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2374 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002375 }
RoboErik55011652014-07-09 15:05:53 -07002376
Adam Powell33b97432010-04-20 10:01:14 -07002377 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 * Set the activity content from a layout resource. The resource will be
2379 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08002380 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002382 *
Romain Guy482b34a62011-01-20 10:59:28 -08002383 * @see #setContentView(android.view.View)
2384 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002386 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002388 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 }
2390
2391 /**
2392 * Set the activity content to an explicit view. This view is placed
2393 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002394 * view hierarchy. When calling this method, the layout parameters of the
2395 * specified view are ignored. Both the width and the height of the view are
2396 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2397 * your own layout parameters, invoke
2398 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2399 * instead.
RoboErik55011652014-07-09 15:05:53 -07002400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08002402 *
2403 * @see #setContentView(int)
2404 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 */
2406 public void setContentView(View view) {
2407 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002408 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 }
2410
2411 /**
2412 * Set the activity content to an explicit view. This view is placed
2413 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002414 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002415 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 * @param view The desired content to display.
2417 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08002418 *
2419 * @see #setContentView(android.view.View)
2420 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 */
2422 public void setContentView(View view, ViewGroup.LayoutParams params) {
2423 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002424 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 }
2426
2427 /**
2428 * Add an additional content view to the activity. Added after any existing
2429 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002430 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 * @param view The desired content to display.
2432 * @param params Layout parameters for the view.
2433 */
2434 public void addContentView(View view, ViewGroup.LayoutParams params) {
2435 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002436 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
2438
2439 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002440 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2441 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2442 *
2443 * <p>This method will return non-null after content has been initialized (e.g. by using
2444 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2445 *
2446 * @return This window's content TransitionManager or null if none is set.
2447 */
2448 public TransitionManager getContentTransitionManager() {
2449 return getWindow().getTransitionManager();
2450 }
2451
2452 /**
2453 * Set the {@link TransitionManager} to use for default transitions in this window.
2454 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2455 *
2456 * @param tm The TransitionManager to use for scene changes.
2457 */
2458 public void setContentTransitionManager(TransitionManager tm) {
2459 getWindow().setTransitionManager(tm);
2460 }
2461
2462 /**
2463 * Retrieve the {@link Scene} representing this window's current content.
2464 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2465 *
2466 * <p>This method will return null if the current content is not represented by a Scene.</p>
2467 *
2468 * @return Current Scene being shown or null
2469 */
2470 public Scene getContentScene() {
2471 return getWindow().getContentScene();
2472 }
2473
2474 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002475 * Sets whether this activity is finished when touched outside its window's
2476 * bounds.
2477 */
2478 public void setFinishOnTouchOutside(boolean finish) {
2479 mWindow.setCloseOnTouchOutside(finish);
2480 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002481
2482 /** @hide */
2483 @IntDef({
2484 DEFAULT_KEYS_DISABLE,
2485 DEFAULT_KEYS_DIALER,
2486 DEFAULT_KEYS_SHORTCUT,
2487 DEFAULT_KEYS_SEARCH_LOCAL,
2488 DEFAULT_KEYS_SEARCH_GLOBAL})
2489 @Retention(RetentionPolicy.SOURCE)
2490 @interface DefaultKeyMode {}
2491
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2494 * keys.
RoboErik55011652014-07-09 15:05:53 -07002495 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 * @see #setDefaultKeyMode
2497 */
2498 static public final int DEFAULT_KEYS_DISABLE = 0;
2499 /**
2500 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2501 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002502 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 * @see #setDefaultKeyMode
2504 */
2505 static public final int DEFAULT_KEYS_DIALER = 1;
2506 /**
2507 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2508 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002509 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002511 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 * @see #setDefaultKeyMode
2513 */
2514 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2515 /**
2516 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2517 * will start an application-defined search. (If the application or activity does not
2518 * actually define a search, the the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002519 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002521 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 * @see #setDefaultKeyMode
2523 */
2524 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2525
2526 /**
2527 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2528 * will start a global search (typically web search, but some platforms may define alternate
2529 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002530 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002532 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 * @see #setDefaultKeyMode
2534 */
2535 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2536
2537 /**
2538 * Select the default key handling for this activity. This controls what
2539 * will happen to key events that are not otherwise handled. The default
2540 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2541 * floor. Other modes allow you to launch the dialer
2542 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2543 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002544 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002546 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 * <p>Note that the mode selected here does not impact the default
2548 * handling of system keys, such as the "back" and "menu" keys, and your
2549 * activity and its views always get a first chance to receive and handle
2550 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002551 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002553 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 * @see #DEFAULT_KEYS_DISABLE
2555 * @see #DEFAULT_KEYS_DIALER
2556 * @see #DEFAULT_KEYS_SHORTCUT
2557 * @see #DEFAULT_KEYS_SEARCH_LOCAL
2558 * @see #DEFAULT_KEYS_SEARCH_GLOBAL
2559 * @see #onKeyDown
2560 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002561 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 // Some modes use a SpannableStringBuilder to track & dispatch input events
2565 // This list must remain in sync with the switch in onKeyDown()
2566 switch (mode) {
2567 case DEFAULT_KEYS_DISABLE:
2568 case DEFAULT_KEYS_SHORTCUT:
2569 mDefaultKeySsb = null; // not used in these modes
2570 break;
2571 case DEFAULT_KEYS_DIALER:
2572 case DEFAULT_KEYS_SEARCH_LOCAL:
2573 case DEFAULT_KEYS_SEARCH_GLOBAL:
2574 mDefaultKeySsb = new SpannableStringBuilder();
2575 Selection.setSelection(mDefaultKeySsb,0);
2576 break;
2577 default:
2578 throw new IllegalArgumentException();
2579 }
2580 }
2581
2582 /**
2583 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002584 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 * is inside a TextView will not trigger the event (unless it is a navigation
2586 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002587 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 * <p>If the focused view didn't want this event, this method is called.
2589 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002590 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
2591 * by calling {@link #onBackPressed()}, though the behavior varies based
2592 * on the application compatibility mode: for
2593 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
2594 * it will set up the dispatch to call {@link #onKeyUp} where the action
2595 * will be performed; for earlier applications, it will perform the
2596 * action immediately in on-down, as those versions of the platform
2597 * behaved.
RoboErik55011652014-07-09 15:05:53 -07002598 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002599 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002600 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07002601 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002603 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 * this event and it should continue to be propagated.
2605 * @see #onKeyUp
2606 * @see android.view.KeyEvent
2607 */
2608 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002609 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002610 if (getApplicationInfo().targetSdkVersion
2611 >= Build.VERSION_CODES.ECLAIR) {
2612 event.startTracking();
2613 } else {
2614 onBackPressed();
2615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 return true;
2617 }
RoboErik55011652014-07-09 15:05:53 -07002618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
2620 return false;
2621 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08002622 Window w = getWindow();
2623 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
2624 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
2625 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002626 return true;
2627 }
2628 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 } else {
2630 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
2631 boolean clearSpannable = false;
2632 boolean handled;
2633 if ((event.getRepeatCount() != 0) || event.isSystem()) {
2634 clearSpannable = true;
2635 handled = false;
2636 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002637 handled = TextKeyListener.getInstance().onKeyDown(
2638 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 if (handled && mDefaultKeySsb.length() > 0) {
2640 // something useable has been typed - dispatch it now.
2641
2642 final String str = mDefaultKeySsb.toString();
2643 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07002644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 switch (mDefaultKeyMode) {
2646 case DEFAULT_KEYS_DIALER:
2647 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
2648 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07002649 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 break;
2651 case DEFAULT_KEYS_SEARCH_LOCAL:
2652 startSearch(str, false, null, false);
2653 break;
2654 case DEFAULT_KEYS_SEARCH_GLOBAL:
2655 startSearch(str, false, null, true);
2656 break;
2657 }
2658 }
2659 }
2660 if (clearSpannable) {
2661 mDefaultKeySsb.clear();
2662 mDefaultKeySsb.clearSpans();
2663 Selection.setSelection(mDefaultKeySsb,0);
2664 }
2665 return handled;
2666 }
2667 }
2668
2669 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002670 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
2671 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
2672 * the event).
2673 */
2674 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2675 return false;
2676 }
2677
2678 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002680 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 * is inside a TextView will not trigger the event (unless it is a navigation
2682 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002683 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002684 * <p>The default implementation handles KEYCODE_BACK to stop the activity
2685 * and go back.
RoboErik55011652014-07-09 15:05:53 -07002686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002688 * further, or <code>false</code> to indicate that you have not handled
2689 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 * @see #onKeyDown
2691 * @see KeyEvent
2692 */
2693 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002694 if (getApplicationInfo().targetSdkVersion
2695 >= Build.VERSION_CODES.ECLAIR) {
2696 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
2697 && !event.isCanceled()) {
2698 onBackPressed();
2699 return true;
2700 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 return false;
2703 }
2704
2705 /**
2706 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
2707 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
2708 * the event).
2709 */
2710 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2711 return false;
2712 }
RoboErik55011652014-07-09 15:05:53 -07002713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002715 * Called when the activity has detected the user's press of the back
2716 * key. The default implementation simply finishes the current activity,
2717 * but you can override this to do whatever you want.
2718 */
2719 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07002720 if (mActionBar != null && mActionBar.collapseActionView()) {
2721 return;
2722 }
2723
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002724 if (!mFragments.getFragmentManager().popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07002725 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07002726 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002727 }
Jeff Brown64da12a2011-01-04 19:57:47 -08002728
2729 /**
2730 * Called when a key shortcut event is not handled by any of the views in the Activity.
2731 * Override this method to implement global key shortcuts for the Activity.
2732 * Key shortcuts can also be implemented by setting the
2733 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
2734 *
2735 * @param keyCode The value in event.getKeyCode().
2736 * @param event Description of the key event.
2737 * @return True if the key shortcut was handled.
2738 */
2739 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
Abodunrinwa Tokia04b7ad2015-06-30 17:44:04 -07002740 // Let the Action Bar have a chance at handling the shortcut.
2741 ActionBar actionBar = getActionBar();
2742 return (actionBar != null && actionBar.onKeyShortcut(keyCode, event));
Jeff Brown64da12a2011-01-04 19:57:47 -08002743 }
2744
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 * Called when a touch screen event was not handled by any of the views
2747 * under it. This is most useful to process touch events that happen
2748 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07002749 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07002751 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 * @return Return true if you have consumed the event, false if you haven't.
2753 * The default implementation always returns false.
2754 */
2755 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002756 if (mWindow.shouldCloseOnTouch(this, event)) {
2757 finish();
2758 return true;
2759 }
RoboErik55011652014-07-09 15:05:53 -07002760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 return false;
2762 }
RoboErik55011652014-07-09 15:05:53 -07002763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 /**
2765 * Called when the trackball was moved and not handled by any of the
2766 * views inside of the activity. So, for example, if the trackball moves
2767 * while focus is on a button, you will receive a call here because
2768 * buttons do not normally do anything with trackball events. The call
2769 * here happens <em>before</em> trackball movements are converted to
2770 * DPAD key events, which then get sent back to the view hierarchy, and
2771 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07002772 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07002774 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 * @return Return true if you have consumed the event, false if you haven't.
2776 * The default implementation always returns false.
2777 */
2778 public boolean onTrackballEvent(MotionEvent event) {
2779 return false;
2780 }
Jeff Browncb1404e2011-01-15 18:14:15 -08002781
2782 /**
2783 * Called when a generic motion event was not handled by any of the
2784 * views inside of the activity.
2785 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08002786 * Generic motion events describe joystick movements, mouse hovers, track pad
2787 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08002788 * {@link MotionEvent#getSource() source} of the motion event specifies
2789 * the class of input that was received. Implementations of this method
2790 * must examine the bits in the source before processing the event.
2791 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08002792 * </p><p>
2793 * Generic motion events with source class
2794 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
2795 * are delivered to the view under the pointer. All other generic motion events are
2796 * delivered to the focused view.
2797 * </p><p>
2798 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
2799 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08002800 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08002801 *
2802 * @param event The generic motion event being processed.
2803 *
2804 * @return Return true if you have consumed the event, false if you haven't.
2805 * The default implementation always returns false.
2806 */
2807 public boolean onGenericMotionEvent(MotionEvent event) {
2808 return false;
2809 }
2810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 /**
2812 * Called whenever a key, touch, or trackball event is dispatched to the
2813 * activity. Implement this method if you wish to know that the user has
2814 * interacted with the device in some way while your activity is running.
2815 * This callback and {@link #onUserLeaveHint} are intended to help
2816 * activities manage status bar notifications intelligently; specifically,
2817 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07002818 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
2820 * be accompanied by calls to {@link #onUserInteraction}. This
2821 * ensures that your activity will be told of relevant user activity such
2822 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07002823 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 * <p>Note that this callback will be invoked for the touch down action
2825 * that begins a touch gesture, but may not be invoked for the touch-moved
2826 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07002827 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 * @see #onUserLeaveHint()
2829 */
2830 public void onUserInteraction() {
2831 }
RoboErik55011652014-07-09 15:05:53 -07002832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
2834 // Update window manager if: we have a view, that view is
2835 // attached to its parent (which will be a RootView), and
2836 // this activity is not embedded.
2837 if (mParent == null) {
2838 View decor = mDecor;
2839 if (decor != null && decor.getParent() != null) {
2840 getWindowManager().updateViewLayout(decor, params);
2841 }
2842 }
2843 }
2844
2845 public void onContentChanged() {
2846 }
2847
2848 /**
2849 * Called when the current {@link Window} of the activity gains or loses
2850 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002851 * to the user. The default implementation clears the key tracking
2852 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07002853 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002854 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 * is managed independently of activity lifecycles. As such, while focus
2856 * changes will generally have some relation to lifecycle changes (an
2857 * activity that is stopped will not generally get window focus), you
2858 * should not rely on any particular order between the callbacks here and
2859 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07002860 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 * <p>As a general rule, however, a resumed activity will have window
2862 * focus... unless it has displayed other dialogs or popups that take
2863 * input focus, in which case the activity itself will not have focus
2864 * when the other windows have it. Likewise, the system may display
2865 * system-level windows (such as the status bar notification panel or
2866 * a system alert) which will temporarily take window input focus without
2867 * pausing the foreground activity.
2868 *
2869 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07002870 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 * @see #hasWindowFocus()
2872 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002873 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 */
2875 public void onWindowFocusChanged(boolean hasFocus) {
2876 }
RoboErik55011652014-07-09 15:05:53 -07002877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002879 * Called when the main window associated with the activity has been
2880 * attached to the window manager.
2881 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
2882 * for more information.
2883 * @see View#onAttachedToWindow
2884 */
2885 public void onAttachedToWindow() {
2886 }
RoboErik55011652014-07-09 15:05:53 -07002887
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002888 /**
2889 * Called when the main window associated with the activity has been
2890 * detached from the window manager.
2891 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
2892 * for more information.
2893 * @see View#onDetachedFromWindow
2894 */
2895 public void onDetachedFromWindow() {
2896 }
RoboErik55011652014-07-09 15:05:53 -07002897
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002898 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 * Returns true if this activity's <em>main</em> window currently has window focus.
2900 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07002901 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07002903 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
2905 */
2906 public boolean hasWindowFocus() {
2907 Window w = getWindow();
2908 if (w != null) {
2909 View d = w.getDecorView();
2910 if (d != null) {
2911 return d.hasWindowFocus();
2912 }
2913 }
2914 return false;
2915 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002916
2917 /**
2918 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07002919 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002920 */
Adam Powell117b6952014-05-05 18:14:56 -07002921 @Override
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002922 public void onWindowDismissed(boolean finishTask) {
2923 finish(finishTask ? FINISH_TASK_WITH_ACTIVITY : DONT_FINISH_TASK_WITH_ACTIVITY);
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002924 }
RoboErik55011652014-07-09 15:05:53 -07002925
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002926
Skuhnece2faa52015-08-11 10:36:38 -07002927 /**
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08002928 * Moves the activity from
2929 * {@link android.app.ActivityManager.StackId#FREEFORM_WORKSPACE_STACK_ID} to
2930 * {@link android.app.ActivityManager.StackId#FULLSCREEN_WORKSPACE_STACK_ID} stack.
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002931 *
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002932 * @hide
2933 */
2934 @Override
Filip Gruszczynski411c06f2016-01-07 09:44:44 -08002935 public void exitFreeformMode() throws RemoteException {
2936 ActivityManagerNative.getDefault().exitFreeformMode(mToken);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07002937 }
2938
2939 /** Returns the current stack Id for the window.
2940 * @hide
2941 */
2942 @Override
2943 public int getWindowStackId() throws RemoteException {
2944 return ActivityManagerNative.getDefault().getActivityStackId(mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 }
2946
2947 /**
RoboErik55011652014-07-09 15:05:53 -07002948 * Called to process key events. You can override this to intercept all
2949 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07002951 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07002953 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 * @return boolean Return true if this event was consumed.
2955 */
2956 public boolean dispatchKeyEvent(KeyEvent event) {
2957 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07002958
2959 // Let action bars open menus in response to the menu key prioritized over
2960 // the window handling it
George Mount5beb26172015-12-15 13:36:01 -08002961 final int keyCode = event.getKeyCode();
2962 if (keyCode == KeyEvent.KEYCODE_MENU &&
Adam Powell07a74542014-05-30 15:52:44 -07002963 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
2964 return true;
George Mountcbe28352016-01-06 16:24:26 -08002965 } else if (event.isCtrlPressed() &&
2966 event.getUnicodeChar(event.getMetaState() & ~KeyEvent.META_CTRL_MASK) == '<') {
2967 // Capture the Control-< and send focus to the ActionBar
George Mount5beb26172015-12-15 13:36:01 -08002968 final int action = event.getAction();
2969 if (action == KeyEvent.ACTION_DOWN) {
George Mountcbe28352016-01-06 16:24:26 -08002970 final ActionBar actionBar = getActionBar();
2971 if (actionBar != null && actionBar.isShowing() && actionBar.requestFocus()) {
2972 mEatKeyUpEvent = true;
2973 return true;
George Mount5beb26172015-12-15 13:36:01 -08002974 }
2975 } else if (action == KeyEvent.ACTION_UP && mEatKeyUpEvent) {
2976 mEatKeyUpEvent = false;
2977 return true;
2978 }
Adam Powell07a74542014-05-30 15:52:44 -07002979 }
2980
Dianne Hackborn8d374262009-09-14 21:21:52 -07002981 Window win = getWindow();
2982 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 return true;
2984 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002985 View decor = mDecor;
2986 if (decor == null) decor = win.getDecorView();
2987 return event.dispatch(this, decor != null
2988 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 }
2990
2991 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08002992 * Called to process a key shortcut event.
2993 * You can override this to intercept all key shortcut events before they are
2994 * dispatched to the window. Be sure to call this implementation for key shortcut
2995 * events that should be handled normally.
2996 *
2997 * @param event The key shortcut event.
2998 * @return True if this event was consumed.
2999 */
3000 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3001 onUserInteraction();
3002 if (getWindow().superDispatchKeyShortcutEvent(event)) {
3003 return true;
3004 }
3005 return onKeyShortcut(event.getKeyCode(), event);
3006 }
3007
3008 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 * Called to process touch screen events. You can override this to
3010 * intercept all touch screen events before they are dispatched to the
3011 * window. Be sure to call this implementation for touch screen events
3012 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003013 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07003015 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 * @return boolean Return true if this event was consumed.
3017 */
3018 public boolean dispatchTouchEvent(MotionEvent ev) {
3019 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
3020 onUserInteraction();
3021 }
3022 if (getWindow().superDispatchTouchEvent(ev)) {
3023 return true;
3024 }
3025 return onTouchEvent(ev);
3026 }
RoboErik55011652014-07-09 15:05:53 -07003027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 /**
3029 * Called to process trackball events. You can override this to
3030 * intercept all trackball events before they are dispatched to the
3031 * window. Be sure to call this implementation for trackball events
3032 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07003033 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07003035 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 * @return boolean Return true if this event was consumed.
3037 */
3038 public boolean dispatchTrackballEvent(MotionEvent ev) {
3039 onUserInteraction();
3040 if (getWindow().superDispatchTrackballEvent(ev)) {
3041 return true;
3042 }
3043 return onTrackballEvent(ev);
3044 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003045
Jeff Browncb1404e2011-01-15 18:14:15 -08003046 /**
3047 * Called to process generic motion events. You can override this to
3048 * intercept all generic motion events before they are dispatched to the
3049 * window. Be sure to call this implementation for generic motion events
3050 * that should be handled normally.
3051 *
3052 * @param ev The generic motion event.
3053 *
3054 * @return boolean Return true if this event was consumed.
3055 */
3056 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
3057 onUserInteraction();
3058 if (getWindow().superDispatchGenericMotionEvent(ev)) {
3059 return true;
3060 }
3061 return onGenericMotionEvent(ev);
3062 }
3063
svetoslavganov75986cf2009-05-14 22:28:01 -07003064 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3065 event.setClassName(getClass().getName());
3066 event.setPackageName(getPackageName());
3067
3068 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08003069 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
3070 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07003071 event.setFullScreen(isFullScreen);
3072
3073 CharSequence title = getTitle();
3074 if (!TextUtils.isEmpty(title)) {
3075 event.getText().add(title);
3076 }
3077
3078 return true;
3079 }
3080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 /**
3082 * Default implementation of
3083 * {@link android.view.Window.Callback#onCreatePanelView}
3084 * for activities. This
3085 * simply returns null so that all panel sub-windows will have the default
3086 * menu behavior.
3087 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003088 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 public View onCreatePanelView(int featureId) {
3090 return null;
3091 }
3092
3093 /**
3094 * Default implementation of
3095 * {@link android.view.Window.Callback#onCreatePanelMenu}
3096 * for activities. This calls through to the new
3097 * {@link #onCreateOptionsMenu} method for the
3098 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3099 * so that subclasses of Activity don't need to deal with feature codes.
3100 */
3101 public boolean onCreatePanelMenu(int featureId, Menu menu) {
3102 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003103 boolean show = onCreateOptionsMenu(menu);
3104 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
3105 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 }
3107 return false;
3108 }
3109
3110 /**
3111 * Default implementation of
3112 * {@link android.view.Window.Callback#onPreparePanel}
3113 * for activities. This
3114 * calls through to the new {@link #onPrepareOptionsMenu} method for the
3115 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3116 * panel, so that subclasses of
3117 * Activity don't need to deal with feature codes.
3118 */
3119 public boolean onPreparePanel(int featureId, View view, Menu menu) {
3120 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
3121 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003122 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07003123 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 }
3125 return true;
3126 }
3127
3128 /**
3129 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07003130 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 * @return The default implementation returns true.
3132 */
3133 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08003134 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07003135 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08003136 if (mActionBar != null) {
3137 mActionBar.dispatchMenuVisibilityChanged(true);
3138 } else {
3139 Log.e(TAG, "Tried to open action bar menu with no action bar");
3140 }
Adam Powell8515ee82010-11-30 14:09:55 -08003141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 return true;
3143 }
3144
3145 /**
3146 * Default implementation of
3147 * {@link android.view.Window.Callback#onMenuItemSelected}
3148 * for activities. This calls through to the new
3149 * {@link #onOptionsItemSelected} method for the
3150 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
3151 * panel, so that subclasses of
3152 * Activity don't need to deal with feature codes.
3153 */
3154 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09003155 CharSequence titleCondensed = item.getTitleCondensed();
3156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 switch (featureId) {
3158 case Window.FEATURE_OPTIONS_PANEL:
3159 // Put event logging here so it gets called even if subclass
3160 // doesn't call through to superclass's implmeentation of each
3161 // of these methods below
zobject686a8052012-12-14 21:11:08 +09003162 if(titleCondensed != null) {
3163 EventLog.writeEvent(50000, 0, titleCondensed.toString());
3164 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003165 if (onOptionsItemSelected(item)) {
3166 return true;
3167 }
Adam Powell04d58112012-04-09 10:22:12 -07003168 if (mFragments.dispatchOptionsItemSelected(item)) {
3169 return true;
3170 }
3171 if (item.getItemId() == android.R.id.home && mActionBar != null &&
3172 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
3173 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07003174 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07003175 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07003176 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07003177 }
Adam Powell04d58112012-04-09 10:22:12 -07003178 }
3179 return false;
RoboErik55011652014-07-09 15:05:53 -07003180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09003182 if(titleCondensed != null) {
3183 EventLog.writeEvent(50000, 1, titleCondensed.toString());
3184 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07003185 if (onContextItemSelected(item)) {
3186 return true;
3187 }
3188 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08003189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 default:
3191 return false;
3192 }
3193 }
RoboErik55011652014-07-09 15:05:53 -07003194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 /**
3196 * Default implementation of
3197 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
3198 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
3199 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
3200 * so that subclasses of Activity don't need to deal with feature codes.
3201 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
3202 * {@link #onContextMenuClosed(Menu)} will be called.
3203 */
3204 public void onPanelClosed(int featureId, Menu menu) {
3205 switch (featureId) {
3206 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003207 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 onOptionsMenuClosed(menu);
3209 break;
RoboErik55011652014-07-09 15:05:53 -07003210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 case Window.FEATURE_CONTEXT_MENU:
3212 onContextMenuClosed(menu);
3213 break;
Adam Powell8515ee82010-11-30 14:09:55 -08003214
3215 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07003216 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08003217 mActionBar.dispatchMenuVisibilityChanged(false);
3218 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 }
3220 }
3221
3222 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003223 * Declare that the options menu has changed, so should be recreated.
3224 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
3225 * time it needs to be displayed.
3226 */
3227 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003228 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3229 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003230 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
3231 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003232 }
RoboErik55011652014-07-09 15:05:53 -07003233
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07003234 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 * Initialize the contents of the Activity's standard options menu. You
3236 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07003237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 * <p>This is only called once, the first time the options menu is
3239 * displayed. To update the menu every time it is displayed, see
3240 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07003241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07003243 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
3244 * they will be correctly ordered with application-defined menu items.
3245 * Deriving classes should always call through to the base implementation.
3246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 * <p>You can safely hold on to <var>menu</var> (and any items created
3248 * from it), making modifications to it as desired, until the next
3249 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07003250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 * <p>When you add items to the menu, you can implement the Activity's
3252 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07003253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07003255 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 * @return You must return true for the menu to be displayed;
3257 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 * @see #onPrepareOptionsMenu
3260 * @see #onOptionsItemSelected
3261 */
3262 public boolean onCreateOptionsMenu(Menu menu) {
3263 if (mParent != null) {
3264 return mParent.onCreateOptionsMenu(menu);
3265 }
3266 return true;
3267 }
3268
3269 /**
3270 * Prepare the Screen's standard options menu to be displayed. This is
3271 * called right before the menu is shown, every time it is shown. You can
3272 * use this method to efficiently enable/disable items or otherwise
3273 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07003274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 * <p>The default implementation updates the system menu items based on the
3276 * activity's state. Deriving classes should always call through to the
3277 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07003278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 * @param menu The options menu as last shown or first initialized by
3280 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07003281 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 * @return You must return true for the menu to be displayed;
3283 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 * @see #onCreateOptionsMenu
3286 */
3287 public boolean onPrepareOptionsMenu(Menu menu) {
3288 if (mParent != null) {
3289 return mParent.onPrepareOptionsMenu(menu);
3290 }
3291 return true;
3292 }
3293
3294 /**
3295 * This hook is called whenever an item in your options menu is selected.
3296 * The default implementation simply returns false to have the normal
3297 * processing happen (calling the item's Runnable or sending a message to
3298 * its Handler as appropriate). You can use this method for any items
3299 * for which you would like to do processing without those other
3300 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003301 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003303 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003304 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 * @return boolean Return false to allow normal menu processing to
3308 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 * @see #onCreateOptionsMenu
3311 */
3312 public boolean onOptionsItemSelected(MenuItem item) {
3313 if (mParent != null) {
3314 return mParent.onOptionsItemSelected(item);
3315 }
3316 return false;
3317 }
3318
3319 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003320 * This method is called whenever the user chooses to navigate Up within your application's
3321 * activity hierarchy from the action bar.
3322 *
3323 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3324 * was specified in the manifest for this activity or an activity-alias to it,
3325 * default Up navigation will be handled automatically. If any activity
3326 * along the parent chain requires extra Intent arguments, the Activity subclass
3327 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3328 * to supply those arguments.</p>
3329 *
3330 * <p>See <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
3331 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3332 * from the design guide for more information about navigating within your app.</p>
3333 *
3334 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3335 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3336 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3337 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3338 *
3339 * @return true if Up navigation completed successfully and this Activity was finished,
3340 * false otherwise.
3341 */
3342 public boolean onNavigateUp() {
3343 // Automatically handle hierarchical Up navigation if the proper
3344 // metadata is available.
3345 Intent upIntent = getParentActivityIntent();
3346 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003347 if (mActivityInfo.taskAffinity == null) {
3348 // Activities with a null affinity are special; they really shouldn't
3349 // specify a parent activity intent in the first place. Just finish
3350 // the current activity and call it a day.
3351 finish();
3352 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003353 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003354 onCreateNavigateUpTaskStack(b);
3355 onPrepareNavigateUpTaskStack(b);
3356 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003357
3358 // We can't finishAffinity if we have a result.
3359 // Fall back and simply finish the current activity instead.
3360 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3361 // Tell the developer what's going on to avoid hair-pulling.
3362 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3363 finish();
3364 } else {
3365 finishAffinity();
3366 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003367 } else {
3368 navigateUpTo(upIntent);
3369 }
3370 return true;
3371 }
3372 return false;
3373 }
3374
3375 /**
3376 * This is called when a child activity of this one attempts to navigate up.
3377 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3378 *
3379 * @param child The activity making the call.
3380 */
3381 public boolean onNavigateUpFromChild(Activity child) {
3382 return onNavigateUp();
3383 }
3384
3385 /**
3386 * Define the synthetic task stack that will be generated during Up navigation from
3387 * a different task.
3388 *
3389 * <p>The default implementation of this method adds the parent chain of this activity
3390 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3391 * may choose to override this method to construct the desired task stack in a different
3392 * way.</p>
3393 *
Adam Powellf0195952012-05-02 21:38:54 -07003394 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3395 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3396 * returned by {@link #getParentActivityIntent()}.</p>
3397 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003398 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3399 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3400 *
3401 * @param builder An empty TaskStackBuilder - the application should add intents representing
3402 * the desired task stack
3403 */
3404 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3405 builder.addParentStack(this);
3406 }
3407
3408 /**
3409 * Prepare the synthetic task stack that will be generated during Up navigation
3410 * from a different task.
3411 *
3412 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3413 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3414 * If any extra data should be added to these intents before launching the new task,
3415 * the application should override this method and add that data here.</p>
3416 *
3417 * @param builder A TaskStackBuilder that has been populated with Intents by
3418 * onCreateNavigateUpTaskStack.
3419 */
3420 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3421 }
3422
3423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 * This hook is called whenever the options menu is being closed (either by the user canceling
3425 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003426 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 * @param menu The options menu as last shown or first initialized by
3428 * onCreateOptionsMenu().
3429 */
3430 public void onOptionsMenuClosed(Menu menu) {
3431 if (mParent != null) {
3432 mParent.onOptionsMenuClosed(menu);
3433 }
3434 }
RoboErik55011652014-07-09 15:05:53 -07003435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 /**
3437 * Programmatically opens the options menu. If the options menu is already
3438 * open, this method does nothing.
3439 */
3440 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003441 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3442 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003443 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 }
RoboErik55011652014-07-09 15:05:53 -07003446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 /**
3448 * Progammatically closes the options menu. If the options menu is already
3449 * closed, this method does nothing.
3450 */
3451 public void closeOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003452 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
3453 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 }
3456
3457 /**
3458 * Called when a context menu for the {@code view} is about to be shown.
3459 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3460 * time the context menu is about to be shown and should be populated for
3461 * the view (or item inside the view for {@link AdapterView} subclasses,
3462 * this can be found in the {@code menuInfo})).
3463 * <p>
3464 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3465 * item has been selected.
3466 * <p>
3467 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003468 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 */
3470 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3471 }
3472
3473 /**
3474 * Registers a context menu to be shown for the given view (multiple views
3475 * can show the context menu). This method will set the
3476 * {@link OnCreateContextMenuListener} on the view to this activity, so
3477 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3478 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003479 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 * @see #unregisterForContextMenu(View)
3481 * @param view The view that should show a context menu.
3482 */
3483 public void registerForContextMenu(View view) {
3484 view.setOnCreateContextMenuListener(this);
3485 }
RoboErik55011652014-07-09 15:05:53 -07003486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 /**
3488 * Prevents a context menu to be shown for the given view. This method will remove the
3489 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003490 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 * @see #registerForContextMenu(View)
3492 * @param view The view that should stop showing a context menu.
3493 */
3494 public void unregisterForContextMenu(View view) {
3495 view.setOnCreateContextMenuListener(null);
3496 }
RoboErik55011652014-07-09 15:05:53 -07003497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 /**
3499 * Programmatically opens the context menu for a particular {@code view}.
3500 * The {@code view} should have been added via
3501 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003502 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 * @param view The view to show the context menu for.
3504 */
3505 public void openContextMenu(View view) {
3506 view.showContextMenu();
3507 }
RoboErik55011652014-07-09 15:05:53 -07003508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 /**
3510 * Programmatically closes the most recently opened context menu, if showing.
3511 */
3512 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08003513 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
3514 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 }
RoboErik55011652014-07-09 15:05:53 -07003517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 /**
3519 * This hook is called whenever an item in a context menu is selected. The
3520 * default implementation simply returns false to have the normal processing
3521 * happen (calling the item's Runnable or sending a message to its Handler
3522 * as appropriate). You can use this method for any items for which you
3523 * would like to do processing without those other facilities.
3524 * <p>
3525 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3526 * View that added this menu item.
3527 * <p>
3528 * Derived classes should call through to the base class for it to perform
3529 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003530 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 * @param item The context menu item that was selected.
3532 * @return boolean Return false to allow normal context menu processing to
3533 * proceed, true to consume it here.
3534 */
3535 public boolean onContextItemSelected(MenuItem item) {
3536 if (mParent != null) {
3537 return mParent.onContextItemSelected(item);
3538 }
3539 return false;
3540 }
3541
3542 /**
3543 * This hook is called whenever the context menu is being closed (either by
3544 * the user canceling the menu with the back/menu button, or when an item is
3545 * selected).
RoboErik55011652014-07-09 15:05:53 -07003546 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 * @param menu The context menu that is being closed.
3548 */
3549 public void onContextMenuClosed(Menu menu) {
3550 if (mParent != null) {
3551 mParent.onContextMenuClosed(menu);
3552 }
3553 }
3554
3555 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003556 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003558 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 protected Dialog onCreateDialog(int id) {
3560 return null;
3561 }
3562
3563 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003564 * Callback for creating dialogs that are managed (saved and restored) for you
3565 * by the activity. The default implementation calls through to
3566 * {@link #onCreateDialog(int)} for compatibility.
3567 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003568 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3569 * or later, consider instead using a {@link DialogFragment} instead.</em>
3570 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003571 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3572 * this method the first time, and hang onto it thereafter. Any dialog
3573 * that is created by this method will automatically be saved and restored
3574 * for you, including whether it is showing.
3575 *
3576 * <p>If you would like the activity to manage saving and restoring dialogs
3577 * for you, you should override this method and handle any ids that are
3578 * passed to {@link #showDialog}.
3579 *
3580 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3581 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3582 *
3583 * @param id The id of the dialog.
3584 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3585 * @return The dialog. If you return null, the dialog will not be created.
3586 *
3587 * @see #onPrepareDialog(int, Dialog, Bundle)
3588 * @see #showDialog(int, Bundle)
3589 * @see #dismissDialog(int)
3590 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003591 *
3592 * @deprecated Use the new {@link DialogFragment} class with
3593 * {@link FragmentManager} instead; this is also
3594 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003595 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003596 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003597 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003598 protected Dialog onCreateDialog(int id, Bundle args) {
3599 return onCreateDialog(id);
3600 }
3601
3602 /**
3603 * @deprecated Old no-arguments version of
3604 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
3605 */
3606 @Deprecated
3607 protected void onPrepareDialog(int id, Dialog dialog) {
3608 dialog.setOwnerActivity(this);
3609 }
3610
3611 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003613 * shown. The default implementation calls through to
3614 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07003615 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 * <p>
3617 * Override this if you need to update a managed dialog based on the state
3618 * of the application each time it is shown. For example, a time picker
3619 * dialog might want to be updated with the current time. You should call
3620 * through to the superclass's implementation. The default implementation
3621 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07003622 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 * @param id The id of the managed dialog.
3624 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003625 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3626 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 * @see #showDialog(int)
3628 * @see #dismissDialog(int)
3629 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003630 *
3631 * @deprecated Use the new {@link DialogFragment} class with
3632 * {@link FragmentManager} instead; this is also
3633 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003635 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003636 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
3637 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 }
3639
3640 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003641 * Simple version of {@link #showDialog(int, Bundle)} that does not
3642 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
3643 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003644 *
3645 * @deprecated Use the new {@link DialogFragment} class with
3646 * {@link FragmentManager} instead; this is also
3647 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003648 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003649 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003650 public final void showDialog(int id) {
3651 showDialog(id, null);
3652 }
3653
3654 /**
3655 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 * will be made with the same id the first time this is called for a given
3657 * id. From thereafter, the dialog will be automatically saved and restored.
3658 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003659 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3660 * or later, consider instead using a {@link DialogFragment} instead.</em>
3661 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003662 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 * be made to provide an opportunity to do any timely preparation.
3664 *
3665 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003666 * @param args Arguments to pass through to the dialog. These will be saved
3667 * and restored for you. Note that if the dialog is already created,
3668 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
3669 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08003670 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003671 * @return Returns true if the Dialog was created; false is returned if
3672 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07003673 *
Joe Onorato37296dc2009-07-31 17:58:55 -07003674 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003675 * @see #onCreateDialog(int, Bundle)
3676 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 * @see #dismissDialog(int)
3678 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003679 *
3680 * @deprecated Use the new {@link DialogFragment} class with
3681 * {@link FragmentManager} instead; this is also
3682 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003684 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003685 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003686 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003688 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003690 ManagedDialog md = mManagedDialogs.get(id);
3691 if (md == null) {
3692 md = new ManagedDialog();
3693 md.mDialog = createDialog(id, null, args);
3694 if (md.mDialog == null) {
3695 return false;
3696 }
3697 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 }
RoboErik55011652014-07-09 15:05:53 -07003699
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003700 md.mArgs = args;
3701 onPrepareDialog(id, md.mDialog, args);
3702 md.mDialog.show();
3703 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 }
3705
3706 /**
3707 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
3708 *
3709 * @param id The id of the managed dialog.
3710 *
3711 * @throws IllegalArgumentException if the id was not previously shown via
3712 * {@link #showDialog(int)}.
3713 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003714 * @see #onCreateDialog(int, Bundle)
3715 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 * @see #showDialog(int)
3717 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003718 *
3719 * @deprecated Use the new {@link DialogFragment} class with
3720 * {@link FragmentManager} instead; this is also
3721 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003723 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 public final void dismissDialog(int id) {
3725 if (mManagedDialogs == null) {
3726 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 }
RoboErik55011652014-07-09 15:05:53 -07003728
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003729 final ManagedDialog md = mManagedDialogs.get(id);
3730 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 throw missingDialog(id);
3732 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003733 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 }
3735
3736 /**
3737 * Creates an exception to throw if a user passed in a dialog id that is
3738 * unexpected.
3739 */
3740 private IllegalArgumentException missingDialog(int id) {
3741 return new IllegalArgumentException("no dialog with id " + id + " was ever "
3742 + "shown via Activity#showDialog");
3743 }
3744
3745 /**
3746 * Removes any internal references to a dialog managed by this Activity.
3747 * If the dialog is showing, it will dismiss it as part of the clean up.
3748 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003749 * <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 -08003750 * want to avoid the overhead of saving and restoring it in the future.
3751 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003752 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
3753 * will not throw an exception if you try to remove an ID that does not
3754 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07003755 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 * @param id The id of the managed dialog.
3757 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003758 * @see #onCreateDialog(int, Bundle)
3759 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 * @see #showDialog(int)
3761 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003762 *
3763 * @deprecated Use the new {@link DialogFragment} class with
3764 * {@link FragmentManager} instead; this is also
3765 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003767 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003769 if (mManagedDialogs != null) {
3770 final ManagedDialog md = mManagedDialogs.get(id);
3771 if (md != null) {
3772 md.mDialog.dismiss();
3773 mManagedDialogs.remove(id);
3774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 }
3777
3778 /**
3779 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07003780 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01003781 * <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 -07003782 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01003783 * calling this function is the same as calling
3784 * {@link #startSearch startSearch(null, false, null, false)}, which launches
3785 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07003786 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 * <p>You can override this function to force global search, e.g. in response to a dedicated
3788 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07003789 *
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003790 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default
3791 * implementation changes to simply return false and you must supply your own custom
3792 * implementation if you want to support search.</p>
3793 *
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003794 * @param searchEvent The {@link SearchEvent} that signaled this search.
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003795 * @return Returns {@code true} if search launched, and {@code false} if the activity does
3796 * not respond to search. The default implementation always returns {@code true}, except
3797 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07003798 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 * @see android.app.SearchManager
3800 */
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003801 public boolean onSearchRequested(@Nullable SearchEvent searchEvent) {
3802 mSearchEvent = searchEvent;
3803 boolean result = onSearchRequested();
3804 mSearchEvent = null;
3805 return result;
3806 }
3807
3808 /**
3809 * @see #onSearchRequested(SearchEvent)
3810 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 public boolean onSearchRequested() {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003812 if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK)
3813 != Configuration.UI_MODE_TYPE_TELEVISION) {
3814 startSearch(null, false, null, false);
3815 return true;
3816 } else {
3817 return false;
3818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 }
RoboErik55011652014-07-09 15:05:53 -07003820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07003822 * During the onSearchRequested() callbacks, this function will return the
3823 * {@link SearchEvent} that triggered the callback, if it exists.
3824 *
3825 * @return SearchEvent The SearchEvent that triggered the {@link
3826 * #onSearchRequested} callback.
3827 */
3828 public final SearchEvent getSearchEvent() {
3829 return mSearchEvent;
3830 }
3831
3832 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07003834 *
3835 * <p>It is typically called from onSearchRequested(), either directly from
3836 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07003838 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 * is to inject specific data such as context data, it is preferred to <i>override</i>
3840 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07003841 *
3842 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003843 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07003844 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 * any further typing will replace it. This is useful for cases where an entire pre-formed
3846 * query is being inserted. If false, the selection point will be placed at the end of the
3847 * inserted query. This is useful when the inserted query is text that the user entered,
3848 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
3849 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07003850 * @param appSearchData An application can insert application-specific
3851 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 * searches. This data will be returned with SEARCH intent(s). Null if
3853 * no extra data is required.
3854 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07003855 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07003856 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07003858 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 * @see android.app.SearchManager
3860 * @see #onSearchRequested
3861 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003862 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
3863 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003864 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01003865 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07003866 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 }
3868
3869 /**
krosaend2d60142009-08-17 08:56:48 -07003870 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
3871 * the search dialog. Made available for testing purposes.
3872 *
3873 * @param query The query to trigger. If empty, the request will be ignored.
3874 * @param appSearchData An application can insert application-specific
3875 * context here, in order to improve quality or specificity of its own
3876 * searches. This data will be returned with SEARCH intent(s). Null if
3877 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07003878 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003879 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07003880 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01003881 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07003882 }
3883
3884 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 * Request that key events come to this activity. Use this if your
3886 * activity has no views with focus, but the activity still wants
3887 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07003888 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 * @see android.view.Window#takeKeyEvents
3890 */
3891 public void takeKeyEvents(boolean get) {
3892 getWindow().takeKeyEvents(get);
3893 }
3894
3895 /**
3896 * Enable extended window features. This is a convenience for calling
3897 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07003898 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 * @param featureId The desired feature as defined in
3900 * {@link android.view.Window}.
3901 * @return Returns true if the requested feature is supported and now
3902 * enabled.
RoboErik55011652014-07-09 15:05:53 -07003903 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 * @see android.view.Window#requestFeature
3905 */
3906 public final boolean requestWindowFeature(int featureId) {
3907 return getWindow().requestFeature(featureId);
3908 }
3909
3910 /**
3911 * Convenience for calling
3912 * {@link android.view.Window#setFeatureDrawableResource}.
3913 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003914 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 getWindow().setFeatureDrawableResource(featureId, resId);
3916 }
3917
3918 /**
3919 * Convenience for calling
3920 * {@link android.view.Window#setFeatureDrawableUri}.
3921 */
3922 public final void setFeatureDrawableUri(int featureId, Uri uri) {
3923 getWindow().setFeatureDrawableUri(featureId, uri);
3924 }
3925
3926 /**
3927 * Convenience for calling
3928 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
3929 */
3930 public final void setFeatureDrawable(int featureId, Drawable drawable) {
3931 getWindow().setFeatureDrawable(featureId, drawable);
3932 }
3933
3934 /**
3935 * Convenience for calling
3936 * {@link android.view.Window#setFeatureDrawableAlpha}.
3937 */
3938 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
3939 getWindow().setFeatureDrawableAlpha(featureId, alpha);
3940 }
3941
3942 /**
3943 * Convenience for calling
3944 * {@link android.view.Window#getLayoutInflater}.
3945 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003946 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 public LayoutInflater getLayoutInflater() {
3948 return getWindow().getLayoutInflater();
3949 }
3950
3951 /**
3952 * Returns a {@link MenuInflater} with this context.
3953 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003954 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07003956 // Make sure that action views can get an appropriate theme.
3957 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07003958 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07003959 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07003960 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07003961 } else {
3962 mMenuInflater = new MenuInflater(this);
3963 }
3964 }
3965 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 }
3967
3968 @Override
Filip Gruszczynski0daf2102015-09-29 08:39:07 -07003969 public void setTheme(int resid) {
3970 super.setTheme(resid);
3971 mWindow.setTheme(resid);
3972 }
3973
3974 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07003975 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003976 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 if (mParent == null) {
3978 super.onApplyThemeResource(theme, resid, first);
3979 } else {
3980 try {
3981 theme.setTo(mParent.getTheme());
3982 } catch (Exception e) {
3983 // Empty
3984 }
3985 theme.applyStyle(resid, false);
3986 }
Winson073a5262016-02-29 16:04:40 -08003987
3988 // Get the primary color and update the TaskDescription for this activity
3989 TypedArray a = theme.obtainStyledAttributes(
3990 com.android.internal.R.styleable.ActivityTaskDescription);
3991 if (mTaskDescription.getPrimaryColor() == 0) {
3992 int colorPrimary = a.getColor(
3993 com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
3994 if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
3995 mTaskDescription.setPrimaryColor(colorPrimary);
3996 }
3997 }
3998 // For dev-preview only.
3999 if (mTaskDescription.getBackgroundColor() == 0) {
4000 int colorBackground = a.getColor(
4001 com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
4002 if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
4003 mTaskDescription.setBackgroundColor(colorBackground);
4004 }
4005 }
4006 a.recycle();
4007 setTaskDescription(mTaskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +00004008 }
4009
4010 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08004011 * Requests permissions to be granted to this application. These permissions
4012 * must be requested in your manifest, they should not be granted to your app,
4013 * and they should have protection level {@link android.content.pm.PermissionInfo
4014 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
4015 * the platform or a third-party app.
4016 * <p>
4017 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
4018 * are granted at install time if requested in the manifest. Signature permissions
4019 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
4020 * install time if requested in the manifest and the signature of your app matches
4021 * the signature of the app declaring the permissions.
4022 * </p>
4023 * <p>
4024 * If your app does not have the requested permissions the user will be presented
4025 * with UI for accepting them. After the user has accepted or rejected the
4026 * requested permissions you will receive a callback on {@link
4027 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
4028 * permissions were granted or not.
4029 * </p>
4030 * <p>
4031 * Note that requesting a permission does not guarantee it will be granted and
4032 * your app should be able to run without having this permission.
4033 * </p>
4034 * <p>
4035 * This method may start an activity allowing the user to choose which permissions
4036 * to grant and which to reject. Hence, you should be prepared that your activity
4037 * may be paused and resumed. Further, granting some permissions may require
4038 * a restart of you application. In such a case, the system will recreate the
4039 * activity stack before delivering the result to {@link
4040 * #onRequestPermissionsResult(int, String[], int[])}.
4041 * </p>
4042 * <p>
4043 * When checking whether you have a permission you should use {@link
4044 * #checkSelfPermission(String)}.
4045 * </p>
4046 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07004047 * Calling this API for permissions already granted to your app would show UI
4048 * to the user to decide whether the app can still hold these permissions. This
4049 * can be useful if the way your app uses data guarded by the permissions
4050 * changes significantly.
4051 * </p>
4052 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07004053 * You cannot request a permission if your activity sets {@link
4054 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4055 * <code>true</code> because in this case the activity would not receive
4056 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
4057 * </p>
4058 * <p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004059 * A sample permissions request looks like this:
4060 * </p>
4061 * <code><pre><p>
4062 * private void showContacts() {
4063 * if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
4064 * != PackageManager.PERMISSION_GRANTED) {
4065 * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
4066 * PERMISSIONS_REQUEST_READ_CONTACTS);
4067 * } else {
4068 * doShowContacts();
4069 * }
4070 * }
4071 *
4072 * {@literal @}Override
4073 * public void onRequestPermissionsResult(int requestCode, String[] permissions,
4074 * int[] grantResults) {
4075 * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS
4076 * && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
4077 * showContacts();
4078 * }
4079 * }
4080 * </code></pre></p>
4081 *
Svet Ganovf66381c2016-02-18 20:02:36 -08004082 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08004083 * @param requestCode Application specific request code to match with a result
4084 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07004085 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08004086 *
4087 * @see #onRequestPermissionsResult(int, String[], int[])
4088 * @see #checkSelfPermission(String)
Svetoslavaa048b62015-06-02 15:08:48 -07004089 * @see #shouldShowRequestPermissionRationale(String)
Svetoslavc6d1c342015-02-26 14:44:43 -08004090 */
4091 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Svetoslavffb32b12015-10-15 16:54:00 -07004092 if (mHasCurrentPermissionsRequest) {
4093 Log.w(TAG, "Can reqeust only one set of permissions at a time");
4094 // Dispatch the callback with empty arrays which means a cancellation.
4095 onRequestPermissionsResult(requestCode, new String[0], new int[0]);
4096 return;
4097 }
Svetoslavc6d1c342015-02-26 14:44:43 -08004098 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
Svetoslav970b59c2015-06-09 16:05:21 -07004099 startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
Svetoslavffb32b12015-10-15 16:54:00 -07004100 mHasCurrentPermissionsRequest = true;
Svetoslavc6d1c342015-02-26 14:44:43 -08004101 }
4102
4103 /**
4104 * Callback for the result from requesting permissions. This method
4105 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07004106 * <p>
4107 * <strong>Note:</strong> It is possible that the permissions request interaction
4108 * with the user is interrupted. In this case you will receive empty permissions
4109 * and results arrays which should be treated as a cancellation.
4110 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08004111 *
4112 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
4113 * @param permissions The requested permissions. Never null.
4114 * @param grantResults The grant results for the corresponding permissions
4115 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
4116 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
4117 *
4118 * @see #requestPermissions(String[], int)
4119 */
4120 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
4121 @NonNull int[] grantResults) {
4122 /* callback - no nothing */
4123 }
4124
4125 /**
Svetoslav20770dd2015-05-29 15:43:04 -07004126 * Gets whether you should show UI with rationale for requesting a permission.
4127 * You should do this only if you do not have the permission and the context in
4128 * which the permission is requested does not clearly communicate to the user
4129 * what would be the benefit from granting this permission.
4130 * <p>
4131 * For example, if you write a camera app, requesting the camera permission
4132 * would be expected by the user and no rationale for why it is requested is
4133 * needed. If however, the app needs location for tagging photos then a non-tech
4134 * savvy user may wonder how location is related to taking photos. In this case
4135 * you may choose to show UI with rationale of requesting this permission.
4136 * </p>
4137 *
4138 * @param permission A permission your app wants to request.
4139 * @return Whether you can show permission rationale UI.
4140 *
4141 * @see #checkSelfPermission(String)
4142 * @see #requestPermissions(String[], int)
4143 * @see #onRequestPermissionsResult(int, String[], int[])
4144 */
Svetoslav9ce9a152015-06-02 17:59:31 -07004145 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
Svetoslav20770dd2015-05-29 15:43:04 -07004146 return getPackageManager().shouldShowRequestPermissionRationale(permission);
4147 }
4148
4149 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004150 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
4151 * with no options.
4152 *
4153 * @param intent The intent to start.
4154 * @param requestCode If >= 0, this code will be returned in
4155 * onActivityResult() when the activity exits.
4156 *
4157 * @throws android.content.ActivityNotFoundException
4158 *
George Mount0a778ed2013-12-13 13:35:36 -08004159 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07004160 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004161 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004162 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004163 }
4164
4165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 * Launch an activity for which you would like a result when it finished.
4167 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08004168 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07004169 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07004171 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 * <p>Note that this method should only be used with Intent protocols
4173 * that are defined to return a result. In other protocols (such as
4174 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
4175 * not get the result when you expect. For example, if the activity you
4176 * are launching uses the singleTask launch mode, it will not run in your
4177 * task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004178 *
RoboErik55011652014-07-09 15:05:53 -07004179 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08004181 * activity, then your window will not be displayed until a result is
4182 * returned back from the started activity. This is to avoid visible
4183 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 * <p>This method throws {@link android.content.ActivityNotFoundException}
4186 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 * @param intent The intent to start.
4189 * @param requestCode If >= 0, this code will be returned in
4190 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004191 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004192 * See {@link android.content.Context#startActivity(Intent, Bundle)
4193 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004194 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004196 *
George Mount0a778ed2013-12-13 13:35:36 -08004197 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004199 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
4200 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 if (mParent == null) {
4202 Instrumentation.ActivityResult ar =
4203 mInstrumentation.execStartActivity(
4204 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004205 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 if (ar != null) {
4207 mMainThread.sendActivityResult(
4208 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
4209 ar.getResultData());
4210 }
4211 if (requestCode >= 0) {
4212 // If this start is requesting a result, we can avoid making
4213 // the activity visible until the result is received. Setting
4214 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4215 // activity hidden during this time, to avoid flickering.
4216 // This can only be done when a result is requested because
4217 // that guarantees we will get information back when the
4218 // activity is finished, no matter what happens to it.
4219 mStartedActivity = true;
4220 }
Adam Powell14874662013-07-18 19:42:41 -07004221
George Mount41725de2015-04-09 08:23:05 -07004222 cancelInputsAndStartExitTransition(options);
Adam Powell14874662013-07-18 19:42:41 -07004223 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004225 if (options != null) {
4226 mParent.startActivityFromChild(this, intent, requestCode, options);
4227 } else {
4228 // Note we want to go through this method for compatibility with
4229 // existing applications that may have overridden it.
4230 mParent.startActivityFromChild(this, intent, requestCode);
4231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 }
George Mount41725de2015-04-09 08:23:05 -07004233 }
4234
4235 /**
4236 * Cancels pending inputs and if an Activity Transition is to be run, starts the transition.
4237 *
4238 * @param options The ActivityOptions bundle used to start an Activity.
4239 */
4240 private void cancelInputsAndStartExitTransition(Bundle options) {
4241 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
4242 if (decor != null) {
4243 decor.cancelPendingInputEvents();
4244 }
George Mountff243282014-07-07 16:12:07 -07004245 if (options != null && !isTopOfTask()) {
4246 mActivityTransitionState.startExitOutTransition(this, options);
4247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 }
4249
4250 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004251 * @hide Implement to provide correct calling token.
4252 */
Alexandra Gherghina83231452014-07-04 12:05:20 +01004253 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
4254 startActivityForResultAsUser(intent, requestCode, null, user);
4255 }
4256
4257 /**
4258 * @hide Implement to provide correct calling token.
4259 */
4260 public void startActivityForResultAsUser(Intent intent, int requestCode,
4261 @Nullable Bundle options, UserHandle user) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004262 if (mParent != null) {
4263 throw new RuntimeException("Can't be called from a child");
4264 }
4265 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
4266 this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode,
4267 options, user);
4268 if (ar != null) {
4269 mMainThread.sendActivityResult(
4270 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
4271 }
4272 if (requestCode >= 0) {
4273 // If this start is requesting a result, we can avoid making
4274 // the activity visible until the result is received. Setting
4275 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4276 // activity hidden during this time, to avoid flickering.
4277 // This can only be done when a result is requested because
4278 // that guarantees we will get information back when the
4279 // activity is finished, no matter what happens to it.
4280 mStartedActivity = true;
4281 }
4282
George Mount41725de2015-04-09 08:23:05 -07004283 cancelInputsAndStartExitTransition(options);
Alexandra Gherghina83231452014-07-04 12:05:20 +01004284 }
4285
4286 /**
4287 * @hide Implement to provide correct calling token.
4288 */
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004289 public void startActivityAsUser(Intent intent, UserHandle user) {
4290 startActivityAsUser(intent, null, user);
4291 }
4292
4293 /**
4294 * @hide Implement to provide correct calling token.
4295 */
4296 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
4297 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01004298 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004299 }
4300 Instrumentation.ActivityResult ar =
4301 mInstrumentation.execStartActivity(
4302 this, mMainThread.getApplicationThread(), mToken, this,
4303 intent, -1, options, user);
4304 if (ar != null) {
4305 mMainThread.sendActivityResult(
4306 mToken, mEmbeddedID, -1, ar.getResultCode(),
4307 ar.getResultData());
4308 }
George Mount41725de2015-04-09 08:23:05 -07004309 cancelInputsAndStartExitTransition(options);
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07004310 }
4311
4312 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004313 * Start a new activity as if it was started by the activity that started our
4314 * current activity. This is for the resolver and chooser activities, which operate
4315 * as intermediaries that dispatch their intent to the target the user selects -- to
4316 * do this, they must perform all security checks including permission grants as if
4317 * their launch had come from the original activity.
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004318 * @param intent The Intent to start.
4319 * @param options ActivityOptions or null.
4320 * @param ignoreTargetSecurity If true, the activity manager will not check whether the
4321 * caller it is doing the start is, is actually allowed to start the target activity.
4322 * If you set this to true, you must set an explicit component in the Intent and do any
4323 * appropriate security checks yourself.
4324 * @param userId The user the new activity should run as.
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004325 * @hide
4326 */
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004327 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
4328 boolean ignoreTargetSecurity, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004329 if (mParent != null) {
4330 throw new RuntimeException("Can't be called from a child");
4331 }
4332 Instrumentation.ActivityResult ar =
4333 mInstrumentation.execStartActivityAsCaller(
4334 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004335 intent, -1, options, ignoreTargetSecurity, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004336 if (ar != null) {
4337 mMainThread.sendActivityResult(
4338 mToken, mEmbeddedID, -1, ar.getResultCode(),
4339 ar.getResultData());
4340 }
George Mount41725de2015-04-09 08:23:05 -07004341 cancelInputsAndStartExitTransition(options);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07004342 }
4343
4344 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004345 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
4346 * Intent, int, int, int, Bundle)} with no options.
4347 *
4348 * @param intent The IntentSender to launch.
4349 * @param requestCode If >= 0, this code will be returned in
4350 * onActivityResult() when the activity exits.
4351 * @param fillInIntent If non-null, this will be provided as the
4352 * intent parameter to {@link IntentSender#sendIntent}.
4353 * @param flagsMask Intent flags in the original IntentSender that you
4354 * would like to change.
4355 * @param flagsValues Desired values for any bits set in
4356 * <var>flagsMask</var>
4357 * @param extraFlags Always set to 0.
4358 */
4359 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004360 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07004361 throws IntentSender.SendIntentException {
4362 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
4363 flagsValues, extraFlags, null);
4364 }
4365
4366 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004367 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004368 * to use a IntentSender to describe the activity to be started. If
4369 * the IntentSender is for an activity, that activity will be started
4370 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
4371 * here; otherwise, its associated action will be executed (such as
4372 * sending a broadcast) as if you had called
4373 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07004374 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004375 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004376 * @param requestCode If >= 0, this code will be returned in
4377 * onActivityResult() when the activity exits.
4378 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004379 * intent parameter to {@link IntentSender#sendIntent}.
4380 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004381 * would like to change.
4382 * @param flagsValues Desired values for any bits set in
4383 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004384 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004385 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004386 * See {@link android.content.Context#startActivity(Intent, Bundle)
4387 * Context.startActivity(Intent, Bundle)} for more details. If options
4388 * have also been supplied by the IntentSender, options given here will
4389 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004390 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004391 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07004392 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004393 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004394 if (mParent == null) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004395 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004396 flagsMask, flagsValues, this, options);
4397 } else if (options != null) {
4398 mParent.startIntentSenderFromChild(this, intent, requestCode,
4399 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004400 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004401 // Note we want to go through this call for compatibility with
4402 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004403 mParent.startIntentSenderFromChild(this, intent, requestCode,
4404 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004405 }
4406 }
4407
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004408 private void startIntentSenderForResultInner(IntentSender intent, int requestCode,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004409 Intent fillInIntent, int flagsMask, int flagsValues, Activity activity,
4410 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004411 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004412 try {
4413 String resolvedType = null;
4414 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004415 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004416 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004417 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
4418 }
4419 int result = ActivityManagerNative.getDefault()
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004420 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004421 fillInIntent, resolvedType, mToken, activity.mEmbeddedID,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004422 requestCode, flagsMask, flagsValues, options);
4423 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004424 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004425 }
4426 Instrumentation.checkStartActivityResult(result, null);
4427 } catch (RemoteException e) {
4428 }
4429 if (requestCode >= 0) {
4430 // If this start is requesting a result, we can avoid making
4431 // the activity visible until the result is received. Setting
4432 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4433 // activity hidden during this time, to avoid flickering.
4434 // This can only be done when a result is requested because
4435 // that guarantees we will get information back when the
4436 // activity is finished, no matter what happens to it.
4437 mStartedActivity = true;
4438 }
4439 }
4440
4441 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004442 * Same as {@link #startActivity(Intent, Bundle)} with no options
4443 * specified.
4444 *
4445 * @param intent The intent to start.
4446 *
4447 * @throws android.content.ActivityNotFoundException
4448 *
4449 * @see {@link #startActivity(Intent, Bundle)}
4450 * @see #startActivityForResult
4451 */
4452 @Override
4453 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00004454 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004455 }
4456
4457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 * Launch a new activity. You will not receive any information about when
4459 * the activity exits. This implementation overrides the base version,
4460 * providing information about
4461 * the activity performing the launch. Because of this additional
4462 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4463 * required; if not specified, the new activity will be added to the
4464 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07004465 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 * <p>This method throws {@link android.content.ActivityNotFoundException}
4467 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004468 *
4469 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004470 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004471 * See {@link android.content.Context#startActivity(Intent, Bundle)
4472 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004473 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004475 *
4476 * @see {@link #startActivity(Intent)}
RoboErik55011652014-07-09 15:05:53 -07004477 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 */
4479 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004480 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004481 if (options != null) {
4482 startActivityForResult(intent, -1, options);
4483 } else {
4484 // Note we want to go through this call for compatibility with
4485 // applications that may have overridden the method.
4486 startActivityForResult(intent, -1);
4487 }
4488 }
4489
4490 /**
4491 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4492 * specified.
4493 *
4494 * @param intents The intents to start.
4495 *
4496 * @throws android.content.ActivityNotFoundException
4497 *
4498 * @see {@link #startActivities(Intent[], Bundle)}
4499 * @see #startActivityForResult
4500 */
4501 @Override
4502 public void startActivities(Intent[] intents) {
4503 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 }
4505
4506 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004507 * Launch a new activity. You will not receive any information about when
4508 * the activity exits. This implementation overrides the base version,
4509 * providing information about
4510 * the activity performing the launch. Because of this additional
4511 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4512 * required; if not specified, the new activity will be added to the
4513 * task of the caller.
4514 *
4515 * <p>This method throws {@link android.content.ActivityNotFoundException}
4516 * if there was no Activity found to run the given Intent.
4517 *
4518 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004519 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004520 * See {@link android.content.Context#startActivity(Intent, Bundle)
4521 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004522 *
4523 * @throws android.content.ActivityNotFoundException
4524 *
Dianne Hackborna4972e92012-03-14 10:38:05 -07004525 * @see {@link #startActivities(Intent[])}
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004526 * @see #startActivityForResult
4527 */
4528 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004529 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004530 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004531 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004532 }
4533
4534 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004535 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4536 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004537 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004538 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004539 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004540 * intent parameter to {@link IntentSender#sendIntent}.
4541 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004542 * would like to change.
4543 * @param flagsValues Desired values for any bits set in
4544 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004545 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004546 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004547 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004548 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004549 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004550 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
4551 extraFlags, null);
4552 }
4553
4554 /**
4555 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
4556 * to start; see
4557 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
4558 * for more information.
4559 *
4560 * @param intent The IntentSender to launch.
4561 * @param fillInIntent If non-null, this will be provided as the
4562 * intent parameter to {@link IntentSender#sendIntent}.
4563 * @param flagsMask Intent flags in the original IntentSender that you
4564 * would like to change.
4565 * @param flagsValues Desired values for any bits set in
4566 * <var>flagsMask</var>
4567 * @param extraFlags Always set to 0.
4568 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004569 * See {@link android.content.Context#startActivity(Intent, Bundle)
4570 * Context.startActivity(Intent, Bundle)} for more details. If options
4571 * have also been supplied by the IntentSender, options given here will
4572 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004573 */
4574 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004575 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004576 Bundle options) throws IntentSender.SendIntentException {
4577 if (options != null) {
4578 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4579 flagsValues, extraFlags, options);
4580 } else {
4581 // Note we want to go through this call for compatibility with
4582 // applications that may have overridden the method.
4583 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4584 flagsValues, extraFlags);
4585 }
4586 }
4587
4588 /**
4589 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
4590 * with no options.
4591 *
4592 * @param intent The intent to start.
4593 * @param requestCode If >= 0, this code will be returned in
4594 * onActivityResult() when the activity exits, as described in
4595 * {@link #startActivityForResult}.
4596 *
4597 * @return If a new activity was launched then true is returned; otherwise
4598 * false is returned and you must handle the Intent yourself.
4599 *
4600 * @see #startActivity
4601 * @see #startActivityForResult
4602 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004603 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4604 int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004605 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004606 }
4607
4608 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004609 * A special variation to launch an activity only if a new activity
4610 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07004611 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07004613 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07004615 * and the activity
4616 * that handles <var>intent</var> is the same as your currently running
4617 * activity, then a new instance is not needed. In this case, instead of
4618 * the normal behavior of calling {@link #onNewIntent} this function will
4619 * return and you can handle the Intent yourself.
4620 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004621 * <p>This function can only be called from a top-level activity; if it is
4622 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07004623 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 * @param intent The intent to start.
4625 * @param requestCode If >= 0, this code will be returned in
4626 * onActivityResult() when the activity exits, as described in
4627 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004628 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004629 * See {@link android.content.Context#startActivity(Intent, Bundle)
4630 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 * @return If a new activity was launched then true is returned; otherwise
4633 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07004634 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 * @see #startActivity
4636 * @see #startActivityForResult
4637 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004638 public boolean startActivityIfNeeded(@RequiresPermission @NonNull Intent intent,
4639 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004641 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004642 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004643 Uri referrer = onProvideReferrer();
4644 if (referrer != null) {
4645 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4646 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004647 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004648 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004649 result = ActivityManagerNative.getDefault()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08004650 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07004651 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
4652 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
4653 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 } catch (RemoteException e) {
4655 // Empty
4656 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08004657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660 if (requestCode >= 0) {
4661 // If this start is requesting a result, we can avoid making
4662 // the activity visible until the result is received. Setting
4663 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4664 // activity hidden during this time, to avoid flickering.
4665 // This can only be done when a result is requested because
4666 // that guarantees we will get information back when the
4667 // activity is finished, no matter what happens to it.
4668 mStartedActivity = true;
4669 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004670 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 }
4672
4673 throw new UnsupportedOperationException(
4674 "startActivityIfNeeded can only be called from a top-level activity");
4675 }
4676
4677 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004678 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
4679 * no options.
4680 *
4681 * @param intent The intent to dispatch to the next activity. For
4682 * correct behavior, this must be the same as the Intent that started
4683 * your own activity; the only changes you can make are to the extras
4684 * inside of it.
4685 *
4686 * @return Returns a boolean indicating whether there was another Activity
4687 * to start: true if there was a next activity to start, false if there
4688 * wasn't. In general, if true is returned you will then want to call
4689 * finish() on yourself.
4690 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004691 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004692 return startNextMatchingActivity(intent, null);
4693 }
4694
4695 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696 * Special version of starting an activity, for use when you are replacing
4697 * other activity components. You can use this to hand the Intent off
4698 * to the next Activity that can handle it. You typically call this in
4699 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07004700 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004701 * @param intent The intent to dispatch to the next activity. For
4702 * correct behavior, this must be the same as the Intent that started
4703 * your own activity; the only changes you can make are to the extras
4704 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004705 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004706 * See {@link android.content.Context#startActivity(Intent, Bundle)
4707 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004708 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004709 * @return Returns a boolean indicating whether there was another Activity
4710 * to start: true if there was a next activity to start, false if there
4711 * wasn't. In general, if true is returned you will then want to call
4712 * finish() on yourself.
4713 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004714 public boolean startNextMatchingActivity(@RequiresPermission @NonNull Intent intent,
4715 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 if (mParent == null) {
4717 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004718 intent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004719 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 return ActivityManagerNative.getDefault()
Dianne Hackborna4972e92012-03-14 10:38:05 -07004721 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 } catch (RemoteException e) {
4723 // Empty
4724 }
4725 return false;
4726 }
4727
4728 throw new UnsupportedOperationException(
4729 "startNextMatchingActivity can only be called from a top-level activity");
4730 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004731
4732 /**
4733 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
4734 * with no options.
4735 *
4736 * @param child The activity making the call.
4737 * @param intent The intent to start.
4738 * @param requestCode Reply request code. < 0 if reply is not requested.
4739 *
4740 * @throws android.content.ActivityNotFoundException
4741 *
4742 * @see #startActivity
4743 * @see #startActivityForResult
4744 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004745 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004746 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004747 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004748 }
4749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004750 /**
RoboErik55011652014-07-09 15:05:53 -07004751 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004752 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07004753 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004754 * <p>This method throws {@link android.content.ActivityNotFoundException}
4755 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004756 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 * @param child The activity making the call.
4758 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004759 * @param requestCode Reply request code. < 0 if reply is not requested.
4760 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004761 * See {@link android.content.Context#startActivity(Intent, Bundle)
4762 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004763 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004765 *
4766 * @see #startActivity
4767 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004768 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004769 public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004770 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004771 Instrumentation.ActivityResult ar =
4772 mInstrumentation.execStartActivity(
4773 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004774 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 if (ar != null) {
4776 mMainThread.sendActivityResult(
4777 mToken, child.mEmbeddedID, requestCode,
4778 ar.getResultCode(), ar.getResultData());
4779 }
George Mount41725de2015-04-09 08:23:05 -07004780 cancelInputsAndStartExitTransition(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 }
4782
4783 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004784 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
4785 * with no options.
4786 *
4787 * @param fragment The fragment making the call.
4788 * @param intent The intent to start.
4789 * @param requestCode Reply request code. < 0 if reply is not requested.
4790 *
4791 * @throws android.content.ActivityNotFoundException
4792 *
4793 * @see Fragment#startActivity
4794 * @see Fragment#startActivityForResult
4795 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004796 public void startActivityFromFragment(@NonNull Fragment fragment,
4797 @RequiresPermission Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004798 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004799 }
4800
4801 /**
RoboErik55011652014-07-09 15:05:53 -07004802 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004803 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
4804 * method.
RoboErik55011652014-07-09 15:05:53 -07004805 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004806 * <p>This method throws {@link android.content.ActivityNotFoundException}
4807 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004808 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004809 * @param fragment The fragment making the call.
4810 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07004811 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004812 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004813 * See {@link android.content.Context#startActivity(Intent, Bundle)
4814 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004815 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004816 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004817 *
4818 * @see Fragment#startActivity
4819 * @see Fragment#startActivityForResult
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004820 */
Tor Norbye788fc2b2015-07-05 16:10:42 -07004821 public void startActivityFromFragment(@NonNull Fragment fragment,
4822 @RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004823 startActivityForResult(fragment.mWho, intent, requestCode, options);
4824 }
4825
4826 /**
4827 * @hide
4828 */
4829 @Override
4830 public void startActivityForResult(
4831 String who, Intent intent, int requestCode, @Nullable Bundle options) {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004832 Uri referrer = onProvideReferrer();
4833 if (referrer != null) {
4834 intent.putExtra(Intent.EXTRA_REFERRER, referrer);
4835 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004836 Instrumentation.ActivityResult ar =
4837 mInstrumentation.execStartActivity(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004838 this, mMainThread.getApplicationThread(), mToken, who,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004839 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004840 if (ar != null) {
4841 mMainThread.sendActivityResult(
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004842 mToken, who, requestCode,
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004843 ar.getResultCode(), ar.getResultData());
4844 }
George Mount41725de2015-04-09 08:23:05 -07004845 cancelInputsAndStartExitTransition(options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004846 }
4847
4848 /**
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00004849 * @hide
4850 */
4851 @Override
4852 public boolean canStartActivityForResult() {
4853 return true;
4854 }
4855
4856 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004857 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
4858 * int, Intent, int, int, int, Bundle)} with no options.
4859 */
4860 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4861 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
4862 int extraFlags)
4863 throws IntentSender.SendIntentException {
4864 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
4865 flagsMask, flagsValues, extraFlags, null);
4866 }
4867
4868 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004869 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004870 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07004871 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004872 * for more information.
4873 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004874 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4875 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07004876 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004877 throws IntentSender.SendIntentException {
4878 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004879 flagsMask, flagsValues, child, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004880 }
4881
4882 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004883 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
4884 * or {@link #finish} to specify an explicit transition animation to
4885 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004886 *
4887 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
4888 * to using this with starting activities is to supply the desired animation
4889 * information through a {@link ActivityOptions} bundle to
4890 * {@link #startActivity(Intent, Bundle) or a related function. This allows
4891 * you to specify a custom animation even when starting an activity from
4892 * outside the context of the current top activity.
4893 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004894 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004895 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004896 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004897 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004898 */
4899 public void overridePendingTransition(int enterAnim, int exitAnim) {
4900 try {
4901 ActivityManagerNative.getDefault().overridePendingTransition(
4902 mToken, getPackageName(), enterAnim, exitAnim);
4903 } catch (RemoteException e) {
4904 }
4905 }
RoboErik55011652014-07-09 15:05:53 -07004906
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004907 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908 * Call this to set the result that your activity will return to its
4909 * caller.
RoboErik55011652014-07-09 15:05:53 -07004910 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 * @param resultCode The result code to propagate back to the originating
4912 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07004913 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 * @see #RESULT_CANCELED
4915 * @see #RESULT_OK
4916 * @see #RESULT_FIRST_USER
4917 * @see #setResult(int, Intent)
4918 */
4919 public final void setResult(int resultCode) {
4920 synchronized (this) {
4921 mResultCode = resultCode;
4922 mResultData = null;
4923 }
4924 }
4925
4926 /**
4927 * Call this to set the result that your activity will return to its
4928 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004929 *
4930 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
4931 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
4932 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
4933 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
4934 * Activity receiving the result access to the specific URIs in the Intent.
4935 * Access will remain until the Activity has finished (it will remain across the hosting
4936 * process being killed and other temporary destruction) and will be added
4937 * to any existing set of URI permissions it already holds.
4938 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 * @param resultCode The result code to propagate back to the originating
4940 * activity, often RESULT_CANCELED or RESULT_OK
4941 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07004942 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 * @see #RESULT_CANCELED
4944 * @see #RESULT_OK
4945 * @see #RESULT_FIRST_USER
4946 * @see #setResult(int)
4947 */
4948 public final void setResult(int resultCode, Intent data) {
4949 synchronized (this) {
4950 mResultCode = resultCode;
4951 mResultData = data;
4952 }
4953 }
4954
4955 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004956 * Return information about who launched this activity. If the launching Intent
4957 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
4958 * that will be returned as-is; otherwise, if known, an
4959 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
4960 * package name that started the Intent will be returned. This may return null if no
4961 * referrer can be identified -- it is neither explicitly specified, nor is it known which
4962 * application package was involved.
4963 *
4964 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
4965 * return the referrer that submitted that new intent to the activity. Otherwise, it
4966 * always returns the referrer of the original Intent.</p>
4967 *
4968 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
4969 * referrer information, applications can spoof it.</p>
4970 */
4971 @Nullable
4972 public Uri getReferrer() {
4973 Intent intent = getIntent();
4974 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
4975 if (referrer != null) {
4976 return referrer;
4977 }
4978 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
4979 if (referrerName != null) {
4980 return Uri.parse(referrerName);
4981 }
4982 if (mReferrer != null) {
4983 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
4984 }
4985 return null;
4986 }
4987
4988 /**
Dianne Hackborna3acdb32015-06-08 17:07:40 -07004989 * Override to generate the desired referrer for the content currently being shown
4990 * by the app. The default implementation returns null, meaning the referrer will simply
4991 * be the android-app: of the package name of this activity. Return a non-null Uri to
4992 * have that supplied as the {@link Intent#EXTRA_REFERRER} of any activities started from it.
4993 */
4994 public Uri onProvideReferrer() {
4995 return null;
4996 }
4997
4998 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 * Return the name of the package that invoked this activity. This is who
5000 * the data in {@link #setResult setResult()} will be sent to. You can
5001 * use this information to validate that the recipient is allowed to
5002 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005003 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005004 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005005 * did not use the {@link #startActivityForResult}
5006 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07005007 * null.</p>
5008 *
5009 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
5010 * the result from this method was unstable. If the process hosting the calling
5011 * package was no longer running, it would return null instead of the proper package
5012 * name. You can use {@link #getCallingActivity()} and retrieve the package name
5013 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07005014 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 * @return The package of the activity that will receive your
5016 * reply, or null if none.
5017 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005018 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005019 public String getCallingPackage() {
5020 try {
5021 return ActivityManagerNative.getDefault().getCallingPackage(mToken);
5022 } catch (RemoteException e) {
5023 return null;
5024 }
5025 }
5026
5027 /**
5028 * Return the name of the activity that invoked this activity. This is
5029 * who the data in {@link #setResult setResult()} will be sent to. You
5030 * can use this information to validate that the recipient is allowed to
5031 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07005032 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005033 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07005034 * did not use the {@link #startActivityForResult}
5035 * form that includes a request code), then the calling package will be
5036 * null.
5037 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07005038 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039 * reply, or null if none.
5040 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005041 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 public ComponentName getCallingActivity() {
5043 try {
5044 return ActivityManagerNative.getDefault().getCallingActivity(mToken);
5045 } catch (RemoteException e) {
5046 return null;
5047 }
5048 }
5049
5050 /**
5051 * Control whether this activity's main window is visible. This is intended
5052 * only for the special case of an activity that is not going to show a
5053 * UI itself, but can't just finish prior to onResume() because it needs
5054 * to wait for a service binding or such. Setting this to false allows
5055 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07005056 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 * <p>The default value for this is taken from the
5058 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
5059 */
5060 public void setVisible(boolean visible) {
5061 if (mVisibleFromClient != visible) {
5062 mVisibleFromClient = visible;
5063 if (mVisibleFromServer) {
5064 if (visible) makeVisible();
5065 else mDecor.setVisibility(View.INVISIBLE);
5066 }
5067 }
5068 }
RoboErik55011652014-07-09 15:05:53 -07005069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 void makeVisible() {
5071 if (!mWindowAdded) {
5072 ViewManager wm = getWindowManager();
5073 wm.addView(mDecor, getWindow().getAttributes());
5074 mWindowAdded = true;
5075 }
5076 mDecor.setVisibility(View.VISIBLE);
5077 }
RoboErik55011652014-07-09 15:05:53 -07005078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 /**
5080 * Check to see whether this activity is in the process of finishing,
5081 * either because you called {@link #finish} on it or someone else
5082 * has requested that it finished. This is often used in
5083 * {@link #onPause} to determine whether the activity is simply pausing or
5084 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07005085 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07005087 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 * @see #finish
5089 */
5090 public boolean isFinishing() {
5091 return mFinished;
5092 }
5093
5094 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07005095 * Returns true if the final {@link #onDestroy()} call has been made
5096 * on the Activity, so this instance is now dead.
5097 */
5098 public boolean isDestroyed() {
5099 return mDestroyed;
5100 }
5101
5102 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005103 * Check to see whether this activity is in the process of being destroyed in order to be
5104 * recreated with a new configuration. This is often used in
5105 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
5106 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07005107 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05005108 * @return If the activity is being torn down in order to be recreated with a new configuration,
5109 * returns true; else returns false.
5110 */
5111 public boolean isChangingConfigurations() {
5112 return mChangingConfigurations;
5113 }
5114
5115 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005116 * Cause this Activity to be recreated with a new instance. This results
5117 * in essentially the same flow as when the Activity is created due to
5118 * a configuration change -- the current instance will go through its
5119 * lifecycle to {@link #onDestroy} and a new instance then created after it.
5120 */
5121 public void recreate() {
5122 if (mParent != null) {
5123 throw new IllegalStateException("Can only be called on top-level activity");
5124 }
5125 if (Looper.myLooper() != mMainThread.getLooper()) {
5126 throw new IllegalStateException("Must be called from main thread");
5127 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07005128 mMainThread.requestRelaunchActivity(mToken, null, null, 0, false, null, null, false,
5129 false /* preserveWindow */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005130 }
5131
5132 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005133 * Finishes the current activity and specifies whether to remove the task associated with this
5134 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135 */
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005136 private void finish(int finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005137 if (mParent == null) {
5138 int resultCode;
5139 Intent resultData;
5140 synchronized (this) {
5141 resultCode = mResultCode;
5142 resultData = mResultData;
5143 }
Joe Onorato43a17652011-04-06 19:22:23 -07005144 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005146 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005147 resultData.prepareToLeaveProcess(this);
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04005148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 if (ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07005150 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151 mFinished = true;
5152 }
5153 } catch (RemoteException e) {
5154 // Empty
5155 }
5156 } else {
5157 mParent.finishFromChild(this);
5158 }
5159 }
5160
5161 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005162 * Call this when your activity is done and should be closed. The
5163 * ActivityResult is propagated back to whoever launched you via
5164 * onActivityResult().
5165 */
5166 public void finish() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005167 finish(DONT_FINISH_TASK_WITH_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005168 }
5169
5170 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07005171 * Finish this activity as well as all activities immediately below it
5172 * in the current task that have the same affinity. This is typically
5173 * used when an application can be launched on to another task (such as
5174 * from an ACTION_VIEW of a content type it understands) and the user
5175 * has used the up navigation to switch out of the current task and in
5176 * to its own task. In this case, if the user has navigated down into
5177 * any other activities of the second application, all of those should
5178 * be removed from the original task as part of the task switch.
5179 *
5180 * <p>Note that this finish does <em>not</em> allow you to deliver results
5181 * to the previous activity, and an exception will be thrown if you are trying
5182 * to do so.</p>
5183 */
5184 public void finishAffinity() {
5185 if (mParent != null) {
5186 throw new IllegalStateException("Can not be called from an embedded activity");
5187 }
5188 if (mResultCode != RESULT_CANCELED || mResultData != null) {
5189 throw new IllegalStateException("Can not be called to deliver a result");
5190 }
5191 try {
5192 if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) {
5193 mFinished = true;
5194 }
5195 } catch (RemoteException e) {
5196 // Empty
5197 }
5198 }
5199
5200 /**
RoboErik55011652014-07-09 15:05:53 -07005201 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202 * {@link #finish} method. The default implementation simply calls
5203 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07005204 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07005206 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 * @see #finish
5208 */
5209 public void finishFromChild(Activity child) {
5210 finish();
5211 }
5212
5213 /**
George Mountcb4b7d92014-02-25 10:47:55 -08005214 * Reverses the Activity Scene entry Transition and triggers the calling Activity
5215 * to reverse its exit Transition. When the exit Transition completes,
5216 * {@link #finish()} is called. If no entry Transition was used, finish() is called
5217 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07005218 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08005219 */
Craig Mautner73f843d2014-05-19 09:42:28 -07005220 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07005221 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07005222 finish();
5223 }
George Mountcb4b7d92014-02-25 10:47:55 -08005224 }
5225
5226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 * Force finish another activity that you had previously started with
5228 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07005229 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 * @param requestCode The request code of the activity that you had
5231 * given to startActivityForResult(). If there are multiple
5232 * activities started with this request code, they
5233 * will all be finished.
5234 */
5235 public void finishActivity(int requestCode) {
5236 if (mParent == null) {
5237 try {
5238 ActivityManagerNative.getDefault()
5239 .finishSubActivity(mToken, mEmbeddedID, requestCode);
5240 } catch (RemoteException e) {
5241 // Empty
5242 }
5243 } else {
5244 mParent.finishActivityFromChild(this, requestCode);
5245 }
5246 }
5247
5248 /**
5249 * This is called when a child activity of this one calls its
5250 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07005251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 * @param child The activity making the call.
5253 * @param requestCode Request code that had been used to start the
5254 * activity.
5255 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005256 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 try {
5258 ActivityManagerNative.getDefault()
5259 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
5260 } catch (RemoteException e) {
5261 // Empty
5262 }
5263 }
5264
5265 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07005266 * Call this when your activity is done and should be closed and the task should be completely
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005267 * removed as a part of finishing the root activity of the task.
Winson Chung3b3f4642014-04-22 10:08:18 -07005268 */
5269 public void finishAndRemoveTask() {
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07005270 finish(FINISH_TASK_WITH_ROOT_ACTIVITY);
Winson Chung3b3f4642014-04-22 10:08:18 -07005271 }
5272
5273 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005274 * Ask that the local app instance of this activity be released to free up its memory.
5275 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
5276 * a new instance of the activity will later be re-created if needed due to the user
5277 * navigating back to it.
5278 *
5279 * @return Returns true if the activity was in a state that it has started the process
5280 * of destroying its current instance; returns false if for any reason this could not
5281 * be done: it is currently visible to the user, it is already being destroyed, it is
5282 * being finished, it hasn't yet saved its state, etc.
5283 */
5284 public boolean releaseInstance() {
5285 try {
5286 return ActivityManagerNative.getDefault().releaseActivityInstance(mToken);
5287 } catch (RemoteException e) {
5288 // Empty
5289 }
5290 return false;
5291 }
5292
5293 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005294 * Called when an activity you launched exits, giving you the requestCode
5295 * you started it with, the resultCode it returned, and any additional
5296 * data from it. The <var>resultCode</var> will be
5297 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
5298 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07005299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 * <p>You will receive this call immediately before onResume() when your
5301 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07005302 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07005303 * <p>This method is never invoked if your activity sets
5304 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
5305 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08005306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 * @param requestCode The integer request code originally supplied to
5308 * startActivityForResult(), allowing you to identify who this
5309 * result came from.
5310 * @param resultCode The integer result code returned by the child activity
5311 * through its setResult().
5312 * @param data An Intent, which can return result data to the caller
5313 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07005314 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005315 * @see #startActivityForResult
5316 * @see #createPendingResult
5317 * @see #setResult(int)
5318 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07005319 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005320 }
5321
5322 /**
George Mount62ab9b72014-05-02 13:51:17 -07005323 * Called when an activity you launched with an activity transition exposes this
5324 * Activity through a returning activity transition, giving you the resultCode
5325 * and any additional data from it. This method will only be called if the activity
5326 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07005327 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07005328 *
5329 * <p>The purpose of this function is to let the called Activity send a hint about
5330 * its state so that this underlying Activity can prepare to be exposed. A call to
5331 * this method does not guarantee that the called Activity has or will be exiting soon.
5332 * It only indicates that it will expose this Activity's Window and it has
5333 * some data to pass to prepare it.</p>
5334 *
5335 * @param resultCode The integer result code returned by the child activity
5336 * through its setResult().
5337 * @param data An Intent, which can return result data to the caller
5338 * (various data can be attached to Intent "extras").
5339 */
Craig Mautner7a629c22014-09-04 14:57:04 -07005340 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07005341 }
5342
5343 /**
RoboErik55011652014-07-09 15:05:53 -07005344 * Create a new PendingIntent object which you can hand to others
5345 * for them to use to send result data back to your
5346 * {@link #onActivityResult} callback. The created object will be either
5347 * one-shot (becoming invalid after a result is sent back) or multiple
5348 * (allowing any number of results to be sent through it).
5349 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 * @param requestCode Private request code for the sender that will be
5351 * associated with the result data when it is returned. The sender can not
5352 * modify this value, allowing you to identify incoming results.
5353 * @param data Default data to supply in the result, which may be modified
5354 * by the sender.
5355 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
5356 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
5357 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
5358 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
5359 * or any of the flags as supported by
5360 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
5361 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07005362 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 * @return Returns an existing or new PendingIntent matching the given
5364 * parameters. May return null only if
5365 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
5366 * supplied.
RoboErik55011652014-07-09 15:05:53 -07005367 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 * @see PendingIntent
5369 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005370 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
5371 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005372 String packageName = getPackageName();
5373 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07005374 data.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 IIntentSender target =
5376 ActivityManagerNative.getDefault().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07005377 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005378 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07005379 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
5380 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 return target != null ? new PendingIntent(target) : null;
5382 } catch (RemoteException e) {
5383 // Empty
5384 }
5385 return null;
5386 }
5387
5388 /**
5389 * Change the desired orientation of this activity. If the activity
5390 * is currently in the foreground or otherwise impacting the screen
5391 * orientation, the screen will immediately be changed (possibly causing
5392 * the activity to be restarted). Otherwise, this will be used the next
5393 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07005394 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005395 * @param requestedOrientation An orientation constant as used in
5396 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5397 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005398 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 if (mParent == null) {
5400 try {
5401 ActivityManagerNative.getDefault().setRequestedOrientation(
5402 mToken, requestedOrientation);
5403 } catch (RemoteException e) {
5404 // Empty
5405 }
5406 } else {
5407 mParent.setRequestedOrientation(requestedOrientation);
5408 }
5409 }
RoboErik55011652014-07-09 15:05:53 -07005410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005411 /**
5412 * Return the current requested orientation of the activity. This will
5413 * either be the orientation requested in its component's manifest, or
5414 * the last requested orientation given to
5415 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07005416 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 * @return Returns an orientation constant as used in
5418 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
5419 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005420 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 public int getRequestedOrientation() {
5422 if (mParent == null) {
5423 try {
5424 return ActivityManagerNative.getDefault()
5425 .getRequestedOrientation(mToken);
5426 } catch (RemoteException e) {
5427 // Empty
5428 }
5429 } else {
5430 return mParent.getRequestedOrientation();
5431 }
5432 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
5433 }
RoboErik55011652014-07-09 15:05:53 -07005434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 /**
5436 * Return the identifier of the task this activity is in. This identifier
5437 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07005438 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 * @return Task identifier, an opaque integer.
5440 */
5441 public int getTaskId() {
5442 try {
5443 return ActivityManagerNative.getDefault()
5444 .getTaskForActivity(mToken, false);
5445 } catch (RemoteException e) {
5446 return -1;
5447 }
5448 }
5449
5450 /**
5451 * Return whether this activity is the root of a task. The root is the
5452 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07005453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 * @return True if this is the root activity, else false.
5455 */
5456 public boolean isTaskRoot() {
5457 try {
Filip Gruszczynski3d026712015-12-16 13:46:38 -08005458 return ActivityManagerNative.getDefault().getTaskForActivity(mToken, true) >= 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 } catch (RemoteException e) {
5460 return false;
5461 }
5462 }
5463
5464 /**
5465 * Move the task containing this activity to the back of the activity
5466 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07005467 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 * @param nonRoot If false then this only works if the activity is the root
5469 * of a task; if true it will work for any activity in
5470 * a task.
RoboErik55011652014-07-09 15:05:53 -07005471 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 * @return If the task was moved (or it was already at the
5473 * back) true is returned, else false.
5474 */
5475 public boolean moveTaskToBack(boolean nonRoot) {
5476 try {
5477 return ActivityManagerNative.getDefault().moveActivityTaskToBack(
5478 mToken, nonRoot);
5479 } catch (RemoteException e) {
5480 // Empty
5481 }
5482 return false;
5483 }
5484
5485 /**
5486 * Returns class name for this activity with the package prefix removed.
5487 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07005488 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 * @return The local class name.
5490 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005491 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005492 public String getLocalClassName() {
5493 final String pkg = getPackageName();
5494 final String cls = mComponent.getClassName();
5495 int packageLen = pkg.length();
5496 if (!cls.startsWith(pkg) || cls.length() <= packageLen
5497 || cls.charAt(packageLen) != '.') {
5498 return cls;
5499 }
5500 return cls.substring(packageLen+1);
5501 }
RoboErik55011652014-07-09 15:05:53 -07005502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 /**
5504 * Returns complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07005505 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 * @return Returns the complete component name for this activity
5507 */
5508 public ComponentName getComponentName()
5509 {
5510 return mComponent;
5511 }
5512
5513 /**
5514 * Retrieve a {@link SharedPreferences} object for accessing preferences
5515 * that are private to this activity. This simply calls the underlying
5516 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
5517 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07005518 *
5519 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
Jeff Sharkey634dc422016-01-30 17:44:15 -07005520 * operation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005521 *
5522 * @return Returns the single SharedPreferences instance that can be used
5523 * to retrieve and modify the preference values.
5524 */
5525 public SharedPreferences getPreferences(int mode) {
5526 return getSharedPreferences(getLocalClassName(), mode);
5527 }
RoboErik55011652014-07-09 15:05:53 -07005528
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005529 private void ensureSearchManager() {
5530 if (mSearchManager != null) {
5531 return;
5532 }
RoboErik55011652014-07-09 15:05:53 -07005533
Amith Yamasanie9ce3f02010-01-25 09:15:50 -08005534 mSearchManager = new SearchManager(this, null);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005535 }
Tor Norbyed9273d62013-05-30 15:59:53 -07005536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005538 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005539 if (getBaseContext() == null) {
5540 throw new IllegalStateException(
5541 "System services not available to Activities before onCreate()");
5542 }
5543
5544 if (WINDOW_SERVICE.equals(name)) {
5545 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005546 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005547 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005548 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005549 }
5550 return super.getSystemService(name);
5551 }
5552
5553 /**
5554 * Change the title associated with this activity. If this is a
5555 * top-level activity, the title for its window will change. If it
5556 * is an embedded activity, the parent can do whatever it wants
5557 * with it.
5558 */
5559 public void setTitle(CharSequence title) {
5560 mTitle = title;
5561 onTitleChanged(title, mTitleColor);
5562
5563 if (mParent != null) {
5564 mParent.onChildTitleChanged(this, title);
5565 }
5566 }
5567
5568 /**
5569 * Change the title associated with this activity. If this is a
5570 * top-level activity, the title for its window will change. If it
5571 * is an embedded activity, the parent can do whatever it wants
5572 * with it.
5573 */
5574 public void setTitle(int titleId) {
5575 setTitle(getText(titleId));
5576 }
5577
Alan Viverette2525d9c2013-11-15 14:42:19 -08005578 /**
5579 * Change the color of the title associated with this activity.
5580 * <p>
5581 * This method is deprecated starting in API Level 11 and replaced by action
5582 * bar styles. For information on styling the Action Bar, read the <a
5583 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
5584 * guide.
5585 *
5586 * @deprecated Use action bar styles instead.
5587 */
5588 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005589 public void setTitleColor(int textColor) {
5590 mTitleColor = textColor;
5591 onTitleChanged(mTitle, textColor);
5592 }
5593
5594 public final CharSequence getTitle() {
5595 return mTitle;
5596 }
5597
5598 public final int getTitleColor() {
5599 return mTitleColor;
5600 }
5601
5602 protected void onTitleChanged(CharSequence title, int color) {
5603 if (mTitleReady) {
5604 final Window win = getWindow();
5605 if (win != null) {
5606 win.setTitle(title);
5607 if (color != 0) {
5608 win.setTitleColor(color);
5609 }
5610 }
Adam Powellaf2d8592014-08-26 18:06:40 -07005611 if (mActionBar != null) {
5612 mActionBar.setWindowTitle(title);
5613 }
Adam Powella557fdc2014-08-21 18:05:53 -07005614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 }
5616
5617 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
5618 }
5619
5620 /**
Winson Chunga449dc02014-05-16 11:15:04 -07005621 * Sets information describing the task with this activity for presentation inside the Recents
5622 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
5623 * are traversed in order from the topmost activity to the bottommost. The traversal continues
5624 * for each property until a suitable value is found. For each task the taskDescription will be
5625 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07005626 *
5627 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07005628 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07005629 *
Winson Chunga449dc02014-05-16 11:15:04 -07005630 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07005631 */
Winson Chunga449dc02014-05-16 11:15:04 -07005632 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winsonb6403152016-02-23 13:32:09 -08005633 if (mTaskDescription != taskDescription) {
5634 mTaskDescription.copyFrom(taskDescription);
5635 // Scale the icon down to something reasonable if it is provided
5636 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
5637 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
5638 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
5639 true);
5640 mTaskDescription.setIcon(icon);
5641 }
Craig Mautner2fbd7542014-03-21 09:34:07 -07005642 }
5643 try {
Winsonb6403152016-02-23 13:32:09 -08005644 ActivityManagerNative.getDefault().setTaskDescription(mToken, mTaskDescription);
Craig Mautner2fbd7542014-03-21 09:34:07 -07005645 } catch (RemoteException e) {
5646 }
5647 }
5648
5649 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 * Sets the visibility of the progress bar in the title.
5651 * <p>
5652 * In order for the progress bar to be shown, the feature must be requested
5653 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005654 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005656 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005658 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659 public final void setProgressBarVisibility(boolean visible) {
5660 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
5661 Window.PROGRESS_VISIBILITY_OFF);
5662 }
5663
5664 /**
5665 * Sets the visibility of the indeterminate progress bar in the title.
5666 * <p>
5667 * In order for the progress bar to be shown, the feature must be requested
5668 * via {@link #requestWindowFeature(int)}.
5669 *
5670 * @param visible Whether to show the progress bars in the title.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005671 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005673 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 public final void setProgressBarIndeterminateVisibility(boolean visible) {
5675 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
5676 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
5677 }
RoboErik55011652014-07-09 15:05:53 -07005678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005679 /**
5680 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
5681 * is always indeterminate).
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 indeterminate Whether the horizontal progress bar should be indeterminate.
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 setProgressBarIndeterminate(boolean indeterminate) {
5691 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00005692 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
5693 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 }
RoboErik55011652014-07-09 15:05:53 -07005695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 /**
5697 * Sets the progress for the progress bars in the title.
5698 * <p>
5699 * In order for the progress bar to be shown, the feature must be requested
5700 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005701 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 * @param progress The progress for the progress bar. Valid ranges are from
5703 * 0 to 10000 (both inclusive). If 10000 is given, the progress
5704 * bar will be completely filled and will fade out.
Alan Viverette4aef7c82015-09-04 14:14:50 -04005705 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005707 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 public final void setProgress(int progress) {
5709 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
5710 }
RoboErik55011652014-07-09 15:05:53 -07005711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 /**
5713 * Sets the secondary progress for the progress bar in the title. This
5714 * progress is drawn between the primary progress (set via
5715 * {@link #setProgress(int)} and the background. It can be ideal for media
5716 * scenarios such as showing the buffering progress while the default
5717 * progress shows the play progress.
5718 * <p>
5719 * In order for the progress bar to be shown, the feature must be requested
5720 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
5723 * 0 to 10000 (both inclusive).
Alan Viverette4aef7c82015-09-04 14:14:50 -04005724 * @deprecated No longer supported starting in API 21.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 */
Alan Viverette4aef7c82015-09-04 14:14:50 -04005726 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 public final void setSecondaryProgress(int secondaryProgress) {
5728 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
5729 secondaryProgress + Window.PROGRESS_SECONDARY_START);
5730 }
5731
5732 /**
5733 * Suggests an audio stream whose volume should be changed by the hardware
5734 * volume controls.
5735 * <p>
5736 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07005737 * Volume requests which are received while the Activity is in the
5738 * foreground will affect this stream.
5739 * <p>
5740 * It is not guaranteed that the hardware volume controls will always change
5741 * this stream's volume (for example, if a call is in progress, its stream's
5742 * volume may be changed instead). To reset back to the default, use
5743 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
5744 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07005746 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 */
5748 public final void setVolumeControlStream(int streamType) {
5749 getWindow().setVolumeControlStream(streamType);
5750 }
5751
5752 /**
5753 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07005754 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07005755 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 * @return The suggested audio stream type whose volume should be changed by
5757 * the hardware volume controls.
5758 * @see #setVolumeControlStream(int)
5759 */
5760 public final int getVolumeControlStream() {
5761 return getWindow().getVolumeControlStream();
5762 }
RoboErik55011652014-07-09 15:05:53 -07005763
5764 /**
5765 * Sets a {@link MediaController} to send media keys and volume changes to.
5766 * <p>
5767 * The controller will be tied to the window of this Activity. Media key and
5768 * volume events which are received while the Activity is in the foreground
5769 * will be forwarded to the controller and used to invoke transport controls
5770 * or adjust the volume. This may be used instead of or in addition to
5771 * {@link #setVolumeControlStream} to affect a specific session instead of a
5772 * specific stream.
5773 * <p>
5774 * It is not guaranteed that the hardware volume controls will always change
5775 * this session's volume (for example, if a call is in progress, its
5776 * stream's volume may be changed instead). To reset back to the default use
5777 * null as the controller.
5778 *
5779 * @param controller The controller for the session which should receive
5780 * media keys and volume changes.
5781 */
5782 public final void setMediaController(MediaController controller) {
5783 getWindow().setMediaController(controller);
5784 }
5785
5786 /**
5787 * Gets the controller which should be receiving media key and volume events
5788 * while this activity is in the foreground.
5789 *
5790 * @return The controller which should receive events.
5791 * @see #setMediaController(android.media.session.MediaController)
5792 */
5793 public final MediaController getMediaController() {
5794 return getWindow().getMediaController();
5795 }
5796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 /**
5798 * Runs the specified action on the UI thread. If the current thread is the UI
5799 * thread, then the action is executed immediately. If the current thread is
5800 * not the UI thread, the action is posted to the event queue of the UI thread.
5801 *
5802 * @param action the action to run on the UI thread
5803 */
5804 public final void runOnUiThread(Runnable action) {
5805 if (Thread.currentThread() != mUiThread) {
5806 mHandler.post(action);
5807 } else {
5808 action.run();
5809 }
5810 }
5811
5812 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005813 * Standard implementation of
5814 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
5815 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005816 * This implementation does nothing and is for
5817 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
5818 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
5819 *
5820 * @see android.view.LayoutInflater#createView
5821 * @see android.view.Window#getLayoutInflater
5822 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005823 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07005824 public View onCreateView(String name, Context context, AttributeSet attrs) {
5825 return null;
5826 }
5827
5828 /**
5829 * Standard implementation of
5830 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
5831 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005832 * This implementation handles <fragment> tags to embed fragments inside
5833 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 *
5835 * @see android.view.LayoutInflater#createView
5836 * @see android.view.Window#getLayoutInflater
5837 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07005838 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005839 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07005840 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005841 }
RoboErik55011652014-07-09 15:05:53 -07005842
Adam Powell371a8092014-06-20 12:51:12 -07005843 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 }
5845
Daniel Sandler69a48172010-06-23 16:29:36 -04005846 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07005847 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07005848 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07005849 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08005850 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005851 * @param fd The raw file descriptor that the dump is being sent to.
5852 * @param writer The PrintWriter to which you should dump your state. This will be
5853 * closed for you after you return.
5854 * @param args additional arguments to the dump request.
5855 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08005856 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07005857 dumpInner(prefix, fd, writer, args);
5858 }
5859
5860 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08005861 writer.print(prefix); writer.print("Local Activity ");
5862 writer.print(Integer.toHexString(System.identityHashCode(this)));
5863 writer.println(" State:");
5864 String innerPrefix = prefix + " ";
5865 writer.print(innerPrefix); writer.print("mResumed=");
5866 writer.print(mResumed); writer.print(" mStopped=");
5867 writer.print(mStopped); writer.print(" mFinished=");
5868 writer.println(mFinished);
Dianne Hackborn30d71892010-12-11 10:37:55 -08005869 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
5870 writer.println(mChangingConfigurations);
5871 writer.print(innerPrefix); writer.print("mCurrentConfig=");
5872 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07005873
Todd Kennedya5fc6f02015-04-14 18:22:54 -07005874 mFragments.dumpLoaders(innerPrefix, fd, writer, args);
5875 mFragments.getFragmentManager().dump(innerPrefix, fd, writer, args);
Dianne Hackborn57dd7372015-07-27 18:11:14 -07005876 if (mVoiceInteractor != null) {
5877 mVoiceInteractor.dump(innerPrefix, fd, writer, args);
5878 }
Jeff Brown5182c782013-10-15 20:31:52 -07005879
Michael Wright5f48dc72013-11-01 12:42:49 -07005880 if (getWindow() != null &&
5881 getWindow().peekDecorView() != null &&
5882 getWindow().peekDecorView().getViewRootImpl() != null) {
5883 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
5884 }
Jeff Brown5182c782013-10-15 20:31:52 -07005885
5886 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07005887 }
5888
5889 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04005890 * Bit indicating that this activity is "immersive" and should not be
5891 * interrupted by notifications if possible.
5892 *
5893 * This value is initially set by the manifest property
5894 * <code>android:immersive</code> but may be changed at runtime by
5895 * {@link #setImmersive}.
5896 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07005897 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04005898 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5899 */
5900 public boolean isImmersive() {
5901 try {
5902 return ActivityManagerNative.getDefault().isImmersive(mToken);
5903 } catch (RemoteException e) {
5904 return false;
5905 }
5906 }
5907
5908 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07005909 * Indication of whether this is the highest level activity in this task. Can be used to
5910 * determine whether an activity launched by this activity was placed in the same task or
5911 * another task.
5912 *
5913 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07005914 */
George Mountff243282014-07-07 16:12:07 -07005915 private boolean isTopOfTask() {
Craig Mautnerd61dc202014-07-07 11:09:11 -07005916 try {
5917 return ActivityManagerNative.getDefault().isTopOfTask(mToken);
5918 } catch (RemoteException e) {
5919 return false;
5920 }
5921 }
5922
5923 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07005924 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
5925 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005926 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005927 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005928 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
5929 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005930 * This call has no effect on non-translucent activities or on activities with the
5931 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005932 *
George Mount62ab9b72014-05-02 13:51:17 -07005933 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
5934 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07005935 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005936 *
5937 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07005938 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005939 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07005940 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07005941 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07005942 mTranslucentCallback = null;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005943 if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) {
5944 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
5945 }
Craig Mautner4addfc52013-06-25 08:05:45 -07005946 } catch (RemoteException e) {
5947 // pass
5948 }
5949 }
5950
5951 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07005952 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
5953 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
5954 * <p>
5955 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
5956 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
5957 * be called indicating that it is safe to make this activity translucent again. Until
5958 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
5959 * behind the frontmost Activity will be indeterminate.
5960 * <p>
5961 * This call has no effect on non-translucent activities or on activities with the
5962 * {@link android.R.attr#windowIsFloating} attribute.
5963 *
5964 * @param callback the method to call when all visible Activities behind this one have been
5965 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07005966 * @param options activity options delivered to the activity below this one. The options
5967 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07005968 * @return <code>true</code> if Window was opaque and will become translucent or
5969 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005970 *
5971 * @see #convertFromTranslucent()
5972 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005973 *
5974 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07005975 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005976 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07005977 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07005978 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07005979 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005980 try {
5981 mTranslucentCallback = callback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005982 mChangeCanvasToTranslucent =
Craig Mautner233ceee2014-05-09 17:05:11 -07005983 ActivityManagerNative.getDefault().convertToTranslucent(mToken, options);
George Mount9e183972014-09-03 12:35:09 -07005984 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07005985 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005986 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07005987 // Make callback return as though it timed out.
5988 mChangeCanvasToTranslucent = false;
5989 drawComplete = false;
5990 }
5991 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
5992 // Window is already translucent.
5993 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07005994 }
George Mount3cc716c2014-06-12 16:35:35 -07005995 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005996 }
5997
5998 /** @hide */
5999 void onTranslucentConversionComplete(boolean drawComplete) {
6000 if (mTranslucentCallback != null) {
6001 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
6002 mTranslucentCallback = null;
6003 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006004 if (mChangeCanvasToTranslucent) {
6005 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
6006 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006007 }
6008
Craig Mautnereb8abf72014-07-02 15:04:09 -07006009 /** @hide */
6010 public void onNewActivityOptions(ActivityOptions options) {
6011 mActivityTransitionState.setEnterActivityOptions(this, options);
6012 if (!mStopped) {
6013 mActivityTransitionState.enterReady(this);
6014 }
6015 }
6016
Craig Mautner5eda9b32013-07-02 11:58:16 -07006017 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07006018 * Retrieve the ActivityOptions passed in from the launching activity or passed back
6019 * from an activity launched by this activity in its call to {@link
6020 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
6021 *
6022 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
6023 * @hide
6024 */
6025 ActivityOptions getActivityOptions() {
6026 try {
6027 return ActivityManagerNative.getDefault().getActivityOptions(mToken);
6028 } catch (RemoteException e) {
6029 }
6030 return null;
6031 }
6032
6033 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006034 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07006035 * this method anytime between the start of {@link #onResume()} and the return from
6036 * {@link #onPause()}. If this call is successful then the activity will remain visible after
6037 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
6038 *
6039 * <p>The actions of this call are reset each time that this activity is brought to the
6040 * front. That is, every time {@link #onResume()} is called the activity will be assumed
6041 * to not have requested visible behind. Therefore, if you want this activity to continue to
6042 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006043 *
6044 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
6045 * for dialog and translucent activities.
6046 *
Craig Mautner64ccb702014-10-01 09:38:40 -07006047 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
6048 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
6049 *
6050 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07006051 * the next call to onResume.
6052 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006053 * @param visible true to notify the system that the activity wishes to be visible behind other
6054 * translucent activities, false to indicate otherwise. Resources must be
6055 * released when passing false to this method.
Craig Mautner64ccb702014-10-01 09:38:40 -07006056 * @return the resulting visibiity state. If true the activity will remain visible beyond
6057 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
6058 * then the activity may not count on being visible behind other translucent activities,
6059 * and must stop any media playback and release resources.
6060 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
6061 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006062 *
Jose Limafcf70832014-08-27 23:09:05 -07006063 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006064 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006065 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006066 public boolean requestVisibleBehind(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006067 if (!mResumed) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006068 // Do not permit paused or stopped activities to do this.
6069 visible = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006070 }
6071 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07006072 mVisibleBehind = ActivityManagerNative.getDefault()
6073 .requestVisibleBehind(mToken, visible) && visible;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006074 } catch (RemoteException e) {
Jose Lima4b6c6692014-08-12 17:41:12 -07006075 mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006076 }
Jose Lima4b6c6692014-08-12 17:41:12 -07006077 return mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -07006078 }
6079
6080 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006081 * Called when a translucent activity over this activity is becoming opaque or another
6082 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07006083 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006084 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006085 * <p>When this method is called the activity has 500 msec to release any resources it may be
6086 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006087 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07006088 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07006089 * process. Otherwise {@link #onStop()} will be called following return.
6090 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006091 * @see #requestVisibleBehind(boolean)
6092 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006093 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006094 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07006095 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006096 mCalled = true;
6097 }
6098
6099 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006100 * Translucent activities may call this to determine if there is an activity below them that
6101 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006102 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006103 * @return true if an activity below is set to visible according to the most recent call to
6104 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006105 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006106 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006107 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006108 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07006109 * @hide
6110 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006111 @SystemApi
6112 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006113 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07006114 return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07006115 } catch (RemoteException e) {
6116 }
6117 return false;
6118 }
6119
6120 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07006121 * The topmost foreground activity will receive this call when the background visibility state
6122 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006123 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006124 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07006125 * due to a background activity finishing itself.
6126 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006127 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07006128 *
Jose Lima4b6c6692014-08-12 17:41:12 -07006129 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07006130 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07006131 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07006132 */
Jose Lima4b6c6692014-08-12 17:41:12 -07006133 @SystemApi
6134 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07006135 }
6136
6137 /**
Craig Mautner8746a472014-07-24 15:12:54 -07006138 * Activities cannot draw during the period that their windows are animating in. In order
6139 * to know when it is safe to begin drawing they can override this method which will be
6140 * called when the entering animation has completed.
6141 */
6142 public void onEnterAnimationComplete() {
6143 }
6144
6145 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08006146 * @hide
6147 */
6148 public void dispatchEnterAnimationComplete() {
6149 onEnterAnimationComplete();
6150 if (getWindow() != null && getWindow().getDecorView() != null) {
6151 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
6152 }
6153 }
6154
6155 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04006156 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07006157 *
Daniel Sandler69a48172010-06-23 16:29:36 -04006158 * Note that changing this value will have no effect on the activity's
6159 * {@link android.content.pm.ActivityInfo} structure; that is, if
6160 * <code>android:immersive</code> is set to <code>true</code>
6161 * in the application's manifest entry for this activity, the {@link
6162 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
6163 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6164 * FLAG_IMMERSIVE} bit set.
6165 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07006166 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04006167 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
6168 */
6169 public void setImmersive(boolean i) {
6170 try {
6171 ActivityManagerNative.getDefault().setImmersive(mToken, i);
6172 } catch (RemoteException e) {
6173 // pass
6174 }
6175 }
6176
Adam Powell6e346362010-07-23 10:18:23 -07006177 /**
Ruben Brunkdd18a0b2015-12-04 16:16:31 -08006178 * Enable or disable virtual reality (VR) mode.
6179 *
6180 * <p>VR mode is a hint to Android system services to switch to modes optimized for
6181 * high-performance stereoscopic rendering.</p>
6182 *
6183 * @param enabled {@code true} to enable this mode.
6184 */
6185 public void setVrMode(boolean enabled) {
6186 try {
6187 ActivityManagerNative.getDefault().setVrMode(mToken, enabled);
6188 } catch (RemoteException e) {
6189 // pass
6190 }
6191 }
6192
6193 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006194 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07006195 *
Clara Bayarri4423d912015-03-02 19:42:48 +00006196 * @param callback Callback that will manage lifecycle events for this action mode
6197 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07006198 *
6199 * @see ActionMode
6200 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006201 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07006202 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07006203 return mWindow.getDecorView().startActionMode(callback);
6204 }
6205
Adam Powelldebf3be2010-11-15 18:58:48 -08006206 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006207 * Start an action mode of the given type.
6208 *
6209 * @param callback Callback that will manage lifecycle events for this action mode
6210 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
6211 * @return The ActionMode that was started, or null if it was canceled
6212 *
6213 * @see ActionMode
6214 */
6215 @Nullable
6216 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
6217 return mWindow.getDecorView().startActionMode(callback, type);
6218 }
6219
6220 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006221 * Give the Activity a chance to control the UI for an action mode requested
6222 * by the system.
6223 *
6224 * <p>Note: If you are looking for a notification callback that an action mode
6225 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
6226 *
6227 * @param callback The callback that should control the new action mode
6228 * @return The new action mode, or <code>null</code> if the activity does not want to
6229 * provide special handling for this action mode. (It will be handled by the system.)
6230 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006231 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07006232 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006233 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00006234 // Only Primary ActionModes are represented in the ActionBar.
6235 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
6236 initWindowDecorActionBar();
6237 if (mActionBar != null) {
6238 return mActionBar.startActionMode(callback);
6239 }
Adam Powell6e346362010-07-23 10:18:23 -07006240 }
6241 return null;
6242 }
6243
Adam Powelldebf3be2010-11-15 18:58:48 -08006244 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00006245 * {@inheritDoc}
6246 */
6247 @Nullable
6248 @Override
6249 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
6250 try {
6251 mActionModeTypeStarting = type;
6252 return onWindowStartingActionMode(callback);
6253 } finally {
6254 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
6255 }
6256 }
6257
6258 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08006259 * Notifies the Activity that an action mode has been started.
6260 * Activity subclasses overriding this method should call the superclass implementation.
6261 *
6262 * @param mode The new action mode.
6263 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006264 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006265 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006266 public void onActionModeStarted(ActionMode mode) {
6267 }
6268
6269 /**
6270 * Notifies the activity that an action mode has finished.
6271 * Activity subclasses overriding this method should call the superclass implementation.
6272 *
6273 * @param mode The action mode that just finished.
6274 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08006275 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07006276 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08006277 public void onActionModeFinished(ActionMode mode) {
6278 }
6279
Adam Powelldd8fab22012-03-22 17:47:27 -07006280 /**
6281 * Returns true if the app should recreate the task when navigating 'up' from this activity
6282 * by using targetIntent.
6283 *
6284 * <p>If this method returns false the app can trivially call
6285 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
6286 * up navigation. If this method returns false, the app should synthesize a new task stack
6287 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
6288 *
6289 * @param targetIntent An intent representing the target destination for up navigation
6290 * @return true if navigating up should recreate a new task stack, false if the same task
6291 * should be used for the destination
6292 */
6293 public boolean shouldUpRecreateTask(Intent targetIntent) {
6294 try {
6295 PackageManager pm = getPackageManager();
6296 ComponentName cn = targetIntent.getComponent();
6297 if (cn == null) {
6298 cn = targetIntent.resolveActivity(pm);
6299 }
6300 ActivityInfo info = pm.getActivityInfo(cn, 0);
6301 if (info.taskAffinity == null) {
6302 return false;
6303 }
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07006304 return ActivityManagerNative.getDefault()
6305 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07006306 } catch (RemoteException e) {
6307 return false;
6308 } catch (NameNotFoundException e) {
6309 return false;
6310 }
6311 }
6312
6313 /**
6314 * Navigate from this activity to the activity specified by upIntent, finishing this activity
6315 * in the process. If the activity indicated by upIntent already exists in the task's history,
6316 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07006317 * finished.
6318 *
6319 * <p>If the indicated activity does not appear in the history stack, this will finish
6320 * each activity in this task until the root activity of the task is reached, resulting in
6321 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
6322 * when an activity may be reached by a path not passing through a canonical parent
6323 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07006324 *
6325 * <p>This method should be used when performing up navigation from within the same task
6326 * as the destination. If up navigation should cross tasks in some cases, see
6327 * {@link #shouldUpRecreateTask(Intent)}.</p>
6328 *
6329 * @param upIntent An intent representing the target destination for up navigation
6330 *
6331 * @return true if up navigation successfully reached the activity indicated by upIntent and
6332 * upIntent was delivered to it. false if an instance of the indicated activity could
6333 * not be found and this activity was simply finished normally.
6334 */
6335 public boolean navigateUpTo(Intent upIntent) {
6336 if (mParent == null) {
6337 ComponentName destInfo = upIntent.getComponent();
6338 if (destInfo == null) {
6339 destInfo = upIntent.resolveActivity(getPackageManager());
6340 if (destInfo == null) {
6341 return false;
6342 }
6343 upIntent = new Intent(upIntent);
6344 upIntent.setComponent(destInfo);
6345 }
6346 int resultCode;
6347 Intent resultData;
6348 synchronized (this) {
6349 resultCode = mResultCode;
6350 resultData = mResultData;
6351 }
6352 if (resultData != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006353 resultData.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006354 }
6355 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07006356 upIntent.prepareToLeaveProcess(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07006357 return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent,
6358 resultCode, resultData);
6359 } catch (RemoteException e) {
6360 return false;
6361 }
6362 } else {
6363 return mParent.navigateUpToFromChild(this, upIntent);
6364 }
6365 }
6366
6367 /**
6368 * This is called when a child activity of this one calls its
6369 * {@link #navigateUpTo} method. The default implementation simply calls
6370 * navigateUpTo(upIntent) on this activity (the parent).
6371 *
6372 * @param child The activity making the call.
6373 * @param upIntent An intent representing the target destination for up navigation
6374 *
6375 * @return true if up navigation successfully reached the activity indicated by upIntent and
6376 * upIntent was delivered to it. false if an instance of the indicated activity could
6377 * not be found and this activity was simply finished normally.
6378 */
6379 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
6380 return navigateUpTo(upIntent);
6381 }
6382
6383 /**
6384 * Obtain an {@link Intent} that will launch an explicit target activity specified by
6385 * this activity's logical parent. The logical parent is named in the application's manifest
6386 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07006387 * Activity subclasses may override this method to modify the Intent returned by
6388 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
6389 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07006390 *
Adam Powell04d58112012-04-09 10:22:12 -07006391 * @return a new Intent targeting the defined parent of this activity or null if
6392 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07006393 */
Tor Norbyed9273d62013-05-30 15:59:53 -07006394 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07006395 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07006396 final String parentName = mActivityInfo.parentActivityName;
6397 if (TextUtils.isEmpty(parentName)) {
6398 return null;
6399 }
Adam Powell5a4010c2012-09-16 15:14:05 -07006400
6401 // If the parent itself has no parent, generate a main activity intent.
6402 final ComponentName target = new ComponentName(this, parentName);
6403 try {
6404 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
6405 final String parentActivity = parentInfo.parentActivityName;
6406 final Intent parentIntent = parentActivity == null
6407 ? Intent.makeMainActivity(target)
6408 : new Intent().setComponent(target);
6409 return parentIntent;
6410 } catch (NameNotFoundException e) {
6411 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
6412 "' in manifest");
6413 return null;
6414 }
Adam Powelldd8fab22012-03-22 17:47:27 -07006415 }
6416
George Mount31a21722014-03-24 17:44:36 -07006417 /**
George Mount62ab9b72014-05-02 13:51:17 -07006418 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006419 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006420 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07006421 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07006422 *
George Mount65580562014-08-29 08:15:48 -07006423 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07006424 */
George Mount65580562014-08-29 08:15:48 -07006425 public void setEnterSharedElementCallback(SharedElementCallback callback) {
6426 if (callback == null) {
6427 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07006428 }
George Mount65580562014-08-29 08:15:48 -07006429 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07006430 }
6431
6432 /**
6433 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07006434 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07006435 * will be called to handle shared elements on the <i>launching</i> Activity. Most
6436 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07006437 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07006438 *
George Mount65580562014-08-29 08:15:48 -07006439 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07006440 */
George Mount65580562014-08-29 08:15:48 -07006441 public void setExitSharedElementCallback(SharedElementCallback callback) {
6442 if (callback == null) {
6443 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07006444 }
George Mount65580562014-08-29 08:15:48 -07006445 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07006446 }
6447
George Mount8c2614c2014-06-10 11:12:01 -07006448 /**
6449 * Postpone the entering activity transition when Activity was started with
6450 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6451 * android.util.Pair[])}.
6452 * <p>This method gives the Activity the ability to delay starting the entering and
6453 * shared element transitions until all data is loaded. Until then, the Activity won't
6454 * draw into its window, leaving the window transparent. This may also cause the
6455 * returning animation to be delayed until data is ready. This method should be
6456 * called in {@link #onCreate(android.os.Bundle)} or in
6457 * {@link #onActivityReenter(int, android.content.Intent)}.
6458 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
6459 * start the transitions. If the Activity did not use
6460 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
6461 * android.util.Pair[])}, then this method does nothing.</p>
6462 */
6463 public void postponeEnterTransition() {
6464 mActivityTransitionState.postponeEnterTransition();
6465 }
6466
6467 /**
6468 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
6469 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
6470 * to have your Activity start drawing.
6471 */
6472 public void startPostponedEnterTransition() {
6473 mActivityTransitionState.startPostponedEnterTransition();
6474 }
6475
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -08006476 /**
6477 * Create {@link DropPermissions} object bound to this activity and controlling the access
6478 * permissions for content URIs associated with the {@link DragEvent}.
6479 * @param event Drag event
6480 * @return The DropPermissions object used to control access to the content URIs. Null if
6481 * no content URIs are associated with the event or if permissions could not be granted.
6482 */
6483 public DropPermissions requestDropPermissions(DragEvent event) {
6484 DropPermissions dropPermissions = DropPermissions.obtain(event);
6485 if (dropPermissions != null && dropPermissions.take(getActivityToken())) {
6486 return dropPermissions;
6487 }
6488 return null;
6489 }
6490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07006492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 final void setParent(Activity parent) {
6494 mParent = parent;
6495 }
6496
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006497 final void attach(Context context, ActivityThread aThread,
6498 Instrumentation instr, IBinder token, int ident,
6499 Application application, Intent intent, ActivityInfo info,
6500 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006501 NonConfigurationInstances lastNonConfigurationInstances,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006502 Configuration config, String referrer, IVoiceInteractor voiceInteractor,
6503 Window window) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504 attachBaseContext(context);
6505
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006506 mFragments.attachHost(null /*parent*/);
RoboErik55011652014-07-09 15:05:53 -07006507
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006508 mWindow = new PhoneWindow(this, window);
Skuhnece2faa52015-08-11 10:36:38 -07006509 mWindow.setWindowControllerCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07006511 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08006512 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006513 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
6514 mWindow.setSoftInputMode(info.softInputMode);
6515 }
Adam Powell269248d2011-08-02 10:26:54 -07006516 if (info.uiOptions != 0) {
6517 mWindow.setUiOptions(info.uiOptions);
6518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006519 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08006520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 mMainThread = aThread;
6522 mInstrumentation = instr;
6523 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006524 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525 mApplication = application;
6526 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006527 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006528 mComponent = intent.getComponent();
6529 mActivityInfo = info;
6530 mTitle = title;
6531 mParent = parent;
6532 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006533 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07006534 if (voiceInteractor != null) {
6535 if (lastNonConfigurationInstances != null) {
6536 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
6537 } else {
6538 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
6539 Looper.myLooper());
6540 }
6541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006542
Jeff Brown98365d72012-08-19 20:30:52 -07006543 mWindow.setWindowManager(
6544 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
6545 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07006546 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006547 if (mParent != null) {
6548 mWindow.setContainer(mParent.getWindow());
6549 }
6550 mWindowManager = mWindow.getWindowManager();
6551 mCurrentConfig = config;
6552 }
6553
Dianne Hackborn5320eb82012-05-18 12:05:04 -07006554 /** @hide */
6555 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 return mParent != null ? mParent.getActivityToken() : mToken;
6557 }
6558
Craig Mautnera0026042014-04-23 11:45:37 -07006559 final void performCreateCommon() {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006560 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
6561 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07006562 mFragments.dispatchActivityCreated();
George Mount62ab9b72014-05-02 13:51:17 -07006563 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006564 }
George Mount0a778ed2013-12-13 13:35:36 -08006565
Craig Mautnera0026042014-04-23 11:45:37 -07006566 final void performCreate(Bundle icicle) {
Svetoslavffb32b12015-10-15 16:54:00 -07006567 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006568 onCreate(icicle);
George Mount62ab9b72014-05-02 13:51:17 -07006569 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006570 performCreateCommon();
6571 }
6572
6573 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
Svetoslavffb32b12015-10-15 16:54:00 -07006574 restoreHasCurrentPermissionRequest(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006575 onCreate(icicle, persistentState);
George Mount62ab9b72014-05-02 13:51:17 -07006576 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006577 performCreateCommon();
6578 }
6579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 final void performStart() {
George Mount62ab9b72014-05-02 13:51:17 -07006581 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006582 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006583 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07006584 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006585 mInstrumentation.callActivityOnStart(this);
6586 if (!mCalled) {
6587 throw new SuperNotCalledException(
6588 "Activity " + mComponent.toShortString() +
6589 " did not call through to super.onStart()");
6590 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006591 mFragments.dispatchStart();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006592 mFragments.reportLoaderStart();
Dimitry Ivanov4449ef52016-02-25 17:41:13 -08006593
6594 // This property is set for all builds except final release
6595 boolean isDlwarningEnabled = SystemProperties.getInt("ro.bionic.ld.warning", 0) == 1;
6596 boolean isAppDebuggable =
6597 (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
6598
6599 if (isAppDebuggable || isDlwarningEnabled) {
6600 String dlwarning = getDlWarning();
6601 if (dlwarning != null) {
6602 String appName = getString(mApplication.getApplicationInfo().labelRes);
6603 String warning = "Detected problems with app native libraries\n" +
6604 "(please consult log for detail):\n" + dlwarning;
6605 if (isAppDebuggable) {
6606 new AlertDialog.Builder(this).
6607 setTitle(appName).
6608 setMessage(warning).
6609 setPositiveButton(android.R.string.ok, null).
6610 setCancelable(false).
6611 show();
6612 } else {
6613 Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
6614 }
6615 }
6616 }
6617
George Mount62ab9b72014-05-02 13:51:17 -07006618 mActivityTransitionState.enterReady(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 }
RoboErik55011652014-07-09 15:05:53 -07006620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 final void performRestart() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006622 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07006623
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006624 if (mToken != null && mParent == null) {
Chong Zhang7687f252016-02-26 12:03:33 -08006625 // No need to check mStopped, the roots will check if they were actually stopped.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07006626 WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
6627 }
6628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006629 if (mStopped) {
6630 mStopped = false;
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006631
6632 synchronized (mManagedCursors) {
6633 final int N = mManagedCursors.size();
6634 for (int i=0; i<N; i++) {
6635 ManagedCursor mc = mManagedCursors.get(i);
6636 if (mc.mReleased || mc.mUpdated) {
6637 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07006638 if (getApplicationInfo().targetSdkVersion
6639 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
6640 throw new IllegalStateException(
6641 "trying to requery an already closed cursor "
6642 + mc.mCursor);
6643 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006644 }
6645 mc.mReleased = false;
6646 mc.mUpdated = false;
6647 }
6648 }
6649 }
6650
6651 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 mInstrumentation.callActivityOnRestart(this);
6653 if (!mCalled) {
6654 throw new SuperNotCalledException(
6655 "Activity " + mComponent.toShortString() +
6656 " did not call through to super.onRestart()");
6657 }
6658 performStart();
6659 }
6660 }
RoboErik55011652014-07-09 15:05:53 -07006661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 final void performResume() {
6663 performRestart();
RoboErik55011652014-07-09 15:05:53 -07006664
Dianne Hackborn445646c2010-06-25 15:52:59 -07006665 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006666
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006667 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07006668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06006670 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 mInstrumentation.callActivityOnResume(this);
6672 if (!mCalled) {
6673 throw new SuperNotCalledException(
6674 "Activity " + mComponent.toShortString() +
6675 " did not call through to super.onResume()");
6676 }
6677
Todd Kennedyee8c9c62014-12-10 14:22:59 -08006678 // invisible activities must be finished before onResume() completes
6679 if (!mVisibleFromClient && !mFinished) {
6680 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
6681 if (getApplicationInfo().targetSdkVersion
6682 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
6683 throw new IllegalStateException(
6684 "Activity " + mComponent.toShortString() +
6685 " did not call finish() prior to onResume() completing");
6686 }
6687 }
6688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07006691
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006692 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07006693 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006695 onPostResume();
6696 if (!mCalled) {
6697 throw new SuperNotCalledException(
6698 "Activity " + mComponent.toShortString() +
6699 " did not call through to super.onPostResume()");
6700 }
6701 }
6702
6703 final void performPause() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006704 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006705 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006706 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006707 onPause();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006708 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006709 if (!mCalled && getApplicationInfo().targetSdkVersion
6710 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
6711 throw new SuperNotCalledException(
6712 "Activity " + mComponent.toShortString() +
6713 " did not call through to super.onPause()");
6714 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06006715 mResumed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006716 }
RoboErik55011652014-07-09 15:05:53 -07006717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006718 final void performUserLeaving() {
6719 onUserInteraction();
6720 onUserLeaveHint();
6721 }
RoboErik55011652014-07-09 15:05:53 -07006722
Chong Zhang7687f252016-02-26 12:03:33 -08006723 final void performStop(boolean preserveWindow) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006724 mDoReportFullyDrawn = false;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006725 mFragments.doLoaderStop(mChangingConfigurations /*retain*/);
RoboErik55011652014-07-09 15:05:53 -07006726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006727 if (!mStopped) {
6728 if (mWindow != null) {
6729 mWindow.closeAllPanels();
6730 }
6731
Chong Zhang7687f252016-02-26 12:03:33 -08006732 // If we're preserving the window, don't setStoppedState to true, since we
6733 // need the window started immediately again. Stopping the window will
6734 // destroys hardware resources and causes flicker.
6735 if (!preserveWindow && mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07006736 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08006737 }
RoboErik55011652014-07-09 15:05:53 -07006738
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006739 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07006740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006741 mCalled = false;
6742 mInstrumentation.callActivityOnStop(this);
6743 if (!mCalled) {
6744 throw new SuperNotCalledException(
6745 "Activity " + mComponent.toShortString() +
6746 " did not call through to super.onStop()");
6747 }
RoboErik55011652014-07-09 15:05:53 -07006748
Makoto Onuki2f6a0182010-02-22 13:26:59 -08006749 synchronized (mManagedCursors) {
6750 final int N = mManagedCursors.size();
6751 for (int i=0; i<N; i++) {
6752 ManagedCursor mc = mManagedCursors.get(i);
6753 if (!mc.mReleased) {
6754 mc.mCursor.deactivate();
6755 mc.mReleased = true;
6756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 }
6758 }
George Mount0a778ed2013-12-13 13:35:36 -08006759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006760 mStopped = true;
6761 }
6762 mResumed = false;
6763 }
6764
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006765 final void performDestroy() {
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006766 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07006767 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006768 mFragments.dispatchDestroy();
6769 onDestroy();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006770 mFragments.doLoaderDestroy();
Dianne Hackborn20d94742014-05-29 18:35:45 -07006771 if (mVoiceInteractor != null) {
6772 mVoiceInteractor.detachActivity();
6773 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006774 }
George Mount0a778ed2013-12-13 13:35:36 -08006775
6776 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06006777 * @hide
6778 */
6779 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006780 return mResumed;
6781 }
6782
Svetoslavffb32b12015-10-15 16:54:00 -07006783 private void storeHasCurrentPermissionRequest(Bundle bundle) {
6784 if (bundle != null && mHasCurrentPermissionsRequest) {
6785 bundle.putBoolean(HAS_CURENT_PERMISSIONS_REQUEST_KEY, true);
6786 }
6787 }
6788
6789 private void restoreHasCurrentPermissionRequest(Bundle bundle) {
6790 if (bundle != null) {
6791 mHasCurrentPermissionsRequest = bundle.getBoolean(
6792 HAS_CURENT_PERMISSIONS_REQUEST_KEY, false);
6793 }
6794 }
6795
George Mount0a778ed2013-12-13 13:35:36 -08006796 void dispatchActivityResult(String who, int requestCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 int resultCode, Intent data) {
Joe Onorato43a17652011-04-06 19:22:23 -07006798 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006799 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
6800 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006801 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 if (who == null) {
Svetoslav970b59c2015-06-09 16:05:21 -07006803 onActivityResult(requestCode, resultCode, data);
6804 } else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
6805 who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
6806 if (TextUtils.isEmpty(who)) {
Svetoslavc6d1c342015-02-26 14:44:43 -08006807 dispatchRequestPermissionsResult(requestCode, data);
6808 } else {
Clara Bayarrid5bf3ed2015-03-27 17:32:45 +00006809 Fragment frag = mFragments.findFragmentByWho(who);
6810 if (frag != null) {
Svetoslav970b59c2015-06-09 16:05:21 -07006811 dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
Svetoslavc6d1c342015-02-26 14:44:43 -08006812 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006813 }
Svetoslav970b59c2015-06-09 16:05:21 -07006814 } else if (who.startsWith("@android:view:")) {
6815 ArrayList<ViewRootImpl> views = WindowManagerGlobal.getInstance().getRootViews(
6816 getActivityToken());
6817 for (ViewRootImpl viewRoot : views) {
6818 if (viewRoot.getView() != null
6819 && viewRoot.getView().dispatchActivityResult(
6820 who, requestCode, resultCode, data)) {
6821 return;
6822 }
6823 }
6824 } else {
6825 Fragment frag = mFragments.findFragmentByWho(who);
6826 if (frag != null) {
6827 frag.onActivityResult(requestCode, resultCode, data);
6828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006829 }
6830 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006831
Jason Monka57e5e02014-05-07 10:06:48 -04006832 /**
Jason Monk62515be2014-05-21 16:06:19 -04006833 * Request to put this Activity in a mode where the user is locked to the
Jason Monka57e5e02014-05-07 10:06:48 -04006834 * current task.
6835 *
Craig Mautner15df08a2015-04-01 12:17:18 -07006836 * This will prevent the user from launching other apps, going to settings, or reaching the
6837 * home screen. This does not include those apps whose {@link android.R.attr#lockTaskMode}
6838 * values permit launching while locked.
Jason Monka57e5e02014-05-07 10:06:48 -04006839 *
Craig Mautner15df08a2015-04-01 12:17:18 -07006840 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true or
6841 * lockTaskMode=lockTaskModeAlways for this component then the app will go directly into
6842 * Lock Task mode. The user will not be able to exit this mode until
6843 * {@link Activity#stopLockTask()} is called.
Jason Monk62515be2014-05-21 16:06:19 -04006844 *
6845 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false
6846 * then the system will prompt the user with a dialog requesting permission to enter
Jason Monk8863b572014-07-29 13:49:32 -04006847 * this mode. When entered through this method the user can exit at any time through
6848 * an action described by the request dialog. Calling stopLockTask will also exit the
6849 * mode.
Craig Mautner15df08a2015-04-01 12:17:18 -07006850 *
6851 * @see android.R.attr#lockTaskMode
Jason Monka57e5e02014-05-07 10:06:48 -04006852 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006853 public void startLockTask() {
6854 try {
6855 ActivityManagerNative.getDefault().startLockTaskMode(mToken);
6856 } catch (RemoteException e) {
6857 }
6858 }
6859
Jason Monka57e5e02014-05-07 10:06:48 -04006860 /**
6861 * Allow the user to switch away from the current task.
6862 *
6863 * Called to end the mode started by {@link Activity#startLockTask}. This
6864 * can only be called by activities that have successfully called
6865 * startLockTask previously.
6866 *
6867 * This will allow the user to exit this app and move onto other activities.
Craig Mautner15df08a2015-04-01 12:17:18 -07006868 * <p>Note: This method should only be called when the activity is user-facing. That is,
6869 * between onResume() and onPause().
6870 * <p>Note: If there are other tasks below this one that are also locked then calling this
6871 * method will immediately finish this task and resume the previous locked one, remaining in
6872 * lockTask mode.
6873 *
6874 * @see android.R.attr#lockTaskMode
6875 * @see ActivityManager#getLockTaskModeState()
Jason Monka57e5e02014-05-07 10:06:48 -04006876 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006877 public void stopLockTask() {
6878 try {
6879 ActivityManagerNative.getDefault().stopLockTaskMode();
6880 } catch (RemoteException e) {
6881 }
6882 }
6883
Craig Mautner5eda9b32013-07-02 11:58:16 -07006884 /**
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07006885 * Shows the user the system defined message for telling the user how to exit
6886 * lock task mode. The task containing this activity must be in lock task mode at the time
6887 * of this call for the message to be displayed.
6888 */
6889 public void showLockTaskEscapeMessage() {
6890 try {
6891 ActivityManagerNative.getDefault().showLockTaskEscapeMessage(mToken);
6892 } catch (RemoteException e) {
6893 }
6894 }
6895
6896 /**
Filip Gruszczynski63250652015-11-18 14:43:01 -08006897 * Set whether the caption should displayed directly on the content rather than push it down.
6898 *
6899 * This affects only freeform windows since they display the caption and only the main
6900 * window of the activity. The caption is used to drag the window around and also shows
6901 * maximize and close action buttons.
6902 */
6903 public void overlayWithDecorCaption(boolean overlay) {
6904 mWindow.setOverlayDecorCaption(overlay);
6905 }
6906
6907 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07006908 * Interface for informing a translucent {@link Activity} once all visible activities below it
6909 * have completed drawing. This is necessary only after an {@link Activity} has been made
6910 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
6911 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07006912 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6913 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07006914 *
6915 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006916 */
Jose Lima14914852014-08-14 09:14:12 -07006917 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006918 public interface TranslucentConversionListener {
6919 /**
6920 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
6921 * below the top one have been redrawn. Following this callback it is safe to make the top
6922 * Activity translucent because the underlying Activity has been drawn.
6923 *
6924 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
6925 * occurred waiting for the Activity to complete drawing.
6926 *
6927 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07006928 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006929 */
6930 public void onTranslucentConversionComplete(boolean drawComplete);
6931 }
Svetoslavc6d1c342015-02-26 14:44:43 -08006932
6933 private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
Svetoslavffb32b12015-10-15 16:54:00 -07006934 mHasCurrentPermissionsRequest = false;
Svet Ganov6a166af2015-06-30 10:15:44 -07006935 // If the package installer crashed we may have not data - best effort.
6936 String[] permissions = (data != null) ? data.getStringArrayExtra(
6937 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
6938 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
6939 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
Svetoslavc6d1c342015-02-26 14:44:43 -08006940 onRequestPermissionsResult(requestCode, permissions, grantResults);
6941 }
6942
6943 private void dispatchRequestPermissionsResultToFragment(int requestCode, Intent data,
Svet Ganov6a166af2015-06-30 10:15:44 -07006944 Fragment fragment) {
6945 // If the package installer crashed we may have not data - best effort.
6946 String[] permissions = (data != null) ? data.getStringArrayExtra(
6947 PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
6948 final int[] grantResults = (data != null) ? data.getIntArrayExtra(
6949 PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
6950 fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
Svetoslavc6d1c342015-02-26 14:44:43 -08006951 }
6952
Todd Kennedya5fc6f02015-04-14 18:22:54 -07006953 class HostCallbacks extends FragmentHostCallback<Activity> {
6954 public HostCallbacks() {
6955 super(Activity.this /*activity*/);
6956 }
6957
6958 @Override
6959 public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
6960 Activity.this.dump(prefix, fd, writer, args);
6961 }
6962
6963 @Override
6964 public boolean onShouldSaveFragmentState(Fragment fragment) {
6965 return !isFinishing();
6966 }
6967
6968 @Override
6969 public LayoutInflater onGetLayoutInflater() {
6970 final LayoutInflater result = Activity.this.getLayoutInflater();
6971 if (onUseFragmentManagerInflaterFactory()) {
6972 return result.cloneInContext(Activity.this);
6973 }
6974 return result;
6975 }
6976
6977 @Override
6978 public boolean onUseFragmentManagerInflaterFactory() {
6979 // Newer platform versions use the child fragment manager's LayoutInflaterFactory.
6980 return getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
6981 }
6982
6983 @Override
6984 public Activity onGetHost() {
6985 return Activity.this;
6986 }
6987
6988 @Override
6989 public void onInvalidateOptionsMenu() {
6990 Activity.this.invalidateOptionsMenu();
6991 }
6992
6993 @Override
6994 public void onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode,
6995 Bundle options) {
6996 Activity.this.startActivityFromFragment(fragment, intent, requestCode, options);
6997 }
6998
6999 @Override
Svetoslav970b59c2015-06-09 16:05:21 -07007000 public void onRequestPermissionsFromFragment(Fragment fragment, String[] permissions,
7001 int requestCode) {
7002 String who = REQUEST_PERMISSIONS_WHO_PREFIX + fragment.mWho;
7003 Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
7004 startActivityForResult(who, intent, requestCode, null);
7005 }
7006
7007 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007008 public boolean onHasWindowAnimations() {
7009 return getWindow() != null;
7010 }
7011
7012 @Override
7013 public int onGetWindowAnimations() {
7014 final Window w = getWindow();
7015 return (w == null) ? 0 : w.getAttributes().windowAnimations;
7016 }
7017
Todd Kennedy434bd652015-05-04 12:29:50 -07007018 @Override
7019 public void onAttachFragment(Fragment fragment) {
7020 Activity.this.onAttachFragment(fragment);
7021 }
7022
Todd Kennedya5fc6f02015-04-14 18:22:54 -07007023 @Nullable
7024 @Override
7025 public View onFindViewById(int id) {
7026 return Activity.this.findViewById(id);
7027 }
7028
7029 @Override
7030 public boolean onHasView() {
7031 final Window w = getWindow();
7032 return (w != null && w.peekDecorView() != null);
7033 }
7034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007035}