blob: dc8380219cc760a378392b395bb131ef83a63db6 [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
Tor Norbyed9273d62013-05-30 15:59:53 -070019import android.annotation.NonNull;
Craig Mautnera0026042014-04-23 11:45:37 -070020import android.os.PersistableBundle;
Adam Powellcfbe9be2013-11-06 14:58:58 -080021import android.transition.Scene;
Adam Powellcfbe9be2013-11-06 14:58:58 -080022import android.transition.TransitionManager;
Dianne Hackborn3e82ba12013-07-16 13:23:55 -070023import android.util.ArrayMap;
Adam Powell14874662013-07-18 19:42:41 -070024import android.util.SuperNotCalledException;
Adam Powelle43340c2014-03-17 19:10:43 -070025import android.widget.Toolbar;
RoboErik55011652014-07-09 15:05:53 -070026
Dianne Hackborn91097de2014-04-04 18:02:06 -070027import com.android.internal.app.IVoiceInteractor;
Adam Powelle43340c2014-03-17 19:10:43 -070028import com.android.internal.app.WindowDecorActionBar;
29import com.android.internal.app.ToolbarActionBar;
Adam Powell6e346362010-07-23 10:18:23 -070030import com.android.internal.policy.PolicyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070031
Tor Norbyed9273d62013-05-30 15:59:53 -070032import android.annotation.IntDef;
33import android.annotation.Nullable;
Jose Lima4b6c6692014-08-12 17:41:12 -070034import android.annotation.SystemApi;
Jason Monk62515be2014-05-21 16:06:19 -040035import android.app.admin.DevicePolicyManager;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070036import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.ComponentName;
38import android.content.ContentResolver;
39import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050040import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070041import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070042import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070043import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.content.SharedPreferences;
45import android.content.pm.ActivityInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070046import android.content.pm.PackageManager;
47import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.res.Configuration;
49import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070050import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.database.Cursor;
52import android.graphics.Bitmap;
53import android.graphics.Canvas;
54import android.graphics.drawable.Drawable;
55import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070056import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.net.Uri;
Dianne Hackborn8d374262009-09-14 21:21:52 -070058import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Handler;
61import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080062import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070063import android.os.Parcelable;
svetoslavganov75986cf2009-05-14 22:28:01 -070064import android.os.RemoteException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080065import android.os.StrictMode;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070066import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070069import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.text.method.TextKeyListener;
71import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.util.EventLog;
73import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070074import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070075import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.util.SparseArray;
Adam Powell6e346362010-07-23 10:18:23 -070077import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070079import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.view.ContextThemeWrapper;
81import android.view.KeyEvent;
82import android.view.LayoutInflater;
83import android.view.Menu;
84import android.view.MenuInflater;
85import android.view.MenuItem;
86import android.view.MotionEvent;
87import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -070088import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -070090import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.view.ViewManager;
92import android.view.Window;
93import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070094import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -070095import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.widget.AdapterView;
97
Dianne Hackborn625ac272010-09-17 18:29:22 -070098import java.io.FileDescriptor;
99import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700100import java.lang.annotation.Retention;
101import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700102import java.util.ArrayList;
103import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
105/**
106 * An activity is a single, focused thing that the user can do. Almost all
107 * activities interact with the user, so the Activity class takes care of
108 * creating a window for you in which you can place your UI with
109 * {@link #setContentView}. While activities are often presented to the user
110 * as full-screen windows, they can also be used in other ways: as floating
111 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
112 * or embedded inside of another activity (using {@link ActivityGroup}).
113 *
114 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700115 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * <ul>
117 * <li> {@link #onCreate} is where you initialize your activity. Most
118 * importantly, here you will usually call {@link #setContentView(int)}
119 * with a layout resource defining your UI, and using {@link #findViewById}
120 * to retrieve the widgets in that UI that you need to interact with
121 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700122 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 * <li> {@link #onPause} is where you deal with the user leaving your
124 * activity. Most importantly, any changes made by the user should at this
125 * point be committed (usually to the
126 * {@link android.content.ContentProvider} holding the data).
127 * </ul>
128 *
129 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
130 * activity classes must have a corresponding
131 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
132 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700133 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * <p>Topics covered here:
135 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700136 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
138 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
139 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
140 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
141 * <li><a href="#Permissions">Permissions</a>
142 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
143 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700144 *
145 * <div class="special reference">
146 * <h3>Developer Guides</h3>
147 * <p>The Activity class is an important part of an application's overall lifecycle,
148 * and the way activities are launched and put together is a fundamental
149 * part of the platform's application model. For a detailed perspective on the structure of an
150 * Android application and how activities behave, please read the
151 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
152 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
153 * developer guides.</p>
154 *
155 * <p>You can also find a detailed discussion about how to create activities in the
156 * <a href="{@docRoot}guide/topics/fundamentals/activities.html">Activities</a>
157 * developer guide.</p>
158 * </div>
159 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700160 * <a name="Fragments"></a>
161 * <h3>Fragments</h3>
162 *
163 * <p>Starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}, Activity
164 * implementations can make use of the {@link Fragment} class to better
165 * modularize their code, build more sophisticated user interfaces for larger
166 * screens, and help scale their application between small and large screens.
167 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 * <a name="ActivityLifecycle"></a>
169 * <h3>Activity Lifecycle</h3>
170 *
171 * <p>Activities in the system are managed as an <em>activity stack</em>.
172 * When a new activity is started, it is placed on the top of the stack
173 * and becomes the running activity -- the previous activity always remains
174 * below it in the stack, and will not come to the foreground again until
175 * the new activity exits.</p>
RoboErik55011652014-07-09 15:05:53 -0700176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * <p>An activity has essentially four states:</p>
178 * <ul>
179 * <li> If an activity in the foreground of the screen (at the top of
180 * the stack),
181 * it is <em>active</em> or <em>running</em>. </li>
182 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
RoboErik55011652014-07-09 15:05:53 -0700183 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * is <em>paused</em>. A paused activity is completely alive (it
185 * maintains all state and member information and remains attached to
186 * the window manager), but can be killed by the system in extreme
187 * low memory situations.
188 * <li>If an activity is completely obscured by another activity,
189 * it is <em>stopped</em>. It still retains all state and member information,
190 * however, it is no longer visible to the user so its window is hidden
191 * and it will often be killed by the system when memory is needed
192 * elsewhere.</li>
193 * <li>If an activity is paused or stopped, the system can drop the activity
194 * from memory by either asking it to finish, or simply killing its
195 * process. When it is displayed again to the user, it must be
196 * completely restarted and restored to its previous state.</li>
197 * </ul>
198 *
199 * <p>The following diagram shows the important state paths of an Activity.
200 * The square rectangles represent callback methods you can implement to
201 * perform operations when the Activity moves between states. The colored
202 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * <p><img src="../../../images/activity_lifecycle.png"
205 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700206 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <p>There are three key loops you may be interested in monitoring within your
208 * activity:
RoboErik55011652014-07-09 15:05:53 -0700209 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * <ul>
211 * <li>The <b>entire lifetime</b> of an activity happens between the first call
212 * to {@link android.app.Activity#onCreate} through to a single final call
213 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
214 * of "global" state in onCreate(), and release all remaining resources in
215 * onDestroy(). For example, if it has a thread running in the background
216 * to download data from the network, it may create that thread in onCreate()
217 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700218 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <li>The <b>visible lifetime</b> of an activity happens between a call to
220 * {@link android.app.Activity#onStart} until a corresponding call to
221 * {@link android.app.Activity#onStop}. During this time the user can see the
222 * activity on-screen, though it may not be in the foreground and interacting
223 * with the user. Between these two methods you can maintain resources that
224 * are needed to show the activity to the user. For example, you can register
225 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900226 * that impact your UI, and unregister it in onStop() when the user no
227 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * can be called multiple times, as the activity becomes visible and hidden
229 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700230 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
232 * {@link android.app.Activity#onResume} until a corresponding call to
233 * {@link android.app.Activity#onPause}. During this time the activity is
234 * in front of all other activities and interacting with the user. An activity
235 * can frequently go between the resumed and paused states -- for example when
236 * the device goes to sleep, when an activity result is delivered, when a new
237 * intent is delivered -- so the code in these methods should be fairly
238 * lightweight.
239 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700240 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 * <p>The entire lifecycle of an activity is defined by the following
242 * Activity methods. All of these are hooks that you can override
243 * to do appropriate work when the activity changes state. All
244 * activities will implement {@link android.app.Activity#onCreate}
245 * to do their initial setup; many will also implement
246 * {@link android.app.Activity#onPause} to commit changes to data and
247 * otherwise prepare to stop interacting with the user. You should always
248 * call up to your superclass when implementing these methods.</p>
249 *
250 * </p>
251 * <pre class="prettyprint">
252 * public class Activity extends ApplicationContext {
253 * protected void onCreate(Bundle savedInstanceState);
254 *
255 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700256 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 * protected void onRestart();
258 *
259 * protected void onResume();
260 *
261 * protected void onPause();
262 *
263 * protected void onStop();
264 *
265 * protected void onDestroy();
266 * }
267 * </pre>
268 *
269 * <p>In general the movement through an activity's lifecycle looks like
270 * this:</p>
271 *
272 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
273 * <colgroup align="left" span="3" />
274 * <colgroup align="left" />
275 * <colgroup align="center" />
276 * <colgroup align="center" />
277 *
278 * <thead>
279 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
280 * </thead>
281 *
282 * <tbody>
283 * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onCreate onCreate()}</th>
284 * <td>Called when the activity is first created.
285 * This is where you should do all of your normal static set up:
286 * create views, bind data to lists, etc. This method also
287 * provides you with a Bundle containing the activity's previously
288 * frozen state, if there was one.
289 * <p>Always followed by <code>onStart()</code>.</td>
290 * <td align="center">No</td>
291 * <td align="center"><code>onStart()</code></td>
292 * </tr>
293 *
294 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
295 * <th colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</th>
296 * <td>Called after your activity has been stopped, prior to it being
297 * started again.
298 * <p>Always followed by <code>onStart()</code></td>
299 * <td align="center">No</td>
300 * <td align="center"><code>onStart()</code></td>
301 * </tr>
302 *
303 * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStart onStart()}</th>
304 * <td>Called when the activity is becoming visible to the user.
305 * <p>Followed by <code>onResume()</code> if the activity comes
306 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
307 * <td align="center">No</td>
308 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
309 * </tr>
310 *
311 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
312 * <th align="left" border="0">{@link android.app.Activity#onResume onResume()}</th>
313 * <td>Called when the activity will start
314 * interacting with the user. At this point your activity is at
315 * the top of the activity stack, with user input going to it.
316 * <p>Always followed by <code>onPause()</code>.</td>
317 * <td align="center">No</td>
318 * <td align="center"><code>onPause()</code></td>
319 * </tr>
320 *
321 * <tr><th align="left" border="0">{@link android.app.Activity#onPause onPause()}</th>
322 * <td>Called when the system is about to start resuming a previous
323 * activity. This is typically used to commit unsaved changes to
324 * persistent data, stop animations and other things that may be consuming
325 * CPU, etc. Implementations of this method must be very quick because
326 * the next activity will not be resumed until this method returns.
327 * <p>Followed by either <code>onResume()</code> if the activity
328 * returns back to the front, or <code>onStop()</code> if it becomes
329 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800330 * <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 -0800331 * <td align="center"><code>onResume()</code> or<br>
332 * <code>onStop()</code></td>
333 * </tr>
334 *
335 * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</th>
336 * <td>Called when the activity is no longer visible to the user, because
337 * another activity has been resumed and is covering this one. This
338 * may happen either because a new activity is being started, an existing
339 * one is being brought in front of this one, or this one is being
340 * destroyed.
341 * <p>Followed by either <code>onRestart()</code> if
342 * this activity is coming back to interact with the user, or
343 * <code>onDestroy()</code> if this activity is going away.</td>
344 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
345 * <td align="center"><code>onRestart()</code> or<br>
346 * <code>onDestroy()</code></td>
347 * </tr>
348 *
349 * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onDestroy onDestroy()}</th>
350 * <td>The final call you receive before your
351 * activity is destroyed. This can happen either because the
352 * activity is finishing (someone called {@link Activity#finish} on
353 * it, or because the system is temporarily destroying this
354 * instance of the activity to save space. You can distinguish
355 * between these two scenarios with the {@link
356 * Activity#isFinishing} method.</td>
357 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
358 * <td align="center"><em>nothing</em></td>
359 * </tr>
360 * </tbody>
361 * </table>
362 *
363 * <p>Note the "Killable" column in the above table -- for those methods that
364 * are marked as being killable, after that method returns the process hosting the
365 * activity may killed by the system <em>at any time</em> without another line
366 * of its code being executed. Because of this, you should use the
367 * {@link #onPause} method to write any persistent data (such as user edits)
368 * to storage. In addition, the method
369 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
370 * in such a background state, allowing you to save away any dynamic instance
371 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700372 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
374 * section for more information on how the lifecycle of a process is tied
375 * to the activities it is hosting. Note that it is important to save
376 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800377 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 * be called in every situation as described in its documentation.</p>
379 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800380 * <p class="note">Be aware that these semantics will change slightly between
381 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
382 * vs. those targeting prior platforms. Starting with Honeycomb, an application
383 * is not in the killable state until its {@link #onStop} has returned. This
384 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
385 * safely called after {@link #onPause()} and allows and application to safely
386 * wait until {@link #onStop()} to save persistent state.</p>
387 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * <p>For those methods that are not marked as being killable, the activity's
389 * process will not be killed by the system starting from the time the method
390 * is called and continuing after it returns. Thus an activity is in the killable
391 * state, for example, between after <code>onPause()</code> to the start of
392 * <code>onResume()</code>.</p>
393 *
394 * <a name="ConfigurationChanges"></a>
395 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700396 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 * <p>If the configuration of the device (as defined by the
398 * {@link Configuration Resources.Configuration} class) changes,
399 * then anything displaying a user interface will need to update to match that
400 * configuration. Because Activity is the primary mechanism for interacting
401 * with the user, it includes special support for handling configuration
402 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700403 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * <p>Unless you specify otherwise, a configuration change (such as a change
405 * in screen orientation, language, input devices, etc) will cause your
406 * current activity to be <em>destroyed</em>, going through the normal activity
407 * lifecycle process of {@link #onPause},
408 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
409 * had been in the foreground or visible to the user, once {@link #onDestroy} is
410 * called in that instance then a new instance of the activity will be
411 * created, with whatever savedInstanceState the previous instance had generated
412 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700413 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 * <p>This is done because any application resource,
415 * including layout files, can change based on any configuration value. Thus
416 * the only safe way to handle a configuration change is to re-retrieve all
417 * resources, including layouts, drawables, and strings. Because activities
418 * must already know how to save their state and re-create themselves from
419 * that state, this is a convenient way to have an activity restart itself
420 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 * <p>In some special cases, you may want to bypass restarting of your
423 * activity based on one or more types of configuration changes. This is
424 * done with the {@link android.R.attr#configChanges android:configChanges}
425 * attribute in its manifest. For any types of configuration changes you say
426 * that you handle there, you will receive a call to your current activity's
427 * {@link #onConfigurationChanged} method instead of being restarted. If
428 * a configuration change involves any that you do not handle, however, the
429 * activity will still be restarted and {@link #onConfigurationChanged}
430 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 * <a name="StartingActivities"></a>
433 * <h3>Starting Activities and Getting Results</h3>
434 *
435 * <p>The {@link android.app.Activity#startActivity}
436 * method is used to start a
437 * new activity, which will be placed at the top of the activity stack. It
438 * takes a single argument, an {@link android.content.Intent Intent},
439 * which describes the activity
440 * to be executed.</p>
441 *
442 * <p>Sometimes you want to get a result back from an activity when it
443 * ends. For example, you may start an activity that lets the user pick
444 * a person in a list of contacts; when it ends, it returns the person
445 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700446 * {@link android.app.Activity#startActivityForResult(Intent, int)}
447 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700449 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 *
451 * <p>When an activity exits, it can call
452 * {@link android.app.Activity#setResult(int)}
453 * to return data back to its parent. It must always supply a result code,
454 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
455 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
456 * return back an Intent containing any additional data it wants. All of this
457 * information appears back on the
458 * parent's <code>Activity.onActivityResult()</code>, along with the integer
459 * identifier it originally supplied.</p>
460 *
461 * <p>If a child activity fails for any reason (such as crashing), the parent
462 * activity will receive a result with the code RESULT_CANCELED.</p>
463 *
464 * <pre class="prettyprint">
465 * public class MyActivity extends Activity {
466 * ...
467 *
468 * static final int PICK_CONTACT_REQUEST = 0;
469 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700470 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
472 * // When the user center presses, let them pick a contact.
473 * startActivityForResult(
474 * new Intent(Intent.ACTION_PICK,
475 * new Uri("content://contacts")),
476 * PICK_CONTACT_REQUEST);
477 * return true;
478 * }
479 * return false;
480 * }
481 *
482 * protected void onActivityResult(int requestCode, int resultCode,
483 * Intent data) {
484 * if (requestCode == PICK_CONTACT_REQUEST) {
485 * if (resultCode == RESULT_OK) {
486 * // A contact was picked. Here we will just display it
487 * // to the user.
488 * startActivity(new Intent(Intent.ACTION_VIEW, data));
489 * }
490 * }
491 * }
492 * }
493 * </pre>
494 *
495 * <a name="SavingPersistentState"></a>
496 * <h3>Saving Persistent State</h3>
497 *
498 * <p>There are generally two kinds of persistent state than an activity
499 * will deal with: shared document-like data (typically stored in a SQLite
500 * database using a {@linkplain android.content.ContentProvider content provider})
501 * and internal state such as user preferences.</p>
502 *
503 * <p>For content provider data, we suggest that activities use a
504 * "edit in place" user model. That is, any edits a user makes are effectively
505 * made immediately without requiring an additional confirmation step.
506 * Supporting this model is generally a simple matter of following two rules:</p>
507 *
508 * <ul>
509 * <li> <p>When creating a new document, the backing database entry or file for
510 * it is created immediately. For example, if the user chooses to write
511 * a new e-mail, a new entry for that e-mail is created as soon as they
512 * start entering data, so that if they go to any other activity after
513 * that point this e-mail will now appear in the list of drafts.</p>
514 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
515 * commit to the backing content provider or file any changes the user
516 * has made. This ensures that those changes will be seen by any other
517 * activity that is about to run. You will probably want to commit
518 * your data even more aggressively at key times during your
519 * activity's lifecycle: for example before starting a new
520 * activity, before finishing your own activity, when the user
521 * switches between input fields, etc.</p>
522 * </ul>
523 *
524 * <p>This model is designed to prevent data loss when a user is navigating
525 * between activities, and allows the system to safely kill an activity (because
526 * system resources are needed somewhere else) at any time after it has been
527 * paused. Note this implies
528 * that the user pressing BACK from your activity does <em>not</em>
529 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800530 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
532 *
533 * <p>See the {@linkplain android.content.ContentProvider content package} for
534 * more information about content providers. These are a key aspect of how
535 * different activities invoke and propagate data between themselves.</p>
536 *
537 * <p>The Activity class also provides an API for managing internal persistent state
538 * associated with an activity. This can be used, for example, to remember
539 * the user's preferred initial display in a calendar (day view or week view)
540 * or the user's default home page in a web browser.</p>
541 *
542 * <p>Activity persistent state is managed
543 * with the method {@link #getPreferences},
544 * allowing you to retrieve and
545 * modify a set of name/value pairs associated with the activity. To use
546 * preferences that are shared across multiple application components
547 * (activities, receivers, services, providers), you can use the underlying
548 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
549 * to retrieve a preferences
550 * object stored under a specific name.
551 * (Note that it is not possible to share settings data across application
552 * packages -- for that you will need a content provider.)</p>
553 *
554 * <p>Here is an excerpt from a calendar activity that stores the user's
555 * preferred view mode in its persistent settings:</p>
556 *
557 * <pre class="prettyprint">
558 * public class CalendarActivity extends Activity {
559 * ...
560 *
561 * static final int DAY_VIEW_MODE = 0;
562 * static final int WEEK_VIEW_MODE = 1;
563 *
564 * private SharedPreferences mPrefs;
565 * private int mCurViewMode;
566 *
567 * protected void onCreate(Bundle savedInstanceState) {
568 * super.onCreate(savedInstanceState);
569 *
570 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700571 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 * }
573 *
574 * protected void onPause() {
575 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700576 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 * SharedPreferences.Editor ed = mPrefs.edit();
578 * ed.putInt("view_mode", mCurViewMode);
579 * ed.commit();
580 * }
581 * }
582 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700583 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 * <a name="Permissions"></a>
585 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700586 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 * <p>The ability to start a particular Activity can be enforced when it is
588 * declared in its
589 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
590 * tag. By doing so, other applications will need to declare a corresponding
591 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
592 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800593 *
594 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
595 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
596 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
597 * Activity access to the specific URIs in the Intent. Access will remain
598 * until the Activity has finished (it will remain across the hosting
599 * process being killed and other temporary destruction). As of
600 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
601 * was already created and a new Intent is being delivered to
602 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
603 * to the existing ones it holds.
604 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
606 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700607 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 * <a name="ProcessLifecycle"></a>
609 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700610 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * <p>The Android system attempts to keep application process around for as
612 * long as possible, but eventually will need to remove old processes when
613 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
614 * Lifecycle</a>, the decision about which process to remove is intimately
615 * tied to the state of the user's interaction with it. In general, there
616 * are four states a process can be in based on the activities running in it,
617 * listed here in order of importance. The system will kill less important
618 * processes (the last ones) before it resorts to killing more important
619 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700620 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * <ol>
622 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
623 * that the user is currently interacting with) is considered the most important.
624 * Its process will only be killed as a last resort, if it uses more memory
625 * than is available on the device. Generally at this point the device has
626 * reached a memory paging state, so this is required in order to keep the user
627 * interface responsive.
628 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
629 * but not in the foreground, such as one sitting behind a foreground dialog)
630 * is considered extremely important and will not be killed unless that is
631 * required to keep the foreground activity running.
632 * <li> <p>A <b>background activity</b> (an activity that is not visible to
633 * the user and has been paused) is no longer critical, so the system may
634 * safely kill its process to reclaim memory for other foreground or
635 * visible processes. If its process needs to be killed, when the user navigates
636 * back to the activity (making it visible on the screen again), its
637 * {@link #onCreate} method will be called with the savedInstanceState it had previously
638 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
639 * state as the user last left it.
640 * <li> <p>An <b>empty process</b> is one hosting no activities or other
641 * application components (such as {@link Service} or
642 * {@link android.content.BroadcastReceiver} classes). These are killed very
643 * quickly by the system as memory becomes low. For this reason, any
644 * background operation you do outside of an activity must be executed in the
645 * context of an activity BroadcastReceiver or Service to ensure that the system
646 * knows it needs to keep your process around.
647 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700648 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 * <p>Sometimes an Activity may need to do a long-running operation that exists
650 * independently of the activity lifecycle itself. An example may be a camera
651 * application that allows you to upload a picture to a web site. The upload
652 * may take a long time, and the application should allow the user to leave
653 * the application will it is executing. To accomplish this, your Activity
654 * should start a {@link Service} in which the upload takes place. This allows
655 * the system to properly prioritize your process (considering it to be more
656 * important than other non-visible applications) for the duration of the
657 * upload, independent of whether the original activity is paused, stopped,
658 * or finished.
659 */
660public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700661 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700663 OnCreateContextMenuListener, ComponentCallbacks2,
664 Window.OnWindowDismissedCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700666 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667
668 /** Standard activity result: operation canceled. */
669 public static final int RESULT_CANCELED = 0;
670 /** Standard activity result: operation succeeded. */
671 public static final int RESULT_OK = -1;
672 /** Start of user-defined activity results. */
673 public static final int RESULT_FIRST_USER = 1;
674
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700675 static final String FRAGMENTS_TAG = "android:fragments";
676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
678 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
679 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
680 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800681 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800683 private static class ManagedDialog {
684 Dialog mDialog;
685 Bundle mArgs;
686 }
687 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688
689 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
690 private Instrumentation mInstrumentation;
691 private IBinder mToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700692 private int mIdent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 /*package*/ String mEmbeddedID;
694 private Application mApplication;
Christopher Tateb70f3df2009-04-07 16:07:59 -0700695 /*package*/ Intent mIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 private ComponentName mComponent;
697 /*package*/ ActivityInfo mActivityInfo;
698 /*package*/ ActivityThread mMainThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 Activity mParent;
700 boolean mCalled;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700701 boolean mCheckedForLoaderManager;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700702 boolean mLoadersStarted;
Jeff Hamilton52d32032011-01-08 15:31:26 -0600703 /*package*/ boolean mResumed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 private boolean mStopped;
705 boolean mFinished;
706 boolean mStartedActivity;
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700707 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700708 private boolean mDoReportFullyDrawn = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700709 /** true if the activity is going through a transient pause */
710 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500711 /** true if the activity is being destroyed in order to recreate it with a new configuration */
712 /*package*/ boolean mChangingConfigurations = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 /*package*/ int mConfigChangeFlags;
714 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100715 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700716 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700718 static final class NonConfigurationInstances {
719 Object activity;
720 HashMap<String, Object> children;
721 ArrayList<Fragment> fragments;
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700722 ArrayMap<String, LoaderManagerImpl> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700723 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700724 }
725 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 private Window mWindow;
728
729 private WindowManager mWindowManager;
730 /*package*/ View mDecor = null;
731 /*package*/ boolean mWindowAdded = false;
732 /*package*/ boolean mVisibleFromServer = false;
733 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700734 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700735 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736
Dianne Hackborn91097de2014-04-04 18:02:06 -0700737 private VoiceInteractor mVoiceInteractor;
738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 private CharSequence mTitle;
740 private int mTitleColor = 0;
741
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700742 final FragmentManagerImpl mFragments = new FragmentManagerImpl();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700743 final FragmentContainer mContainer = new FragmentContainer() {
744 @Override
745 public View findViewById(int id) {
746 return Activity.this.findViewById(id);
747 }
George Mount0b26e4d2014-09-17 16:36:42 -0700748 @Override
749 public boolean hasView() {
750 Window window = Activity.this.getWindow();
751 return (window != null && window.peekDecorView() != null);
752 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700753 };
Craig Mautneree2e45a2014-06-27 12:10:03 -0700754
Jose Lima4b6c6692014-08-12 17:41:12 -0700755 // Most recent call to requestVisibleBehind().
756 boolean mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700757
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700758 ArrayMap<String, LoaderManagerImpl> mAllLoaderManagers;
Dianne Hackborn4911b782010-07-15 12:54:39 -0700759 LoaderManagerImpl mLoaderManager;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 private static final class ManagedCursor {
762 ManagedCursor(Cursor cursor) {
763 mCursor = cursor;
764 mReleased = false;
765 mUpdated = false;
766 }
767
768 private final Cursor mCursor;
769 private boolean mReleased;
770 private boolean mUpdated;
771 }
772 private final ArrayList<ManagedCursor> mManagedCursors =
773 new ArrayList<ManagedCursor>();
774
RoboErik55011652014-07-09 15:05:53 -0700775 // protected by synchronized (this)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 int mResultCode = RESULT_CANCELED;
777 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700778
Craig Mautner5eda9b32013-07-02 11:58:16 -0700779 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700780 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781
782 private boolean mTitleReady = false;
783
784 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
785 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
788
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700789 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700790 private final Object mInstanceTracker = StrictMode.trackActivity(this);
791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 private Thread mUiThread;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700793 final Handler mHandler = new Handler();
George Mount62ab9b72014-05-02 13:51:17 -0700794
George Mount1fecfb22014-06-18 14:55:55 -0700795 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700796 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
797 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 /** Return the intent that started this activity. */
800 public Intent getIntent() {
801 return mIntent;
802 }
803
RoboErik55011652014-07-09 15:05:53 -0700804 /**
805 * Change the intent returned by {@link #getIntent}. This holds a
806 * reference to the given intent; it does not copy it. Often used in
807 * conjunction with {@link #onNewIntent}.
808 *
809 * @param newIntent The new Intent object to return from getIntent
810 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 * @see #getIntent
812 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700813 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 public void setIntent(Intent newIntent) {
815 mIntent = newIntent;
816 }
817
818 /** Return the application that owns this activity. */
819 public final Application getApplication() {
820 return mApplication;
821 }
822
823 /** Is this activity embedded inside of another activity? */
824 public final boolean isChild() {
825 return mParent != null;
826 }
RoboErik55011652014-07-09 15:05:53 -0700827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 /** Return the parent activity if this view is an embedded child. */
829 public final Activity getParent() {
830 return mParent;
831 }
832
833 /** Retrieve the window manager for showing custom windows. */
834 public WindowManager getWindowManager() {
835 return mWindowManager;
836 }
837
838 /**
839 * Retrieve the current {@link android.view.Window} for the activity.
840 * This can be used to directly access parts of the Window API that
841 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700842 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 * @return Window The current window, or null if the activity is not
844 * visual.
845 */
846 public Window getWindow() {
847 return mWindow;
848 }
849
850 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800851 * Return the LoaderManager for this activity, creating it if needed.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700852 */
853 public LoaderManager getLoaderManager() {
854 if (mLoaderManager != null) {
855 return mLoaderManager;
856 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700857 mCheckedForLoaderManager = true;
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700858 mLoaderManager = getLoaderManager("(root)", mLoadersStarted, true);
Dianne Hackbornc8017682010-07-06 13:34:38 -0700859 return mLoaderManager;
860 }
RoboErik55011652014-07-09 15:05:53 -0700861
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700862 LoaderManagerImpl getLoaderManager(String who, boolean started, boolean create) {
Dianne Hackbornc8017682010-07-06 13:34:38 -0700863 if (mAllLoaderManagers == null) {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700864 mAllLoaderManagers = new ArrayMap<String, LoaderManagerImpl>();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700865 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700866 LoaderManagerImpl lm = mAllLoaderManagers.get(who);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700867 if (lm == null) {
868 if (create) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700869 lm = new LoaderManagerImpl(who, this, started);
870 mAllLoaderManagers.put(who, lm);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700871 }
872 } else {
873 lm.updateActivity(this);
Dianne Hackbornc8017682010-07-06 13:34:38 -0700874 }
875 return lm;
876 }
RoboErik55011652014-07-09 15:05:53 -0700877
Dianne Hackbornc8017682010-07-06 13:34:38 -0700878 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 * Calls {@link android.view.Window#getCurrentFocus} on the
880 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -0700881 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -0700883 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 * @see #getWindow
885 * @see android.view.Window#getCurrentFocus
886 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700887 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 public View getCurrentFocus() {
889 return mWindow != null ? mWindow.getCurrentFocus() : null;
890 }
891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 /**
893 * Called when the activity is starting. This is where most initialization
894 * should go: calling {@link #setContentView(int)} to inflate the
895 * activity's UI, using {@link #findViewById} to programmatically interact
896 * with widgets in the UI, calling
897 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
898 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -0700899 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 * <p>You can call {@link #finish} from within this function, in
901 * which case onDestroy() will be immediately called without any of the rest
902 * of the activity lifecycle ({@link #onStart}, {@link #onResume},
903 * {@link #onPause}, etc) executing.
RoboErik55011652014-07-09 15:05:53 -0700904 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 * <p><em>Derived classes must call through to the super class's
906 * implementation of this method. If they do not, an exception will be
907 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -0700908 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 * @param savedInstanceState If the activity is being re-initialized after
910 * previously being shut down then this Bundle contains the data it most
911 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -0700912 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 * @see #onStart
914 * @see #onSaveInstanceState
915 * @see #onRestoreInstanceState
916 * @see #onPostCreate
917 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700918 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700919 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700920 if (mLastNonConfigurationInstances != null) {
921 mAllLoaderManagers = mLastNonConfigurationInstances.loaders;
922 }
Adam Powelldd8fab22012-03-22 17:47:27 -0700923 if (mActivityInfo.parentActivityName != null) {
924 if (mActionBar == null) {
925 mEnableDefaultActionBarUp = true;
926 } else {
927 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
928 }
929 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700930 if (savedInstanceState != null) {
931 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
932 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
933 ? mLastNonConfigurationInstances.fragments : null);
934 }
935 mFragments.dispatchCreate();
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700936 getApplication().dispatchActivityCreated(this, savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -0700937 if (mVoiceInteractor != null) {
938 mVoiceInteractor.attachActivity(this);
939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 mCalled = true;
941 }
942
943 /**
Craig Mautnera0026042014-04-23 11:45:37 -0700944 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -0700945 * the attribute {@link android.R.attr#persistableMode} set to
946 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -0700947 *
948 * @param savedInstanceState if the activity is being re-initialized after
949 * previously being shut down then this Bundle contains the data it most
950 * recently supplied in {@link #onSaveInstanceState}.
951 * <b><i>Note: Otherwise it is null.</i></b>
952 * @param persistentState if the activity is being re-initialized after
953 * previously being shut down or powered off then this Bundle contains the data it most
954 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
955 * <b><i>Note: Otherwise it is null.</i></b>
956 *
957 * @see #onCreate(android.os.Bundle)
958 * @see #onStart
959 * @see #onSaveInstanceState
960 * @see #onRestoreInstanceState
961 * @see #onPostCreate
962 */
Craig Mautner7a629c22014-09-04 14:57:04 -0700963 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -0700964 @Nullable PersistableBundle persistentState) {
965 onCreate(savedInstanceState);
966 }
967
968 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 * The hook for {@link ActivityThread} to restore the state of this activity.
970 *
971 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
972 * {@link #restoreManagedDialogs(android.os.Bundle)}.
973 *
974 * @param savedInstanceState contains the saved state
975 */
976 final void performRestoreInstanceState(Bundle savedInstanceState) {
977 onRestoreInstanceState(savedInstanceState);
978 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980
981 /**
Craig Mautnera0026042014-04-23 11:45:37 -0700982 * The hook for {@link ActivityThread} to restore the state of this activity.
983 *
984 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
985 * {@link #restoreManagedDialogs(android.os.Bundle)}.
986 *
987 * @param savedInstanceState contains the saved state
988 * @param persistentState contains the persistable saved state
989 */
990 final void performRestoreInstanceState(Bundle savedInstanceState,
991 PersistableBundle persistentState) {
992 onRestoreInstanceState(savedInstanceState, persistentState);
993 if (savedInstanceState != null) {
994 restoreManagedDialogs(savedInstanceState);
995 }
996 }
997
998 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 * This method is called after {@link #onStart} when the activity is
1000 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001001 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 * to restore their state, but it is sometimes convenient to do it here
1003 * after all of the initialization has been done or to allow subclasses to
1004 * decide whether to use your default implementation. The default
1005 * implementation of this method performs a restore of any view state that
1006 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 * <p>This method is called between {@link #onStart} and
1009 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001010 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001012 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 * @see #onCreate
1014 * @see #onPostCreate
1015 * @see #onResume
1016 * @see #onSaveInstanceState
1017 */
1018 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1019 if (mWindow != null) {
1020 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1021 if (windowState != null) {
1022 mWindow.restoreHierarchyState(windowState);
1023 }
1024 }
1025 }
Craig Mautnera0026042014-04-23 11:45:37 -07001026
1027 /**
1028 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001029 * created with the attribute {@link android.R.attr#persistableMode} set to
1030 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1031 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001032 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1033 *
1034 * <p>This method is called between {@link #onStart} and
1035 * {@link #onPostCreate}.
1036 *
1037 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1038 *
1039 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
1040 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}.
1041 *
1042 * @see #onRestoreInstanceState(Bundle)
1043 * @see #onCreate
1044 * @see #onPostCreate
1045 * @see #onResume
1046 * @see #onSaveInstanceState
1047 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001048 public void onRestoreInstanceState(Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001049 PersistableBundle persistentState) {
1050 if (savedInstanceState != null) {
1051 onRestoreInstanceState(savedInstanceState);
1052 }
1053 }
1054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
1056 * Restore the state of any saved managed dialogs.
1057 *
1058 * @param savedInstanceState The bundle to restore from.
1059 */
1060 private void restoreManagedDialogs(Bundle savedInstanceState) {
1061 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1062 if (b == null) {
1063 return;
1064 }
1065
1066 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1067 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001068 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 for (int i = 0; i < numDialogs; i++) {
1070 final Integer dialogId = ids[i];
1071 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1072 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001073 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1074 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001075 final ManagedDialog md = new ManagedDialog();
1076 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1077 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1078 if (md.mDialog != null) {
1079 mManagedDialogs.put(dialogId, md);
1080 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1081 md.mDialog.onRestoreInstanceState(dialogState);
1082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
1084 }
1085 }
1086
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001087 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1088 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001089 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001090 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001091 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001092 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001093 return dialog;
1094 }
1095
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001096 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 return SAVED_DIALOG_KEY_PREFIX + key;
1098 }
1099
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001100 private static String savedDialogArgsKeyFor(int key) {
1101 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103
1104 /**
1105 * Called when activity start-up is complete (after {@link #onStart}
1106 * and {@link #onRestoreInstanceState} have been called). Applications will
1107 * generally not implement this method; it is intended for system
1108 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001109 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 * <p><em>Derived classes must call through to the super class's
1111 * implementation of this method. If they do not, an exception will be
1112 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001113 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 * @param savedInstanceState If the activity is being re-initialized after
1115 * previously being shut down then this Bundle contains the data it most
1116 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1117 * @see #onCreate
1118 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001119 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 if (!isChild()) {
1121 mTitleReady = true;
1122 onTitleChanged(getTitle(), getTitleColor());
1123 }
1124 mCalled = true;
1125 }
1126
1127 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001128 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001129 * created with the attribute {@link android.R.attr#persistableMode} set to
1130 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001131 *
1132 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1133 * @param persistentState The data caming from the PersistableBundle first
1134 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1135 *
1136 * @see #onCreate
1137 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001138 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001139 @Nullable PersistableBundle persistentState) {
1140 onPostCreate(savedInstanceState);
1141 }
1142
1143 /**
RoboErik55011652014-07-09 15:05:53 -07001144 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1145 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001146 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 *
1148 * <p><em>Derived classes must call through to the super class's
1149 * implementation of this method. If they do not, an exception will be
1150 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 * @see #onCreate
1153 * @see #onStop
1154 * @see #onResume
1155 */
1156 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001157 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001159
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001160 if (!mLoadersStarted) {
1161 mLoadersStarted = true;
1162 if (mLoaderManager != null) {
1163 mLoaderManager.doStart();
1164 } else if (!mCheckedForLoaderManager) {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -07001165 mLoaderManager = getLoaderManager("(root)", mLoadersStarted, false);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001166 }
1167 mCheckedForLoaderManager = true;
Dianne Hackborn2707d602010-07-09 18:01:20 -07001168 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001169
1170 getApplication().dispatchActivityStarted(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172
1173 /**
1174 * Called after {@link #onStop} when the current activity is being
1175 * re-displayed to the user (the user has navigated back to it). It will
1176 * be followed by {@link #onStart} and then {@link #onResume}.
1177 *
1178 * <p>For activities that are using raw {@link Cursor} objects (instead of
1179 * creating them through
1180 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1181 * this is usually the place
1182 * where the cursor should be requeried (because you had deactivated it in
1183 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 * <p><em>Derived classes must call through to the super class's
1186 * implementation of this method. If they do not, an exception will be
1187 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001188 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 * @see #onStop
1190 * @see #onStart
1191 * @see #onResume
1192 */
1193 protected void onRestart() {
1194 mCalled = true;
1195 }
1196
1197 /**
1198 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1199 * {@link #onPause}, for your activity to start interacting with the user.
1200 * This is a good place to begin animations, open exclusive-access devices
1201 * (such as the camera), etc.
1202 *
1203 * <p>Keep in mind that onResume is not the best indicator that your activity
1204 * is visible to the user; a system window such as the keyguard may be in
1205 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1206 * activity is visible to the user (for example, to resume a game).
1207 *
1208 * <p><em>Derived classes must call through to the super class's
1209 * implementation of this method. If they do not, an exception will be
1210 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001211 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 * @see #onRestoreInstanceState
1213 * @see #onRestart
1214 * @see #onPostResume
1215 * @see #onPause
1216 */
1217 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001218 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001219 getApplication().dispatchActivityResumed(this);
George Mount8cab50a2014-05-15 09:57:17 -07001220 mActivityTransitionState.onResume();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 mCalled = true;
1222 }
1223
1224 /**
1225 * Called when activity resume is complete (after {@link #onResume} has
1226 * been called). Applications will generally not implement this method;
1227 * it is intended for system classes to do final setup after application
1228 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001229 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 * <p><em>Derived classes must call through to the super class's
1231 * implementation of this method. If they do not, an exception will be
1232 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 * @see #onResume
1235 */
1236 protected void onPostResume() {
1237 final Window win = getWindow();
1238 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001239 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 mCalled = true;
1241 }
1242
1243 /**
Dianne Hackbornd7c03952014-08-29 17:32:04 -07001244 * @hide
Dianne Hackborn91097de2014-04-04 18:02:06 -07001245 * Check whether this activity is running as part of a voice interaction with the user.
1246 * If true, it should perform its interaction with the user through the
1247 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1248 */
Dianne Hackbornd7c03952014-08-29 17:32:04 -07001249 @SystemApi
Dianne Hackborn91097de2014-04-04 18:02:06 -07001250 public boolean isVoiceInteraction() {
1251 return mVoiceInteractor != null;
1252 }
1253
1254 /**
Dianne Hackbornd7c03952014-08-29 17:32:04 -07001255 * @hide
Dianne Hackborn91097de2014-04-04 18:02:06 -07001256 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1257 * interact with this activity.
1258 */
Dianne Hackbornd7c03952014-08-29 17:32:04 -07001259 @SystemApi
Dianne Hackborn91097de2014-04-04 18:02:06 -07001260 public VoiceInteractor getVoiceInteractor() {
1261 return mVoiceInteractor;
1262 }
1263
1264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 * This is called for activities that set launchMode to "singleTop" in
1266 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1267 * flag when calling {@link #startActivity}. In either case, when the
1268 * activity is re-launched while at the top of the activity stack instead
1269 * of a new instance of the activity being started, onNewIntent() will be
1270 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001271 * re-launch it.
1272 *
1273 * <p>An activity will always be paused before receiving a new intent, so
1274 * you can count on {@link #onResume} being called after this method.
1275 *
1276 * <p>Note that {@link #getIntent} still returns the original Intent. You
1277 * can use {@link #setIntent} to update it to this new Intent.
1278 *
1279 * @param intent The new intent that was started for the activity.
1280 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001282 * @see #setIntent
1283 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 */
1285 protected void onNewIntent(Intent intent) {
1286 }
1287
1288 /**
1289 * The hook for {@link ActivityThread} to save the state of this activity.
1290 *
1291 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1292 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1293 *
1294 * @param outState The bundle to save the state to.
1295 */
1296 final void performSaveInstanceState(Bundle outState) {
1297 onSaveInstanceState(outState);
1298 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001299 mActivityTransitionState.saveState(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001300 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 }
1302
1303 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001304 * The hook for {@link ActivityThread} to save the state of this activity.
1305 *
1306 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1307 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1308 *
1309 * @param outState The bundle to save the state to.
1310 * @param outPersistentState The bundle to save persistent state to.
1311 */
1312 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1313 onSaveInstanceState(outState, outPersistentState);
1314 saveManagedDialogs(outState);
1315 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1316 ", " + outPersistentState);
1317 }
1318
1319 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 * Called to retrieve per-instance state from an activity before being killed
1321 * so that the state can be restored in {@link #onCreate} or
1322 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1323 * will be passed to both).
1324 *
1325 * <p>This method is called before an activity may be killed so that when it
1326 * comes back some time in the future it can restore its state. For example,
1327 * if activity B is launched in front of activity A, and at some point activity
1328 * A is killed to reclaim resources, activity A will have a chance to save the
1329 * current state of its user interface via this method so that when the user
1330 * returns to activity A, the state of the user interface can be restored
1331 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1332 *
1333 * <p>Do not confuse this method with activity lifecycle callbacks such as
1334 * {@link #onPause}, which is always called when an activity is being placed
1335 * in the background or on its way to destruction, or {@link #onStop} which
1336 * is called before destruction. One example of when {@link #onPause} and
1337 * {@link #onStop} is called and not this method is when a user navigates back
1338 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1339 * on B because that particular instance will never be restored, so the
1340 * system avoids calling it. An example when {@link #onPause} is called and
1341 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1342 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1343 * killed during the lifetime of B since the state of the user interface of
1344 * A will stay intact.
1345 *
1346 * <p>The default implementation takes care of most of the UI per-instance
1347 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1348 * view in the hierarchy that has an id, and by saving the id of the currently
1349 * focused view (all of which is restored by the default implementation of
1350 * {@link #onRestoreInstanceState}). If you override this method to save additional
1351 * information not captured by each individual view, you will likely want to
1352 * call through to the default implementation, otherwise be prepared to save
1353 * all of the state of each view yourself.
1354 *
1355 * <p>If called, this method will occur before {@link #onStop}. There are
1356 * no guarantees about whether it will occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001357 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001359 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 * @see #onCreate
1361 * @see #onRestoreInstanceState
1362 * @see #onPause
1363 */
1364 protected void onSaveInstanceState(Bundle outState) {
1365 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001366 Parcelable p = mFragments.saveAllState();
1367 if (p != null) {
1368 outState.putParcelable(FRAGMENTS_TAG, p);
1369 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001370 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372
1373 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001374 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001375 * created with the attribute {@link android.R.attr#persistableMode} set to
1376 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1377 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1378 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001379 *
1380 * @param outState Bundle in which to place your saved state.
1381 * @param outPersistentState State which will be saved across reboots.
1382 *
1383 * @see #onSaveInstanceState(Bundle)
1384 * @see #onCreate
1385 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1386 * @see #onPause
1387 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001388 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001389 onSaveInstanceState(outState);
1390 }
1391
1392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 * Save the state of any managed dialogs.
1394 *
1395 * @param outState place to store the saved state.
1396 */
1397 private void saveManagedDialogs(Bundle outState) {
1398 if (mManagedDialogs == null) {
1399 return;
1400 }
1401
1402 final int numDialogs = mManagedDialogs.size();
1403 if (numDialogs == 0) {
1404 return;
1405 }
1406
1407 Bundle dialogState = new Bundle();
1408
1409 int[] ids = new int[mManagedDialogs.size()];
1410
1411 // save each dialog's bundle, gather the ids
1412 for (int i = 0; i < numDialogs; i++) {
1413 final int key = mManagedDialogs.keyAt(i);
1414 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001415 final ManagedDialog md = mManagedDialogs.valueAt(i);
1416 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1417 if (md.mArgs != null) {
1418 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
1421
1422 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1423 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1424 }
1425
1426
1427 /**
1428 * Called as part of the activity lifecycle when an activity is going into
1429 * the background, but has not (yet) been killed. The counterpart to
1430 * {@link #onResume}.
1431 *
1432 * <p>When activity B is launched in front of activity A, this callback will
1433 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1434 * so be sure to not do anything lengthy here.
1435 *
1436 * <p>This callback is mostly used for saving any persistent state the
1437 * activity is editing, to present a "edit in place" model to the user and
1438 * making sure nothing is lost if there are not enough resources to start
1439 * the new activity without first killing this one. This is also a good
1440 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001441 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 * as fast as possible, or to close resources that are exclusive access
1443 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001444 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 * <p>In situations where the system needs more memory it may kill paused
1446 * processes to reclaim resources. Because of this, you should be sure
1447 * that all of your state is saved by the time you return from
1448 * this function. In general {@link #onSaveInstanceState} is used to save
1449 * per-instance state in the activity and this method is used to store
1450 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001451 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 * <p>After receiving this call you will usually receive a following call
1453 * to {@link #onStop} (after the next activity has been resumed and
1454 * displayed), however in some cases there will be a direct call back to
1455 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 * <p><em>Derived classes must call through to the super class's
1458 * implementation of this method. If they do not, an exception will be
1459 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001460 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 * @see #onResume
1462 * @see #onSaveInstanceState
1463 * @see #onStop
1464 */
1465 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001466 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001467 getApplication().dispatchActivityPaused(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 mCalled = true;
1469 }
1470
1471 /**
1472 * Called as part of the activity lifecycle when an activity is about to go
1473 * into the background as the result of user choice. For example, when the
1474 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1475 * when an incoming phone call causes the in-call Activity to be automatically
1476 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1477 * the activity being interrupted. In cases when it is invoked, this method
1478 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001479 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * <p>This callback and {@link #onUserInteraction} are intended to help
1481 * activities manage status bar notifications intelligently; specifically,
1482 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07001483 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 * @see #onUserInteraction()
1485 */
1486 protected void onUserLeaveHint() {
1487 }
RoboErik55011652014-07-09 15:05:53 -07001488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 /**
1490 * Generate a new thumbnail for this activity. This method is called before
1491 * pausing the activity, and should draw into <var>outBitmap</var> the
1492 * imagery for the desired thumbnail in the dimensions of that bitmap. It
1493 * can use the given <var>canvas</var>, which is configured to draw into the
1494 * bitmap, for rendering if desired.
RoboErik55011652014-07-09 15:05:53 -07001495 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001496 * <p>The default implementation returns fails and does not draw a thumbnail;
1497 * this will result in the platform creating its own thumbnail if needed.
RoboErik55011652014-07-09 15:05:53 -07001498 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 * @param outBitmap The bitmap to contain the thumbnail.
1500 * @param canvas Can be used to render into the bitmap.
RoboErik55011652014-07-09 15:05:53 -07001501 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 * @return Return true if you have drawn into the bitmap; otherwise after
1503 * you return it will be filled with a default thumbnail.
RoboErik55011652014-07-09 15:05:53 -07001504 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 * @see #onCreateDescription
1506 * @see #onSaveInstanceState
1507 * @see #onPause
1508 */
1509 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001510 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
1512
1513 /**
1514 * Generate a new description for this activity. This method is called
1515 * before pausing the activity and can, if desired, return some textual
1516 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001517 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 * <p>The default implementation returns null, which will cause you to
1519 * inherit the description from the previous activity. If all activities
1520 * return null, generally the label of the top activity will be used as the
1521 * description.
RoboErik55011652014-07-09 15:05:53 -07001522 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 * @return A description of what the user is doing. It should be short and
1524 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001525 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 * @see #onCreateThumbnail
1527 * @see #onSaveInstanceState
1528 * @see #onPause
1529 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001530 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 public CharSequence onCreateDescription() {
1532 return null;
1533 }
1534
1535 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001536 * This is called when the user is requesting an assist, to build a full
1537 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1538 * application. You can override this method to place into the bundle anything
1539 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
1540 * of the assist Intent. The default implementation does nothing.
1541 *
1542 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001543 * been registered with {@link Application#registerOnProvideAssistDataListener
1544 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001545 */
1546 public void onProvideAssistData(Bundle data) {
1547 }
1548
1549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 * Called when you are no longer visible to the user. You will next
1551 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1552 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001553 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 * <p>Note that this method may never be called, in low memory situations
1555 * where the system does not have enough memory to keep your activity's
1556 * process running after its {@link #onPause} method is called.
RoboErik55011652014-07-09 15:05:53 -07001557 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 * <p><em>Derived classes must call through to the super class's
1559 * implementation of this method. If they do not, an exception will be
1560 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001561 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 * @see #onRestart
1563 * @see #onResume
1564 * @see #onSaveInstanceState
1565 * @see #onDestroy
1566 */
1567 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001568 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001569 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001570 mActivityTransitionState.onStop();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001571 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001572 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 mCalled = true;
1574 }
1575
1576 /**
1577 * Perform any final cleanup before an activity is destroyed. This can
1578 * happen either because the activity is finishing (someone called
1579 * {@link #finish} on it, or because the system is temporarily destroying
1580 * this instance of the activity to save space. You can distinguish
1581 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001582 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 * <p><em>Note: do not count on this method being called as a place for
1584 * saving data! For example, if an activity is editing data in a content
1585 * provider, those edits should be committed in either {@link #onPause} or
1586 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1587 * free resources like threads that are associated with an activity, so
1588 * that a destroyed activity does not leave such things around while the
1589 * rest of its application is still running. There are situations where
1590 * the system will simply kill the activity's hosting process without
1591 * calling this method (or any others) in it, so it should not be used to
1592 * do things that are intended to remain around after the process goes
1593 * away.
RoboErik55011652014-07-09 15:05:53 -07001594 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 * <p><em>Derived classes must call through to the super class's
1596 * implementation of this method. If they do not, an exception will be
1597 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001598 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 * @see #onPause
1600 * @see #onStop
1601 * @see #finish
1602 * @see #isFinishing
1603 */
1604 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001605 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 mCalled = true;
1607
1608 // dismiss any dialogs we are managing.
1609 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 final int numDialogs = mManagedDialogs.size();
1611 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001612 final ManagedDialog md = mManagedDialogs.valueAt(i);
1613 if (md.mDialog.isShowing()) {
1614 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 }
1616 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001617 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619
1620 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001621 synchronized (mManagedCursors) {
1622 int numCursors = mManagedCursors.size();
1623 for (int i = 0; i < numCursors; i++) {
1624 ManagedCursor c = mManagedCursors.get(i);
1625 if (c != null) {
1626 c.mCursor.close();
1627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001629 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
Amith Yamasani49860442010-03-17 20:54:10 -07001631
1632 // Close any open search dialog
1633 if (mSearchManager != null) {
1634 mSearchManager.stopSearch();
1635 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001636
1637 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
1639
1640 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001641 * Report to the system that your app is now fully drawn, purely for diagnostic
1642 * purposes (calling it does not impact the visible behavior of the activity).
1643 * This is only used to help instrument application launch times, so that the
1644 * app can report when it is fully in a usable state; without this, the only thing
1645 * the system itself can determine is the point at which the activity's window
1646 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001647 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001648 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001649 * entirely drawn your UI and populated with all of the significant data. You
1650 * can safely call this method any time after first launch as well, in which case
1651 * it will simply be ignored.
1652 */
1653 public void reportFullyDrawn() {
1654 if (mDoReportFullyDrawn) {
1655 mDoReportFullyDrawn = false;
1656 try {
1657 ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken);
1658 } catch (RemoteException e) {
1659 }
1660 }
1661 }
1662
1663 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 * Called by the system when the device configuration changes while your
1665 * activity is running. Note that this will <em>only</em> be called if
1666 * you have selected configurations you would like to handle with the
1667 * {@link android.R.attr#configChanges} attribute in your manifest. If
1668 * any configuration change occurs that is not selected to be reported
1669 * by that attribute, then instead of reporting it the system will stop
1670 * and restart the activity (to have it launched with the new
1671 * configuration).
RoboErik55011652014-07-09 15:05:53 -07001672 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 * <p>At the time that this function has been called, your Resources
1674 * object will have been updated to return resource values matching the
1675 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07001676 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 * @param newConfig The new device configuration.
1678 */
1679 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001680 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01001682
Dianne Hackborn9d071802010-12-08 14:49:15 -08001683 mFragments.dispatchConfigurationChanged(newConfig);
1684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 if (mWindow != null) {
1686 // Pass the configuration changed event to the window
1687 mWindow.onConfigurationChanged(newConfig);
1688 }
Adam Powell45c0b192011-07-28 15:11:57 -07001689
1690 if (mActionBar != null) {
1691 // Do this last; the action bar will need to access
1692 // view changes from above.
1693 mActionBar.onConfigurationChanged(newConfig);
1694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
RoboErik55011652014-07-09 15:05:53 -07001696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 /**
1698 * If this activity is being destroyed because it can not handle a
1699 * configuration parameter being changed (and thus its
1700 * {@link #onConfigurationChanged(Configuration)} method is
1701 * <em>not</em> being called), then you can use this method to discover
1702 * the set of changes that have occurred while in the process of being
1703 * destroyed. Note that there is no guarantee that these will be
1704 * accurate (other changes could have happened at any time), so you should
1705 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07001706 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 * @return Returns a bit field of the configuration parameters that are
1708 * changing, as defined by the {@link android.content.res.Configuration}
1709 * class.
1710 */
1711 public int getChangingConfigurations() {
1712 return mConfigChangeFlags;
1713 }
RoboErik55011652014-07-09 15:05:53 -07001714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 /**
1716 * Retrieve the non-configuration instance data that was previously
1717 * returned by {@link #onRetainNonConfigurationInstance()}. This will
1718 * be available from the initial {@link #onCreate} and
1719 * {@link #onStart} calls to the new instance, allowing you to extract
1720 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07001721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 * <p>Note that the data you retrieve here should <em>only</em> be used
1723 * as an optimization for handling configuration changes. You should always
1724 * be able to handle getting a null pointer back, and an activity must
1725 * still be able to restore itself to its previous state (through the
1726 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
1727 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07001728 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 * @return Returns the object previously returned by
1730 * {@link #onRetainNonConfigurationInstance()}.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07001731 *
1732 * @deprecated Use the new {@link Fragment} API
1733 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
1734 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001736 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07001737 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001739 return mLastNonConfigurationInstances != null
1740 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
RoboErik55011652014-07-09 15:05:53 -07001742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 /**
1744 * Called by the system, as part of destroying an
1745 * activity due to a configuration change, when it is known that a new
1746 * instance will immediately be created for the new configuration. You
1747 * can return any object you like here, including the activity instance
1748 * itself, which can later be retrieved by calling
1749 * {@link #getLastNonConfigurationInstance()} in the new activity
1750 * instance.
RoboErik55011652014-07-09 15:05:53 -07001751 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001752 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1753 * or later, consider instead using a {@link Fragment} with
1754 * {@link Fragment#setRetainInstance(boolean)
1755 * Fragment.setRetainInstance(boolean}.</em>
1756 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 * <p>This function is called purely as an optimization, and you must
1758 * not rely on it being called. When it is called, a number of guarantees
1759 * will be made to help optimize configuration switching:
1760 * <ul>
1761 * <li> The function will be called between {@link #onStop} and
1762 * {@link #onDestroy}.
1763 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001764 * created after this one's {@link #onDestroy()} is called. In particular,
1765 * <em>no</em> messages will be dispatched during this time (when the returned
1766 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 * <li> The object you return here will <em>always</em> be available from
1768 * the {@link #getLastNonConfigurationInstance()} method of the following
1769 * activity instance as described there.
1770 * </ul>
RoboErik55011652014-07-09 15:05:53 -07001771 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 * <p>These guarantees are designed so that an activity can use this API
1773 * to propagate extensive state from the old to new activity instance, from
1774 * loaded bitmaps, to network connections, to evenly actively running
1775 * threads. Note that you should <em>not</em> propagate any data that
1776 * may change based on the configuration, including any data loaded from
1777 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07001778 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001779 * <p>The guarantee of no message handling during the switch to the next
1780 * activity simplifies use with active objects. For example if your retained
1781 * state is an {@link android.os.AsyncTask} you are guaranteed that its
1782 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
1783 * not be called from the call here until you execute the next instance's
1784 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
1785 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
1786 * running in a separate thread.)
1787 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 * @return Return any Object holding the desired state to propagate to the
1789 * next activity instance.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07001790 *
1791 * @deprecated Use the new {@link Fragment} API
1792 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
1793 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 */
1795 public Object onRetainNonConfigurationInstance() {
1796 return null;
1797 }
RoboErik55011652014-07-09 15:05:53 -07001798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 /**
1800 * Retrieve the non-configuration instance data that was previously
1801 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
1802 * be available from the initial {@link #onCreate} and
1803 * {@link #onStart} calls to the new instance, allowing you to extract
1804 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07001805 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 * <p>Note that the data you retrieve here should <em>only</em> be used
1807 * as an optimization for handling configuration changes. You should always
1808 * be able to handle getting a null pointer back, and an activity must
1809 * still be able to restore itself to its previous state (through the
1810 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
1811 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07001812 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 * @return Returns the object previously returned by
1814 * {@link #onRetainNonConfigurationChildInstances()}
1815 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001816 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001817 HashMap<String, Object> getLastNonConfigurationChildInstances() {
1818 return mLastNonConfigurationInstances != null
1819 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 }
RoboErik55011652014-07-09 15:05:53 -07001821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 /**
1823 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
1824 * it should return either a mapping from child activity id strings to arbitrary objects,
1825 * or null. This method is intended to be used by Activity framework subclasses that control a
1826 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
1827 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
1828 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001829 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
1831 return null;
1832 }
RoboErik55011652014-07-09 15:05:53 -07001833
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001834 NonConfigurationInstances retainNonConfigurationInstances() {
1835 Object activity = onRetainNonConfigurationInstance();
1836 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
1837 ArrayList<Fragment> fragments = mFragments.retainNonConfig();
Dianne Hackborn2707d602010-07-09 18:01:20 -07001838 boolean retainLoaders = false;
1839 if (mAllLoaderManagers != null) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001840 // prune out any loader managers that were already stopped and so
Dianne Hackborn2707d602010-07-09 18:01:20 -07001841 // have nothing useful to retain.
Dianne Hackborn3e82ba12013-07-16 13:23:55 -07001842 final int N = mAllLoaderManagers.size();
1843 LoaderManagerImpl loaders[] = new LoaderManagerImpl[N];
1844 for (int i=N-1; i>=0; i--) {
1845 loaders[i] = mAllLoaderManagers.valueAt(i);
1846 }
1847 for (int i=0; i<N; i++) {
1848 LoaderManagerImpl lm = loaders[i];
1849 if (lm.mRetaining) {
1850 retainLoaders = true;
1851 } else {
1852 lm.doDestroy();
1853 mAllLoaderManagers.remove(lm.mWho);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001854 }
1855 }
1856 }
Dianne Hackborn20d94742014-05-29 18:35:45 -07001857 if (activity == null && children == null && fragments == null && !retainLoaders
1858 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001859 return null;
1860 }
RoboErik55011652014-07-09 15:05:53 -07001861
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001862 NonConfigurationInstances nci = new NonConfigurationInstances();
1863 nci.activity = activity;
1864 nci.children = children;
1865 nci.fragments = fragments;
Dianne Hackborn2707d602010-07-09 18:01:20 -07001866 nci.loaders = mAllLoaderManagers;
Dianne Hackborn20d94742014-05-29 18:35:45 -07001867 nci.voiceInteractor = mVoiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001868 return nci;
1869 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001872 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08001874 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001876
1877 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001878 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001879 mCalled = true;
1880 mFragments.dispatchTrimMemory(level);
1881 }
1882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001884 * Return the FragmentManager for interacting with fragments associated
1885 * with this activity.
1886 */
1887 public FragmentManager getFragmentManager() {
1888 return mFragments;
1889 }
1890
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001891 void invalidateFragment(String who) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001892 //Log.v(TAG, "invalidateFragmentIndex: index=" + index);
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07001893 if (mAllLoaderManagers != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001894 LoaderManagerImpl lm = mAllLoaderManagers.get(who);
Dianne Hackbornd04ad542011-07-25 16:16:15 -07001895 if (lm != null && !lm.mRetaining) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001896 lm.doDestroy();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001897 mAllLoaderManagers.remove(who);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001898 }
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07001899 }
1900 }
RoboErik55011652014-07-09 15:05:53 -07001901
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001902 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001903 * Called when a Fragment is being attached to this activity, immediately
1904 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
1905 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
1906 */
1907 public void onAttachFragment(Fragment fragment) {
1908 }
RoboErik55011652014-07-09 15:05:53 -07001909
Dianne Hackbornc8017682010-07-06 13:34:38 -07001910 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 * Wrapper around
1912 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
1913 * that gives the resulting {@link Cursor} to call
1914 * {@link #startManagingCursor} so that the activity will manage its
1915 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07001916 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001917 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1918 * or later, consider instead using {@link LoaderManager} instead, available
1919 * via {@link #getLoaderManager()}.</em>
1920 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07001921 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
1922 * this method, because the activity will do that for you at the appropriate time. However, if
1923 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
1924 * not</em> automatically close the cursor and, in that case, you must call
1925 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07001926 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 * @param uri The URI of the content provider to query.
1928 * @param projection List of columns to return.
1929 * @param selection SQL WHERE clause.
1930 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07001931 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07001933 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
1935 * @see #startManagingCursor
1936 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05001937 *
1938 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 */
Jason parks6ed50de2010-08-25 10:18:50 -05001940 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07001941 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
1942 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
1944 if (c != null) {
1945 startManagingCursor(c);
1946 }
1947 return c;
1948 }
1949
1950 /**
1951 * Wrapper around
1952 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
1953 * that gives the resulting {@link Cursor} to call
1954 * {@link #startManagingCursor} so that the activity will manage its
1955 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07001956 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001957 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1958 * or later, consider instead using {@link LoaderManager} instead, available
1959 * via {@link #getLoaderManager()}.</em>
1960 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07001961 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
1962 * this method, because the activity will do that for you at the appropriate time. However, if
1963 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
1964 * not</em> automatically close the cursor and, in that case, you must call
1965 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07001966 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 * @param uri The URI of the content provider to query.
1968 * @param projection List of columns to return.
1969 * @param selection SQL WHERE clause.
1970 * @param selectionArgs The arguments to selection, if any ?s are pesent
1971 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07001972 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07001974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
1976 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05001977 *
1978 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 */
Jason parks6ed50de2010-08-25 10:18:50 -05001980 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07001981 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
1982 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
1984 if (c != null) {
1985 startManagingCursor(c);
1986 }
1987 return c;
1988 }
1989
1990 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 * This method allows the activity to take care of managing the given
1992 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
1993 * That is, when the activity is stopped it will automatically call
1994 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
1995 * it will call {@link Cursor#requery} for you. When the activity is
1996 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07001997 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001998 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1999 * or later, consider instead using {@link LoaderManager} instead, available
2000 * via {@link #getLoaderManager()}.</em>
2001 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002002 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2003 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2004 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2005 * <em>will not</em> automatically close the cursor and, in that case, you must call
2006 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2011 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002012 *
2013 * @deprecated Use the new {@link android.content.CursorLoader} class with
2014 * {@link LoaderManager} instead; this is also
2015 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 */
Jason parks6ed50de2010-08-25 10:18:50 -05002017 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 public void startManagingCursor(Cursor c) {
2019 synchronized (mManagedCursors) {
2020 mManagedCursors.add(new ManagedCursor(c));
2021 }
2022 }
2023
2024 /**
2025 * Given a Cursor that was previously given to
2026 * {@link #startManagingCursor}, stop the activity's management of that
2027 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002028 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002029 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002030 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002031 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002032 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002036 *
2037 * @deprecated Use the new {@link android.content.CursorLoader} class with
2038 * {@link LoaderManager} instead; this is also
2039 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 */
Jason parks6ed50de2010-08-25 10:18:50 -05002041 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 public void stopManagingCursor(Cursor c) {
2043 synchronized (mManagedCursors) {
2044 final int N = mManagedCursors.size();
2045 for (int i=0; i<N; i++) {
2046 ManagedCursor mc = mManagedCursors.get(i);
2047 if (mc.mCursor == c) {
2048 mManagedCursors.remove(i);
2049 break;
2050 }
2051 }
2052 }
2053 }
2054
2055 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002056 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2057 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002058 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002060 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
2063
2064 /**
2065 * Finds a view that was identified by the id attribute from the XML that
2066 * was processed in {@link #onCreate}.
2067 *
2068 * @return The view if found or null otherwise.
2069 */
2070 public View findViewById(int id) {
2071 return getWindow().findViewById(id);
2072 }
RoboErik55011652014-07-09 15:05:53 -07002073
Adam Powell33b97432010-04-20 10:01:14 -07002074 /**
2075 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002076 *
Adam Powell33b97432010-04-20 10:01:14 -07002077 * @return The Activity's ActionBar, or null if it does not have one.
2078 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002079 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002080 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002081 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002082 return mActionBar;
2083 }
Adam Powelle43340c2014-03-17 19:10:43 -07002084
2085 /**
2086 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2087 * Activity window.
2088 *
2089 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2090 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2091 * a traditional window decor action bar. The toolbar's menu will be populated with the
2092 * Activity's options menu and the navigation button will be wired through the standard
2093 * {@link android.R.id#home home} menu select action.</p>
2094 *
2095 * <p>In order to use a Toolbar within the Activity's window content the application
2096 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2097 *
Adam Powell37780142014-06-01 13:31:00 -07002098 * @param toolbar Toolbar to set as the Activity's action bar
Adam Powelle43340c2014-03-17 19:10:43 -07002099 */
Adam Powell37780142014-06-01 13:31:00 -07002100 public void setActionBar(@Nullable Toolbar toolbar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002101 if (getActionBar() instanceof WindowDecorActionBar) {
2102 throw new IllegalStateException("This Activity already has an action bar supplied " +
2103 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2104 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2105 }
Adam Powell14e1afe2014-08-18 15:58:23 -07002106 ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2107 mActionBar = tbab;
2108 mWindow.setCallback(tbab.getWrappedWindowCallback());
Adam Powell07a74542014-05-30 15:52:44 -07002109 mActionBar.invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002110 }
RoboErik55011652014-07-09 15:05:53 -07002111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 /**
Adam Powell33b97432010-04-20 10:01:14 -07002113 * Creates a new ActionBar, locates the inflated ActionBarView,
2114 * initializes the ActionBar with the view, and sets mActionBar.
2115 */
Adam Powelle43340c2014-03-17 19:10:43 -07002116 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002117 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002118
2119 // Initializing the window decor can change window feature flags.
2120 // Make sure that we have the correct set before performing the test below.
2121 window.getDecorView();
2122
Adam Powell9b4c8042010-08-10 15:36:44 -07002123 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002124 return;
2125 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002126
Adam Powelle43340c2014-03-17 19:10:43 -07002127 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002128 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002129
2130 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2131 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002132 }
RoboErik55011652014-07-09 15:05:53 -07002133
Adam Powell33b97432010-04-20 10:01:14 -07002134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 * Set the activity content from a layout resource. The resource will be
2136 * inflated, adding all top-level views to the activity.
Romain Guy482b34a2011-01-20 10:59:28 -08002137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002139 *
Romain Guy482b34a2011-01-20 10:59:28 -08002140 * @see #setContentView(android.view.View)
2141 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 */
2143 public void setContentView(int layoutResID) {
2144 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002145 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
2147
2148 /**
2149 * Set the activity content to an explicit view. This view is placed
2150 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a2011-01-20 10:59:28 -08002151 * view hierarchy. When calling this method, the layout parameters of the
2152 * specified view are ignored. Both the width and the height of the view are
2153 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2154 * your own layout parameters, invoke
2155 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2156 * instead.
RoboErik55011652014-07-09 15:05:53 -07002157 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 * @param view The desired content to display.
Romain Guy482b34a2011-01-20 10:59:28 -08002159 *
2160 * @see #setContentView(int)
2161 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 */
2163 public void setContentView(View view) {
2164 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002165 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 }
2167
2168 /**
2169 * Set the activity content to an explicit view. This view is placed
2170 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a2011-01-20 10:59:28 -08002171 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 * @param view The desired content to display.
2174 * @param params Layout parameters for the view.
Romain Guy482b34a2011-01-20 10:59:28 -08002175 *
2176 * @see #setContentView(android.view.View)
2177 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 */
2179 public void setContentView(View view, ViewGroup.LayoutParams params) {
2180 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002181 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 }
2183
2184 /**
2185 * Add an additional content view to the activity. Added after any existing
2186 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 * @param view The desired content to display.
2189 * @param params Layout parameters for the view.
2190 */
2191 public void addContentView(View view, ViewGroup.LayoutParams params) {
2192 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002193 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
2195
2196 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002197 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2198 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2199 *
2200 * <p>This method will return non-null after content has been initialized (e.g. by using
2201 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2202 *
2203 * @return This window's content TransitionManager or null if none is set.
2204 */
2205 public TransitionManager getContentTransitionManager() {
2206 return getWindow().getTransitionManager();
2207 }
2208
2209 /**
2210 * Set the {@link TransitionManager} to use for default transitions in this window.
2211 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2212 *
2213 * @param tm The TransitionManager to use for scene changes.
2214 */
2215 public void setContentTransitionManager(TransitionManager tm) {
2216 getWindow().setTransitionManager(tm);
2217 }
2218
2219 /**
2220 * Retrieve the {@link Scene} representing this window's current content.
2221 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2222 *
2223 * <p>This method will return null if the current content is not represented by a Scene.</p>
2224 *
2225 * @return Current Scene being shown or null
2226 */
2227 public Scene getContentScene() {
2228 return getWindow().getContentScene();
2229 }
2230
2231 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002232 * Sets whether this activity is finished when touched outside its window's
2233 * bounds.
2234 */
2235 public void setFinishOnTouchOutside(boolean finish) {
2236 mWindow.setCloseOnTouchOutside(finish);
2237 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002238
2239 /** @hide */
2240 @IntDef({
2241 DEFAULT_KEYS_DISABLE,
2242 DEFAULT_KEYS_DIALER,
2243 DEFAULT_KEYS_SHORTCUT,
2244 DEFAULT_KEYS_SEARCH_LOCAL,
2245 DEFAULT_KEYS_SEARCH_GLOBAL})
2246 @Retention(RetentionPolicy.SOURCE)
2247 @interface DefaultKeyMode {}
2248
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2251 * keys.
RoboErik55011652014-07-09 15:05:53 -07002252 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 * @see #setDefaultKeyMode
2254 */
2255 static public final int DEFAULT_KEYS_DISABLE = 0;
2256 /**
2257 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2258 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002259 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 * @see #setDefaultKeyMode
2261 */
2262 static public final int DEFAULT_KEYS_DIALER = 1;
2263 /**
2264 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2265 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002266 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002268 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 * @see #setDefaultKeyMode
2270 */
2271 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2272 /**
2273 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2274 * will start an application-defined search. (If the application or activity does not
2275 * actually define a search, the the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002276 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 * @see #setDefaultKeyMode
2280 */
2281 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2282
2283 /**
2284 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2285 * will start a global search (typically web search, but some platforms may define alternate
2286 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002287 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002289 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 * @see #setDefaultKeyMode
2291 */
2292 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2293
2294 /**
2295 * Select the default key handling for this activity. This controls what
2296 * will happen to key events that are not otherwise handled. The default
2297 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2298 * floor. Other modes allow you to launch the dialer
2299 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2300 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002301 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002303 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 * <p>Note that the mode selected here does not impact the default
2305 * handling of system keys, such as the "back" and "menu" keys, and your
2306 * activity and its views always get a first chance to receive and handle
2307 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002308 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002310 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 * @see #DEFAULT_KEYS_DISABLE
2312 * @see #DEFAULT_KEYS_DIALER
2313 * @see #DEFAULT_KEYS_SHORTCUT
2314 * @see #DEFAULT_KEYS_SEARCH_LOCAL
2315 * @see #DEFAULT_KEYS_SEARCH_GLOBAL
2316 * @see #onKeyDown
2317 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002318 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 // Some modes use a SpannableStringBuilder to track & dispatch input events
2322 // This list must remain in sync with the switch in onKeyDown()
2323 switch (mode) {
2324 case DEFAULT_KEYS_DISABLE:
2325 case DEFAULT_KEYS_SHORTCUT:
2326 mDefaultKeySsb = null; // not used in these modes
2327 break;
2328 case DEFAULT_KEYS_DIALER:
2329 case DEFAULT_KEYS_SEARCH_LOCAL:
2330 case DEFAULT_KEYS_SEARCH_GLOBAL:
2331 mDefaultKeySsb = new SpannableStringBuilder();
2332 Selection.setSelection(mDefaultKeySsb,0);
2333 break;
2334 default:
2335 throw new IllegalArgumentException();
2336 }
2337 }
2338
2339 /**
2340 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002341 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 * is inside a TextView will not trigger the event (unless it is a navigation
2343 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002344 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 * <p>If the focused view didn't want this event, this method is called.
2346 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002347 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
2348 * by calling {@link #onBackPressed()}, though the behavior varies based
2349 * on the application compatibility mode: for
2350 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
2351 * it will set up the dispatch to call {@link #onKeyUp} where the action
2352 * will be performed; for earlier applications, it will perform the
2353 * action immediately in on-down, as those versions of the platform
2354 * behaved.
RoboErik55011652014-07-09 15:05:53 -07002355 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002356 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002357 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07002358 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002360 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 * this event and it should continue to be propagated.
2362 * @see #onKeyUp
2363 * @see android.view.KeyEvent
2364 */
2365 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002366 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002367 if (getApplicationInfo().targetSdkVersion
2368 >= Build.VERSION_CODES.ECLAIR) {
2369 event.startTracking();
2370 } else {
2371 onBackPressed();
2372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 return true;
2374 }
RoboErik55011652014-07-09 15:05:53 -07002375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
2377 return false;
2378 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
RoboErik55011652014-07-09 15:05:53 -07002379 if (getWindow().performPanelShortcut(Window.FEATURE_OPTIONS_PANEL,
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002380 keyCode, event, Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
2381 return true;
2382 }
2383 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 } else {
2385 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
2386 boolean clearSpannable = false;
2387 boolean handled;
2388 if ((event.getRepeatCount() != 0) || event.isSystem()) {
2389 clearSpannable = true;
2390 handled = false;
2391 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002392 handled = TextKeyListener.getInstance().onKeyDown(
2393 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 if (handled && mDefaultKeySsb.length() > 0) {
2395 // something useable has been typed - dispatch it now.
2396
2397 final String str = mDefaultKeySsb.toString();
2398 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07002399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 switch (mDefaultKeyMode) {
2401 case DEFAULT_KEYS_DIALER:
2402 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
2403 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07002404 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 break;
2406 case DEFAULT_KEYS_SEARCH_LOCAL:
2407 startSearch(str, false, null, false);
2408 break;
2409 case DEFAULT_KEYS_SEARCH_GLOBAL:
2410 startSearch(str, false, null, true);
2411 break;
2412 }
2413 }
2414 }
2415 if (clearSpannable) {
2416 mDefaultKeySsb.clear();
2417 mDefaultKeySsb.clearSpans();
2418 Selection.setSelection(mDefaultKeySsb,0);
2419 }
2420 return handled;
2421 }
2422 }
2423
2424 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002425 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
2426 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
2427 * the event).
2428 */
2429 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2430 return false;
2431 }
2432
2433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002435 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 * is inside a TextView will not trigger the event (unless it is a navigation
2437 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002438 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002439 * <p>The default implementation handles KEYCODE_BACK to stop the activity
2440 * and go back.
RoboErik55011652014-07-09 15:05:53 -07002441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002443 * further, or <code>false</code> to indicate that you have not handled
2444 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 * @see #onKeyDown
2446 * @see KeyEvent
2447 */
2448 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002449 if (getApplicationInfo().targetSdkVersion
2450 >= Build.VERSION_CODES.ECLAIR) {
2451 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
2452 && !event.isCanceled()) {
2453 onBackPressed();
2454 return true;
2455 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 return false;
2458 }
2459
2460 /**
2461 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
2462 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
2463 * the event).
2464 */
2465 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2466 return false;
2467 }
RoboErik55011652014-07-09 15:05:53 -07002468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002470 * Called when the activity has detected the user's press of the back
2471 * key. The default implementation simply finishes the current activity,
2472 * but you can override this to do whatever you want.
2473 */
2474 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07002475 if (mActionBar != null && mActionBar.collapseActionView()) {
2476 return;
2477 }
2478
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08002479 if (!mFragments.popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07002480 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07002481 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002482 }
Jeff Brown64da12a2011-01-04 19:57:47 -08002483
2484 /**
2485 * Called when a key shortcut event is not handled by any of the views in the Activity.
2486 * Override this method to implement global key shortcuts for the Activity.
2487 * Key shortcuts can also be implemented by setting the
2488 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
2489 *
2490 * @param keyCode The value in event.getKeyCode().
2491 * @param event Description of the key event.
2492 * @return True if the key shortcut was handled.
2493 */
2494 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2495 return false;
2496 }
2497
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002498 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 * Called when a touch screen event was not handled by any of the views
2500 * under it. This is most useful to process touch events that happen
2501 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07002502 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07002504 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 * @return Return true if you have consumed the event, false if you haven't.
2506 * The default implementation always returns false.
2507 */
2508 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002509 if (mWindow.shouldCloseOnTouch(this, event)) {
2510 finish();
2511 return true;
2512 }
RoboErik55011652014-07-09 15:05:53 -07002513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 return false;
2515 }
RoboErik55011652014-07-09 15:05:53 -07002516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 /**
2518 * Called when the trackball was moved and not handled by any of the
2519 * views inside of the activity. So, for example, if the trackball moves
2520 * while focus is on a button, you will receive a call here because
2521 * buttons do not normally do anything with trackball events. The call
2522 * here happens <em>before</em> trackball movements are converted to
2523 * DPAD key events, which then get sent back to the view hierarchy, and
2524 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07002525 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07002527 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 * @return Return true if you have consumed the event, false if you haven't.
2529 * The default implementation always returns false.
2530 */
2531 public boolean onTrackballEvent(MotionEvent event) {
2532 return false;
2533 }
Jeff Browncb1404e2011-01-15 18:14:15 -08002534
2535 /**
2536 * Called when a generic motion event was not handled by any of the
2537 * views inside of the activity.
2538 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08002539 * Generic motion events describe joystick movements, mouse hovers, track pad
2540 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08002541 * {@link MotionEvent#getSource() source} of the motion event specifies
2542 * the class of input that was received. Implementations of this method
2543 * must examine the bits in the source before processing the event.
2544 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08002545 * </p><p>
2546 * Generic motion events with source class
2547 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
2548 * are delivered to the view under the pointer. All other generic motion events are
2549 * delivered to the focused view.
2550 * </p><p>
2551 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
2552 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08002553 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08002554 *
2555 * @param event The generic motion event being processed.
2556 *
2557 * @return Return true if you have consumed the event, false if you haven't.
2558 * The default implementation always returns false.
2559 */
2560 public boolean onGenericMotionEvent(MotionEvent event) {
2561 return false;
2562 }
2563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 /**
2565 * Called whenever a key, touch, or trackball event is dispatched to the
2566 * activity. Implement this method if you wish to know that the user has
2567 * interacted with the device in some way while your activity is running.
2568 * This callback and {@link #onUserLeaveHint} are intended to help
2569 * activities manage status bar notifications intelligently; specifically,
2570 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07002571 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
2573 * be accompanied by calls to {@link #onUserInteraction}. This
2574 * ensures that your activity will be told of relevant user activity such
2575 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07002576 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 * <p>Note that this callback will be invoked for the touch down action
2578 * that begins a touch gesture, but may not be invoked for the touch-moved
2579 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07002580 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 * @see #onUserLeaveHint()
2582 */
2583 public void onUserInteraction() {
2584 }
RoboErik55011652014-07-09 15:05:53 -07002585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
2587 // Update window manager if: we have a view, that view is
2588 // attached to its parent (which will be a RootView), and
2589 // this activity is not embedded.
2590 if (mParent == null) {
2591 View decor = mDecor;
2592 if (decor != null && decor.getParent() != null) {
2593 getWindowManager().updateViewLayout(decor, params);
2594 }
2595 }
2596 }
2597
2598 public void onContentChanged() {
2599 }
2600
2601 /**
2602 * Called when the current {@link Window} of the activity gains or loses
2603 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002604 * to the user. The default implementation clears the key tracking
2605 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07002606 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002607 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 * is managed independently of activity lifecycles. As such, while focus
2609 * changes will generally have some relation to lifecycle changes (an
2610 * activity that is stopped will not generally get window focus), you
2611 * should not rely on any particular order between the callbacks here and
2612 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07002613 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 * <p>As a general rule, however, a resumed activity will have window
2615 * focus... unless it has displayed other dialogs or popups that take
2616 * input focus, in which case the activity itself will not have focus
2617 * when the other windows have it. Likewise, the system may display
2618 * system-level windows (such as the status bar notification panel or
2619 * a system alert) which will temporarily take window input focus without
2620 * pausing the foreground activity.
2621 *
2622 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07002623 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 * @see #hasWindowFocus()
2625 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002626 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 */
2628 public void onWindowFocusChanged(boolean hasFocus) {
2629 }
RoboErik55011652014-07-09 15:05:53 -07002630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002632 * Called when the main window associated with the activity has been
2633 * attached to the window manager.
2634 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
2635 * for more information.
2636 * @see View#onAttachedToWindow
2637 */
2638 public void onAttachedToWindow() {
2639 }
RoboErik55011652014-07-09 15:05:53 -07002640
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002641 /**
2642 * Called when the main window associated with the activity has been
2643 * detached from the window manager.
2644 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
2645 * for more information.
2646 * @see View#onDetachedFromWindow
2647 */
2648 public void onDetachedFromWindow() {
2649 }
RoboErik55011652014-07-09 15:05:53 -07002650
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002651 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 * Returns true if this activity's <em>main</em> window currently has window focus.
2653 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07002654 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07002656 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
2658 */
2659 public boolean hasWindowFocus() {
2660 Window w = getWindow();
2661 if (w != null) {
2662 View d = w.getDecorView();
2663 if (d != null) {
2664 return d.hasWindowFocus();
2665 }
2666 }
2667 return false;
2668 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002669
2670 /**
2671 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07002672 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002673 */
Adam Powell117b6952014-05-05 18:14:56 -07002674 @Override
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002675 public void onWindowDismissed() {
2676 finish();
2677 }
RoboErik55011652014-07-09 15:05:53 -07002678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 /**
RoboErik55011652014-07-09 15:05:53 -07002680 * Called to process key events. You can override this to intercept all
2681 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07002683 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07002685 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 * @return boolean Return true if this event was consumed.
2687 */
2688 public boolean dispatchKeyEvent(KeyEvent event) {
2689 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07002690
2691 // Let action bars open menus in response to the menu key prioritized over
2692 // the window handling it
2693 if (event.getKeyCode() == KeyEvent.KEYCODE_MENU &&
2694 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
2695 return true;
2696 }
2697
Dianne Hackborn8d374262009-09-14 21:21:52 -07002698 Window win = getWindow();
2699 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 return true;
2701 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002702 View decor = mDecor;
2703 if (decor == null) decor = win.getDecorView();
2704 return event.dispatch(this, decor != null
2705 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 }
2707
2708 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08002709 * Called to process a key shortcut event.
2710 * You can override this to intercept all key shortcut events before they are
2711 * dispatched to the window. Be sure to call this implementation for key shortcut
2712 * events that should be handled normally.
2713 *
2714 * @param event The key shortcut event.
2715 * @return True if this event was consumed.
2716 */
2717 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
2718 onUserInteraction();
2719 if (getWindow().superDispatchKeyShortcutEvent(event)) {
2720 return true;
2721 }
2722 return onKeyShortcut(event.getKeyCode(), event);
2723 }
2724
2725 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 * Called to process touch screen events. You can override this to
2727 * intercept all touch screen events before they are dispatched to the
2728 * window. Be sure to call this implementation for touch screen events
2729 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07002730 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07002732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 * @return boolean Return true if this event was consumed.
2734 */
2735 public boolean dispatchTouchEvent(MotionEvent ev) {
2736 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
2737 onUserInteraction();
2738 }
2739 if (getWindow().superDispatchTouchEvent(ev)) {
2740 return true;
2741 }
2742 return onTouchEvent(ev);
2743 }
RoboErik55011652014-07-09 15:05:53 -07002744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 /**
2746 * Called to process trackball events. You can override this to
2747 * intercept all trackball events before they are dispatched to the
2748 * window. Be sure to call this implementation for trackball events
2749 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07002750 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07002752 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 * @return boolean Return true if this event was consumed.
2754 */
2755 public boolean dispatchTrackballEvent(MotionEvent ev) {
2756 onUserInteraction();
2757 if (getWindow().superDispatchTrackballEvent(ev)) {
2758 return true;
2759 }
2760 return onTrackballEvent(ev);
2761 }
svetoslavganov75986cf2009-05-14 22:28:01 -07002762
Jeff Browncb1404e2011-01-15 18:14:15 -08002763 /**
2764 * Called to process generic motion events. You can override this to
2765 * intercept all generic motion events before they are dispatched to the
2766 * window. Be sure to call this implementation for generic motion events
2767 * that should be handled normally.
2768 *
2769 * @param ev The generic motion event.
2770 *
2771 * @return boolean Return true if this event was consumed.
2772 */
2773 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
2774 onUserInteraction();
2775 if (getWindow().superDispatchGenericMotionEvent(ev)) {
2776 return true;
2777 }
2778 return onGenericMotionEvent(ev);
2779 }
2780
svetoslavganov75986cf2009-05-14 22:28:01 -07002781 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2782 event.setClassName(getClass().getName());
2783 event.setPackageName(getPackageName());
2784
2785 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08002786 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
2787 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07002788 event.setFullScreen(isFullScreen);
2789
2790 CharSequence title = getTitle();
2791 if (!TextUtils.isEmpty(title)) {
2792 event.getText().add(title);
2793 }
2794
2795 return true;
2796 }
2797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 /**
2799 * Default implementation of
2800 * {@link android.view.Window.Callback#onCreatePanelView}
2801 * for activities. This
2802 * simply returns null so that all panel sub-windows will have the default
2803 * menu behavior.
2804 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002805 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 public View onCreatePanelView(int featureId) {
2807 return null;
2808 }
2809
2810 /**
2811 * Default implementation of
2812 * {@link android.view.Window.Callback#onCreatePanelMenu}
2813 * for activities. This calls through to the new
2814 * {@link #onCreateOptionsMenu} method for the
2815 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
2816 * so that subclasses of Activity don't need to deal with feature codes.
2817 */
2818 public boolean onCreatePanelMenu(int featureId, Menu menu) {
2819 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84b2010-06-08 18:04:35 -07002820 boolean show = onCreateOptionsMenu(menu);
2821 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
2822 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
2824 return false;
2825 }
2826
2827 /**
2828 * Default implementation of
2829 * {@link android.view.Window.Callback#onPreparePanel}
2830 * for activities. This
2831 * calls through to the new {@link #onPrepareOptionsMenu} method for the
2832 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
2833 * panel, so that subclasses of
2834 * Activity don't need to deal with feature codes.
2835 */
2836 public boolean onPreparePanel(int featureId, View view, Menu menu) {
2837 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
2838 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84b2010-06-08 18:04:35 -07002839 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07002840 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 }
2842 return true;
2843 }
2844
2845 /**
2846 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07002847 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 * @return The default implementation returns true.
2849 */
2850 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08002851 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07002852 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08002853 if (mActionBar != null) {
2854 mActionBar.dispatchMenuVisibilityChanged(true);
2855 } else {
2856 Log.e(TAG, "Tried to open action bar menu with no action bar");
2857 }
Adam Powell8515ee82010-11-30 14:09:55 -08002858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 return true;
2860 }
2861
2862 /**
2863 * Default implementation of
2864 * {@link android.view.Window.Callback#onMenuItemSelected}
2865 * for activities. This calls through to the new
2866 * {@link #onOptionsItemSelected} method for the
2867 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
2868 * panel, so that subclasses of
2869 * Activity don't need to deal with feature codes.
2870 */
2871 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09002872 CharSequence titleCondensed = item.getTitleCondensed();
2873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 switch (featureId) {
2875 case Window.FEATURE_OPTIONS_PANEL:
2876 // Put event logging here so it gets called even if subclass
2877 // doesn't call through to superclass's implmeentation of each
2878 // of these methods below
zobject686a8052012-12-14 21:11:08 +09002879 if(titleCondensed != null) {
2880 EventLog.writeEvent(50000, 0, titleCondensed.toString());
2881 }
Dianne Hackbornb31e84b2010-06-08 18:04:35 -07002882 if (onOptionsItemSelected(item)) {
2883 return true;
2884 }
Adam Powell04d58112012-04-09 10:22:12 -07002885 if (mFragments.dispatchOptionsItemSelected(item)) {
2886 return true;
2887 }
2888 if (item.getItemId() == android.R.id.home && mActionBar != null &&
2889 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
2890 if (mParent == null) {
Adam Powell07304f52012-05-07 17:32:44 -07002891 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07002892 } else {
Adam Powell07304f52012-05-07 17:32:44 -07002893 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07002894 }
Adam Powell04d58112012-04-09 10:22:12 -07002895 }
2896 return false;
RoboErik55011652014-07-09 15:05:53 -07002897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09002899 if(titleCondensed != null) {
2900 EventLog.writeEvent(50000, 1, titleCondensed.toString());
2901 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07002902 if (onContextItemSelected(item)) {
2903 return true;
2904 }
2905 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08002906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 default:
2908 return false;
2909 }
2910 }
RoboErik55011652014-07-09 15:05:53 -07002911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 /**
2913 * Default implementation of
2914 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
2915 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
2916 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
2917 * so that subclasses of Activity don't need to deal with feature codes.
2918 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
2919 * {@link #onContextMenuClosed(Menu)} will be called.
2920 */
2921 public void onPanelClosed(int featureId, Menu menu) {
2922 switch (featureId) {
2923 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84b2010-06-08 18:04:35 -07002924 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 onOptionsMenuClosed(menu);
2926 break;
RoboErik55011652014-07-09 15:05:53 -07002927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 case Window.FEATURE_CONTEXT_MENU:
2929 onContextMenuClosed(menu);
2930 break;
Adam Powell8515ee82010-11-30 14:09:55 -08002931
2932 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07002933 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08002934 mActionBar.dispatchMenuVisibilityChanged(false);
2935 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 }
2937 }
2938
2939 /**
Dianne Hackbornb31e84b2010-06-08 18:04:35 -07002940 * Declare that the options menu has changed, so should be recreated.
2941 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
2942 * time it needs to be displayed.
2943 */
2944 public void invalidateOptionsMenu() {
Adam Powell07a74542014-05-30 15:52:44 -07002945 if (mActionBar == null || !mActionBar.invalidateOptionsMenu()) {
2946 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
2947 }
Dianne Hackbornb31e84b2010-06-08 18:04:35 -07002948 }
RoboErik55011652014-07-09 15:05:53 -07002949
Dianne Hackbornb31e84b2010-06-08 18:04:35 -07002950 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 * Initialize the contents of the Activity's standard options menu. You
2952 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07002953 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 * <p>This is only called once, the first time the options menu is
2955 * displayed. To update the menu every time it is displayed, see
2956 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07002957 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07002959 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
2960 * they will be correctly ordered with application-defined menu items.
2961 * Deriving classes should always call through to the base implementation.
2962 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 * <p>You can safely hold on to <var>menu</var> (and any items created
2964 * from it), making modifications to it as desired, until the next
2965 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07002966 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 * <p>When you add items to the menu, you can implement the Activity's
2968 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07002969 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07002971 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 * @return You must return true for the menu to be displayed;
2973 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07002974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 * @see #onPrepareOptionsMenu
2976 * @see #onOptionsItemSelected
2977 */
2978 public boolean onCreateOptionsMenu(Menu menu) {
2979 if (mParent != null) {
2980 return mParent.onCreateOptionsMenu(menu);
2981 }
2982 return true;
2983 }
2984
2985 /**
2986 * Prepare the Screen's standard options menu to be displayed. This is
2987 * called right before the menu is shown, every time it is shown. You can
2988 * use this method to efficiently enable/disable items or otherwise
2989 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07002990 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 * <p>The default implementation updates the system menu items based on the
2992 * activity's state. Deriving classes should always call through to the
2993 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07002994 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 * @param menu The options menu as last shown or first initialized by
2996 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07002997 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 * @return You must return true for the menu to be displayed;
2999 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003000 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 * @see #onCreateOptionsMenu
3002 */
3003 public boolean onPrepareOptionsMenu(Menu menu) {
3004 if (mParent != null) {
3005 return mParent.onPrepareOptionsMenu(menu);
3006 }
3007 return true;
3008 }
3009
3010 /**
3011 * This hook is called whenever an item in your options menu is selected.
3012 * The default implementation simply returns false to have the normal
3013 * processing happen (calling the item's Runnable or sending a message to
3014 * its Handler as appropriate). You can use this method for any items
3015 * for which you would like to do processing without those other
3016 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003017 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003019 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003020 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003022 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 * @return boolean Return false to allow normal menu processing to
3024 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003025 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 * @see #onCreateOptionsMenu
3027 */
3028 public boolean onOptionsItemSelected(MenuItem item) {
3029 if (mParent != null) {
3030 return mParent.onOptionsItemSelected(item);
3031 }
3032 return false;
3033 }
3034
3035 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003036 * This method is called whenever the user chooses to navigate Up within your application's
3037 * activity hierarchy from the action bar.
3038 *
3039 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3040 * was specified in the manifest for this activity or an activity-alias to it,
3041 * default Up navigation will be handled automatically. If any activity
3042 * along the parent chain requires extra Intent arguments, the Activity subclass
3043 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3044 * to supply those arguments.</p>
3045 *
3046 * <p>See <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
3047 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3048 * from the design guide for more information about navigating within your app.</p>
3049 *
3050 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3051 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3052 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3053 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3054 *
3055 * @return true if Up navigation completed successfully and this Activity was finished,
3056 * false otherwise.
3057 */
3058 public boolean onNavigateUp() {
3059 // Automatically handle hierarchical Up navigation if the proper
3060 // metadata is available.
3061 Intent upIntent = getParentActivityIntent();
3062 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003063 if (mActivityInfo.taskAffinity == null) {
3064 // Activities with a null affinity are special; they really shouldn't
3065 // specify a parent activity intent in the first place. Just finish
3066 // the current activity and call it a day.
3067 finish();
3068 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003069 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003070 onCreateNavigateUpTaskStack(b);
3071 onPrepareNavigateUpTaskStack(b);
3072 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003073
3074 // We can't finishAffinity if we have a result.
3075 // Fall back and simply finish the current activity instead.
3076 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3077 // Tell the developer what's going on to avoid hair-pulling.
3078 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3079 finish();
3080 } else {
3081 finishAffinity();
3082 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003083 } else {
3084 navigateUpTo(upIntent);
3085 }
3086 return true;
3087 }
3088 return false;
3089 }
3090
3091 /**
3092 * This is called when a child activity of this one attempts to navigate up.
3093 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3094 *
3095 * @param child The activity making the call.
3096 */
3097 public boolean onNavigateUpFromChild(Activity child) {
3098 return onNavigateUp();
3099 }
3100
3101 /**
3102 * Define the synthetic task stack that will be generated during Up navigation from
3103 * a different task.
3104 *
3105 * <p>The default implementation of this method adds the parent chain of this activity
3106 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3107 * may choose to override this method to construct the desired task stack in a different
3108 * way.</p>
3109 *
Adam Powellf0195952012-05-02 21:38:54 -07003110 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3111 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3112 * returned by {@link #getParentActivityIntent()}.</p>
3113 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003114 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3115 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3116 *
3117 * @param builder An empty TaskStackBuilder - the application should add intents representing
3118 * the desired task stack
3119 */
3120 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3121 builder.addParentStack(this);
3122 }
3123
3124 /**
3125 * Prepare the synthetic task stack that will be generated during Up navigation
3126 * from a different task.
3127 *
3128 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3129 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3130 * If any extra data should be added to these intents before launching the new task,
3131 * the application should override this method and add that data here.</p>
3132 *
3133 * @param builder A TaskStackBuilder that has been populated with Intents by
3134 * onCreateNavigateUpTaskStack.
3135 */
3136 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3137 }
3138
3139 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 * This hook is called whenever the options menu is being closed (either by the user canceling
3141 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 * @param menu The options menu as last shown or first initialized by
3144 * onCreateOptionsMenu().
3145 */
3146 public void onOptionsMenuClosed(Menu menu) {
3147 if (mParent != null) {
3148 mParent.onOptionsMenuClosed(menu);
3149 }
3150 }
RoboErik55011652014-07-09 15:05:53 -07003151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 /**
3153 * Programmatically opens the options menu. If the options menu is already
3154 * open, this method does nothing.
3155 */
3156 public void openOptionsMenu() {
Adam Powell07a74542014-05-30 15:52:44 -07003157 if (mActionBar == null || !mActionBar.openOptionsMenu()) {
3158 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 }
RoboErik55011652014-07-09 15:05:53 -07003161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 /**
3163 * Progammatically closes the options menu. If the options menu is already
3164 * closed, this method does nothing.
3165 */
3166 public void closeOptionsMenu() {
3167 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3168 }
3169
3170 /**
3171 * Called when a context menu for the {@code view} is about to be shown.
3172 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3173 * time the context menu is about to be shown and should be populated for
3174 * the view (or item inside the view for {@link AdapterView} subclasses,
3175 * this can be found in the {@code menuInfo})).
3176 * <p>
3177 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3178 * item has been selected.
3179 * <p>
3180 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003181 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 */
3183 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3184 }
3185
3186 /**
3187 * Registers a context menu to be shown for the given view (multiple views
3188 * can show the context menu). This method will set the
3189 * {@link OnCreateContextMenuListener} on the view to this activity, so
3190 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3191 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003192 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 * @see #unregisterForContextMenu(View)
3194 * @param view The view that should show a context menu.
3195 */
3196 public void registerForContextMenu(View view) {
3197 view.setOnCreateContextMenuListener(this);
3198 }
RoboErik55011652014-07-09 15:05:53 -07003199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 /**
3201 * Prevents a context menu to be shown for the given view. This method will remove the
3202 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 * @see #registerForContextMenu(View)
3205 * @param view The view that should stop showing a context menu.
3206 */
3207 public void unregisterForContextMenu(View view) {
3208 view.setOnCreateContextMenuListener(null);
3209 }
RoboErik55011652014-07-09 15:05:53 -07003210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 /**
3212 * Programmatically opens the context menu for a particular {@code view}.
3213 * The {@code view} should have been added via
3214 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003215 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 * @param view The view to show the context menu for.
3217 */
3218 public void openContextMenu(View view) {
3219 view.showContextMenu();
3220 }
RoboErik55011652014-07-09 15:05:53 -07003221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 /**
3223 * Programmatically closes the most recently opened context menu, if showing.
3224 */
3225 public void closeContextMenu() {
3226 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3227 }
RoboErik55011652014-07-09 15:05:53 -07003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 /**
3230 * This hook is called whenever an item in a context menu is selected. The
3231 * default implementation simply returns false to have the normal processing
3232 * happen (calling the item's Runnable or sending a message to its Handler
3233 * as appropriate). You can use this method for any items for which you
3234 * would like to do processing without those other facilities.
3235 * <p>
3236 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3237 * View that added this menu item.
3238 * <p>
3239 * Derived classes should call through to the base class for it to perform
3240 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 * @param item The context menu item that was selected.
3243 * @return boolean Return false to allow normal context menu processing to
3244 * proceed, true to consume it here.
3245 */
3246 public boolean onContextItemSelected(MenuItem item) {
3247 if (mParent != null) {
3248 return mParent.onContextItemSelected(item);
3249 }
3250 return false;
3251 }
3252
3253 /**
3254 * This hook is called whenever the context menu is being closed (either by
3255 * the user canceling the menu with the back/menu button, or when an item is
3256 * selected).
RoboErik55011652014-07-09 15:05:53 -07003257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 * @param menu The context menu that is being closed.
3259 */
3260 public void onContextMenuClosed(Menu menu) {
3261 if (mParent != null) {
3262 mParent.onContextMenuClosed(menu);
3263 }
3264 }
3265
3266 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003267 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003269 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 protected Dialog onCreateDialog(int id) {
3271 return null;
3272 }
3273
3274 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003275 * Callback for creating dialogs that are managed (saved and restored) for you
3276 * by the activity. The default implementation calls through to
3277 * {@link #onCreateDialog(int)} for compatibility.
3278 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003279 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3280 * or later, consider instead using a {@link DialogFragment} instead.</em>
3281 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003282 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3283 * this method the first time, and hang onto it thereafter. Any dialog
3284 * that is created by this method will automatically be saved and restored
3285 * for you, including whether it is showing.
3286 *
3287 * <p>If you would like the activity to manage saving and restoring dialogs
3288 * for you, you should override this method and handle any ids that are
3289 * passed to {@link #showDialog}.
3290 *
3291 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3292 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3293 *
3294 * @param id The id of the dialog.
3295 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3296 * @return The dialog. If you return null, the dialog will not be created.
3297 *
3298 * @see #onPrepareDialog(int, Dialog, Bundle)
3299 * @see #showDialog(int, Bundle)
3300 * @see #dismissDialog(int)
3301 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003302 *
3303 * @deprecated Use the new {@link DialogFragment} class with
3304 * {@link FragmentManager} instead; this is also
3305 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003306 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003307 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003308 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003309 protected Dialog onCreateDialog(int id, Bundle args) {
3310 return onCreateDialog(id);
3311 }
3312
3313 /**
3314 * @deprecated Old no-arguments version of
3315 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
3316 */
3317 @Deprecated
3318 protected void onPrepareDialog(int id, Dialog dialog) {
3319 dialog.setOwnerActivity(this);
3320 }
3321
3322 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003324 * shown. The default implementation calls through to
3325 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07003326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 * <p>
3328 * Override this if you need to update a managed dialog based on the state
3329 * of the application each time it is shown. For example, a time picker
3330 * dialog might want to be updated with the current time. You should call
3331 * through to the superclass's implementation. The default implementation
3332 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07003333 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 * @param id The id of the managed dialog.
3335 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003336 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3337 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 * @see #showDialog(int)
3339 * @see #dismissDialog(int)
3340 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003341 *
3342 * @deprecated Use the new {@link DialogFragment} class with
3343 * {@link FragmentManager} instead; this is also
3344 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003346 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003347 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
3348 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 }
3350
3351 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003352 * Simple version of {@link #showDialog(int, Bundle)} that does not
3353 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
3354 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003355 *
3356 * @deprecated Use the new {@link DialogFragment} class with
3357 * {@link FragmentManager} instead; this is also
3358 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003359 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003360 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003361 public final void showDialog(int id) {
3362 showDialog(id, null);
3363 }
3364
3365 /**
3366 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 * will be made with the same id the first time this is called for a given
3368 * id. From thereafter, the dialog will be automatically saved and restored.
3369 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003370 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3371 * or later, consider instead using a {@link DialogFragment} instead.</em>
3372 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003373 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 * be made to provide an opportunity to do any timely preparation.
3375 *
3376 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003377 * @param args Arguments to pass through to the dialog. These will be saved
3378 * and restored for you. Note that if the dialog is already created,
3379 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
3380 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08003381 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003382 * @return Returns true if the Dialog was created; false is returned if
3383 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07003384 *
Joe Onorato37296dc2009-07-31 17:58:55 -07003385 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003386 * @see #onCreateDialog(int, Bundle)
3387 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 * @see #dismissDialog(int)
3389 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003390 *
3391 * @deprecated Use the new {@link DialogFragment} class with
3392 * {@link FragmentManager} instead; this is also
3393 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003395 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003396 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003397 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003399 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003401 ManagedDialog md = mManagedDialogs.get(id);
3402 if (md == null) {
3403 md = new ManagedDialog();
3404 md.mDialog = createDialog(id, null, args);
3405 if (md.mDialog == null) {
3406 return false;
3407 }
3408 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 }
RoboErik55011652014-07-09 15:05:53 -07003410
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003411 md.mArgs = args;
3412 onPrepareDialog(id, md.mDialog, args);
3413 md.mDialog.show();
3414 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 }
3416
3417 /**
3418 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
3419 *
3420 * @param id The id of the managed dialog.
3421 *
3422 * @throws IllegalArgumentException if the id was not previously shown via
3423 * {@link #showDialog(int)}.
3424 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003425 * @see #onCreateDialog(int, Bundle)
3426 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 * @see #showDialog(int)
3428 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003429 *
3430 * @deprecated Use the new {@link DialogFragment} class with
3431 * {@link FragmentManager} instead; this is also
3432 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003434 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 public final void dismissDialog(int id) {
3436 if (mManagedDialogs == null) {
3437 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 }
RoboErik55011652014-07-09 15:05:53 -07003439
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003440 final ManagedDialog md = mManagedDialogs.get(id);
3441 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 throw missingDialog(id);
3443 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003444 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 }
3446
3447 /**
3448 * Creates an exception to throw if a user passed in a dialog id that is
3449 * unexpected.
3450 */
3451 private IllegalArgumentException missingDialog(int id) {
3452 return new IllegalArgumentException("no dialog with id " + id + " was ever "
3453 + "shown via Activity#showDialog");
3454 }
3455
3456 /**
3457 * Removes any internal references to a dialog managed by this Activity.
3458 * If the dialog is showing, it will dismiss it as part of the clean up.
3459 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003460 * <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 -08003461 * want to avoid the overhead of saving and restoring it in the future.
3462 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003463 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
3464 * will not throw an exception if you try to remove an ID that does not
3465 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07003466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 * @param id The id of the managed dialog.
3468 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003469 * @see #onCreateDialog(int, Bundle)
3470 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 * @see #showDialog(int)
3472 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003473 *
3474 * @deprecated Use the new {@link DialogFragment} class with
3475 * {@link FragmentManager} instead; this is also
3476 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003478 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003480 if (mManagedDialogs != null) {
3481 final ManagedDialog md = mManagedDialogs.get(id);
3482 if (md != null) {
3483 md.mDialog.dismiss();
3484 mManagedDialogs.remove(id);
3485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
3488
3489 /**
3490 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07003491 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01003492 * <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 -07003493 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01003494 * calling this function is the same as calling
3495 * {@link #startSearch startSearch(null, false, null, false)}, which launches
3496 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07003497 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 * <p>You can override this function to force global search, e.g. in response to a dedicated
3499 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07003500 *
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003501 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default
3502 * implementation changes to simply return false and you must supply your own custom
3503 * implementation if you want to support search.</p>
3504 *
3505 * @return Returns {@code true} if search launched, and {@code false} if the activity does
3506 * not respond to search. The default implementation always returns {@code true}, except
3507 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07003508 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 * @see android.app.SearchManager
3510 */
3511 public boolean onSearchRequested() {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003512 if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK)
3513 != Configuration.UI_MODE_TYPE_TELEVISION) {
3514 startSearch(null, false, null, false);
3515 return true;
3516 } else {
3517 return false;
3518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 }
RoboErik55011652014-07-09 15:05:53 -07003520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 /**
3522 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07003523 *
3524 * <p>It is typically called from onSearchRequested(), either directly from
3525 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07003527 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 * is to inject specific data such as context data, it is preferred to <i>override</i>
3529 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07003530 *
3531 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07003533 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 * any further typing will replace it. This is useful for cases where an entire pre-formed
3535 * query is being inserted. If false, the selection point will be placed at the end of the
3536 * inserted query. This is useful when the inserted query is text that the user entered,
3537 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
3538 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07003539 * @param appSearchData An application can insert application-specific
3540 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 * searches. This data will be returned with SEARCH intent(s). Null if
3542 * no extra data is required.
3543 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07003544 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07003545 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07003547 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 * @see android.app.SearchManager
3549 * @see #onSearchRequested
3550 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003551 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
3552 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003553 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01003554 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07003555 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 }
3557
3558 /**
krosaend2d60142009-08-17 08:56:48 -07003559 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
3560 * the search dialog. Made available for testing purposes.
3561 *
3562 * @param query The query to trigger. If empty, the request will be ignored.
3563 * @param appSearchData An application can insert application-specific
3564 * context here, in order to improve quality or specificity of its own
3565 * searches. This data will be returned with SEARCH intent(s). Null if
3566 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07003567 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003568 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07003569 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01003570 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07003571 }
3572
3573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 * Request that key events come to this activity. Use this if your
3575 * activity has no views with focus, but the activity still wants
3576 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07003577 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 * @see android.view.Window#takeKeyEvents
3579 */
3580 public void takeKeyEvents(boolean get) {
3581 getWindow().takeKeyEvents(get);
3582 }
3583
3584 /**
3585 * Enable extended window features. This is a convenience for calling
3586 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07003587 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 * @param featureId The desired feature as defined in
3589 * {@link android.view.Window}.
3590 * @return Returns true if the requested feature is supported and now
3591 * enabled.
RoboErik55011652014-07-09 15:05:53 -07003592 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 * @see android.view.Window#requestFeature
3594 */
3595 public final boolean requestWindowFeature(int featureId) {
3596 return getWindow().requestFeature(featureId);
3597 }
3598
3599 /**
3600 * Convenience for calling
3601 * {@link android.view.Window#setFeatureDrawableResource}.
3602 */
3603 public final void setFeatureDrawableResource(int featureId, int resId) {
3604 getWindow().setFeatureDrawableResource(featureId, resId);
3605 }
3606
3607 /**
3608 * Convenience for calling
3609 * {@link android.view.Window#setFeatureDrawableUri}.
3610 */
3611 public final void setFeatureDrawableUri(int featureId, Uri uri) {
3612 getWindow().setFeatureDrawableUri(featureId, uri);
3613 }
3614
3615 /**
3616 * Convenience for calling
3617 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
3618 */
3619 public final void setFeatureDrawable(int featureId, Drawable drawable) {
3620 getWindow().setFeatureDrawable(featureId, drawable);
3621 }
3622
3623 /**
3624 * Convenience for calling
3625 * {@link android.view.Window#setFeatureDrawableAlpha}.
3626 */
3627 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
3628 getWindow().setFeatureDrawableAlpha(featureId, alpha);
3629 }
3630
3631 /**
3632 * Convenience for calling
3633 * {@link android.view.Window#getLayoutInflater}.
3634 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003635 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 public LayoutInflater getLayoutInflater() {
3637 return getWindow().getLayoutInflater();
3638 }
3639
3640 /**
3641 * Returns a {@link MenuInflater} with this context.
3642 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003643 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07003645 // Make sure that action views can get an appropriate theme.
3646 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07003647 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07003648 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07003649 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07003650 } else {
3651 mMenuInflater = new MenuInflater(this);
3652 }
3653 }
3654 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 }
3656
3657 @Override
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003658 protected void onApplyThemeResource(Resources.Theme theme, int resid,
3659 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 if (mParent == null) {
3661 super.onApplyThemeResource(theme, resid, first);
3662 } else {
3663 try {
3664 theme.setTo(mParent.getTheme());
3665 } catch (Exception e) {
3666 // Empty
3667 }
3668 theme.applyStyle(resid, false);
3669 }
Winson Chung03a9bae2014-05-02 09:56:12 -07003670
Winson Chunga449dc02014-05-16 11:15:04 -07003671 // Get the primary color and update the TaskDescription for this activity
Winson Chung4d1a4342014-05-09 16:42:00 -07003672 if (theme != null) {
3673 TypedArray a = theme.obtainStyledAttributes(com.android.internal.R.styleable.Theme);
Winson Chungaca96022014-05-09 15:04:08 -07003674 int colorPrimary = a.getColor(com.android.internal.R.styleable.Theme_colorPrimary, 0);
3675 a.recycle();
3676 if (colorPrimary != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07003677 ActivityManager.TaskDescription v = new ActivityManager.TaskDescription(null, null,
3678 colorPrimary);
3679 setTaskDescription(v);
Winson Chungaca96022014-05-09 15:04:08 -07003680 }
Winson Chung03a9bae2014-05-02 09:56:12 -07003681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 }
3683
3684 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07003685 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
3686 * with no options.
3687 *
3688 * @param intent The intent to start.
3689 * @param requestCode If >= 0, this code will be returned in
3690 * onActivityResult() when the activity exits.
3691 *
3692 * @throws android.content.ActivityNotFoundException
3693 *
George Mount0a778ed2013-12-13 13:35:36 -08003694 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07003695 */
3696 public void startActivityForResult(Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07003697 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07003698 }
3699
3700 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 * Launch an activity for which you would like a result when it finished.
3702 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08003703 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07003704 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07003706 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 * <p>Note that this method should only be used with Intent protocols
3708 * that are defined to return a result. In other protocols (such as
3709 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
3710 * not get the result when you expect. For example, if the activity you
3711 * are launching uses the singleTask launch mode, it will not run in your
3712 * task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003713 *
RoboErik55011652014-07-09 15:05:53 -07003714 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08003716 * activity, then your window will not be displayed until a result is
3717 * returned back from the started activity. This is to avoid visible
3718 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003719 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 * <p>This method throws {@link android.content.ActivityNotFoundException}
3721 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003722 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 * @param intent The intent to start.
3724 * @param requestCode If >= 0, this code will be returned in
3725 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003726 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003727 * See {@link android.content.Context#startActivity(Intent, Bundle)
3728 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003729 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07003731 *
George Mount0a778ed2013-12-13 13:35:36 -08003732 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003734 public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 if (mParent == null) {
3736 Instrumentation.ActivityResult ar =
3737 mInstrumentation.execStartActivity(
3738 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003739 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 if (ar != null) {
3741 mMainThread.sendActivityResult(
3742 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
3743 ar.getResultData());
3744 }
3745 if (requestCode >= 0) {
3746 // If this start is requesting a result, we can avoid making
3747 // the activity visible until the result is received. Setting
3748 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
3749 // activity hidden during this time, to avoid flickering.
3750 // This can only be done when a result is requested because
3751 // that guarantees we will get information back when the
3752 // activity is finished, no matter what happens to it.
3753 mStartedActivity = true;
3754 }
Adam Powell14874662013-07-18 19:42:41 -07003755
3756 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
3757 if (decor != null) {
3758 decor.cancelPendingInputEvents();
3759 }
3760 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07003762 if (options != null) {
3763 mParent.startActivityFromChild(this, intent, requestCode, options);
3764 } else {
3765 // Note we want to go through this method for compatibility with
3766 // existing applications that may have overridden it.
3767 mParent.startActivityFromChild(this, intent, requestCode);
3768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 }
George Mountff243282014-07-07 16:12:07 -07003770 if (options != null && !isTopOfTask()) {
3771 mActivityTransitionState.startExitOutTransition(this, options);
3772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 }
3774
3775 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07003776 * @hide Implement to provide correct calling token.
3777 */
Alexandra Gherghina83231452014-07-04 12:05:20 +01003778 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
3779 startActivityForResultAsUser(intent, requestCode, null, user);
3780 }
3781
3782 /**
3783 * @hide Implement to provide correct calling token.
3784 */
3785 public void startActivityForResultAsUser(Intent intent, int requestCode,
3786 @Nullable Bundle options, UserHandle user) {
3787 if (options != null) {
3788 mActivityTransitionState.startExitOutTransition(this, options);
3789 }
3790 if (mParent != null) {
3791 throw new RuntimeException("Can't be called from a child");
3792 }
3793 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
3794 this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode,
3795 options, user);
3796 if (ar != null) {
3797 mMainThread.sendActivityResult(
3798 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
3799 }
3800 if (requestCode >= 0) {
3801 // If this start is requesting a result, we can avoid making
3802 // the activity visible until the result is received. Setting
3803 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
3804 // activity hidden during this time, to avoid flickering.
3805 // This can only be done when a result is requested because
3806 // that guarantees we will get information back when the
3807 // activity is finished, no matter what happens to it.
3808 mStartedActivity = true;
3809 }
3810
3811 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
3812 if (decor != null) {
3813 decor.cancelPendingInputEvents();
3814 }
3815 }
3816
3817 /**
3818 * @hide Implement to provide correct calling token.
3819 */
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07003820 public void startActivityAsUser(Intent intent, UserHandle user) {
3821 startActivityAsUser(intent, null, user);
3822 }
3823
3824 /**
3825 * @hide Implement to provide correct calling token.
3826 */
3827 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
3828 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01003829 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07003830 }
3831 Instrumentation.ActivityResult ar =
3832 mInstrumentation.execStartActivity(
3833 this, mMainThread.getApplicationThread(), mToken, this,
3834 intent, -1, options, user);
3835 if (ar != null) {
3836 mMainThread.sendActivityResult(
3837 mToken, mEmbeddedID, -1, ar.getResultCode(),
3838 ar.getResultData());
3839 }
3840 }
3841
3842 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07003843 * Start a new activity as if it was started by the activity that started our
3844 * current activity. This is for the resolver and chooser activities, which operate
3845 * as intermediaries that dispatch their intent to the target the user selects -- to
3846 * do this, they must perform all security checks including permission grants as if
3847 * their launch had come from the original activity.
3848 * @hide
3849 */
Jeff Sharkey97978802014-10-14 10:48:18 -07003850 public void startActivityAsCaller(Intent intent, @Nullable Bundle options, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07003851 if (mParent != null) {
3852 throw new RuntimeException("Can't be called from a child");
3853 }
3854 Instrumentation.ActivityResult ar =
3855 mInstrumentation.execStartActivityAsCaller(
3856 this, mMainThread.getApplicationThread(), mToken, this,
Jeff Sharkey97978802014-10-14 10:48:18 -07003857 intent, -1, options, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07003858 if (ar != null) {
3859 mMainThread.sendActivityResult(
3860 mToken, mEmbeddedID, -1, ar.getResultCode(),
3861 ar.getResultData());
3862 }
3863 }
3864
3865 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07003866 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
3867 * Intent, int, int, int, Bundle)} with no options.
3868 *
3869 * @param intent The IntentSender to launch.
3870 * @param requestCode If >= 0, this code will be returned in
3871 * onActivityResult() when the activity exits.
3872 * @param fillInIntent If non-null, this will be provided as the
3873 * intent parameter to {@link IntentSender#sendIntent}.
3874 * @param flagsMask Intent flags in the original IntentSender that you
3875 * would like to change.
3876 * @param flagsValues Desired values for any bits set in
3877 * <var>flagsMask</var>
3878 * @param extraFlags Always set to 0.
3879 */
3880 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07003881 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07003882 throws IntentSender.SendIntentException {
3883 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
3884 flagsValues, extraFlags, null);
3885 }
3886
3887 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003888 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003889 * to use a IntentSender to describe the activity to be started. If
3890 * the IntentSender is for an activity, that activity will be started
3891 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
3892 * here; otherwise, its associated action will be executed (such as
3893 * sending a broadcast) as if you had called
3894 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07003895 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003896 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003897 * @param requestCode If >= 0, this code will be returned in
3898 * onActivityResult() when the activity exits.
3899 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003900 * intent parameter to {@link IntentSender#sendIntent}.
3901 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003902 * would like to change.
3903 * @param flagsValues Desired values for any bits set in
3904 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003905 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003906 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003907 * See {@link android.content.Context#startActivity(Intent, Bundle)
3908 * Context.startActivity(Intent, Bundle)} for more details. If options
3909 * have also been supplied by the IntentSender, options given here will
3910 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003911 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003912 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07003913 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003914 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003915 if (mParent == null) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003916 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003917 flagsMask, flagsValues, this, options);
3918 } else if (options != null) {
3919 mParent.startIntentSenderFromChild(this, intent, requestCode,
3920 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003921 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07003922 // Note we want to go through this call for compatibility with
3923 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003924 mParent.startIntentSenderFromChild(this, intent, requestCode,
3925 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003926 }
3927 }
3928
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003929 private void startIntentSenderForResultInner(IntentSender intent, int requestCode,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003930 Intent fillInIntent, int flagsMask, int flagsValues, Activity activity,
3931 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003932 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003933 try {
3934 String resolvedType = null;
3935 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07003936 fillInIntent.migrateExtraStreamToClipData();
3937 fillInIntent.prepareToLeaveProcess();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003938 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
3939 }
3940 int result = ActivityManagerNative.getDefault()
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003941 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003942 fillInIntent, resolvedType, mToken, activity.mEmbeddedID,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003943 requestCode, flagsMask, flagsValues, options);
3944 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003945 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003946 }
3947 Instrumentation.checkStartActivityResult(result, null);
3948 } catch (RemoteException e) {
3949 }
3950 if (requestCode >= 0) {
3951 // If this start is requesting a result, we can avoid making
3952 // the activity visible until the result is received. Setting
3953 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
3954 // activity hidden during this time, to avoid flickering.
3955 // This can only be done when a result is requested because
3956 // that guarantees we will get information back when the
3957 // activity is finished, no matter what happens to it.
3958 mStartedActivity = true;
3959 }
3960 }
3961
3962 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07003963 * Same as {@link #startActivity(Intent, Bundle)} with no options
3964 * specified.
3965 *
3966 * @param intent The intent to start.
3967 *
3968 * @throws android.content.ActivityNotFoundException
3969 *
3970 * @see {@link #startActivity(Intent, Bundle)}
3971 * @see #startActivityForResult
3972 */
3973 @Override
3974 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00003975 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07003976 }
3977
3978 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 * Launch a new activity. You will not receive any information about when
3980 * the activity exits. This implementation overrides the base version,
3981 * providing information about
3982 * the activity performing the launch. Because of this additional
3983 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
3984 * required; if not specified, the new activity will be added to the
3985 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07003986 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 * <p>This method throws {@link android.content.ActivityNotFoundException}
3988 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07003989 *
3990 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003991 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003992 * See {@link android.content.Context#startActivity(Intent, Bundle)
3993 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07003994 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07003996 *
3997 * @see {@link #startActivity(Intent)}
RoboErik55011652014-07-09 15:05:53 -07003998 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 */
4000 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004001 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004002 if (options != null) {
4003 startActivityForResult(intent, -1, options);
4004 } else {
4005 // Note we want to go through this call for compatibility with
4006 // applications that may have overridden the method.
4007 startActivityForResult(intent, -1);
4008 }
4009 }
4010
4011 /**
4012 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4013 * specified.
4014 *
4015 * @param intents The intents to start.
4016 *
4017 * @throws android.content.ActivityNotFoundException
4018 *
4019 * @see {@link #startActivities(Intent[], Bundle)}
4020 * @see #startActivityForResult
4021 */
4022 @Override
4023 public void startActivities(Intent[] intents) {
4024 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 }
4026
4027 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004028 * Launch a new activity. You will not receive any information about when
4029 * the activity exits. This implementation overrides the base version,
4030 * providing information about
4031 * the activity performing the launch. Because of this additional
4032 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4033 * required; if not specified, the new activity will be added to the
4034 * task of the caller.
4035 *
4036 * <p>This method throws {@link android.content.ActivityNotFoundException}
4037 * if there was no Activity found to run the given Intent.
4038 *
4039 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004040 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004041 * See {@link android.content.Context#startActivity(Intent, Bundle)
4042 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004043 *
4044 * @throws android.content.ActivityNotFoundException
4045 *
Dianne Hackborna4972e92012-03-14 10:38:05 -07004046 * @see {@link #startActivities(Intent[])}
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004047 * @see #startActivityForResult
4048 */
4049 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004050 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004051 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004052 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004053 }
4054
4055 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004056 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4057 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004058 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004059 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004060 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004061 * intent parameter to {@link IntentSender#sendIntent}.
4062 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004063 * would like to change.
4064 * @param flagsValues Desired values for any bits set in
4065 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004066 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004067 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004068 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004069 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004070 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004071 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
4072 extraFlags, null);
4073 }
4074
4075 /**
4076 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
4077 * to start; see
4078 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
4079 * for more information.
4080 *
4081 * @param intent The IntentSender to launch.
4082 * @param fillInIntent If non-null, this will be provided as the
4083 * intent parameter to {@link IntentSender#sendIntent}.
4084 * @param flagsMask Intent flags in the original IntentSender that you
4085 * would like to change.
4086 * @param flagsValues Desired values for any bits set in
4087 * <var>flagsMask</var>
4088 * @param extraFlags Always set to 0.
4089 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004090 * See {@link android.content.Context#startActivity(Intent, Bundle)
4091 * Context.startActivity(Intent, Bundle)} for more details. If options
4092 * have also been supplied by the IntentSender, options given here will
4093 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004094 */
4095 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004096 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004097 Bundle options) throws IntentSender.SendIntentException {
4098 if (options != null) {
4099 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4100 flagsValues, extraFlags, options);
4101 } else {
4102 // Note we want to go through this call for compatibility with
4103 // applications that may have overridden the method.
4104 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4105 flagsValues, extraFlags);
4106 }
4107 }
4108
4109 /**
4110 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
4111 * with no options.
4112 *
4113 * @param intent The intent to start.
4114 * @param requestCode If >= 0, this code will be returned in
4115 * onActivityResult() when the activity exits, as described in
4116 * {@link #startActivityForResult}.
4117 *
4118 * @return If a new activity was launched then true is returned; otherwise
4119 * false is returned and you must handle the Intent yourself.
4120 *
4121 * @see #startActivity
4122 * @see #startActivityForResult
4123 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004124 public boolean startActivityIfNeeded(@NonNull Intent intent, int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004125 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004126 }
4127
4128 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004129 * A special variation to launch an activity only if a new activity
4130 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07004131 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07004133 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07004135 * and the activity
4136 * that handles <var>intent</var> is the same as your currently running
4137 * activity, then a new instance is not needed. In this case, instead of
4138 * the normal behavior of calling {@link #onNewIntent} this function will
4139 * return and you can handle the Intent yourself.
4140 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 * <p>This function can only be called from a top-level activity; if it is
4142 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07004143 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 * @param intent The intent to start.
4145 * @param requestCode If >= 0, this code will be returned in
4146 * onActivityResult() when the activity exits, as described in
4147 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004148 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004149 * See {@link android.content.Context#startActivity(Intent, Bundle)
4150 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 * @return If a new activity was launched then true is returned; otherwise
4153 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07004154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 * @see #startActivity
4156 * @see #startActivityForResult
4157 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004158 public boolean startActivityIfNeeded(@NonNull Intent intent, int requestCode,
4159 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004161 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004163 intent.migrateExtraStreamToClipData();
4164 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 result = ActivityManagerNative.getDefault()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08004166 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07004167 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
4168 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
4169 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 } catch (RemoteException e) {
4171 // Empty
4172 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08004173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 if (requestCode >= 0) {
4177 // If this start is requesting a result, we can avoid making
4178 // the activity visible until the result is received. Setting
4179 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4180 // activity hidden during this time, to avoid flickering.
4181 // This can only be done when a result is requested because
4182 // that guarantees we will get information back when the
4183 // activity is finished, no matter what happens to it.
4184 mStartedActivity = true;
4185 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004186 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 }
4188
4189 throw new UnsupportedOperationException(
4190 "startActivityIfNeeded can only be called from a top-level activity");
4191 }
4192
4193 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004194 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
4195 * no options.
4196 *
4197 * @param intent The intent to dispatch to the next activity. For
4198 * correct behavior, this must be the same as the Intent that started
4199 * your own activity; the only changes you can make are to the extras
4200 * inside of it.
4201 *
4202 * @return Returns a boolean indicating whether there was another Activity
4203 * to start: true if there was a next activity to start, false if there
4204 * wasn't. In general, if true is returned you will then want to call
4205 * finish() on yourself.
4206 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004207 public boolean startNextMatchingActivity(@NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004208 return startNextMatchingActivity(intent, null);
4209 }
4210
4211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 * Special version of starting an activity, for use when you are replacing
4213 * other activity components. You can use this to hand the Intent off
4214 * to the next Activity that can handle it. You typically call this in
4215 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07004216 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 * @param intent The intent to dispatch to the next activity. For
4218 * correct behavior, this must be the same as the Intent that started
4219 * your own activity; the only changes you can make are to the extras
4220 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004221 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004222 * See {@link android.content.Context#startActivity(Intent, Bundle)
4223 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004224 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 * @return Returns a boolean indicating whether there was another Activity
4226 * to start: true if there was a next activity to start, false if there
4227 * wasn't. In general, if true is returned you will then want to call
4228 * finish() on yourself.
4229 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004230 public boolean startNextMatchingActivity(@NonNull Intent intent, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 if (mParent == null) {
4232 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004233 intent.migrateExtraStreamToClipData();
4234 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 return ActivityManagerNative.getDefault()
Dianne Hackborna4972e92012-03-14 10:38:05 -07004236 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 } catch (RemoteException e) {
4238 // Empty
4239 }
4240 return false;
4241 }
4242
4243 throw new UnsupportedOperationException(
4244 "startNextMatchingActivity can only be called from a top-level activity");
4245 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004246
4247 /**
4248 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
4249 * with no options.
4250 *
4251 * @param child The activity making the call.
4252 * @param intent The intent to start.
4253 * @param requestCode Reply request code. < 0 if reply is not requested.
4254 *
4255 * @throws android.content.ActivityNotFoundException
4256 *
4257 * @see #startActivity
4258 * @see #startActivityForResult
4259 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004260 public void startActivityFromChild(@NonNull Activity child, Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004261 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004262 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004263 }
4264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 /**
RoboErik55011652014-07-09 15:05:53 -07004266 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07004268 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 * <p>This method throws {@link android.content.ActivityNotFoundException}
4270 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004271 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272 * @param child The activity making the call.
4273 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004274 * @param requestCode Reply request code. < 0 if reply is not requested.
4275 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004276 * See {@link android.content.Context#startActivity(Intent, Bundle)
4277 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004280 *
4281 * @see #startActivity
4282 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004284 public void startActivityFromChild(@NonNull Activity child, Intent intent,
4285 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 Instrumentation.ActivityResult ar =
4287 mInstrumentation.execStartActivity(
4288 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004289 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004290 if (ar != null) {
4291 mMainThread.sendActivityResult(
4292 mToken, child.mEmbeddedID, requestCode,
4293 ar.getResultCode(), ar.getResultData());
4294 }
4295 }
4296
4297 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004298 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
4299 * with no options.
4300 *
4301 * @param fragment The fragment making the call.
4302 * @param intent The intent to start.
4303 * @param requestCode Reply request code. < 0 if reply is not requested.
4304 *
4305 * @throws android.content.ActivityNotFoundException
4306 *
4307 * @see Fragment#startActivity
4308 * @see Fragment#startActivityForResult
4309 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004310 public void startActivityFromFragment(@NonNull Fragment fragment, Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004311 int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004312 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004313 }
4314
4315 /**
RoboErik55011652014-07-09 15:05:53 -07004316 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004317 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
4318 * method.
RoboErik55011652014-07-09 15:05:53 -07004319 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004320 * <p>This method throws {@link android.content.ActivityNotFoundException}
4321 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004322 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004323 * @param fragment The fragment making the call.
4324 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07004325 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004326 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004327 * See {@link android.content.Context#startActivity(Intent, Bundle)
4328 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004329 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004330 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004331 *
4332 * @see Fragment#startActivity
4333 * @see Fragment#startActivityForResult
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004334 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004335 public void startActivityFromFragment(@NonNull Fragment fragment, Intent intent,
4336 int requestCode, @Nullable Bundle options) {
George Mounta8bd9292014-05-21 10:54:17 -07004337 if (options != null) {
4338 mActivityTransitionState.startExitOutTransition(this, options);
4339 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004340 Instrumentation.ActivityResult ar =
4341 mInstrumentation.execStartActivity(
4342 this, mMainThread.getApplicationThread(), mToken, fragment,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004343 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004344 if (ar != null) {
4345 mMainThread.sendActivityResult(
4346 mToken, fragment.mWho, requestCode,
4347 ar.getResultCode(), ar.getResultData());
4348 }
4349 }
4350
4351 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004352 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
4353 * int, Intent, int, int, int, Bundle)} with no options.
4354 */
4355 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4356 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
4357 int extraFlags)
4358 throws IntentSender.SendIntentException {
4359 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
4360 flagsMask, flagsValues, extraFlags, null);
4361 }
4362
4363 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004364 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004365 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07004366 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004367 * for more information.
4368 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004369 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4370 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07004371 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004372 throws IntentSender.SendIntentException {
4373 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004374 flagsMask, flagsValues, child, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004375 }
4376
4377 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004378 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
4379 * or {@link #finish} to specify an explicit transition animation to
4380 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004381 *
4382 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
4383 * to using this with starting activities is to supply the desired animation
4384 * information through a {@link ActivityOptions} bundle to
4385 * {@link #startActivity(Intent, Bundle) or a related function. This allows
4386 * you to specify a custom animation even when starting an activity from
4387 * outside the context of the current top activity.
4388 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004389 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004390 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004391 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004392 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004393 */
4394 public void overridePendingTransition(int enterAnim, int exitAnim) {
4395 try {
4396 ActivityManagerNative.getDefault().overridePendingTransition(
4397 mToken, getPackageName(), enterAnim, exitAnim);
4398 } catch (RemoteException e) {
4399 }
4400 }
RoboErik55011652014-07-09 15:05:53 -07004401
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004402 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 * Call this to set the result that your activity will return to its
4404 * caller.
RoboErik55011652014-07-09 15:05:53 -07004405 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 * @param resultCode The result code to propagate back to the originating
4407 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07004408 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 * @see #RESULT_CANCELED
4410 * @see #RESULT_OK
4411 * @see #RESULT_FIRST_USER
4412 * @see #setResult(int, Intent)
4413 */
4414 public final void setResult(int resultCode) {
4415 synchronized (this) {
4416 mResultCode = resultCode;
4417 mResultData = null;
4418 }
4419 }
4420
4421 /**
4422 * Call this to set the result that your activity will return to its
4423 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004424 *
4425 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
4426 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
4427 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
4428 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
4429 * Activity receiving the result access to the specific URIs in the Intent.
4430 * Access will remain until the Activity has finished (it will remain across the hosting
4431 * process being killed and other temporary destruction) and will be added
4432 * to any existing set of URI permissions it already holds.
4433 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 * @param resultCode The result code to propagate back to the originating
4435 * activity, often RESULT_CANCELED or RESULT_OK
4436 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07004437 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 * @see #RESULT_CANCELED
4439 * @see #RESULT_OK
4440 * @see #RESULT_FIRST_USER
4441 * @see #setResult(int)
4442 */
4443 public final void setResult(int resultCode, Intent data) {
4444 synchronized (this) {
4445 mResultCode = resultCode;
4446 mResultData = data;
4447 }
4448 }
4449
4450 /**
4451 * Return the name of the package that invoked this activity. This is who
4452 * the data in {@link #setResult setResult()} will be sent to. You can
4453 * use this information to validate that the recipient is allowed to
4454 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07004455 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07004456 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07004457 * did not use the {@link #startActivityForResult}
4458 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07004459 * null.</p>
4460 *
4461 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
4462 * the result from this method was unstable. If the process hosting the calling
4463 * package was no longer running, it would return null instead of the proper package
4464 * name. You can use {@link #getCallingActivity()} and retrieve the package name
4465 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07004466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 * @return The package of the activity that will receive your
4468 * reply, or null if none.
4469 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004470 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 public String getCallingPackage() {
4472 try {
4473 return ActivityManagerNative.getDefault().getCallingPackage(mToken);
4474 } catch (RemoteException e) {
4475 return null;
4476 }
4477 }
4478
4479 /**
4480 * Return the name of the activity that invoked this activity. This is
4481 * who the data in {@link #setResult setResult()} will be sent to. You
4482 * can use this information to validate that the recipient is allowed to
4483 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07004484 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07004485 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07004486 * did not use the {@link #startActivityForResult}
4487 * form that includes a request code), then the calling package will be
4488 * null.
4489 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07004490 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 * reply, or null if none.
4492 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004493 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 public ComponentName getCallingActivity() {
4495 try {
4496 return ActivityManagerNative.getDefault().getCallingActivity(mToken);
4497 } catch (RemoteException e) {
4498 return null;
4499 }
4500 }
4501
4502 /**
4503 * Control whether this activity's main window is visible. This is intended
4504 * only for the special case of an activity that is not going to show a
4505 * UI itself, but can't just finish prior to onResume() because it needs
4506 * to wait for a service binding or such. Setting this to false allows
4507 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07004508 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 * <p>The default value for this is taken from the
4510 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
4511 */
4512 public void setVisible(boolean visible) {
4513 if (mVisibleFromClient != visible) {
4514 mVisibleFromClient = visible;
4515 if (mVisibleFromServer) {
4516 if (visible) makeVisible();
4517 else mDecor.setVisibility(View.INVISIBLE);
4518 }
4519 }
4520 }
RoboErik55011652014-07-09 15:05:53 -07004521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 void makeVisible() {
4523 if (!mWindowAdded) {
4524 ViewManager wm = getWindowManager();
4525 wm.addView(mDecor, getWindow().getAttributes());
4526 mWindowAdded = true;
4527 }
4528 mDecor.setVisibility(View.VISIBLE);
4529 }
RoboErik55011652014-07-09 15:05:53 -07004530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 /**
4532 * Check to see whether this activity is in the process of finishing,
4533 * either because you called {@link #finish} on it or someone else
4534 * has requested that it finished. This is often used in
4535 * {@link #onPause} to determine whether the activity is simply pausing or
4536 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07004537 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07004539 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 * @see #finish
4541 */
4542 public boolean isFinishing() {
4543 return mFinished;
4544 }
4545
4546 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07004547 * Returns true if the final {@link #onDestroy()} call has been made
4548 * on the Activity, so this instance is now dead.
4549 */
4550 public boolean isDestroyed() {
4551 return mDestroyed;
4552 }
4553
4554 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05004555 * Check to see whether this activity is in the process of being destroyed in order to be
4556 * recreated with a new configuration. This is often used in
4557 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
4558 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07004559 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05004560 * @return If the activity is being torn down in order to be recreated with a new configuration,
4561 * returns true; else returns false.
4562 */
4563 public boolean isChangingConfigurations() {
4564 return mChangingConfigurations;
4565 }
4566
4567 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004568 * Cause this Activity to be recreated with a new instance. This results
4569 * in essentially the same flow as when the Activity is created due to
4570 * a configuration change -- the current instance will go through its
4571 * lifecycle to {@link #onDestroy} and a new instance then created after it.
4572 */
4573 public void recreate() {
4574 if (mParent != null) {
4575 throw new IllegalStateException("Can only be called on top-level activity");
4576 }
4577 if (Looper.myLooper() != mMainThread.getLooper()) {
4578 throw new IllegalStateException("Must be called from main thread");
4579 }
4580 mMainThread.requestRelaunchActivity(mToken, null, null, 0, false, null, false);
4581 }
4582
4583 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07004584 * Finishes the current activity and specifies whether to remove the task associated with this
4585 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 */
Winson Chung3b3f4642014-04-22 10:08:18 -07004587 private void finish(boolean finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 if (mParent == null) {
4589 int resultCode;
4590 Intent resultData;
4591 synchronized (this) {
4592 resultCode = mResultCode;
4593 resultData = mResultData;
4594 }
Joe Onorato43a17652011-04-06 19:22:23 -07004595 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04004597 if (resultData != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004598 resultData.prepareToLeaveProcess();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04004599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004600 if (ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07004601 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004602 mFinished = true;
4603 }
4604 } catch (RemoteException e) {
4605 // Empty
4606 }
4607 } else {
4608 mParent.finishFromChild(this);
4609 }
4610 }
4611
4612 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07004613 * Call this when your activity is done and should be closed. The
4614 * ActivityResult is propagated back to whoever launched you via
4615 * onActivityResult().
4616 */
4617 public void finish() {
4618 finish(false);
4619 }
4620
4621 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07004622 * Finish this activity as well as all activities immediately below it
4623 * in the current task that have the same affinity. This is typically
4624 * used when an application can be launched on to another task (such as
4625 * from an ACTION_VIEW of a content type it understands) and the user
4626 * has used the up navigation to switch out of the current task and in
4627 * to its own task. In this case, if the user has navigated down into
4628 * any other activities of the second application, all of those should
4629 * be removed from the original task as part of the task switch.
4630 *
4631 * <p>Note that this finish does <em>not</em> allow you to deliver results
4632 * to the previous activity, and an exception will be thrown if you are trying
4633 * to do so.</p>
4634 */
4635 public void finishAffinity() {
4636 if (mParent != null) {
4637 throw new IllegalStateException("Can not be called from an embedded activity");
4638 }
4639 if (mResultCode != RESULT_CANCELED || mResultData != null) {
4640 throw new IllegalStateException("Can not be called to deliver a result");
4641 }
4642 try {
4643 if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) {
4644 mFinished = true;
4645 }
4646 } catch (RemoteException e) {
4647 // Empty
4648 }
4649 }
4650
4651 /**
RoboErik55011652014-07-09 15:05:53 -07004652 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 * {@link #finish} method. The default implementation simply calls
4654 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07004655 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004656 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07004657 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 * @see #finish
4659 */
4660 public void finishFromChild(Activity child) {
4661 finish();
4662 }
4663
4664 /**
George Mountcb4b7d92014-02-25 10:47:55 -08004665 * Reverses the Activity Scene entry Transition and triggers the calling Activity
4666 * to reverse its exit Transition. When the exit Transition completes,
4667 * {@link #finish()} is called. If no entry Transition was used, finish() is called
4668 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07004669 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08004670 */
Craig Mautner73f843d2014-05-19 09:42:28 -07004671 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07004672 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07004673 finish();
4674 }
George Mountcb4b7d92014-02-25 10:47:55 -08004675 }
4676
4677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 * Force finish another activity that you had previously started with
4679 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07004680 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004681 * @param requestCode The request code of the activity that you had
4682 * given to startActivityForResult(). If there are multiple
4683 * activities started with this request code, they
4684 * will all be finished.
4685 */
4686 public void finishActivity(int requestCode) {
4687 if (mParent == null) {
4688 try {
4689 ActivityManagerNative.getDefault()
4690 .finishSubActivity(mToken, mEmbeddedID, requestCode);
4691 } catch (RemoteException e) {
4692 // Empty
4693 }
4694 } else {
4695 mParent.finishActivityFromChild(this, requestCode);
4696 }
4697 }
4698
4699 /**
4700 * This is called when a child activity of this one calls its
4701 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07004702 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 * @param child The activity making the call.
4704 * @param requestCode Request code that had been used to start the
4705 * activity.
4706 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004707 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004708 try {
4709 ActivityManagerNative.getDefault()
4710 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
4711 } catch (RemoteException e) {
4712 // Empty
4713 }
4714 }
4715
4716 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07004717 * Call this when your activity is done and should be closed and the task should be completely
4718 * removed as a part of finishing the Activity.
4719 */
4720 public void finishAndRemoveTask() {
4721 finish(true);
4722 }
4723
4724 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004725 * Ask that the local app instance of this activity be released to free up its memory.
4726 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
4727 * a new instance of the activity will later be re-created if needed due to the user
4728 * navigating back to it.
4729 *
4730 * @return Returns true if the activity was in a state that it has started the process
4731 * of destroying its current instance; returns false if for any reason this could not
4732 * be done: it is currently visible to the user, it is already being destroyed, it is
4733 * being finished, it hasn't yet saved its state, etc.
4734 */
4735 public boolean releaseInstance() {
4736 try {
4737 return ActivityManagerNative.getDefault().releaseActivityInstance(mToken);
4738 } catch (RemoteException e) {
4739 // Empty
4740 }
4741 return false;
4742 }
4743
4744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 * Called when an activity you launched exits, giving you the requestCode
4746 * you started it with, the resultCode it returned, and any additional
4747 * data from it. The <var>resultCode</var> will be
4748 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
4749 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07004750 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004751 * <p>You will receive this call immediately before onResume() when your
4752 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07004753 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07004754 * <p>This method is never invoked if your activity sets
4755 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4756 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08004757 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 * @param requestCode The integer request code originally supplied to
4759 * startActivityForResult(), allowing you to identify who this
4760 * result came from.
4761 * @param resultCode The integer result code returned by the child activity
4762 * through its setResult().
4763 * @param data An Intent, which can return result data to the caller
4764 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07004765 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004766 * @see #startActivityForResult
4767 * @see #createPendingResult
4768 * @see #setResult(int)
4769 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004770 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004771 }
4772
4773 /**
George Mount62ab9b72014-05-02 13:51:17 -07004774 * Called when an activity you launched with an activity transition exposes this
4775 * Activity through a returning activity transition, giving you the resultCode
4776 * and any additional data from it. This method will only be called if the activity
4777 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07004778 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07004779 *
4780 * <p>The purpose of this function is to let the called Activity send a hint about
4781 * its state so that this underlying Activity can prepare to be exposed. A call to
4782 * this method does not guarantee that the called Activity has or will be exiting soon.
4783 * It only indicates that it will expose this Activity's Window and it has
4784 * some data to pass to prepare it.</p>
4785 *
4786 * @param resultCode The integer result code returned by the child activity
4787 * through its setResult().
4788 * @param data An Intent, which can return result data to the caller
4789 * (various data can be attached to Intent "extras").
4790 */
Craig Mautner7a629c22014-09-04 14:57:04 -07004791 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07004792 }
4793
4794 /**
RoboErik55011652014-07-09 15:05:53 -07004795 * Create a new PendingIntent object which you can hand to others
4796 * for them to use to send result data back to your
4797 * {@link #onActivityResult} callback. The created object will be either
4798 * one-shot (becoming invalid after a result is sent back) or multiple
4799 * (allowing any number of results to be sent through it).
4800 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 * @param requestCode Private request code for the sender that will be
4802 * associated with the result data when it is returned. The sender can not
4803 * modify this value, allowing you to identify incoming results.
4804 * @param data Default data to supply in the result, which may be modified
4805 * by the sender.
4806 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
4807 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
4808 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
4809 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
4810 * or any of the flags as supported by
4811 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
4812 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07004813 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004814 * @return Returns an existing or new PendingIntent matching the given
4815 * parameters. May return null only if
4816 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
4817 * supplied.
RoboErik55011652014-07-09 15:05:53 -07004818 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819 * @see PendingIntent
4820 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004821 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
4822 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 String packageName = getPackageName();
4824 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004825 data.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826 IIntentSender target =
4827 ActivityManagerNative.getDefault().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07004828 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004829 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07004830 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
4831 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 return target != null ? new PendingIntent(target) : null;
4833 } catch (RemoteException e) {
4834 // Empty
4835 }
4836 return null;
4837 }
4838
4839 /**
4840 * Change the desired orientation of this activity. If the activity
4841 * is currently in the foreground or otherwise impacting the screen
4842 * orientation, the screen will immediately be changed (possibly causing
4843 * the activity to be restarted). Otherwise, this will be used the next
4844 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07004845 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 * @param requestedOrientation An orientation constant as used in
4847 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
4848 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004849 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004850 if (mParent == null) {
4851 try {
4852 ActivityManagerNative.getDefault().setRequestedOrientation(
4853 mToken, requestedOrientation);
4854 } catch (RemoteException e) {
4855 // Empty
4856 }
4857 } else {
4858 mParent.setRequestedOrientation(requestedOrientation);
4859 }
4860 }
RoboErik55011652014-07-09 15:05:53 -07004861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004862 /**
4863 * Return the current requested orientation of the activity. This will
4864 * either be the orientation requested in its component's manifest, or
4865 * the last requested orientation given to
4866 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07004867 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 * @return Returns an orientation constant as used in
4869 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
4870 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004871 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004872 public int getRequestedOrientation() {
4873 if (mParent == null) {
4874 try {
4875 return ActivityManagerNative.getDefault()
4876 .getRequestedOrientation(mToken);
4877 } catch (RemoteException e) {
4878 // Empty
4879 }
4880 } else {
4881 return mParent.getRequestedOrientation();
4882 }
4883 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
4884 }
RoboErik55011652014-07-09 15:05:53 -07004885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886 /**
4887 * Return the identifier of the task this activity is in. This identifier
4888 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07004889 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 * @return Task identifier, an opaque integer.
4891 */
4892 public int getTaskId() {
4893 try {
4894 return ActivityManagerNative.getDefault()
4895 .getTaskForActivity(mToken, false);
4896 } catch (RemoteException e) {
4897 return -1;
4898 }
4899 }
4900
4901 /**
4902 * Return whether this activity is the root of a task. The root is the
4903 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07004904 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 * @return True if this is the root activity, else false.
4906 */
4907 public boolean isTaskRoot() {
4908 try {
4909 return ActivityManagerNative.getDefault()
4910 .getTaskForActivity(mToken, true) >= 0;
4911 } catch (RemoteException e) {
4912 return false;
4913 }
4914 }
4915
4916 /**
4917 * Move the task containing this activity to the back of the activity
4918 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07004919 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920 * @param nonRoot If false then this only works if the activity is the root
4921 * of a task; if true it will work for any activity in
4922 * a task.
RoboErik55011652014-07-09 15:05:53 -07004923 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 * @return If the task was moved (or it was already at the
4925 * back) true is returned, else false.
4926 */
4927 public boolean moveTaskToBack(boolean nonRoot) {
4928 try {
4929 return ActivityManagerNative.getDefault().moveActivityTaskToBack(
4930 mToken, nonRoot);
4931 } catch (RemoteException e) {
4932 // Empty
4933 }
4934 return false;
4935 }
4936
4937 /**
4938 * Returns class name for this activity with the package prefix removed.
4939 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07004940 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 * @return The local class name.
4942 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004943 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 public String getLocalClassName() {
4945 final String pkg = getPackageName();
4946 final String cls = mComponent.getClassName();
4947 int packageLen = pkg.length();
4948 if (!cls.startsWith(pkg) || cls.length() <= packageLen
4949 || cls.charAt(packageLen) != '.') {
4950 return cls;
4951 }
4952 return cls.substring(packageLen+1);
4953 }
RoboErik55011652014-07-09 15:05:53 -07004954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 /**
4956 * Returns complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07004957 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 * @return Returns the complete component name for this activity
4959 */
4960 public ComponentName getComponentName()
4961 {
4962 return mComponent;
4963 }
4964
4965 /**
4966 * Retrieve a {@link SharedPreferences} object for accessing preferences
4967 * that are private to this activity. This simply calls the underlying
4968 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
4969 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07004970 *
4971 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
4972 * operation, {@link #MODE_WORLD_READABLE} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 * {@link #MODE_WORLD_WRITEABLE} to control permissions.
4974 *
4975 * @return Returns the single SharedPreferences instance that can be used
4976 * to retrieve and modify the preference values.
4977 */
4978 public SharedPreferences getPreferences(int mode) {
4979 return getSharedPreferences(getLocalClassName(), mode);
4980 }
RoboErik55011652014-07-09 15:05:53 -07004981
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004982 private void ensureSearchManager() {
4983 if (mSearchManager != null) {
4984 return;
4985 }
RoboErik55011652014-07-09 15:05:53 -07004986
Amith Yamasanie9ce3f02010-01-25 09:15:50 -08004987 mSearchManager = new SearchManager(this, null);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004988 }
Tor Norbyed9273d62013-05-30 15:59:53 -07004989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004990 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004991 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004992 if (getBaseContext() == null) {
4993 throw new IllegalStateException(
4994 "System services not available to Activities before onCreate()");
4995 }
4996
4997 if (WINDOW_SERVICE.equals(name)) {
4998 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01004999 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005000 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005001 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005002 }
5003 return super.getSystemService(name);
5004 }
5005
5006 /**
5007 * Change the title associated with this activity. If this is a
5008 * top-level activity, the title for its window will change. If it
5009 * is an embedded activity, the parent can do whatever it wants
5010 * with it.
5011 */
5012 public void setTitle(CharSequence title) {
5013 mTitle = title;
5014 onTitleChanged(title, mTitleColor);
5015
5016 if (mParent != null) {
5017 mParent.onChildTitleChanged(this, title);
5018 }
5019 }
5020
5021 /**
5022 * Change the title associated with this activity. If this is a
5023 * top-level activity, the title for its window will change. If it
5024 * is an embedded activity, the parent can do whatever it wants
5025 * with it.
5026 */
5027 public void setTitle(int titleId) {
5028 setTitle(getText(titleId));
5029 }
5030
Alan Viverette2525d9c2013-11-15 14:42:19 -08005031 /**
5032 * Change the color of the title associated with this activity.
5033 * <p>
5034 * This method is deprecated starting in API Level 11 and replaced by action
5035 * bar styles. For information on styling the Action Bar, read the <a
5036 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
5037 * guide.
5038 *
5039 * @deprecated Use action bar styles instead.
5040 */
5041 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 public void setTitleColor(int textColor) {
5043 mTitleColor = textColor;
5044 onTitleChanged(mTitle, textColor);
5045 }
5046
5047 public final CharSequence getTitle() {
5048 return mTitle;
5049 }
5050
5051 public final int getTitleColor() {
5052 return mTitleColor;
5053 }
5054
5055 protected void onTitleChanged(CharSequence title, int color) {
5056 if (mTitleReady) {
5057 final Window win = getWindow();
5058 if (win != null) {
5059 win.setTitle(title);
5060 if (color != 0) {
5061 win.setTitleColor(color);
5062 }
5063 }
Adam Powellaf2d8592014-08-26 18:06:40 -07005064 if (mActionBar != null) {
5065 mActionBar.setWindowTitle(title);
5066 }
Adam Powella557fdc2014-08-21 18:05:53 -07005067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 }
5069
5070 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
5071 }
5072
5073 /**
Winson Chunga449dc02014-05-16 11:15:04 -07005074 * Sets information describing the task with this activity for presentation inside the Recents
5075 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
5076 * are traversed in order from the topmost activity to the bottommost. The traversal continues
5077 * for each property until a suitable value is found. For each task the taskDescription will be
5078 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07005079 *
5080 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07005081 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07005082 *
Winson Chunga449dc02014-05-16 11:15:04 -07005083 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07005084 */
Winson Chunga449dc02014-05-16 11:15:04 -07005085 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
5086 ActivityManager.TaskDescription td;
5087 // Scale the icon down to something reasonable if it is provided
Craig Mautner648f69b2014-09-18 14:16:26 -07005088 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
Craig Mautner2fbd7542014-03-21 09:34:07 -07005089 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
Winson Chunga449dc02014-05-16 11:15:04 -07005090 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size, true);
5091 td = new ActivityManager.TaskDescription(taskDescription.getLabel(), icon,
5092 taskDescription.getPrimaryColor());
5093 } else {
5094 td = taskDescription;
Craig Mautner2fbd7542014-03-21 09:34:07 -07005095 }
5096 try {
Winson Chunga449dc02014-05-16 11:15:04 -07005097 ActivityManagerNative.getDefault().setTaskDescription(mToken, td);
Craig Mautner2fbd7542014-03-21 09:34:07 -07005098 } catch (RemoteException e) {
5099 }
5100 }
5101
5102 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 * Sets the visibility of the progress bar in the title.
5104 * <p>
5105 * In order for the progress bar to be shown, the feature must be requested
5106 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 * @param visible Whether to show the progress bars in the title.
5109 */
5110 public final void setProgressBarVisibility(boolean visible) {
5111 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
5112 Window.PROGRESS_VISIBILITY_OFF);
5113 }
5114
5115 /**
5116 * Sets the visibility of the indeterminate progress bar in the title.
5117 * <p>
5118 * In order for the progress bar to be shown, the feature must be requested
5119 * via {@link #requestWindowFeature(int)}.
5120 *
5121 * @param visible Whether to show the progress bars in the title.
5122 */
5123 public final void setProgressBarIndeterminateVisibility(boolean visible) {
5124 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
5125 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
5126 }
RoboErik55011652014-07-09 15:05:53 -07005127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 /**
5129 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
5130 * is always indeterminate).
5131 * <p>
5132 * In order for the progress bar to be shown, the feature must be requested
5133 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
5136 */
5137 public final void setProgressBarIndeterminate(boolean indeterminate) {
5138 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00005139 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
5140 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141 }
RoboErik55011652014-07-09 15:05:53 -07005142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 /**
5144 * Sets the progress for the progress bars in the title.
5145 * <p>
5146 * In order for the progress bar to be shown, the feature must be requested
5147 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005148 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 * @param progress The progress for the progress bar. Valid ranges are from
5150 * 0 to 10000 (both inclusive). If 10000 is given, the progress
5151 * bar will be completely filled and will fade out.
5152 */
5153 public final void setProgress(int progress) {
5154 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
5155 }
RoboErik55011652014-07-09 15:05:53 -07005156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 /**
5158 * Sets the secondary progress for the progress bar in the title. This
5159 * progress is drawn between the primary progress (set via
5160 * {@link #setProgress(int)} and the background. It can be ideal for media
5161 * scenarios such as showing the buffering progress while the default
5162 * progress shows the play progress.
5163 * <p>
5164 * In order for the progress bar to be shown, the feature must be requested
5165 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
5168 * 0 to 10000 (both inclusive).
5169 */
5170 public final void setSecondaryProgress(int secondaryProgress) {
5171 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
5172 secondaryProgress + Window.PROGRESS_SECONDARY_START);
5173 }
5174
5175 /**
5176 * Suggests an audio stream whose volume should be changed by the hardware
5177 * volume controls.
5178 * <p>
5179 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07005180 * Volume requests which are received while the Activity is in the
5181 * foreground will affect this stream.
5182 * <p>
5183 * It is not guaranteed that the hardware volume controls will always change
5184 * this stream's volume (for example, if a call is in progress, its stream's
5185 * volume may be changed instead). To reset back to the default, use
5186 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
5187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07005189 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 */
5191 public final void setVolumeControlStream(int streamType) {
5192 getWindow().setVolumeControlStream(streamType);
5193 }
5194
5195 /**
5196 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07005197 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07005198 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 * @return The suggested audio stream type whose volume should be changed by
5200 * the hardware volume controls.
5201 * @see #setVolumeControlStream(int)
5202 */
5203 public final int getVolumeControlStream() {
5204 return getWindow().getVolumeControlStream();
5205 }
RoboErik55011652014-07-09 15:05:53 -07005206
5207 /**
5208 * Sets a {@link MediaController} to send media keys and volume changes to.
5209 * <p>
5210 * The controller will be tied to the window of this Activity. Media key and
5211 * volume events which are received while the Activity is in the foreground
5212 * will be forwarded to the controller and used to invoke transport controls
5213 * or adjust the volume. This may be used instead of or in addition to
5214 * {@link #setVolumeControlStream} to affect a specific session instead of a
5215 * specific stream.
5216 * <p>
5217 * It is not guaranteed that the hardware volume controls will always change
5218 * this session's volume (for example, if a call is in progress, its
5219 * stream's volume may be changed instead). To reset back to the default use
5220 * null as the controller.
5221 *
5222 * @param controller The controller for the session which should receive
5223 * media keys and volume changes.
5224 */
5225 public final void setMediaController(MediaController controller) {
5226 getWindow().setMediaController(controller);
5227 }
5228
5229 /**
5230 * Gets the controller which should be receiving media key and volume events
5231 * while this activity is in the foreground.
5232 *
5233 * @return The controller which should receive events.
5234 * @see #setMediaController(android.media.session.MediaController)
5235 */
5236 public final MediaController getMediaController() {
5237 return getWindow().getMediaController();
5238 }
5239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 /**
5241 * Runs the specified action on the UI thread. If the current thread is the UI
5242 * thread, then the action is executed immediately. If the current thread is
5243 * not the UI thread, the action is posted to the event queue of the UI thread.
5244 *
5245 * @param action the action to run on the UI thread
5246 */
5247 public final void runOnUiThread(Runnable action) {
5248 if (Thread.currentThread() != mUiThread) {
5249 mHandler.post(action);
5250 } else {
5251 action.run();
5252 }
5253 }
5254
5255 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005256 * Standard implementation of
5257 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
5258 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005259 * This implementation does nothing and is for
5260 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
5261 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
5262 *
5263 * @see android.view.LayoutInflater#createView
5264 * @see android.view.Window#getLayoutInflater
5265 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005266 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07005267 public View onCreateView(String name, Context context, AttributeSet attrs) {
5268 return null;
5269 }
5270
5271 /**
5272 * Standard implementation of
5273 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
5274 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005275 * This implementation handles <fragment> tags to embed fragments inside
5276 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 *
5278 * @see android.view.LayoutInflater#createView
5279 * @see android.view.Window#getLayoutInflater
5280 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07005281 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005282 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07005283 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005284 }
RoboErik55011652014-07-09 15:05:53 -07005285
Adam Powell371a8092014-06-20 12:51:12 -07005286 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005287 }
5288
Daniel Sandler69a48172010-06-23 16:29:36 -04005289 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07005290 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07005291 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07005292 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08005293 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005294 * @param fd The raw file descriptor that the dump is being sent to.
5295 * @param writer The PrintWriter to which you should dump your state. This will be
5296 * closed for you after you return.
5297 * @param args additional arguments to the dump request.
5298 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08005299 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07005300 dumpInner(prefix, fd, writer, args);
5301 }
5302
5303 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08005304 writer.print(prefix); writer.print("Local Activity ");
5305 writer.print(Integer.toHexString(System.identityHashCode(this)));
5306 writer.println(" State:");
5307 String innerPrefix = prefix + " ";
5308 writer.print(innerPrefix); writer.print("mResumed=");
5309 writer.print(mResumed); writer.print(" mStopped=");
5310 writer.print(mStopped); writer.print(" mFinished=");
5311 writer.println(mFinished);
5312 writer.print(innerPrefix); writer.print("mLoadersStarted=");
5313 writer.println(mLoadersStarted);
5314 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
5315 writer.println(mChangingConfigurations);
5316 writer.print(innerPrefix); writer.print("mCurrentConfig=");
5317 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07005318
Dianne Hackborn30d71892010-12-11 10:37:55 -08005319 if (mLoaderManager != null) {
5320 writer.print(prefix); writer.print("Loader Manager ");
5321 writer.print(Integer.toHexString(System.identityHashCode(mLoaderManager)));
5322 writer.println(":");
5323 mLoaderManager.dump(prefix + " ", fd, writer, args);
5324 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005325
Jeff Brown5182c782013-10-15 20:31:52 -07005326 mFragments.dump(prefix, fd, writer, args);
5327
Michael Wright5f48dc72013-11-01 12:42:49 -07005328 if (getWindow() != null &&
5329 getWindow().peekDecorView() != null &&
5330 getWindow().peekDecorView().getViewRootImpl() != null) {
5331 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
5332 }
Jeff Brown5182c782013-10-15 20:31:52 -07005333
5334 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07005335 }
5336
5337 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04005338 * Bit indicating that this activity is "immersive" and should not be
5339 * interrupted by notifications if possible.
5340 *
5341 * This value is initially set by the manifest property
5342 * <code>android:immersive</code> but may be changed at runtime by
5343 * {@link #setImmersive}.
5344 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07005345 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04005346 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5347 */
5348 public boolean isImmersive() {
5349 try {
5350 return ActivityManagerNative.getDefault().isImmersive(mToken);
5351 } catch (RemoteException e) {
5352 return false;
5353 }
5354 }
5355
5356 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07005357 * Indication of whether this is the highest level activity in this task. Can be used to
5358 * determine whether an activity launched by this activity was placed in the same task or
5359 * another task.
5360 *
5361 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07005362 */
George Mountff243282014-07-07 16:12:07 -07005363 private boolean isTopOfTask() {
Craig Mautnerd61dc202014-07-07 11:09:11 -07005364 try {
5365 return ActivityManagerNative.getDefault().isTopOfTask(mToken);
5366 } catch (RemoteException e) {
5367 return false;
5368 }
5369 }
5370
5371 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07005372 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
5373 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005374 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005375 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005376 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
5377 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005378 * This call has no effect on non-translucent activities or on activities with the
5379 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005380 *
George Mount62ab9b72014-05-02 13:51:17 -07005381 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
5382 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07005383 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005384 *
5385 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07005386 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005387 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07005388 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07005389 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07005390 mTranslucentCallback = null;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005391 if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) {
5392 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
5393 }
Craig Mautner4addfc52013-06-25 08:05:45 -07005394 } catch (RemoteException e) {
5395 // pass
5396 }
5397 }
5398
5399 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07005400 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
5401 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
5402 * <p>
5403 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
5404 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
5405 * be called indicating that it is safe to make this activity translucent again. Until
5406 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
5407 * behind the frontmost Activity will be indeterminate.
5408 * <p>
5409 * This call has no effect on non-translucent activities or on activities with the
5410 * {@link android.R.attr#windowIsFloating} attribute.
5411 *
5412 * @param callback the method to call when all visible Activities behind this one have been
5413 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07005414 * @param options activity options delivered to the activity below this one. The options
5415 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07005416 * @return <code>true</code> if Window was opaque and will become translucent or
5417 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005418 *
5419 * @see #convertFromTranslucent()
5420 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005421 *
5422 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07005423 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005424 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07005425 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07005426 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07005427 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005428 try {
5429 mTranslucentCallback = callback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005430 mChangeCanvasToTranslucent =
Craig Mautner233ceee2014-05-09 17:05:11 -07005431 ActivityManagerNative.getDefault().convertToTranslucent(mToken, options);
George Mount9e183972014-09-03 12:35:09 -07005432 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07005433 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005434 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07005435 // Make callback return as though it timed out.
5436 mChangeCanvasToTranslucent = false;
5437 drawComplete = false;
5438 }
5439 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
5440 // Window is already translucent.
5441 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07005442 }
George Mount3cc716c2014-06-12 16:35:35 -07005443 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005444 }
5445
5446 /** @hide */
5447 void onTranslucentConversionComplete(boolean drawComplete) {
5448 if (mTranslucentCallback != null) {
5449 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
5450 mTranslucentCallback = null;
5451 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005452 if (mChangeCanvasToTranslucent) {
5453 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
5454 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07005455 }
5456
Craig Mautnereb8abf72014-07-02 15:04:09 -07005457 /** @hide */
5458 public void onNewActivityOptions(ActivityOptions options) {
5459 mActivityTransitionState.setEnterActivityOptions(this, options);
5460 if (!mStopped) {
5461 mActivityTransitionState.enterReady(this);
5462 }
5463 }
5464
Craig Mautner5eda9b32013-07-02 11:58:16 -07005465 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07005466 * Retrieve the ActivityOptions passed in from the launching activity or passed back
5467 * from an activity launched by this activity in its call to {@link
5468 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
5469 *
5470 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
5471 * @hide
5472 */
5473 ActivityOptions getActivityOptions() {
5474 try {
5475 return ActivityManagerNative.getDefault().getActivityOptions(mToken);
5476 } catch (RemoteException e) {
5477 }
5478 return null;
5479 }
5480
5481 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07005482 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07005483 * this method anytime between the start of {@link #onResume()} and the return from
5484 * {@link #onPause()}. If this call is successful then the activity will remain visible after
5485 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
5486 *
5487 * <p>The actions of this call are reset each time that this activity is brought to the
5488 * front. That is, every time {@link #onResume()} is called the activity will be assumed
5489 * to not have requested visible behind. Therefore, if you want this activity to continue to
5490 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005491 *
5492 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
5493 * for dialog and translucent activities.
5494 *
Craig Mautner64ccb702014-10-01 09:38:40 -07005495 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
5496 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
5497 *
5498 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07005499 * the next call to onResume.
5500 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005501 * @param visible true to notify the system that the activity wishes to be visible behind other
5502 * translucent activities, false to indicate otherwise. Resources must be
5503 * released when passing false to this method.
Craig Mautner64ccb702014-10-01 09:38:40 -07005504 * @return the resulting visibiity state. If true the activity will remain visible beyond
5505 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
5506 * then the activity may not count on being visible behind other translucent activities,
5507 * and must stop any media playback and release resources.
5508 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
5509 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005510 *
Jose Limafcf70832014-08-27 23:09:05 -07005511 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07005512 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07005513 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005514 public boolean requestVisibleBehind(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07005515 if (!mResumed) {
Jose Lima4b6c6692014-08-12 17:41:12 -07005516 // Do not permit paused or stopped activities to do this.
5517 visible = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005518 }
5519 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07005520 mVisibleBehind = ActivityManagerNative.getDefault()
5521 .requestVisibleBehind(mToken, visible) && visible;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005522 } catch (RemoteException e) {
Jose Lima4b6c6692014-08-12 17:41:12 -07005523 mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005524 }
Jose Lima4b6c6692014-08-12 17:41:12 -07005525 return mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005526 }
5527
5528 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07005529 * Called when a translucent activity over this activity is becoming opaque or another
5530 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07005531 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005532 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005533 * <p>When this method is called the activity has 500 msec to release any resources it may be
5534 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005535 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07005536 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07005537 * process. Otherwise {@link #onStop()} will be called following return.
5538 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005539 * @see #requestVisibleBehind(boolean)
5540 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07005541 */
Jose Limafcf70832014-08-27 23:09:05 -07005542 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07005543 mCalled = true;
5544 }
5545
5546 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07005547 * Translucent activities may call this to determine if there is an activity below them that
5548 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005549 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005550 * @return true if an activity below is set to visible according to the most recent call to
5551 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005552 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005553 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07005554 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07005555 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07005556 * @hide
5557 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005558 @SystemApi
5559 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07005560 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07005561 return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07005562 } catch (RemoteException e) {
5563 }
5564 return false;
5565 }
5566
5567 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07005568 * The topmost foreground activity will receive this call when the background visibility state
5569 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005570 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005571 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07005572 * due to a background activity finishing itself.
5573 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005574 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005575 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005576 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07005577 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07005578 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07005579 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005580 @SystemApi
5581 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07005582 }
5583
5584 /**
Craig Mautner8746a472014-07-24 15:12:54 -07005585 * Activities cannot draw during the period that their windows are animating in. In order
5586 * to know when it is safe to begin drawing they can override this method which will be
5587 * called when the entering animation has completed.
5588 */
5589 public void onEnterAnimationComplete() {
5590 }
5591
5592 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08005593 * @hide
5594 */
5595 public void dispatchEnterAnimationComplete() {
5596 onEnterAnimationComplete();
5597 if (getWindow() != null && getWindow().getDecorView() != null) {
5598 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
5599 }
5600 }
5601
5602 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04005603 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07005604 *
Daniel Sandler69a48172010-06-23 16:29:36 -04005605 * Note that changing this value will have no effect on the activity's
5606 * {@link android.content.pm.ActivityInfo} structure; that is, if
5607 * <code>android:immersive</code> is set to <code>true</code>
5608 * in the application's manifest entry for this activity, the {@link
5609 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
5610 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5611 * FLAG_IMMERSIVE} bit set.
5612 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07005613 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04005614 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5615 */
5616 public void setImmersive(boolean i) {
5617 try {
5618 ActivityManagerNative.getDefault().setImmersive(mToken, i);
5619 } catch (RemoteException e) {
5620 // pass
5621 }
5622 }
5623
Adam Powell6e346362010-07-23 10:18:23 -07005624 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08005625 * Start an action mode.
Adam Powell6e346362010-07-23 10:18:23 -07005626 *
5627 * @param callback Callback that will manage lifecycle events for this context mode
5628 * @return The ContextMode that was started, or null if it was canceled
5629 *
5630 * @see ActionMode
5631 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005632 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07005633 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07005634 return mWindow.getDecorView().startActionMode(callback);
5635 }
5636
Adam Powelldebf3be2010-11-15 18:58:48 -08005637 /**
5638 * Give the Activity a chance to control the UI for an action mode requested
5639 * by the system.
5640 *
5641 * <p>Note: If you are looking for a notification callback that an action mode
5642 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
5643 *
5644 * @param callback The callback that should control the new action mode
5645 * @return The new action mode, or <code>null</code> if the activity does not want to
5646 * provide special handling for this action mode. (It will be handled by the system.)
5647 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005648 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07005649 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08005650 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Adam Powelle43340c2014-03-17 19:10:43 -07005651 initWindowDecorActionBar();
Adam Powell6e346362010-07-23 10:18:23 -07005652 if (mActionBar != null) {
Adam Powell5d279772010-07-27 16:34:07 -07005653 return mActionBar.startActionMode(callback);
Adam Powell6e346362010-07-23 10:18:23 -07005654 }
5655 return null;
5656 }
5657
Adam Powelldebf3be2010-11-15 18:58:48 -08005658 /**
5659 * Notifies the Activity that an action mode has been started.
5660 * Activity subclasses overriding this method should call the superclass implementation.
5661 *
5662 * @param mode The new action mode.
5663 */
Craig Mautner5eda9b32013-07-02 11:58:16 -07005664 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08005665 public void onActionModeStarted(ActionMode mode) {
5666 }
5667
5668 /**
5669 * Notifies the activity that an action mode has finished.
5670 * Activity subclasses overriding this method should call the superclass implementation.
5671 *
5672 * @param mode The action mode that just finished.
5673 */
Craig Mautner5eda9b32013-07-02 11:58:16 -07005674 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08005675 public void onActionModeFinished(ActionMode mode) {
5676 }
5677
Adam Powelldd8fab22012-03-22 17:47:27 -07005678 /**
5679 * Returns true if the app should recreate the task when navigating 'up' from this activity
5680 * by using targetIntent.
5681 *
5682 * <p>If this method returns false the app can trivially call
5683 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
5684 * up navigation. If this method returns false, the app should synthesize a new task stack
5685 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
5686 *
5687 * @param targetIntent An intent representing the target destination for up navigation
5688 * @return true if navigating up should recreate a new task stack, false if the same task
5689 * should be used for the destination
5690 */
5691 public boolean shouldUpRecreateTask(Intent targetIntent) {
5692 try {
5693 PackageManager pm = getPackageManager();
5694 ComponentName cn = targetIntent.getComponent();
5695 if (cn == null) {
5696 cn = targetIntent.resolveActivity(pm);
5697 }
5698 ActivityInfo info = pm.getActivityInfo(cn, 0);
5699 if (info.taskAffinity == null) {
5700 return false;
5701 }
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07005702 return ActivityManagerNative.getDefault()
5703 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07005704 } catch (RemoteException e) {
5705 return false;
5706 } catch (NameNotFoundException e) {
5707 return false;
5708 }
5709 }
5710
5711 /**
5712 * Navigate from this activity to the activity specified by upIntent, finishing this activity
5713 * in the process. If the activity indicated by upIntent already exists in the task's history,
5714 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07005715 * finished.
5716 *
5717 * <p>If the indicated activity does not appear in the history stack, this will finish
5718 * each activity in this task until the root activity of the task is reached, resulting in
5719 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
5720 * when an activity may be reached by a path not passing through a canonical parent
5721 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07005722 *
5723 * <p>This method should be used when performing up navigation from within the same task
5724 * as the destination. If up navigation should cross tasks in some cases, see
5725 * {@link #shouldUpRecreateTask(Intent)}.</p>
5726 *
5727 * @param upIntent An intent representing the target destination for up navigation
5728 *
5729 * @return true if up navigation successfully reached the activity indicated by upIntent and
5730 * upIntent was delivered to it. false if an instance of the indicated activity could
5731 * not be found and this activity was simply finished normally.
5732 */
5733 public boolean navigateUpTo(Intent upIntent) {
5734 if (mParent == null) {
5735 ComponentName destInfo = upIntent.getComponent();
5736 if (destInfo == null) {
5737 destInfo = upIntent.resolveActivity(getPackageManager());
5738 if (destInfo == null) {
5739 return false;
5740 }
5741 upIntent = new Intent(upIntent);
5742 upIntent.setComponent(destInfo);
5743 }
5744 int resultCode;
5745 Intent resultData;
5746 synchronized (this) {
5747 resultCode = mResultCode;
5748 resultData = mResultData;
5749 }
5750 if (resultData != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005751 resultData.prepareToLeaveProcess();
Adam Powelldd8fab22012-03-22 17:47:27 -07005752 }
5753 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005754 upIntent.prepareToLeaveProcess();
Adam Powelldd8fab22012-03-22 17:47:27 -07005755 return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent,
5756 resultCode, resultData);
5757 } catch (RemoteException e) {
5758 return false;
5759 }
5760 } else {
5761 return mParent.navigateUpToFromChild(this, upIntent);
5762 }
5763 }
5764
5765 /**
5766 * This is called when a child activity of this one calls its
5767 * {@link #navigateUpTo} method. The default implementation simply calls
5768 * navigateUpTo(upIntent) on this activity (the parent).
5769 *
5770 * @param child The activity making the call.
5771 * @param upIntent An intent representing the target destination for up navigation
5772 *
5773 * @return true if up navigation successfully reached the activity indicated by upIntent and
5774 * upIntent was delivered to it. false if an instance of the indicated activity could
5775 * not be found and this activity was simply finished normally.
5776 */
5777 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
5778 return navigateUpTo(upIntent);
5779 }
5780
5781 /**
5782 * Obtain an {@link Intent} that will launch an explicit target activity specified by
5783 * this activity's logical parent. The logical parent is named in the application's manifest
5784 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07005785 * Activity subclasses may override this method to modify the Intent returned by
5786 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
5787 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07005788 *
Adam Powell04d58112012-04-09 10:22:12 -07005789 * @return a new Intent targeting the defined parent of this activity or null if
5790 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07005791 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005792 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07005793 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07005794 final String parentName = mActivityInfo.parentActivityName;
5795 if (TextUtils.isEmpty(parentName)) {
5796 return null;
5797 }
Adam Powell5a4010c2012-09-16 15:14:05 -07005798
5799 // If the parent itself has no parent, generate a main activity intent.
5800 final ComponentName target = new ComponentName(this, parentName);
5801 try {
5802 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
5803 final String parentActivity = parentInfo.parentActivityName;
5804 final Intent parentIntent = parentActivity == null
5805 ? Intent.makeMainActivity(target)
5806 : new Intent().setComponent(target);
5807 return parentIntent;
5808 } catch (NameNotFoundException e) {
5809 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
5810 "' in manifest");
5811 return null;
5812 }
Adam Powelldd8fab22012-03-22 17:47:27 -07005813 }
5814
George Mount31a21722014-03-24 17:44:36 -07005815 /**
George Mount62ab9b72014-05-02 13:51:17 -07005816 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07005817 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07005818 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07005819 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07005820 *
George Mount65580562014-08-29 08:15:48 -07005821 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07005822 */
George Mount65580562014-08-29 08:15:48 -07005823 public void setEnterSharedElementCallback(SharedElementCallback callback) {
5824 if (callback == null) {
5825 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07005826 }
George Mount65580562014-08-29 08:15:48 -07005827 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07005828 }
5829
5830 /**
5831 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07005832 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07005833 * will be called to handle shared elements on the <i>launching</i> Activity. Most
5834 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07005835 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07005836 *
George Mount65580562014-08-29 08:15:48 -07005837 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07005838 */
George Mount65580562014-08-29 08:15:48 -07005839 public void setExitSharedElementCallback(SharedElementCallback callback) {
5840 if (callback == null) {
5841 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07005842 }
George Mount65580562014-08-29 08:15:48 -07005843 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07005844 }
5845
George Mount8c2614c2014-06-10 11:12:01 -07005846 /**
5847 * Postpone the entering activity transition when Activity was started with
5848 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
5849 * android.util.Pair[])}.
5850 * <p>This method gives the Activity the ability to delay starting the entering and
5851 * shared element transitions until all data is loaded. Until then, the Activity won't
5852 * draw into its window, leaving the window transparent. This may also cause the
5853 * returning animation to be delayed until data is ready. This method should be
5854 * called in {@link #onCreate(android.os.Bundle)} or in
5855 * {@link #onActivityReenter(int, android.content.Intent)}.
5856 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
5857 * start the transitions. If the Activity did not use
5858 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
5859 * android.util.Pair[])}, then this method does nothing.</p>
5860 */
5861 public void postponeEnterTransition() {
5862 mActivityTransitionState.postponeEnterTransition();
5863 }
5864
5865 /**
5866 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
5867 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
5868 * to have your Activity start drawing.
5869 */
5870 public void startPostponedEnterTransition() {
5871 mActivityTransitionState.startPostponedEnterTransition();
5872 }
5873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07005875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 final void setParent(Activity parent) {
5877 mParent = parent;
5878 }
5879
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005880 final void attach(Context context, ActivityThread aThread,
5881 Instrumentation instr, IBinder token, int ident,
5882 Application application, Intent intent, ActivityInfo info,
5883 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005884 NonConfigurationInstances lastNonConfigurationInstances,
Craig Mautner233ceee2014-05-09 17:05:11 -07005885 Configuration config, IVoiceInteractor voiceInteractor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 attachBaseContext(context);
5887
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07005888 mFragments.attachActivity(this, mContainer, null);
RoboErik55011652014-07-09 15:05:53 -07005889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 mWindow = PolicyManager.makeNewWindow(this);
5891 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07005892 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08005893 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
5895 mWindow.setSoftInputMode(info.softInputMode);
5896 }
Adam Powell269248d2011-08-02 10:26:54 -07005897 if (info.uiOptions != 0) {
5898 mWindow.setUiOptions(info.uiOptions);
5899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08005901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 mMainThread = aThread;
5903 mInstrumentation = instr;
5904 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005905 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906 mApplication = application;
5907 mIntent = intent;
5908 mComponent = intent.getComponent();
5909 mActivityInfo = info;
5910 mTitle = title;
5911 mParent = parent;
5912 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005913 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07005914 if (voiceInteractor != null) {
5915 if (lastNonConfigurationInstances != null) {
5916 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
5917 } else {
5918 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
5919 Looper.myLooper());
5920 }
5921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922
Jeff Brown98365d72012-08-19 20:30:52 -07005923 mWindow.setWindowManager(
5924 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
5925 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07005926 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005927 if (mParent != null) {
5928 mWindow.setContainer(mParent.getWindow());
5929 }
5930 mWindowManager = mWindow.getWindowManager();
5931 mCurrentConfig = config;
5932 }
5933
Dianne Hackborn5320eb82012-05-18 12:05:04 -07005934 /** @hide */
5935 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 return mParent != null ? mParent.getActivityToken() : mToken;
5937 }
5938
Craig Mautnera0026042014-04-23 11:45:37 -07005939 final void performCreateCommon() {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08005940 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
5941 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07005942 mFragments.dispatchActivityCreated();
George Mount62ab9b72014-05-02 13:51:17 -07005943 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackborn2dedce62010-04-15 14:45:25 -07005944 }
George Mount0a778ed2013-12-13 13:35:36 -08005945
Craig Mautnera0026042014-04-23 11:45:37 -07005946 final void performCreate(Bundle icicle) {
5947 onCreate(icicle);
George Mount62ab9b72014-05-02 13:51:17 -07005948 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07005949 performCreateCommon();
5950 }
5951
5952 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
5953 onCreate(icicle, persistentState);
George Mount62ab9b72014-05-02 13:51:17 -07005954 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07005955 performCreateCommon();
5956 }
5957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 final void performStart() {
George Mount62ab9b72014-05-02 13:51:17 -07005959 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07005960 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07005962 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005963 mInstrumentation.callActivityOnStart(this);
5964 if (!mCalled) {
5965 throw new SuperNotCalledException(
5966 "Activity " + mComponent.toShortString() +
5967 " did not call through to super.onStart()");
5968 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07005969 mFragments.dispatchStart();
Dianne Hackborn2707d602010-07-09 18:01:20 -07005970 if (mAllLoaderManagers != null) {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -07005971 final int N = mAllLoaderManagers.size();
5972 LoaderManagerImpl loaders[] = new LoaderManagerImpl[N];
5973 for (int i=N-1; i>=0; i--) {
5974 loaders[i] = mAllLoaderManagers.valueAt(i);
5975 }
5976 for (int i=0; i<N; i++) {
5977 LoaderManagerImpl lm = loaders[i];
5978 lm.finishRetain();
5979 lm.doReportStart();
Dianne Hackborn2707d602010-07-09 18:01:20 -07005980 }
5981 }
George Mount62ab9b72014-05-02 13:51:17 -07005982 mActivityTransitionState.enterReady(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 }
RoboErik55011652014-07-09 15:05:53 -07005984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 final void performRestart() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07005986 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07005987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 if (mStopped) {
5989 mStopped = false;
Dianne Hackbornce418e62011-03-01 14:31:38 -08005990 if (mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07005991 WindowManagerGlobal.getInstance().setStoppedState(mToken, false);
Dianne Hackbornce418e62011-03-01 14:31:38 -08005992 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07005993
5994 synchronized (mManagedCursors) {
5995 final int N = mManagedCursors.size();
5996 for (int i=0; i<N; i++) {
5997 ManagedCursor mc = mManagedCursors.get(i);
5998 if (mc.mReleased || mc.mUpdated) {
5999 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07006000 if (getApplicationInfo().targetSdkVersion
6001 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
6002 throw new IllegalStateException(
6003 "trying to requery an already closed cursor "
6004 + mc.mCursor);
6005 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006006 }
6007 mc.mReleased = false;
6008 mc.mUpdated = false;
6009 }
6010 }
6011 }
6012
6013 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006014 mInstrumentation.callActivityOnRestart(this);
6015 if (!mCalled) {
6016 throw new SuperNotCalledException(
6017 "Activity " + mComponent.toShortString() +
6018 " did not call through to super.onRestart()");
6019 }
6020 performStart();
6021 }
6022 }
RoboErik55011652014-07-09 15:05:53 -07006023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 final void performResume() {
6025 performRestart();
RoboErik55011652014-07-09 15:05:53 -07006026
Dianne Hackborn445646c2010-06-25 15:52:59 -07006027 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006028
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006029 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07006030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06006032 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 mInstrumentation.callActivityOnResume(this);
6034 if (!mCalled) {
6035 throw new SuperNotCalledException(
6036 "Activity " + mComponent.toShortString() +
6037 " did not call through to super.onResume()");
6038 }
6039
6040 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07006042
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006043 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07006044 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046 onPostResume();
6047 if (!mCalled) {
6048 throw new SuperNotCalledException(
6049 "Activity " + mComponent.toShortString() +
6050 " did not call through to super.onPostResume()");
6051 }
6052 }
6053
6054 final void performPause() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006055 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006056 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006057 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006058 onPause();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006059 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006060 if (!mCalled && getApplicationInfo().targetSdkVersion
6061 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
6062 throw new SuperNotCalledException(
6063 "Activity " + mComponent.toShortString() +
6064 " did not call through to super.onPause()");
6065 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06006066 mResumed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 }
RoboErik55011652014-07-09 15:05:53 -07006068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069 final void performUserLeaving() {
6070 onUserInteraction();
6071 onUserLeaveHint();
6072 }
RoboErik55011652014-07-09 15:05:53 -07006073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 final void performStop() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006075 mDoReportFullyDrawn = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006076 if (mLoadersStarted) {
6077 mLoadersStarted = false;
Dianne Hackborn2707d602010-07-09 18:01:20 -07006078 if (mLoaderManager != null) {
6079 if (!mChangingConfigurations) {
6080 mLoaderManager.doStop();
6081 } else {
6082 mLoaderManager.doRetain();
6083 }
6084 }
6085 }
RoboErik55011652014-07-09 15:05:53 -07006086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006087 if (!mStopped) {
6088 if (mWindow != null) {
6089 mWindow.closeAllPanels();
6090 }
6091
Dianne Hackbornce418e62011-03-01 14:31:38 -08006092 if (mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07006093 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08006094 }
RoboErik55011652014-07-09 15:05:53 -07006095
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006096 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07006097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 mCalled = false;
6099 mInstrumentation.callActivityOnStop(this);
6100 if (!mCalled) {
6101 throw new SuperNotCalledException(
6102 "Activity " + mComponent.toShortString() +
6103 " did not call through to super.onStop()");
6104 }
RoboErik55011652014-07-09 15:05:53 -07006105
Makoto Onuki2f6a0182010-02-22 13:26:59 -08006106 synchronized (mManagedCursors) {
6107 final int N = mManagedCursors.size();
6108 for (int i=0; i<N; i++) {
6109 ManagedCursor mc = mManagedCursors.get(i);
6110 if (!mc.mReleased) {
6111 mc.mCursor.deactivate();
6112 mc.mReleased = true;
6113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 }
6115 }
George Mount0a778ed2013-12-13 13:35:36 -08006116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 mStopped = true;
6118 }
6119 mResumed = false;
6120 }
6121
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006122 final void performDestroy() {
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006123 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07006124 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006125 mFragments.dispatchDestroy();
6126 onDestroy();
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07006127 if (mLoaderManager != null) {
6128 mLoaderManager.doDestroy();
6129 }
Dianne Hackborn20d94742014-05-29 18:35:45 -07006130 if (mVoiceInteractor != null) {
6131 mVoiceInteractor.detachActivity();
6132 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006133 }
George Mount0a778ed2013-12-13 13:35:36 -08006134
6135 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06006136 * @hide
6137 */
6138 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 return mResumed;
6140 }
6141
George Mount0a778ed2013-12-13 13:35:36 -08006142 void dispatchActivityResult(String who, int requestCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006143 int resultCode, Intent data) {
Joe Onorato43a17652011-04-06 19:22:23 -07006144 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
6146 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006147 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 if (who == null) {
6149 onActivityResult(requestCode, resultCode, data);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006150 } else {
6151 Fragment frag = mFragments.findFragmentByWho(who);
6152 if (frag != null) {
6153 frag.onActivityResult(requestCode, resultCode, data);
6154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 }
6156 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006157
Jason Monka57e5e02014-05-07 10:06:48 -04006158 /**
Jason Monk62515be2014-05-21 16:06:19 -04006159 * Request to put this Activity in a mode where the user is locked to the
Jason Monka57e5e02014-05-07 10:06:48 -04006160 * current task.
6161 *
6162 * This will prevent the user from launching other apps, going to settings,
6163 * or reaching the home screen.
6164 *
Jason Monk62515be2014-05-21 16:06:19 -04006165 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true
6166 * for this component then the app will go directly into Lock Task mode. The user
6167 * will not be able to exit this mode until {@link Activity#stopLockTask()} is called.
6168 *
6169 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false
6170 * then the system will prompt the user with a dialog requesting permission to enter
Jason Monk8863b572014-07-29 13:49:32 -04006171 * this mode. When entered through this method the user can exit at any time through
6172 * an action described by the request dialog. Calling stopLockTask will also exit the
6173 * mode.
Jason Monka57e5e02014-05-07 10:06:48 -04006174 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006175 public void startLockTask() {
6176 try {
6177 ActivityManagerNative.getDefault().startLockTaskMode(mToken);
6178 } catch (RemoteException e) {
6179 }
6180 }
6181
Jason Monka57e5e02014-05-07 10:06:48 -04006182 /**
6183 * Allow the user to switch away from the current task.
6184 *
6185 * Called to end the mode started by {@link Activity#startLockTask}. This
6186 * can only be called by activities that have successfully called
6187 * startLockTask previously.
6188 *
6189 * This will allow the user to exit this app and move onto other activities.
6190 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006191 public void stopLockTask() {
6192 try {
6193 ActivityManagerNative.getDefault().stopLockTaskMode();
6194 } catch (RemoteException e) {
6195 }
6196 }
6197
Craig Mautner5eda9b32013-07-02 11:58:16 -07006198 /**
6199 * Interface for informing a translucent {@link Activity} once all visible activities below it
6200 * have completed drawing. This is necessary only after an {@link Activity} has been made
6201 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
6202 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07006203 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6204 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07006205 *
6206 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006207 */
Jose Lima14914852014-08-14 09:14:12 -07006208 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006209 public interface TranslucentConversionListener {
6210 /**
6211 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
6212 * below the top one have been redrawn. Following this callback it is safe to make the top
6213 * Activity translucent because the underlying Activity has been drawn.
6214 *
6215 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
6216 * occurred waiting for the Activity to complete drawing.
6217 *
6218 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07006219 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006220 */
6221 public void onTranslucentConversionComplete(boolean drawComplete);
6222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223}