blob: 7fcbe35839ad4d3dcf6f0ad333d156059d79966b [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 Norbyec615c6f2015-03-02 10:11:44 -080019import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
21import android.annotation.IdRes;
22import android.annotation.IntDef;
23import android.annotation.LayoutRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070024import android.annotation.NonNull;
Tor Norbye7b9c9122013-05-30 16:48:33 -070025import android.annotation.Nullable;
26import android.annotation.StyleRes;
Craig Mautnera0026042014-04-23 11:45:37 -070027import android.os.PersistableBundle;
Adam Powellcfbe9be2013-11-06 14:58:58 -080028import android.transition.Scene;
Adam Powellcfbe9be2013-11-06 14:58:58 -080029import android.transition.TransitionManager;
Dianne Hackborn3e82ba12013-07-16 13:23:55 -070030import android.util.ArrayMap;
Adam Powell14874662013-07-18 19:42:41 -070031import android.util.SuperNotCalledException;
Adam Powelle43340c2014-03-17 19:10:43 -070032import android.widget.Toolbar;
RoboErik55011652014-07-09 15:05:53 -070033
Dianne Hackborn91097de2014-04-04 18:02:06 -070034import com.android.internal.app.IVoiceInteractor;
Adam Powelle43340c2014-03-17 19:10:43 -070035import com.android.internal.app.WindowDecorActionBar;
36import com.android.internal.app.ToolbarActionBar;
svetoslavganov75986cf2009-05-14 22:28:01 -070037
Jose Lima4b6c6692014-08-12 17:41:12 -070038import android.annotation.SystemApi;
Jason Monk62515be2014-05-21 16:06:19 -040039import android.app.admin.DevicePolicyManager;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070040import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.ComponentName;
42import android.content.ContentResolver;
43import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050044import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070045import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070046import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070047import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.SharedPreferences;
49import android.content.pm.ActivityInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070050import android.content.pm.PackageManager;
51import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.res.Configuration;
53import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070054import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.database.Cursor;
56import android.graphics.Bitmap;
57import android.graphics.Canvas;
58import android.graphics.drawable.Drawable;
59import android.media.AudioManager;
RoboErik55011652014-07-09 15:05:53 -070060import android.media.session.MediaController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.net.Uri;
Dianne Hackborn8d374262009-09-14 21:21:52 -070062import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Handler;
65import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080066import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070067import android.os.Parcelable;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.os.RemoteException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080069import android.os.StrictMode;
Dianne Hackbornf1c26e22012-08-23 13:54:58 -070070import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.text.Selection;
72import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.text.method.TextKeyListener;
75import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.util.EventLog;
77import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070078import android.util.PrintWriterPrinter;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070079import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.util.SparseArray;
Adam Powell6e346362010-07-23 10:18:23 -070081import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070083import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.view.ContextThemeWrapper;
85import android.view.KeyEvent;
86import android.view.LayoutInflater;
87import android.view.Menu;
88import android.view.MenuInflater;
89import android.view.MenuItem;
90import android.view.MotionEvent;
Jorim Jaggib10e33f2015-02-04 21:57:40 +010091import android.view.PhoneWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -070093import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -070095import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.ViewManager;
97import android.view.Window;
98import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070099import android.view.WindowManagerGlobal;
svetoslavganov75986cf2009-05-14 22:28:01 -0700100import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.widget.AdapterView;
102
Dianne Hackborn625ac272010-09-17 18:29:22 -0700103import java.io.FileDescriptor;
104import java.io.PrintWriter;
Tor Norbyed9273d62013-05-30 15:59:53 -0700105import java.lang.annotation.Retention;
106import java.lang.annotation.RetentionPolicy;
Adam Powell6e346362010-07-23 10:18:23 -0700107import java.util.ArrayList;
108import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110/**
111 * An activity is a single, focused thing that the user can do. Almost all
112 * activities interact with the user, so the Activity class takes care of
113 * creating a window for you in which you can place your UI with
114 * {@link #setContentView}. While activities are often presented to the user
115 * as full-screen windows, they can also be used in other ways: as floating
116 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
117 * or embedded inside of another activity (using {@link ActivityGroup}).
118 *
119 * There are two methods almost all subclasses of Activity will implement:
RoboErik55011652014-07-09 15:05:53 -0700120 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 * <ul>
122 * <li> {@link #onCreate} is where you initialize your activity. Most
123 * importantly, here you will usually call {@link #setContentView(int)}
124 * with a layout resource defining your UI, and using {@link #findViewById}
125 * to retrieve the widgets in that UI that you need to interact with
126 * programmatically.
RoboErik55011652014-07-09 15:05:53 -0700127 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 * <li> {@link #onPause} is where you deal with the user leaving your
129 * activity. Most importantly, any changes made by the user should at this
130 * point be committed (usually to the
131 * {@link android.content.ContentProvider} holding the data).
132 * </ul>
133 *
134 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
135 * activity classes must have a corresponding
136 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
137 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
RoboErik55011652014-07-09 15:05:53 -0700138 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * <p>Topics covered here:
140 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700141 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
143 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
144 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
145 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
146 * <li><a href="#Permissions">Permissions</a>
147 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
148 * </ol>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700149 *
150 * <div class="special reference">
151 * <h3>Developer Guides</h3>
152 * <p>The Activity class is an important part of an application's overall lifecycle,
153 * and the way activities are launched and put together is a fundamental
154 * part of the platform's application model. For a detailed perspective on the structure of an
155 * Android application and how activities behave, please read the
156 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
157 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
158 * developer guides.</p>
159 *
160 * <p>You can also find a detailed discussion about how to create activities in the
161 * <a href="{@docRoot}guide/topics/fundamentals/activities.html">Activities</a>
162 * developer guide.</p>
163 * </div>
164 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700165 * <a name="Fragments"></a>
166 * <h3>Fragments</h3>
167 *
168 * <p>Starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}, Activity
169 * implementations can make use of the {@link Fragment} class to better
170 * modularize their code, build more sophisticated user interfaces for larger
171 * screens, and help scale their application between small and large screens.
172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * <a name="ActivityLifecycle"></a>
174 * <h3>Activity Lifecycle</h3>
175 *
176 * <p>Activities in the system are managed as an <em>activity stack</em>.
177 * When a new activity is started, it is placed on the top of the stack
178 * and becomes the running activity -- the previous activity always remains
179 * below it in the stack, and will not come to the foreground again until
180 * the new activity exits.</p>
RoboErik55011652014-07-09 15:05:53 -0700181 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * <p>An activity has essentially four states:</p>
183 * <ul>
184 * <li> If an activity in the foreground of the screen (at the top of
185 * the stack),
186 * it is <em>active</em> or <em>running</em>. </li>
187 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
RoboErik55011652014-07-09 15:05:53 -0700188 * or transparent activity has focus on top of your activity), it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * is <em>paused</em>. A paused activity is completely alive (it
190 * maintains all state and member information and remains attached to
191 * the window manager), but can be killed by the system in extreme
192 * low memory situations.
193 * <li>If an activity is completely obscured by another activity,
194 * it is <em>stopped</em>. It still retains all state and member information,
195 * however, it is no longer visible to the user so its window is hidden
196 * and it will often be killed by the system when memory is needed
197 * elsewhere.</li>
198 * <li>If an activity is paused or stopped, the system can drop the activity
199 * from memory by either asking it to finish, or simply killing its
200 * process. When it is displayed again to the user, it must be
201 * completely restarted and restored to its previous state.</li>
202 * </ul>
203 *
204 * <p>The following diagram shows the important state paths of an Activity.
205 * The square rectangles represent callback methods you can implement to
206 * perform operations when the Activity moves between states. The colored
207 * ovals are major states the Activity can be in.</p>
RoboErik55011652014-07-09 15:05:53 -0700208 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * <p><img src="../../../images/activity_lifecycle.png"
210 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
RoboErik55011652014-07-09 15:05:53 -0700211 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <p>There are three key loops you may be interested in monitoring within your
213 * activity:
RoboErik55011652014-07-09 15:05:53 -0700214 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * <ul>
216 * <li>The <b>entire lifetime</b> of an activity happens between the first call
217 * to {@link android.app.Activity#onCreate} through to a single final call
218 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
219 * of "global" state in onCreate(), and release all remaining resources in
220 * onDestroy(). For example, if it has a thread running in the background
221 * to download data from the network, it may create that thread in onCreate()
222 * and then stop the thread in onDestroy().
RoboErik55011652014-07-09 15:05:53 -0700223 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <li>The <b>visible lifetime</b> of an activity happens between a call to
225 * {@link android.app.Activity#onStart} until a corresponding call to
226 * {@link android.app.Activity#onStop}. During this time the user can see the
227 * activity on-screen, though it may not be in the foreground and interacting
228 * with the user. Between these two methods you can maintain resources that
229 * are needed to show the activity to the user. For example, you can register
230 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900231 * that impact your UI, and unregister it in onStop() when the user no
232 * longer sees what you are displaying. The onStart() and onStop() methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 * can be called multiple times, as the activity becomes visible and hidden
234 * to the user.
RoboErik55011652014-07-09 15:05:53 -0700235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
237 * {@link android.app.Activity#onResume} until a corresponding call to
238 * {@link android.app.Activity#onPause}. During this time the activity is
239 * in front of all other activities and interacting with the user. An activity
240 * can frequently go between the resumed and paused states -- for example when
241 * the device goes to sleep, when an activity result is delivered, when a new
242 * intent is delivered -- so the code in these methods should be fairly
243 * lightweight.
244 * </ul>
RoboErik55011652014-07-09 15:05:53 -0700245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * <p>The entire lifecycle of an activity is defined by the following
247 * Activity methods. All of these are hooks that you can override
248 * to do appropriate work when the activity changes state. All
249 * activities will implement {@link android.app.Activity#onCreate}
250 * to do their initial setup; many will also implement
251 * {@link android.app.Activity#onPause} to commit changes to data and
252 * otherwise prepare to stop interacting with the user. You should always
253 * call up to your superclass when implementing these methods.</p>
254 *
255 * </p>
256 * <pre class="prettyprint">
257 * public class Activity extends ApplicationContext {
258 * protected void onCreate(Bundle savedInstanceState);
259 *
260 * protected void onStart();
RoboErik55011652014-07-09 15:05:53 -0700261 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 * protected void onRestart();
263 *
264 * protected void onResume();
265 *
266 * protected void onPause();
267 *
268 * protected void onStop();
269 *
270 * protected void onDestroy();
271 * }
272 * </pre>
273 *
274 * <p>In general the movement through an activity's lifecycle looks like
275 * this:</p>
276 *
277 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
278 * <colgroup align="left" span="3" />
279 * <colgroup align="left" />
280 * <colgroup align="center" />
281 * <colgroup align="center" />
282 *
283 * <thead>
284 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
285 * </thead>
286 *
287 * <tbody>
288 * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onCreate onCreate()}</th>
289 * <td>Called when the activity is first created.
290 * This is where you should do all of your normal static set up:
291 * create views, bind data to lists, etc. This method also
292 * provides you with a Bundle containing the activity's previously
293 * frozen state, if there was one.
294 * <p>Always followed by <code>onStart()</code>.</td>
295 * <td align="center">No</td>
296 * <td align="center"><code>onStart()</code></td>
297 * </tr>
298 *
299 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
300 * <th colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</th>
301 * <td>Called after your activity has been stopped, prior to it being
302 * started again.
303 * <p>Always followed by <code>onStart()</code></td>
304 * <td align="center">No</td>
305 * <td align="center"><code>onStart()</code></td>
306 * </tr>
307 *
308 * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStart onStart()}</th>
309 * <td>Called when the activity is becoming visible to the user.
310 * <p>Followed by <code>onResume()</code> if the activity comes
311 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
312 * <td align="center">No</td>
313 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
314 * </tr>
315 *
316 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
317 * <th align="left" border="0">{@link android.app.Activity#onResume onResume()}</th>
318 * <td>Called when the activity will start
319 * interacting with the user. At this point your activity is at
320 * the top of the activity stack, with user input going to it.
321 * <p>Always followed by <code>onPause()</code>.</td>
322 * <td align="center">No</td>
323 * <td align="center"><code>onPause()</code></td>
324 * </tr>
325 *
326 * <tr><th align="left" border="0">{@link android.app.Activity#onPause onPause()}</th>
327 * <td>Called when the system is about to start resuming a previous
328 * activity. This is typically used to commit unsaved changes to
329 * persistent data, stop animations and other things that may be consuming
330 * CPU, etc. Implementations of this method must be very quick because
331 * the next activity will not be resumed until this method returns.
332 * <p>Followed by either <code>onResume()</code> if the activity
333 * returns back to the front, or <code>onStop()</code> if it becomes
334 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800335 * <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 -0800336 * <td align="center"><code>onResume()</code> or<br>
337 * <code>onStop()</code></td>
338 * </tr>
339 *
340 * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</th>
341 * <td>Called when the activity is no longer visible to the user, because
342 * another activity has been resumed and is covering this one. This
343 * may happen either because a new activity is being started, an existing
344 * one is being brought in front of this one, or this one is being
345 * destroyed.
346 * <p>Followed by either <code>onRestart()</code> if
347 * this activity is coming back to interact with the user, or
348 * <code>onDestroy()</code> if this activity is going away.</td>
349 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
350 * <td align="center"><code>onRestart()</code> or<br>
351 * <code>onDestroy()</code></td>
352 * </tr>
353 *
354 * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onDestroy onDestroy()}</th>
355 * <td>The final call you receive before your
356 * activity is destroyed. This can happen either because the
357 * activity is finishing (someone called {@link Activity#finish} on
358 * it, or because the system is temporarily destroying this
359 * instance of the activity to save space. You can distinguish
360 * between these two scenarios with the {@link
361 * Activity#isFinishing} method.</td>
362 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
363 * <td align="center"><em>nothing</em></td>
364 * </tr>
365 * </tbody>
366 * </table>
367 *
368 * <p>Note the "Killable" column in the above table -- for those methods that
369 * are marked as being killable, after that method returns the process hosting the
Kris Giesing8b6216d2014-12-16 17:23:15 -0800370 * activity may be killed by the system <em>at any time</em> without another line
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 * of its code being executed. Because of this, you should use the
372 * {@link #onPause} method to write any persistent data (such as user edits)
373 * to storage. In addition, the method
374 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
375 * in such a background state, allowing you to save away any dynamic instance
376 * state in your activity into the given Bundle, to be later received in
RoboErik55011652014-07-09 15:05:53 -0700377 * {@link #onCreate} if the activity needs to be re-created.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
379 * section for more information on how the lifecycle of a process is tied
380 * to the activities it is hosting. Note that it is important to save
381 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800382 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 * be called in every situation as described in its documentation.</p>
384 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800385 * <p class="note">Be aware that these semantics will change slightly between
386 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
387 * vs. those targeting prior platforms. Starting with Honeycomb, an application
388 * is not in the killable state until its {@link #onStop} has returned. This
389 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
390 * safely called after {@link #onPause()} and allows and application to safely
391 * wait until {@link #onStop()} to save persistent state.</p>
392 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * <p>For those methods that are not marked as being killable, the activity's
394 * process will not be killed by the system starting from the time the method
395 * is called and continuing after it returns. Thus an activity is in the killable
396 * state, for example, between after <code>onPause()</code> to the start of
397 * <code>onResume()</code>.</p>
398 *
399 * <a name="ConfigurationChanges"></a>
400 * <h3>Configuration Changes</h3>
RoboErik55011652014-07-09 15:05:53 -0700401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 * <p>If the configuration of the device (as defined by the
403 * {@link Configuration Resources.Configuration} class) changes,
404 * then anything displaying a user interface will need to update to match that
405 * configuration. Because Activity is the primary mechanism for interacting
406 * with the user, it includes special support for handling configuration
407 * changes.</p>
RoboErik55011652014-07-09 15:05:53 -0700408 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * <p>Unless you specify otherwise, a configuration change (such as a change
410 * in screen orientation, language, input devices, etc) will cause your
411 * current activity to be <em>destroyed</em>, going through the normal activity
412 * lifecycle process of {@link #onPause},
413 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
414 * had been in the foreground or visible to the user, once {@link #onDestroy} is
415 * called in that instance then a new instance of the activity will be
416 * created, with whatever savedInstanceState the previous instance had generated
417 * from {@link #onSaveInstanceState}.</p>
RoboErik55011652014-07-09 15:05:53 -0700418 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 * <p>This is done because any application resource,
420 * including layout files, can change based on any configuration value. Thus
421 * the only safe way to handle a configuration change is to re-retrieve all
422 * resources, including layouts, drawables, and strings. Because activities
423 * must already know how to save their state and re-create themselves from
424 * that state, this is a convenient way to have an activity restart itself
425 * with a new configuration.</p>
RoboErik55011652014-07-09 15:05:53 -0700426 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 * <p>In some special cases, you may want to bypass restarting of your
428 * activity based on one or more types of configuration changes. This is
429 * done with the {@link android.R.attr#configChanges android:configChanges}
430 * attribute in its manifest. For any types of configuration changes you say
431 * that you handle there, you will receive a call to your current activity's
432 * {@link #onConfigurationChanged} method instead of being restarted. If
433 * a configuration change involves any that you do not handle, however, the
434 * activity will still be restarted and {@link #onConfigurationChanged}
435 * will not be called.</p>
RoboErik55011652014-07-09 15:05:53 -0700436 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 * <a name="StartingActivities"></a>
438 * <h3>Starting Activities and Getting Results</h3>
439 *
440 * <p>The {@link android.app.Activity#startActivity}
441 * method is used to start a
442 * new activity, which will be placed at the top of the activity stack. It
443 * takes a single argument, an {@link android.content.Intent Intent},
444 * which describes the activity
445 * to be executed.</p>
446 *
447 * <p>Sometimes you want to get a result back from an activity when it
448 * ends. For example, you may start an activity that lets the user pick
449 * a person in a list of contacts; when it ends, it returns the person
450 * that was selected. To do this, you call the
RoboErik55011652014-07-09 15:05:53 -0700451 * {@link android.app.Activity#startActivityForResult(Intent, int)}
452 * version with a second integer parameter identifying the call. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * will come back through your {@link android.app.Activity#onActivityResult}
RoboErik55011652014-07-09 15:05:53 -0700454 * method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 *
456 * <p>When an activity exits, it can call
457 * {@link android.app.Activity#setResult(int)}
458 * to return data back to its parent. It must always supply a result code,
459 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
460 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
461 * return back an Intent containing any additional data it wants. All of this
462 * information appears back on the
463 * parent's <code>Activity.onActivityResult()</code>, along with the integer
464 * identifier it originally supplied.</p>
465 *
466 * <p>If a child activity fails for any reason (such as crashing), the parent
467 * activity will receive a result with the code RESULT_CANCELED.</p>
468 *
469 * <pre class="prettyprint">
470 * public class MyActivity extends Activity {
471 * ...
472 *
473 * static final int PICK_CONTACT_REQUEST = 0;
474 *
Michael Wright5438e4e2014-07-25 14:05:42 -0700475 * public boolean onKeyDown(int keyCode, KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
477 * // When the user center presses, let them pick a contact.
478 * startActivityForResult(
479 * new Intent(Intent.ACTION_PICK,
480 * new Uri("content://contacts")),
481 * PICK_CONTACT_REQUEST);
482 * return true;
483 * }
484 * return false;
485 * }
486 *
487 * protected void onActivityResult(int requestCode, int resultCode,
488 * Intent data) {
489 * if (requestCode == PICK_CONTACT_REQUEST) {
490 * if (resultCode == RESULT_OK) {
491 * // A contact was picked. Here we will just display it
492 * // to the user.
493 * startActivity(new Intent(Intent.ACTION_VIEW, data));
494 * }
495 * }
496 * }
497 * }
498 * </pre>
499 *
500 * <a name="SavingPersistentState"></a>
501 * <h3>Saving Persistent State</h3>
502 *
503 * <p>There are generally two kinds of persistent state than an activity
504 * will deal with: shared document-like data (typically stored in a SQLite
505 * database using a {@linkplain android.content.ContentProvider content provider})
506 * and internal state such as user preferences.</p>
507 *
508 * <p>For content provider data, we suggest that activities use a
509 * "edit in place" user model. That is, any edits a user makes are effectively
510 * made immediately without requiring an additional confirmation step.
511 * Supporting this model is generally a simple matter of following two rules:</p>
512 *
513 * <ul>
514 * <li> <p>When creating a new document, the backing database entry or file for
515 * it is created immediately. For example, if the user chooses to write
516 * a new e-mail, a new entry for that e-mail is created as soon as they
517 * start entering data, so that if they go to any other activity after
518 * that point this e-mail will now appear in the list of drafts.</p>
519 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
520 * commit to the backing content provider or file any changes the user
521 * has made. This ensures that those changes will be seen by any other
522 * activity that is about to run. You will probably want to commit
523 * your data even more aggressively at key times during your
524 * activity's lifecycle: for example before starting a new
525 * activity, before finishing your own activity, when the user
526 * switches between input fields, etc.</p>
527 * </ul>
528 *
529 * <p>This model is designed to prevent data loss when a user is navigating
530 * between activities, and allows the system to safely kill an activity (because
531 * system resources are needed somewhere else) at any time after it has been
532 * paused. Note this implies
533 * that the user pressing BACK from your activity does <em>not</em>
534 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800535 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
537 *
538 * <p>See the {@linkplain android.content.ContentProvider content package} for
539 * more information about content providers. These are a key aspect of how
540 * different activities invoke and propagate data between themselves.</p>
541 *
542 * <p>The Activity class also provides an API for managing internal persistent state
543 * associated with an activity. This can be used, for example, to remember
544 * the user's preferred initial display in a calendar (day view or week view)
545 * or the user's default home page in a web browser.</p>
546 *
547 * <p>Activity persistent state is managed
548 * with the method {@link #getPreferences},
549 * allowing you to retrieve and
550 * modify a set of name/value pairs associated with the activity. To use
551 * preferences that are shared across multiple application components
552 * (activities, receivers, services, providers), you can use the underlying
553 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
554 * to retrieve a preferences
555 * object stored under a specific name.
556 * (Note that it is not possible to share settings data across application
557 * packages -- for that you will need a content provider.)</p>
558 *
559 * <p>Here is an excerpt from a calendar activity that stores the user's
560 * preferred view mode in its persistent settings:</p>
561 *
562 * <pre class="prettyprint">
563 * public class CalendarActivity extends Activity {
564 * ...
565 *
566 * static final int DAY_VIEW_MODE = 0;
567 * static final int WEEK_VIEW_MODE = 1;
568 *
569 * private SharedPreferences mPrefs;
570 * private int mCurViewMode;
571 *
572 * protected void onCreate(Bundle savedInstanceState) {
573 * super.onCreate(savedInstanceState);
574 *
575 * SharedPreferences mPrefs = getSharedPreferences();
Quddus Chong3fa98cb2012-04-13 11:17:24 -0700576 * mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 * }
578 *
579 * protected void onPause() {
580 * super.onPause();
RoboErik55011652014-07-09 15:05:53 -0700581 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 * SharedPreferences.Editor ed = mPrefs.edit();
583 * ed.putInt("view_mode", mCurViewMode);
584 * ed.commit();
585 * }
586 * }
587 * </pre>
RoboErik55011652014-07-09 15:05:53 -0700588 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 * <a name="Permissions"></a>
590 * <h3>Permissions</h3>
RoboErik55011652014-07-09 15:05:53 -0700591 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 * <p>The ability to start a particular Activity can be enforced when it is
593 * declared in its
594 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
595 * tag. By doing so, other applications will need to declare a corresponding
596 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
597 * element in their own manifest to be able to start that activity.
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800598 *
599 * <p>When starting an Activity you can set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
600 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
601 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the
602 * Activity access to the specific URIs in the Intent. Access will remain
603 * until the Activity has finished (it will remain across the hosting
604 * process being killed and other temporary destruction). As of
605 * {@link android.os.Build.VERSION_CODES#GINGERBREAD}, if the Activity
606 * was already created and a new Intent is being delivered to
607 * {@link #onNewIntent(Intent)}, any newly granted URI permissions will be added
608 * to the existing ones it holds.
609 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
611 * document for more information on permissions and security in general.
RoboErik55011652014-07-09 15:05:53 -0700612 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 * <a name="ProcessLifecycle"></a>
614 * <h3>Process Lifecycle</h3>
RoboErik55011652014-07-09 15:05:53 -0700615 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 * <p>The Android system attempts to keep application process around for as
617 * long as possible, but eventually will need to remove old processes when
618 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
619 * Lifecycle</a>, the decision about which process to remove is intimately
620 * tied to the state of the user's interaction with it. In general, there
621 * are four states a process can be in based on the activities running in it,
622 * listed here in order of importance. The system will kill less important
623 * processes (the last ones) before it resorts to killing more important
624 * processes (the first ones).
RoboErik55011652014-07-09 15:05:53 -0700625 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 * <ol>
627 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
628 * that the user is currently interacting with) is considered the most important.
629 * Its process will only be killed as a last resort, if it uses more memory
630 * than is available on the device. Generally at this point the device has
631 * reached a memory paging state, so this is required in order to keep the user
632 * interface responsive.
633 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
634 * but not in the foreground, such as one sitting behind a foreground dialog)
635 * is considered extremely important and will not be killed unless that is
636 * required to keep the foreground activity running.
637 * <li> <p>A <b>background activity</b> (an activity that is not visible to
638 * the user and has been paused) is no longer critical, so the system may
639 * safely kill its process to reclaim memory for other foreground or
640 * visible processes. If its process needs to be killed, when the user navigates
641 * back to the activity (making it visible on the screen again), its
642 * {@link #onCreate} method will be called with the savedInstanceState it had previously
643 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
644 * state as the user last left it.
645 * <li> <p>An <b>empty process</b> is one hosting no activities or other
646 * application components (such as {@link Service} or
647 * {@link android.content.BroadcastReceiver} classes). These are killed very
648 * quickly by the system as memory becomes low. For this reason, any
649 * background operation you do outside of an activity must be executed in the
650 * context of an activity BroadcastReceiver or Service to ensure that the system
651 * knows it needs to keep your process around.
652 * </ol>
RoboErik55011652014-07-09 15:05:53 -0700653 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 * <p>Sometimes an Activity may need to do a long-running operation that exists
655 * independently of the activity lifecycle itself. An example may be a camera
656 * application that allows you to upload a picture to a web site. The upload
657 * may take a long time, and the application should allow the user to leave
658 * the application will it is executing. To accomplish this, your Activity
659 * should start a {@link Service} in which the upload takes place. This allows
660 * the system to properly prioritize your process (considering it to be more
661 * important than other non-visible applications) for the duration of the
662 * upload, independent of whether the original activity is paused, stopped,
663 * or finished.
664 */
665public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700666 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 Window.Callback, KeyEvent.Callback,
Adam Powell117b6952014-05-05 18:14:56 -0700668 OnCreateContextMenuListener, ComponentCallbacks2,
669 Window.OnWindowDismissedCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 private static final String TAG = "Activity";
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700671 private static final boolean DEBUG_LIFECYCLE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672
673 /** Standard activity result: operation canceled. */
674 public static final int RESULT_CANCELED = 0;
675 /** Standard activity result: operation succeeded. */
676 public static final int RESULT_OK = -1;
677 /** Start of user-defined activity results. */
678 public static final int RESULT_FIRST_USER = 1;
679
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700680 static final String FRAGMENTS_TAG = "android:fragments";
681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
683 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
684 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
685 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800686 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800688 private static class ManagedDialog {
689 Dialog mDialog;
690 Bundle mArgs;
691 }
692 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693
694 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
695 private Instrumentation mInstrumentation;
696 private IBinder mToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700697 private int mIdent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 /*package*/ String mEmbeddedID;
699 private Application mApplication;
Christopher Tateb70f3df2009-04-07 16:07:59 -0700700 /*package*/ Intent mIntent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800701 /*package*/ String mReferrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 private ComponentName mComponent;
703 /*package*/ ActivityInfo mActivityInfo;
704 /*package*/ ActivityThread mMainThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 Activity mParent;
706 boolean mCalled;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700707 boolean mCheckedForLoaderManager;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700708 boolean mLoadersStarted;
Jeff Hamilton52d32032011-01-08 15:31:26 -0600709 /*package*/ boolean mResumed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 private boolean mStopped;
711 boolean mFinished;
712 boolean mStartedActivity;
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700713 private boolean mDestroyed;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700714 private boolean mDoReportFullyDrawn = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700715 /** true if the activity is going through a transient pause */
716 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500717 /** true if the activity is being destroyed in order to recreate it with a new configuration */
718 /*package*/ boolean mChangingConfigurations = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 /*package*/ int mConfigChangeFlags;
720 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100721 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700722 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700724 static final class NonConfigurationInstances {
725 Object activity;
726 HashMap<String, Object> children;
727 ArrayList<Fragment> fragments;
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700728 ArrayMap<String, LoaderManagerImpl> loaders;
Dianne Hackborn20d94742014-05-29 18:35:45 -0700729 VoiceInteractor voiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700730 }
731 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
RoboErik55011652014-07-09 15:05:53 -0700732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 private Window mWindow;
734
735 private WindowManager mWindowManager;
736 /*package*/ View mDecor = null;
737 /*package*/ boolean mWindowAdded = false;
738 /*package*/ boolean mVisibleFromServer = false;
739 /*package*/ boolean mVisibleFromClient = true;
Adam Powelle43340c2014-03-17 19:10:43 -0700740 /*package*/ ActionBar mActionBar = null;
Adam Powelldd8fab22012-03-22 17:47:27 -0700741 private boolean mEnableDefaultActionBarUp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742
Dianne Hackborn91097de2014-04-04 18:02:06 -0700743 private VoiceInteractor mVoiceInteractor;
744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 private CharSequence mTitle;
746 private int mTitleColor = 0;
747
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700748 final FragmentManagerImpl mFragments = new FragmentManagerImpl();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700749 final FragmentContainer mContainer = new FragmentContainer() {
750 @Override
Scott Kennedyc0519552015-02-11 15:33:10 -0800751 @Nullable
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700752 public View findViewById(int id) {
753 return Activity.this.findViewById(id);
754 }
George Mount0b26e4d2014-09-17 16:36:42 -0700755 @Override
756 public boolean hasView() {
757 Window window = Activity.this.getWindow();
758 return (window != null && window.peekDecorView() != null);
759 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700760 };
Craig Mautneree2e45a2014-06-27 12:10:03 -0700761
Jose Lima4b6c6692014-08-12 17:41:12 -0700762 // Most recent call to requestVisibleBehind().
763 boolean mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700764
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700765 ArrayMap<String, LoaderManagerImpl> mAllLoaderManagers;
Dianne Hackborn4911b782010-07-15 12:54:39 -0700766 LoaderManagerImpl mLoaderManager;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 private static final class ManagedCursor {
769 ManagedCursor(Cursor cursor) {
770 mCursor = cursor;
771 mReleased = false;
772 mUpdated = false;
773 }
774
775 private final Cursor mCursor;
776 private boolean mReleased;
777 private boolean mUpdated;
778 }
779 private final ArrayList<ManagedCursor> mManagedCursors =
780 new ArrayList<ManagedCursor>();
781
RoboErik55011652014-07-09 15:05:53 -0700782 // protected by synchronized (this)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 int mResultCode = RESULT_CANCELED;
784 Intent mResultData = null;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700785
Craig Mautner5eda9b32013-07-02 11:58:16 -0700786 private TranslucentConversionListener mTranslucentCallback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700787 private boolean mChangeCanvasToTranslucent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788
789 private boolean mTitleReady = false;
Clara Bayarri4423d912015-03-02 19:42:48 +0000790 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791
792 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
793 private SpannableStringBuilder mDefaultKeySsb = null;
RoboErik55011652014-07-09 15:05:53 -0700794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
796
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700797 @SuppressWarnings("unused")
Jeff Brown7e442832011-06-10 18:00:16 -0700798 private final Object mInstanceTracker = StrictMode.trackActivity(this);
799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 private Thread mUiThread;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700801 final Handler mHandler = new Handler();
George Mount62ab9b72014-05-02 13:51:17 -0700802
George Mount1fecfb22014-06-18 14:55:55 -0700803 ActivityTransitionState mActivityTransitionState = new ActivityTransitionState();
George Mount65580562014-08-29 08:15:48 -0700804 SharedElementCallback mEnterTransitionListener = SharedElementCallback.NULL_CALLBACK;
805 SharedElementCallback mExitTransitionListener = SharedElementCallback.NULL_CALLBACK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 /** Return the intent that started this activity. */
808 public Intent getIntent() {
809 return mIntent;
810 }
811
RoboErik55011652014-07-09 15:05:53 -0700812 /**
813 * Change the intent returned by {@link #getIntent}. This holds a
814 * reference to the given intent; it does not copy it. Often used in
815 * conjunction with {@link #onNewIntent}.
816 *
817 * @param newIntent The new Intent object to return from getIntent
818 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 * @see #getIntent
820 * @see #onNewIntent
RoboErik55011652014-07-09 15:05:53 -0700821 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 public void setIntent(Intent newIntent) {
823 mIntent = newIntent;
824 }
825
826 /** Return the application that owns this activity. */
827 public final Application getApplication() {
828 return mApplication;
829 }
830
831 /** Is this activity embedded inside of another activity? */
832 public final boolean isChild() {
833 return mParent != null;
834 }
RoboErik55011652014-07-09 15:05:53 -0700835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 /** Return the parent activity if this view is an embedded child. */
837 public final Activity getParent() {
838 return mParent;
839 }
840
841 /** Retrieve the window manager for showing custom windows. */
842 public WindowManager getWindowManager() {
843 return mWindowManager;
844 }
845
846 /**
847 * Retrieve the current {@link android.view.Window} for the activity.
848 * This can be used to directly access parts of the Window API that
849 * are not available through Activity/Screen.
RoboErik55011652014-07-09 15:05:53 -0700850 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 * @return Window The current window, or null if the activity is not
852 * visual.
853 */
854 public Window getWindow() {
855 return mWindow;
856 }
857
858 /**
Mark Doliner9525f2a2014-01-02 11:17:47 -0800859 * Return the LoaderManager for this activity, creating it if needed.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700860 */
861 public LoaderManager getLoaderManager() {
862 if (mLoaderManager != null) {
863 return mLoaderManager;
864 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700865 mCheckedForLoaderManager = true;
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700866 mLoaderManager = getLoaderManager("(root)", mLoadersStarted, true);
Dianne Hackbornc8017682010-07-06 13:34:38 -0700867 return mLoaderManager;
868 }
RoboErik55011652014-07-09 15:05:53 -0700869
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700870 LoaderManagerImpl getLoaderManager(String who, boolean started, boolean create) {
Dianne Hackbornc8017682010-07-06 13:34:38 -0700871 if (mAllLoaderManagers == null) {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700872 mAllLoaderManagers = new ArrayMap<String, LoaderManagerImpl>();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700873 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700874 LoaderManagerImpl lm = mAllLoaderManagers.get(who);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700875 if (lm == null) {
876 if (create) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700877 lm = new LoaderManagerImpl(who, this, started);
878 mAllLoaderManagers.put(who, lm);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700879 }
880 } else {
881 lm.updateActivity(this);
Dianne Hackbornc8017682010-07-06 13:34:38 -0700882 }
883 return lm;
884 }
RoboErik55011652014-07-09 15:05:53 -0700885
Dianne Hackbornc8017682010-07-06 13:34:38 -0700886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 * Calls {@link android.view.Window#getCurrentFocus} on the
888 * Window of this Activity to return the currently focused view.
RoboErik55011652014-07-09 15:05:53 -0700889 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 * @return View The current View with focus or null.
RoboErik55011652014-07-09 15:05:53 -0700891 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 * @see #getWindow
893 * @see android.view.Window#getCurrentFocus
894 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700895 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 public View getCurrentFocus() {
897 return mWindow != null ? mWindow.getCurrentFocus() : null;
898 }
899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 /**
901 * Called when the activity is starting. This is where most initialization
902 * should go: calling {@link #setContentView(int)} to inflate the
903 * activity's UI, using {@link #findViewById} to programmatically interact
904 * with widgets in the UI, calling
905 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
906 * cursors for data being displayed, etc.
RoboErik55011652014-07-09 15:05:53 -0700907 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 * <p>You can call {@link #finish} from within this function, in
909 * which case onDestroy() will be immediately called without any of the rest
910 * of the activity lifecycle ({@link #onStart}, {@link #onResume},
911 * {@link #onPause}, etc) executing.
RoboErik55011652014-07-09 15:05:53 -0700912 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 * <p><em>Derived classes must call through to the super class's
914 * implementation of this method. If they do not, an exception will be
915 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -0700916 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 * @param savedInstanceState If the activity is being re-initialized after
918 * previously being shut down then this Bundle contains the data it most
919 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
RoboErik55011652014-07-09 15:05:53 -0700920 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 * @see #onStart
922 * @see #onSaveInstanceState
923 * @see #onRestoreInstanceState
924 * @see #onPostCreate
925 */
Tor Norbyec615c6f2015-03-02 10:11:44 -0800926 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -0700927 protected void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700928 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700929 if (mLastNonConfigurationInstances != null) {
930 mAllLoaderManagers = mLastNonConfigurationInstances.loaders;
931 }
Adam Powelldd8fab22012-03-22 17:47:27 -0700932 if (mActivityInfo.parentActivityName != null) {
933 if (mActionBar == null) {
934 mEnableDefaultActionBarUp = true;
935 } else {
936 mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
937 }
938 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700939 if (savedInstanceState != null) {
940 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
941 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
942 ? mLastNonConfigurationInstances.fragments : null);
943 }
944 mFragments.dispatchCreate();
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700945 getApplication().dispatchActivityCreated(this, savedInstanceState);
Dianne Hackborn20d94742014-05-29 18:35:45 -0700946 if (mVoiceInteractor != null) {
947 mVoiceInteractor.attachActivity(this);
948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 mCalled = true;
950 }
951
952 /**
Craig Mautnera0026042014-04-23 11:45:37 -0700953 * Same as {@link #onCreate(android.os.Bundle)} but called for those activities created with
Craig Mautner43e52ed2014-06-16 17:18:52 -0700954 * the attribute {@link android.R.attr#persistableMode} set to
955 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -0700956 *
957 * @param savedInstanceState if the activity is being re-initialized after
958 * previously being shut down then this Bundle contains the data it most
959 * recently supplied in {@link #onSaveInstanceState}.
960 * <b><i>Note: Otherwise it is null.</i></b>
961 * @param persistentState if the activity is being re-initialized after
962 * previously being shut down or powered off then this Bundle contains the data it most
963 * recently supplied to outPersistentState in {@link #onSaveInstanceState}.
964 * <b><i>Note: Otherwise it is null.</i></b>
965 *
966 * @see #onCreate(android.os.Bundle)
967 * @see #onStart
968 * @see #onSaveInstanceState
969 * @see #onRestoreInstanceState
970 * @see #onPostCreate
971 */
Craig Mautner7a629c22014-09-04 14:57:04 -0700972 public void onCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -0700973 @Nullable PersistableBundle persistentState) {
974 onCreate(savedInstanceState);
975 }
976
977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 * The hook for {@link ActivityThread} to restore the state of this activity.
979 *
980 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
981 * {@link #restoreManagedDialogs(android.os.Bundle)}.
982 *
983 * @param savedInstanceState contains the saved state
984 */
985 final void performRestoreInstanceState(Bundle savedInstanceState) {
986 onRestoreInstanceState(savedInstanceState);
987 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989
990 /**
Craig Mautnera0026042014-04-23 11:45:37 -0700991 * The hook for {@link ActivityThread} to restore the state of this activity.
992 *
993 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
994 * {@link #restoreManagedDialogs(android.os.Bundle)}.
995 *
996 * @param savedInstanceState contains the saved state
997 * @param persistentState contains the persistable saved state
998 */
999 final void performRestoreInstanceState(Bundle savedInstanceState,
1000 PersistableBundle persistentState) {
1001 onRestoreInstanceState(savedInstanceState, persistentState);
1002 if (savedInstanceState != null) {
1003 restoreManagedDialogs(savedInstanceState);
1004 }
1005 }
1006
1007 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 * This method is called after {@link #onStart} when the activity is
1009 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -08001010 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 * to restore their state, but it is sometimes convenient to do it here
1012 * after all of the initialization has been done or to allow subclasses to
1013 * decide whether to use your default implementation. The default
1014 * implementation of this method performs a restore of any view state that
1015 * had previously been frozen by {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001016 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 * <p>This method is called between {@link #onStart} and
1018 * {@link #onPostCreate}.
RoboErik55011652014-07-09 15:05:53 -07001019 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
RoboErik55011652014-07-09 15:05:53 -07001021 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 * @see #onCreate
1023 * @see #onPostCreate
1024 * @see #onResume
1025 * @see #onSaveInstanceState
1026 */
1027 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1028 if (mWindow != null) {
1029 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
1030 if (windowState != null) {
1031 mWindow.restoreHierarchyState(windowState);
1032 }
1033 }
1034 }
Craig Mautnera0026042014-04-23 11:45:37 -07001035
1036 /**
1037 * This is the same as {@link #onRestoreInstanceState(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001038 * created with the attribute {@link android.R.attr#persistableMode} set to
1039 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1040 * came from the restored PersistableBundle first
Craig Mautnera0026042014-04-23 11:45:37 -07001041 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1042 *
1043 * <p>This method is called between {@link #onStart} and
1044 * {@link #onPostCreate}.
1045 *
1046 * <p>If this method is called {@link #onRestoreInstanceState(Bundle)} will not be called.
1047 *
1048 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
1049 * @param persistentState the data most recently supplied in {@link #onSaveInstanceState}.
1050 *
1051 * @see #onRestoreInstanceState(Bundle)
1052 * @see #onCreate
1053 * @see #onPostCreate
1054 * @see #onResume
1055 * @see #onSaveInstanceState
1056 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001057 public void onRestoreInstanceState(Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001058 PersistableBundle persistentState) {
1059 if (savedInstanceState != null) {
1060 onRestoreInstanceState(savedInstanceState);
1061 }
1062 }
1063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
1065 * Restore the state of any saved managed dialogs.
1066 *
1067 * @param savedInstanceState The bundle to restore from.
1068 */
1069 private void restoreManagedDialogs(Bundle savedInstanceState) {
1070 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
1071 if (b == null) {
1072 return;
1073 }
1074
1075 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
1076 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001077 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 for (int i = 0; i < numDialogs; i++) {
1079 final Integer dialogId = ids[i];
1080 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
1081 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -07001082 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
1083 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001084 final ManagedDialog md = new ManagedDialog();
1085 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
1086 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
1087 if (md.mDialog != null) {
1088 mManagedDialogs.put(dialogId, md);
1089 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
1090 md.mDialog.onRestoreInstanceState(dialogState);
1091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093 }
1094 }
1095
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001096 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
1097 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -07001098 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001099 return null;
Romain Guy764d5332009-06-17 16:52:22 -07001100 }
Romain Guy6de4aed2009-07-08 10:54:45 -07001101 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -07001102 return dialog;
1103 }
1104
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001105 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 return SAVED_DIALOG_KEY_PREFIX + key;
1107 }
1108
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001109 private static String savedDialogArgsKeyFor(int key) {
1110 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
1111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112
1113 /**
1114 * Called when activity start-up is complete (after {@link #onStart}
1115 * and {@link #onRestoreInstanceState} have been called). Applications will
1116 * generally not implement this method; it is intended for system
1117 * classes to do final initialization after application code has run.
RoboErik55011652014-07-09 15:05:53 -07001118 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 * <p><em>Derived classes must call through to the super class's
1120 * implementation of this method. If they do not, an exception will be
1121 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001122 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 * @param savedInstanceState If the activity is being re-initialized after
1124 * previously being shut down then this Bundle contains the data it most
1125 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
1126 * @see #onCreate
1127 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001128 @CallSuper
Tor Norbyed9273d62013-05-30 15:59:53 -07001129 protected void onPostCreate(@Nullable Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 if (!isChild()) {
1131 mTitleReady = true;
1132 onTitleChanged(getTitle(), getTitleColor());
1133 }
1134 mCalled = true;
1135 }
1136
1137 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001138 * This is the same as {@link #onPostCreate(Bundle)} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001139 * created with the attribute {@link android.R.attr#persistableMode} set to
1140 * <code>persistAcrossReboots</code>.
Craig Mautnera0026042014-04-23 11:45:37 -07001141 *
1142 * @param savedInstanceState The data most recently supplied in {@link #onSaveInstanceState}
1143 * @param persistentState The data caming from the PersistableBundle first
1144 * saved in {@link #onSaveInstanceState(Bundle, PersistableBundle)}.
1145 *
1146 * @see #onCreate
1147 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001148 public void onPostCreate(@Nullable Bundle savedInstanceState,
Craig Mautnera0026042014-04-23 11:45:37 -07001149 @Nullable PersistableBundle persistentState) {
1150 onPostCreate(savedInstanceState);
1151 }
1152
1153 /**
RoboErik55011652014-07-09 15:05:53 -07001154 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
1155 * the activity had been stopped, but is now again being displayed to the
John Spurlock8a985d22014-02-25 09:40:05 -05001156 * user. It will be followed by {@link #onResume}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 *
1158 * <p><em>Derived classes must call through to the super class's
1159 * implementation of this method. If they do not, an exception will be
1160 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 * @see #onCreate
1163 * @see #onStop
1164 * @see #onResume
1165 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001166 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 protected void onStart() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001168 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 mCalled = true;
RoboErik55011652014-07-09 15:05:53 -07001170
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001171 if (!mLoadersStarted) {
1172 mLoadersStarted = true;
1173 if (mLoaderManager != null) {
1174 mLoaderManager.doStart();
1175 } else if (!mCheckedForLoaderManager) {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -07001176 mLoaderManager = getLoaderManager("(root)", mLoadersStarted, false);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001177 }
1178 mCheckedForLoaderManager = true;
Dianne Hackborn2707d602010-07-09 18:01:20 -07001179 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001180
1181 getApplication().dispatchActivityStarted(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183
1184 /**
1185 * Called after {@link #onStop} when the current activity is being
1186 * re-displayed to the user (the user has navigated back to it). It will
1187 * be followed by {@link #onStart} and then {@link #onResume}.
1188 *
1189 * <p>For activities that are using raw {@link Cursor} objects (instead of
1190 * creating them through
1191 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1192 * this is usually the place
1193 * where the cursor should be requeried (because you had deactivated it in
1194 * {@link #onStop}.
RoboErik55011652014-07-09 15:05:53 -07001195 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 * <p><em>Derived classes must call through to the super class's
1197 * implementation of this method. If they do not, an exception will be
1198 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001199 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 * @see #onStop
1201 * @see #onStart
1202 * @see #onResume
1203 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001204 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 protected void onRestart() {
1206 mCalled = true;
1207 }
1208
1209 /**
1210 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1211 * {@link #onPause}, for your activity to start interacting with the user.
1212 * This is a good place to begin animations, open exclusive-access devices
1213 * (such as the camera), etc.
1214 *
1215 * <p>Keep in mind that onResume is not the best indicator that your activity
1216 * is visible to the user; a system window such as the keyguard may be in
1217 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1218 * activity is visible to the user (for example, to resume a game).
1219 *
1220 * <p><em>Derived classes must call through to the super class's
1221 * implementation of this method. If they do not, an exception will be
1222 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001223 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 * @see #onRestoreInstanceState
1225 * @see #onRestart
1226 * @see #onPostResume
1227 * @see #onPause
1228 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001229 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 protected void onResume() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001231 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001232 getApplication().dispatchActivityResumed(this);
George Mount8cab50a2014-05-15 09:57:17 -07001233 mActivityTransitionState.onResume();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 mCalled = true;
1235 }
1236
1237 /**
1238 * Called when activity resume is complete (after {@link #onResume} has
1239 * been called). Applications will generally not implement this method;
1240 * it is intended for system classes to do final setup after application
1241 * resume code has run.
RoboErik55011652014-07-09 15:05:53 -07001242 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 * <p><em>Derived classes must call through to the super class's
1244 * implementation of this method. If they do not, an exception will be
1245 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 * @see #onResume
1248 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001249 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 protected void onPostResume() {
1251 final Window win = getWindow();
1252 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001253 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 mCalled = true;
1255 }
1256
1257 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -07001258 * Check whether this activity is running as part of a voice interaction with the user.
1259 * If true, it should perform its interaction with the user through the
1260 * {@link VoiceInteractor} returned by {@link #getVoiceInteractor}.
1261 */
1262 public boolean isVoiceInteraction() {
1263 return mVoiceInteractor != null;
1264 }
1265
1266 /**
1267 * Retrieve the active {@link VoiceInteractor} that the user is going through to
1268 * interact with this activity.
1269 */
1270 public VoiceInteractor getVoiceInteractor() {
1271 return mVoiceInteractor;
1272 }
1273
1274 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 * This is called for activities that set launchMode to "singleTop" in
1276 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1277 * flag when calling {@link #startActivity}. In either case, when the
1278 * activity is re-launched while at the top of the activity stack instead
1279 * of a new instance of the activity being started, onNewIntent() will be
1280 * called on the existing instance with the Intent that was used to
RoboErik55011652014-07-09 15:05:53 -07001281 * re-launch it.
1282 *
1283 * <p>An activity will always be paused before receiving a new intent, so
1284 * you can count on {@link #onResume} being called after this method.
1285 *
1286 * <p>Note that {@link #getIntent} still returns the original Intent. You
1287 * can use {@link #setIntent} to update it to this new Intent.
1288 *
1289 * @param intent The new intent that was started for the activity.
1290 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 * @see #getIntent
RoboErik55011652014-07-09 15:05:53 -07001292 * @see #setIntent
1293 * @see #onResume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 */
1295 protected void onNewIntent(Intent intent) {
1296 }
1297
1298 /**
1299 * The hook for {@link ActivityThread} to save the state of this activity.
1300 *
1301 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1302 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1303 *
1304 * @param outState The bundle to save the state to.
1305 */
1306 final void performSaveInstanceState(Bundle outState) {
1307 onSaveInstanceState(outState);
1308 saveManagedDialogs(outState);
George Mount62ab9b72014-05-02 13:51:17 -07001309 mActivityTransitionState.saveState(outState);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001310 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
1312
1313 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001314 * The hook for {@link ActivityThread} to save the state of this activity.
1315 *
1316 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1317 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1318 *
1319 * @param outState The bundle to save the state to.
1320 * @param outPersistentState The bundle to save persistent state to.
1321 */
1322 final void performSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
1323 onSaveInstanceState(outState, outPersistentState);
1324 saveManagedDialogs(outState);
1325 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
1326 ", " + outPersistentState);
1327 }
1328
1329 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 * Called to retrieve per-instance state from an activity before being killed
1331 * so that the state can be restored in {@link #onCreate} or
1332 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1333 * will be passed to both).
1334 *
1335 * <p>This method is called before an activity may be killed so that when it
1336 * comes back some time in the future it can restore its state. For example,
1337 * if activity B is launched in front of activity A, and at some point activity
1338 * A is killed to reclaim resources, activity A will have a chance to save the
1339 * current state of its user interface via this method so that when the user
1340 * returns to activity A, the state of the user interface can be restored
1341 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1342 *
1343 * <p>Do not confuse this method with activity lifecycle callbacks such as
1344 * {@link #onPause}, which is always called when an activity is being placed
1345 * in the background or on its way to destruction, or {@link #onStop} which
1346 * is called before destruction. One example of when {@link #onPause} and
1347 * {@link #onStop} is called and not this method is when a user navigates back
1348 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1349 * on B because that particular instance will never be restored, so the
1350 * system avoids calling it. An example when {@link #onPause} is called and
1351 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1352 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1353 * killed during the lifetime of B since the state of the user interface of
1354 * A will stay intact.
1355 *
1356 * <p>The default implementation takes care of most of the UI per-instance
1357 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1358 * view in the hierarchy that has an id, and by saving the id of the currently
1359 * focused view (all of which is restored by the default implementation of
1360 * {@link #onRestoreInstanceState}). If you override this method to save additional
1361 * information not captured by each individual view, you will likely want to
1362 * call through to the default implementation, otherwise be prepared to save
1363 * all of the state of each view yourself.
1364 *
1365 * <p>If called, this method will occur before {@link #onStop}. There are
1366 * no guarantees about whether it will occur before or after {@link #onPause}.
RoboErik55011652014-07-09 15:05:53 -07001367 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 * @param outState Bundle in which to place your saved state.
RoboErik55011652014-07-09 15:05:53 -07001369 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 * @see #onCreate
1371 * @see #onRestoreInstanceState
1372 * @see #onPause
1373 */
1374 protected void onSaveInstanceState(Bundle outState) {
1375 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001376 Parcelable p = mFragments.saveAllState();
1377 if (p != null) {
1378 outState.putParcelable(FRAGMENTS_TAG, p);
1379 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001380 getApplication().dispatchActivitySaveInstanceState(this, outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382
1383 /**
Craig Mautnera0026042014-04-23 11:45:37 -07001384 * This is the same as {@link #onSaveInstanceState} but is called for activities
Craig Mautner43e52ed2014-06-16 17:18:52 -07001385 * created with the attribute {@link android.R.attr#persistableMode} set to
1386 * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
1387 * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
1388 * the first time that this activity is restarted following the next device reboot.
Craig Mautnera0026042014-04-23 11:45:37 -07001389 *
1390 * @param outState Bundle in which to place your saved state.
1391 * @param outPersistentState State which will be saved across reboots.
1392 *
1393 * @see #onSaveInstanceState(Bundle)
1394 * @see #onCreate
1395 * @see #onRestoreInstanceState(Bundle, PersistableBundle)
1396 * @see #onPause
1397 */
Craig Mautner7a629c22014-09-04 14:57:04 -07001398 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Craig Mautnera0026042014-04-23 11:45:37 -07001399 onSaveInstanceState(outState);
1400 }
1401
1402 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 * Save the state of any managed dialogs.
1404 *
1405 * @param outState place to store the saved state.
1406 */
1407 private void saveManagedDialogs(Bundle outState) {
1408 if (mManagedDialogs == null) {
1409 return;
1410 }
1411
1412 final int numDialogs = mManagedDialogs.size();
1413 if (numDialogs == 0) {
1414 return;
1415 }
1416
1417 Bundle dialogState = new Bundle();
1418
1419 int[] ids = new int[mManagedDialogs.size()];
1420
1421 // save each dialog's bundle, gather the ids
1422 for (int i = 0; i < numDialogs; i++) {
1423 final int key = mManagedDialogs.keyAt(i);
1424 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001425 final ManagedDialog md = mManagedDialogs.valueAt(i);
1426 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1427 if (md.mArgs != null) {
1428 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
1431
1432 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1433 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1434 }
1435
1436
1437 /**
1438 * Called as part of the activity lifecycle when an activity is going into
1439 * the background, but has not (yet) been killed. The counterpart to
1440 * {@link #onResume}.
1441 *
1442 * <p>When activity B is launched in front of activity A, this callback will
1443 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1444 * so be sure to not do anything lengthy here.
1445 *
1446 * <p>This callback is mostly used for saving any persistent state the
1447 * activity is editing, to present a "edit in place" model to the user and
1448 * making sure nothing is lost if there are not enough resources to start
1449 * the new activity without first killing this one. This is also a good
1450 * place to do things like stop animations and other things that consume a
Pin Ting14a93102012-04-25 11:27:03 +08001451 * noticeable amount of CPU in order to make the switch to the next activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 * as fast as possible, or to close resources that are exclusive access
1453 * such as the camera.
RoboErik55011652014-07-09 15:05:53 -07001454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 * <p>In situations where the system needs more memory it may kill paused
1456 * processes to reclaim resources. Because of this, you should be sure
1457 * that all of your state is saved by the time you return from
1458 * this function. In general {@link #onSaveInstanceState} is used to save
1459 * per-instance state in the activity and this method is used to store
1460 * global persistent data (in content providers, files, etc.)
RoboErik55011652014-07-09 15:05:53 -07001461 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 * <p>After receiving this call you will usually receive a following call
1463 * to {@link #onStop} (after the next activity has been resumed and
1464 * displayed), however in some cases there will be a direct call back to
1465 * {@link #onResume} without going through the stopped state.
RoboErik55011652014-07-09 15:05:53 -07001466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 * <p><em>Derived classes must call through to the super class's
1468 * implementation of this method. If they do not, an exception will be
1469 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001470 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 * @see #onResume
1472 * @see #onSaveInstanceState
1473 * @see #onStop
1474 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001475 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 protected void onPause() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001477 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001478 getApplication().dispatchActivityPaused(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 mCalled = true;
1480 }
1481
1482 /**
1483 * Called as part of the activity lifecycle when an activity is about to go
1484 * into the background as the result of user choice. For example, when the
1485 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1486 * when an incoming phone call causes the in-call Activity to be automatically
1487 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1488 * the activity being interrupted. In cases when it is invoked, this method
1489 * is called right before the activity's {@link #onPause} callback.
RoboErik55011652014-07-09 15:05:53 -07001490 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 * <p>This callback and {@link #onUserInteraction} are intended to help
1492 * activities manage status bar notifications intelligently; specifically,
1493 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07001494 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 * @see #onUserInteraction()
1496 */
1497 protected void onUserLeaveHint() {
1498 }
RoboErik55011652014-07-09 15:05:53 -07001499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 /**
1501 * Generate a new thumbnail for this activity. This method is called before
1502 * pausing the activity, and should draw into <var>outBitmap</var> the
1503 * imagery for the desired thumbnail in the dimensions of that bitmap. It
1504 * can use the given <var>canvas</var>, which is configured to draw into the
1505 * bitmap, for rendering if desired.
RoboErik55011652014-07-09 15:05:53 -07001506 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001507 * <p>The default implementation returns fails and does not draw a thumbnail;
1508 * this will result in the platform creating its own thumbnail if needed.
RoboErik55011652014-07-09 15:05:53 -07001509 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * @param outBitmap The bitmap to contain the thumbnail.
1511 * @param canvas Can be used to render into the bitmap.
RoboErik55011652014-07-09 15:05:53 -07001512 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 * @return Return true if you have drawn into the bitmap; otherwise after
1514 * you return it will be filled with a default thumbnail.
RoboErik55011652014-07-09 15:05:53 -07001515 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 * @see #onCreateDescription
1517 * @see #onSaveInstanceState
1518 * @see #onPause
1519 */
1520 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001521 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523
1524 /**
1525 * Generate a new description for this activity. This method is called
1526 * before pausing the activity and can, if desired, return some textual
1527 * description of its current state to be displayed to the user.
RoboErik55011652014-07-09 15:05:53 -07001528 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 * <p>The default implementation returns null, which will cause you to
1530 * inherit the description from the previous activity. If all activities
1531 * return null, generally the label of the top activity will be used as the
1532 * description.
RoboErik55011652014-07-09 15:05:53 -07001533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * @return A description of what the user is doing. It should be short and
1535 * sweet (only a few words).
RoboErik55011652014-07-09 15:05:53 -07001536 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 * @see #onCreateThumbnail
1538 * @see #onSaveInstanceState
1539 * @see #onPause
1540 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001541 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 public CharSequence onCreateDescription() {
1543 return null;
1544 }
1545
1546 /**
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001547 * This is called when the user is requesting an assist, to build a full
1548 * {@link Intent#ACTION_ASSIST} Intent with all of the context of the current
1549 * application. You can override this method to place into the bundle anything
1550 * you would like to appear in the {@link Intent#EXTRA_ASSIST_CONTEXT} part
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001551 * of the assist Intent.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001552 *
1553 * <p>This function will be called after any global assist callbacks that had
Ying Wang4e0eb222013-04-18 20:39:48 -07001554 * been registered with {@link Application#registerOnProvideAssistDataListener
1555 * Application.registerOnProvideAssistDataListener}.
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001556 */
1557 public void onProvideAssistData(Bundle data) {
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07001558 }
1559
1560 /**
1561 * This is called when the user is requesting an assist, to provide references
1562 * to content related to the current activity. Before being called, the
1563 * {@code outContent} Intent is filled with the base Intent of the activity (the Intent
1564 * returned by {@link #getIntent()}). The Intent's extras are stripped of any types
1565 * that are not valid for {@link PersistableBundle} or non-framework Parcelables, and
1566 * the flags {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} and
1567 * {@link Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION} are cleared from the Intent.
1568 *
1569 * <p>Custom implementation may adjust the content intent to better reflect the top-level
1570 * context of the activity, and fill in its ClipData with additional content of
1571 * interest that the user is currently viewing. For example, an image gallery application
1572 * that has launched in to an activity allowing the user to swipe through pictures should
1573 * modify the intent to reference the current image they are looking it; such an
1574 * application when showing a list of pictures should add a ClipData that has
1575 * references to all of the pictures currently visible on screen.</p>
1576 *
1577 * @param outContent The assist content to return.
1578 */
1579 public void onProvideAssistContent(AssistContent outContent) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001580 }
1581
1582 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 * Called when you are no longer visible to the user. You will next
1584 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1585 * depending on later user activity.
RoboErik55011652014-07-09 15:05:53 -07001586 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 * <p>Note that this method may never be called, in low memory situations
1588 * where the system does not have enough memory to keep your activity's
1589 * process running after its {@link #onPause} method is called.
RoboErik55011652014-07-09 15:05:53 -07001590 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 * <p><em>Derived classes must call through to the super class's
1592 * implementation of this method. If they do not, an exception will be
1593 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001594 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 * @see #onRestart
1596 * @see #onResume
1597 * @see #onSaveInstanceState
1598 * @see #onDestroy
1599 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001600 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 protected void onStop() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001602 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
Adam Powell50efbed2011-02-08 16:20:15 -08001603 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
George Mount62ab9b72014-05-02 13:51:17 -07001604 mActivityTransitionState.onStop();
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001605 getApplication().dispatchActivityStopped(this);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001606 mTranslucentCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 mCalled = true;
1608 }
1609
1610 /**
1611 * Perform any final cleanup before an activity is destroyed. This can
1612 * happen either because the activity is finishing (someone called
1613 * {@link #finish} on it, or because the system is temporarily destroying
1614 * this instance of the activity to save space. You can distinguish
1615 * between these two scenarios with the {@link #isFinishing} method.
RoboErik55011652014-07-09 15:05:53 -07001616 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 * <p><em>Note: do not count on this method being called as a place for
1618 * saving data! For example, if an activity is editing data in a content
1619 * provider, those edits should be committed in either {@link #onPause} or
1620 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1621 * free resources like threads that are associated with an activity, so
1622 * that a destroyed activity does not leave such things around while the
1623 * rest of its application is still running. There are situations where
1624 * the system will simply kill the activity's hosting process without
1625 * calling this method (or any others) in it, so it should not be used to
1626 * do things that are intended to remain around after the process goes
1627 * away.
RoboErik55011652014-07-09 15:05:53 -07001628 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 * <p><em>Derived classes must call through to the super class's
1630 * implementation of this method. If they do not, an exception will be
1631 * thrown.</em></p>
RoboErik55011652014-07-09 15:05:53 -07001632 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 * @see #onPause
1634 * @see #onStop
1635 * @see #finish
1636 * @see #isFinishing
1637 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001638 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 protected void onDestroy() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001640 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 mCalled = true;
1642
1643 // dismiss any dialogs we are managing.
1644 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 final int numDialogs = mManagedDialogs.size();
1646 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001647 final ManagedDialog md = mManagedDialogs.valueAt(i);
1648 if (md.mDialog.isShowing()) {
1649 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
1651 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001652 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654
1655 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001656 synchronized (mManagedCursors) {
1657 int numCursors = mManagedCursors.size();
1658 for (int i = 0; i < numCursors; i++) {
1659 ManagedCursor c = mManagedCursors.get(i);
1660 if (c != null) {
1661 c.mCursor.close();
1662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001664 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
Amith Yamasani49860442010-03-17 20:54:10 -07001666
1667 // Close any open search dialog
1668 if (mSearchManager != null) {
1669 mSearchManager.stopSearch();
1670 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001671
1672 getApplication().dispatchActivityDestroyed(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
1674
1675 /**
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001676 * Report to the system that your app is now fully drawn, purely for diagnostic
1677 * purposes (calling it does not impact the visible behavior of the activity).
1678 * This is only used to help instrument application launch times, so that the
1679 * app can report when it is fully in a usable state; without this, the only thing
1680 * the system itself can determine is the point at which the activity's window
1681 * is <em>first</em> drawn and displayed. To participate in app launch time
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001682 * measurement, you should always call this method after first launch (when
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001683 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001684 * entirely drawn your UI and populated with all of the significant data. You
1685 * can safely call this method any time after first launch as well, in which case
1686 * it will simply be ignored.
1687 */
1688 public void reportFullyDrawn() {
1689 if (mDoReportFullyDrawn) {
1690 mDoReportFullyDrawn = false;
1691 try {
1692 ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken);
1693 } catch (RemoteException e) {
1694 }
1695 }
1696 }
1697
1698 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 * Called by the system when the device configuration changes while your
1700 * activity is running. Note that this will <em>only</em> be called if
1701 * you have selected configurations you would like to handle with the
1702 * {@link android.R.attr#configChanges} attribute in your manifest. If
1703 * any configuration change occurs that is not selected to be reported
1704 * by that attribute, then instead of reporting it the system will stop
1705 * and restart the activity (to have it launched with the new
1706 * configuration).
RoboErik55011652014-07-09 15:05:53 -07001707 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 * <p>At the time that this function has been called, your Resources
1709 * object will have been updated to return resource values matching the
1710 * new configuration.
RoboErik55011652014-07-09 15:05:53 -07001711 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 * @param newConfig The new device configuration.
1713 */
1714 public void onConfigurationChanged(Configuration newConfig) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001715 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01001717
Dianne Hackborn9d071802010-12-08 14:49:15 -08001718 mFragments.dispatchConfigurationChanged(newConfig);
1719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 if (mWindow != null) {
1721 // Pass the configuration changed event to the window
1722 mWindow.onConfigurationChanged(newConfig);
1723 }
Adam Powell45c0b192011-07-28 15:11:57 -07001724
1725 if (mActionBar != null) {
1726 // Do this last; the action bar will need to access
1727 // view changes from above.
1728 mActionBar.onConfigurationChanged(newConfig);
1729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 }
RoboErik55011652014-07-09 15:05:53 -07001731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 /**
1733 * If this activity is being destroyed because it can not handle a
1734 * configuration parameter being changed (and thus its
1735 * {@link #onConfigurationChanged(Configuration)} method is
1736 * <em>not</em> being called), then you can use this method to discover
1737 * the set of changes that have occurred while in the process of being
1738 * destroyed. Note that there is no guarantee that these will be
1739 * accurate (other changes could have happened at any time), so you should
1740 * only use this as an optimization hint.
RoboErik55011652014-07-09 15:05:53 -07001741 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 * @return Returns a bit field of the configuration parameters that are
1743 * changing, as defined by the {@link android.content.res.Configuration}
1744 * class.
1745 */
1746 public int getChangingConfigurations() {
1747 return mConfigChangeFlags;
1748 }
RoboErik55011652014-07-09 15:05:53 -07001749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 /**
1751 * Retrieve the non-configuration instance data that was previously
1752 * returned by {@link #onRetainNonConfigurationInstance()}. This will
1753 * be available from the initial {@link #onCreate} and
1754 * {@link #onStart} calls to the new instance, allowing you to extract
1755 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07001756 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 * <p>Note that the data you retrieve here should <em>only</em> be used
1758 * as an optimization for handling configuration changes. You should always
1759 * be able to handle getting a null pointer back, and an activity must
1760 * still be able to restore itself to its previous state (through the
1761 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
1762 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07001763 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 * @return Returns the object previously returned by
1765 * {@link #onRetainNonConfigurationInstance()}.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07001766 *
1767 * @deprecated Use the new {@link Fragment} API
1768 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
1769 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001771 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07001772 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001774 return mLastNonConfigurationInstances != null
1775 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
RoboErik55011652014-07-09 15:05:53 -07001777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 /**
1779 * Called by the system, as part of destroying an
1780 * activity due to a configuration change, when it is known that a new
1781 * instance will immediately be created for the new configuration. You
1782 * can return any object you like here, including the activity instance
1783 * itself, which can later be retrieved by calling
1784 * {@link #getLastNonConfigurationInstance()} in the new activity
1785 * instance.
RoboErik55011652014-07-09 15:05:53 -07001786 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001787 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1788 * or later, consider instead using a {@link Fragment} with
1789 * {@link Fragment#setRetainInstance(boolean)
1790 * Fragment.setRetainInstance(boolean}.</em>
1791 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 * <p>This function is called purely as an optimization, and you must
1793 * not rely on it being called. When it is called, a number of guarantees
1794 * will be made to help optimize configuration switching:
1795 * <ul>
1796 * <li> The function will be called between {@link #onStop} and
1797 * {@link #onDestroy}.
1798 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001799 * created after this one's {@link #onDestroy()} is called. In particular,
1800 * <em>no</em> messages will be dispatched during this time (when the returned
1801 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 * <li> The object you return here will <em>always</em> be available from
1803 * the {@link #getLastNonConfigurationInstance()} method of the following
1804 * activity instance as described there.
1805 * </ul>
RoboErik55011652014-07-09 15:05:53 -07001806 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 * <p>These guarantees are designed so that an activity can use this API
1808 * to propagate extensive state from the old to new activity instance, from
1809 * loaded bitmaps, to network connections, to evenly actively running
1810 * threads. Note that you should <em>not</em> propagate any data that
1811 * may change based on the configuration, including any data loaded from
1812 * resources such as strings, layouts, or drawables.
RoboErik55011652014-07-09 15:05:53 -07001813 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001814 * <p>The guarantee of no message handling during the switch to the next
1815 * activity simplifies use with active objects. For example if your retained
1816 * state is an {@link android.os.AsyncTask} you are guaranteed that its
1817 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
1818 * not be called from the call here until you execute the next instance's
1819 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
1820 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
1821 * running in a separate thread.)
1822 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 * @return Return any Object holding the desired state to propagate to the
1824 * next activity instance.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07001825 *
1826 * @deprecated Use the new {@link Fragment} API
1827 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
1828 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 */
1830 public Object onRetainNonConfigurationInstance() {
1831 return null;
1832 }
RoboErik55011652014-07-09 15:05:53 -07001833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 /**
1835 * Retrieve the non-configuration instance data that was previously
1836 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
1837 * be available from the initial {@link #onCreate} and
1838 * {@link #onStart} calls to the new instance, allowing you to extract
1839 * any useful dynamic state from the previous instance.
RoboErik55011652014-07-09 15:05:53 -07001840 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 * <p>Note that the data you retrieve here should <em>only</em> be used
1842 * as an optimization for handling configuration changes. You should always
1843 * be able to handle getting a null pointer back, and an activity must
1844 * still be able to restore itself to its previous state (through the
1845 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
1846 * function returns null.
RoboErik55011652014-07-09 15:05:53 -07001847 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 * @return Returns the object previously returned by
1849 * {@link #onRetainNonConfigurationChildInstances()}
1850 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001851 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001852 HashMap<String, Object> getLastNonConfigurationChildInstances() {
1853 return mLastNonConfigurationInstances != null
1854 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 }
RoboErik55011652014-07-09 15:05:53 -07001856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 /**
1858 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
1859 * it should return either a mapping from child activity id strings to arbitrary objects,
1860 * or null. This method is intended to be used by Activity framework subclasses that control a
1861 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
1862 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
1863 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001864 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
1866 return null;
1867 }
RoboErik55011652014-07-09 15:05:53 -07001868
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001869 NonConfigurationInstances retainNonConfigurationInstances() {
1870 Object activity = onRetainNonConfigurationInstance();
1871 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
1872 ArrayList<Fragment> fragments = mFragments.retainNonConfig();
Dianne Hackborn2707d602010-07-09 18:01:20 -07001873 boolean retainLoaders = false;
1874 if (mAllLoaderManagers != null) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001875 // prune out any loader managers that were already stopped and so
Dianne Hackborn2707d602010-07-09 18:01:20 -07001876 // have nothing useful to retain.
Dianne Hackborn3e82ba12013-07-16 13:23:55 -07001877 final int N = mAllLoaderManagers.size();
1878 LoaderManagerImpl loaders[] = new LoaderManagerImpl[N];
1879 for (int i=N-1; i>=0; i--) {
1880 loaders[i] = mAllLoaderManagers.valueAt(i);
1881 }
1882 for (int i=0; i<N; i++) {
1883 LoaderManagerImpl lm = loaders[i];
1884 if (lm.mRetaining) {
1885 retainLoaders = true;
1886 } else {
1887 lm.doDestroy();
1888 mAllLoaderManagers.remove(lm.mWho);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001889 }
1890 }
1891 }
Dianne Hackborn20d94742014-05-29 18:35:45 -07001892 if (activity == null && children == null && fragments == null && !retainLoaders
1893 && mVoiceInteractor == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001894 return null;
1895 }
RoboErik55011652014-07-09 15:05:53 -07001896
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001897 NonConfigurationInstances nci = new NonConfigurationInstances();
1898 nci.activity = activity;
1899 nci.children = children;
1900 nci.fragments = fragments;
Dianne Hackborn2707d602010-07-09 18:01:20 -07001901 nci.loaders = mAllLoaderManagers;
Dianne Hackborn20d94742014-05-29 18:35:45 -07001902 nci.voiceInteractor = mVoiceInteractor;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001903 return nci;
1904 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 public void onLowMemory() {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001907 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08001909 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001911
1912 public void onTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001913 if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001914 mCalled = true;
1915 mFragments.dispatchTrimMemory(level);
1916 }
1917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001919 * Return the FragmentManager for interacting with fragments associated
1920 * with this activity.
1921 */
1922 public FragmentManager getFragmentManager() {
1923 return mFragments;
1924 }
1925
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001926 void invalidateFragment(String who) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001927 //Log.v(TAG, "invalidateFragmentIndex: index=" + index);
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07001928 if (mAllLoaderManagers != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001929 LoaderManagerImpl lm = mAllLoaderManagers.get(who);
Dianne Hackbornd04ad542011-07-25 16:16:15 -07001930 if (lm != null && !lm.mRetaining) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001931 lm.doDestroy();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001932 mAllLoaderManagers.remove(who);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001933 }
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07001934 }
1935 }
RoboErik55011652014-07-09 15:05:53 -07001936
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001937 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001938 * Called when a Fragment is being attached to this activity, immediately
1939 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
1940 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
1941 */
1942 public void onAttachFragment(Fragment fragment) {
1943 }
RoboErik55011652014-07-09 15:05:53 -07001944
Dianne Hackbornc8017682010-07-06 13:34:38 -07001945 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 * Wrapper around
1947 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
1948 * that gives the resulting {@link Cursor} to call
1949 * {@link #startManagingCursor} so that the activity will manage its
1950 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07001951 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001952 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1953 * or later, consider instead using {@link LoaderManager} instead, available
1954 * via {@link #getLoaderManager()}.</em>
1955 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07001956 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
1957 * this method, because the activity will do that for you at the appropriate time. However, if
1958 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
1959 * not</em> automatically close the cursor and, in that case, you must call
1960 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07001961 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 * @param uri The URI of the content provider to query.
1963 * @param projection List of columns to return.
1964 * @param selection SQL WHERE clause.
1965 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07001966 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07001968 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
1970 * @see #startManagingCursor
1971 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05001972 *
1973 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 */
Jason parks6ed50de2010-08-25 10:18:50 -05001975 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07001976 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
1977 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
1979 if (c != null) {
1980 startManagingCursor(c);
1981 }
1982 return c;
1983 }
1984
1985 /**
1986 * Wrapper around
1987 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
1988 * that gives the resulting {@link Cursor} to call
1989 * {@link #startManagingCursor} so that the activity will manage its
1990 * lifecycle for you.
RoboErik55011652014-07-09 15:05:53 -07001991 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001992 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1993 * or later, consider instead using {@link LoaderManager} instead, available
1994 * via {@link #getLoaderManager()}.</em>
1995 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07001996 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on a cursor obtained using
1997 * this method, because the activity will do that for you at the appropriate time. However, if
1998 * you call {@link #stopManagingCursor} on a cursor from a managed query, the system <em>will
1999 * not</em> automatically close the cursor and, in that case, you must call
2000 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002001 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 * @param uri The URI of the content provider to query.
2003 * @param projection List of columns to return.
2004 * @param selection SQL WHERE clause.
2005 * @param selectionArgs The arguments to selection, if any ?s are pesent
2006 * @param sortOrder SQL ORDER BY clause.
RoboErik55011652014-07-09 15:05:53 -07002007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 * @return The Cursor that was returned by query().
RoboErik55011652014-07-09 15:05:53 -07002009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
2011 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05002012 *
2013 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 */
Jason parks6ed50de2010-08-25 10:18:50 -05002015 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07002016 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
2017 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
2019 if (c != null) {
2020 startManagingCursor(c);
2021 }
2022 return c;
2023 }
2024
2025 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 * This method allows the activity to take care of managing the given
2027 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
2028 * That is, when the activity is stopped it will automatically call
2029 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
2030 * it will call {@link Cursor#requery} for you. When the activity is
2031 * destroyed, all managed Cursors will be closed automatically.
RoboErik55011652014-07-09 15:05:53 -07002032 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002033 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2034 * or later, consider instead using {@link LoaderManager} instead, available
2035 * via {@link #getLoaderManager()}.</em>
2036 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002037 * <p><strong>Warning:</strong> Do not call {@link Cursor#close()} on cursor obtained from
2038 * {@link #managedQuery}, because the activity will do that for you at the appropriate time.
2039 * However, if you call {@link #stopManagingCursor} on a cursor from a managed query, the system
2040 * <em>will not</em> automatically close the cursor and, in that case, you must call
2041 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002042 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 * @param c The Cursor to be managed.
RoboErik55011652014-07-09 15:05:53 -07002044 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
2046 * @see #stopManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002047 *
2048 * @deprecated Use the new {@link android.content.CursorLoader} class with
2049 * {@link LoaderManager} instead; this is also
2050 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 */
Jason parks6ed50de2010-08-25 10:18:50 -05002052 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 public void startManagingCursor(Cursor c) {
2054 synchronized (mManagedCursors) {
2055 mManagedCursors.add(new ManagedCursor(c));
2056 }
2057 }
2058
2059 /**
2060 * Given a Cursor that was previously given to
2061 * {@link #startManagingCursor}, stop the activity's management of that
2062 * cursor.
RoboErik55011652014-07-09 15:05:53 -07002063 *
Joe Fernandez8ef69702011-09-02 14:32:22 -07002064 * <p><strong>Warning:</strong> After calling this method on a cursor from a managed query,
RoboErik55011652014-07-09 15:05:53 -07002065 * the system <em>will not</em> automatically close the cursor and you must call
Joe Fernandez8ef69702011-09-02 14:32:22 -07002066 * {@link Cursor#close()}.</p>
RoboErik55011652014-07-09 15:05:53 -07002067 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 * @param c The Cursor that was being managed.
RoboErik55011652014-07-09 15:05:53 -07002069 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 * @see #startManagingCursor
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07002071 *
2072 * @deprecated Use the new {@link android.content.CursorLoader} class with
2073 * {@link LoaderManager} instead; this is also
2074 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 */
Jason parks6ed50de2010-08-25 10:18:50 -05002076 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 public void stopManagingCursor(Cursor c) {
2078 synchronized (mManagedCursors) {
2079 final int N = mManagedCursors.size();
2080 for (int i=0; i<N; i++) {
2081 ManagedCursor mc = mManagedCursors.get(i);
2082 if (mc.mCursor == c) {
2083 mManagedCursors.remove(i);
2084 break;
2085 }
2086 }
2087 }
2088 }
2089
2090 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07002091 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
2092 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08002093 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07002095 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
2098
2099 /**
2100 * Finds a view that was identified by the id attribute from the XML that
2101 * was processed in {@link #onCreate}.
2102 *
2103 * @return The view if found or null otherwise.
2104 */
Scott Kennedyc0519552015-02-11 15:33:10 -08002105 @Nullable
Tor Norbye7b9c9122013-05-30 16:48:33 -07002106 public View findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 return getWindow().findViewById(id);
2108 }
RoboErik55011652014-07-09 15:05:53 -07002109
Adam Powell33b97432010-04-20 10:01:14 -07002110 /**
2111 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07002112 *
Adam Powell33b97432010-04-20 10:01:14 -07002113 * @return The Activity's ActionBar, or null if it does not have one.
2114 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002115 @Nullable
Adam Powell33b97432010-04-20 10:01:14 -07002116 public ActionBar getActionBar() {
Adam Powelle43340c2014-03-17 19:10:43 -07002117 initWindowDecorActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07002118 return mActionBar;
2119 }
Adam Powelle43340c2014-03-17 19:10:43 -07002120
2121 /**
2122 * Set a {@link android.widget.Toolbar Toolbar} to act as the {@link ActionBar} for this
2123 * Activity window.
2124 *
2125 * <p>When set to a non-null value the {@link #getActionBar()} method will return
2126 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
2127 * a traditional window decor action bar. The toolbar's menu will be populated with the
2128 * Activity's options menu and the navigation button will be wired through the standard
2129 * {@link android.R.id#home home} menu select action.</p>
2130 *
2131 * <p>In order to use a Toolbar within the Activity's window content the application
2132 * must not request the window feature {@link Window#FEATURE_ACTION_BAR FEATURE_ACTION_BAR}.</p>
2133 *
Adam Powell37780142014-06-01 13:31:00 -07002134 * @param toolbar Toolbar to set as the Activity's action bar
Adam Powelle43340c2014-03-17 19:10:43 -07002135 */
Adam Powell37780142014-06-01 13:31:00 -07002136 public void setActionBar(@Nullable Toolbar toolbar) {
Adam Powelle43340c2014-03-17 19:10:43 -07002137 if (getActionBar() instanceof WindowDecorActionBar) {
2138 throw new IllegalStateException("This Activity already has an action bar supplied " +
2139 "by the window decor. Do not request Window.FEATURE_ACTION_BAR and set " +
2140 "android:windowActionBar to false in your theme to use a Toolbar instead.");
2141 }
Adam Powell14e1afe2014-08-18 15:58:23 -07002142 ToolbarActionBar tbab = new ToolbarActionBar(toolbar, getTitle(), this);
2143 mActionBar = tbab;
2144 mWindow.setCallback(tbab.getWrappedWindowCallback());
Adam Powell07a74542014-05-30 15:52:44 -07002145 mActionBar.invalidateOptionsMenu();
Adam Powelle43340c2014-03-17 19:10:43 -07002146 }
RoboErik55011652014-07-09 15:05:53 -07002147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 /**
Adam Powell33b97432010-04-20 10:01:14 -07002149 * Creates a new ActionBar, locates the inflated ActionBarView,
2150 * initializes the ActionBar with the view, and sets mActionBar.
2151 */
Adam Powelle43340c2014-03-17 19:10:43 -07002152 private void initWindowDecorActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07002153 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07002154
2155 // Initializing the window decor can change window feature flags.
2156 // Make sure that we have the correct set before performing the test below.
2157 window.getDecorView();
2158
Adam Powell9b4c8042010-08-10 15:36:44 -07002159 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07002160 return;
2161 }
Adam Powell04fe6eb2013-05-31 14:39:48 -07002162
Adam Powelle43340c2014-03-17 19:10:43 -07002163 mActionBar = new WindowDecorActionBar(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07002164 mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
Adam Powell04fe6eb2013-05-31 14:39:48 -07002165
2166 mWindow.setDefaultIcon(mActivityInfo.getIconResource());
2167 mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
Adam Powell33b97432010-04-20 10:01:14 -07002168 }
RoboErik55011652014-07-09 15:05:53 -07002169
Adam Powell33b97432010-04-20 10:01:14 -07002170 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 * Set the activity content from a layout resource. The resource will be
2172 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08002173 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 * @param layoutResID Resource ID to be inflated.
RoboErik55011652014-07-09 15:05:53 -07002175 *
Romain Guy482b34a62011-01-20 10:59:28 -08002176 * @see #setContentView(android.view.View)
2177 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002179 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 getWindow().setContentView(layoutResID);
Adam Powelle43340c2014-03-17 19:10:43 -07002181 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 }
2183
2184 /**
2185 * Set the activity content to an explicit view. This view is placed
2186 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002187 * view hierarchy. When calling this method, the layout parameters of the
2188 * specified view are ignored. Both the width and the height of the view are
2189 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
2190 * your own layout parameters, invoke
2191 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
2192 * instead.
RoboErik55011652014-07-09 15:05:53 -07002193 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08002195 *
2196 * @see #setContentView(int)
2197 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 */
2199 public void setContentView(View view) {
2200 getWindow().setContentView(view);
Adam Powelle43340c2014-03-17 19:10:43 -07002201 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 }
2203
2204 /**
2205 * Set the activity content to an explicit view. This view is placed
2206 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08002207 * view hierarchy.
RoboErik55011652014-07-09 15:05:53 -07002208 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 * @param view The desired content to display.
2210 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08002211 *
2212 * @see #setContentView(android.view.View)
2213 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 */
2215 public void setContentView(View view, ViewGroup.LayoutParams params) {
2216 getWindow().setContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002217 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 }
2219
2220 /**
2221 * Add an additional content view to the activity. Added after any existing
2222 * ones in the activity -- existing views are NOT removed.
RoboErik55011652014-07-09 15:05:53 -07002223 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 * @param view The desired content to display.
2225 * @param params Layout parameters for the view.
2226 */
2227 public void addContentView(View view, ViewGroup.LayoutParams params) {
2228 getWindow().addContentView(view, params);
Adam Powelle43340c2014-03-17 19:10:43 -07002229 initWindowDecorActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 }
2231
2232 /**
Adam Powellcfbe9be2013-11-06 14:58:58 -08002233 * Retrieve the {@link TransitionManager} responsible for default transitions in this window.
2234 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2235 *
2236 * <p>This method will return non-null after content has been initialized (e.g. by using
2237 * {@link #setContentView}) if {@link Window#FEATURE_CONTENT_TRANSITIONS} has been granted.</p>
2238 *
2239 * @return This window's content TransitionManager or null if none is set.
2240 */
2241 public TransitionManager getContentTransitionManager() {
2242 return getWindow().getTransitionManager();
2243 }
2244
2245 /**
2246 * Set the {@link TransitionManager} to use for default transitions in this window.
2247 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2248 *
2249 * @param tm The TransitionManager to use for scene changes.
2250 */
2251 public void setContentTransitionManager(TransitionManager tm) {
2252 getWindow().setTransitionManager(tm);
2253 }
2254
2255 /**
2256 * Retrieve the {@link Scene} representing this window's current content.
2257 * Requires {@link Window#FEATURE_CONTENT_TRANSITIONS}.
2258 *
2259 * <p>This method will return null if the current content is not represented by a Scene.</p>
2260 *
2261 * @return Current Scene being shown or null
2262 */
2263 public Scene getContentScene() {
2264 return getWindow().getContentScene();
2265 }
2266
2267 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002268 * Sets whether this activity is finished when touched outside its window's
2269 * bounds.
2270 */
2271 public void setFinishOnTouchOutside(boolean finish) {
2272 mWindow.setCloseOnTouchOutside(finish);
2273 }
Tor Norbyed9273d62013-05-30 15:59:53 -07002274
2275 /** @hide */
2276 @IntDef({
2277 DEFAULT_KEYS_DISABLE,
2278 DEFAULT_KEYS_DIALER,
2279 DEFAULT_KEYS_SHORTCUT,
2280 DEFAULT_KEYS_SEARCH_LOCAL,
2281 DEFAULT_KEYS_SEARCH_GLOBAL})
2282 @Retention(RetentionPolicy.SOURCE)
2283 @interface DefaultKeyMode {}
2284
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 * Use with {@link #setDefaultKeyMode} to turn off default handling of
2287 * keys.
RoboErik55011652014-07-09 15:05:53 -07002288 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 * @see #setDefaultKeyMode
2290 */
2291 static public final int DEFAULT_KEYS_DISABLE = 0;
2292 /**
2293 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
2294 * key handling.
RoboErik55011652014-07-09 15:05:53 -07002295 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 * @see #setDefaultKeyMode
2297 */
2298 static public final int DEFAULT_KEYS_DIALER = 1;
2299 /**
2300 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
2301 * default key handling.
RoboErik55011652014-07-09 15:05:53 -07002302 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
RoboErik55011652014-07-09 15:05:53 -07002304 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 * @see #setDefaultKeyMode
2306 */
2307 static public final int DEFAULT_KEYS_SHORTCUT = 2;
2308 /**
2309 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2310 * will start an application-defined search. (If the application or activity does not
2311 * actually define a search, the the keys will be ignored.)
RoboErik55011652014-07-09 15:05:53 -07002312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002314 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 * @see #setDefaultKeyMode
2316 */
2317 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
2318
2319 /**
2320 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
2321 * will start a global search (typically web search, but some platforms may define alternate
2322 * methods for global search)
RoboErik55011652014-07-09 15:05:53 -07002323 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
RoboErik55011652014-07-09 15:05:53 -07002325 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 * @see #setDefaultKeyMode
2327 */
2328 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
2329
2330 /**
2331 * Select the default key handling for this activity. This controls what
2332 * will happen to key events that are not otherwise handled. The default
2333 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
2334 * floor. Other modes allow you to launch the dialer
2335 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
2336 * menu without requiring the menu key be held down
RoboErik55011652014-07-09 15:05:53 -07002337 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
RoboErik55011652014-07-09 15:05:53 -07002339 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 * <p>Note that the mode selected here does not impact the default
2341 * handling of system keys, such as the "back" and "menu" keys, and your
2342 * activity and its views always get a first chance to receive and handle
2343 * all application keys.
RoboErik55011652014-07-09 15:05:53 -07002344 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 * @param mode The desired default key mode constant.
RoboErik55011652014-07-09 15:05:53 -07002346 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 * @see #DEFAULT_KEYS_DISABLE
2348 * @see #DEFAULT_KEYS_DIALER
2349 * @see #DEFAULT_KEYS_SHORTCUT
2350 * @see #DEFAULT_KEYS_SEARCH_LOCAL
2351 * @see #DEFAULT_KEYS_SEARCH_GLOBAL
2352 * @see #onKeyDown
2353 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002354 public final void setDefaultKeyMode(@DefaultKeyMode int mode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 mDefaultKeyMode = mode;
RoboErik55011652014-07-09 15:05:53 -07002356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 // Some modes use a SpannableStringBuilder to track & dispatch input events
2358 // This list must remain in sync with the switch in onKeyDown()
2359 switch (mode) {
2360 case DEFAULT_KEYS_DISABLE:
2361 case DEFAULT_KEYS_SHORTCUT:
2362 mDefaultKeySsb = null; // not used in these modes
2363 break;
2364 case DEFAULT_KEYS_DIALER:
2365 case DEFAULT_KEYS_SEARCH_LOCAL:
2366 case DEFAULT_KEYS_SEARCH_GLOBAL:
2367 mDefaultKeySsb = new SpannableStringBuilder();
2368 Selection.setSelection(mDefaultKeySsb,0);
2369 break;
2370 default:
2371 throw new IllegalArgumentException();
2372 }
2373 }
2374
2375 /**
2376 * Called when a key was pressed down and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002377 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 * is inside a TextView will not trigger the event (unless it is a navigation
2379 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002380 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 * <p>If the focused view didn't want this event, this method is called.
2382 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002383 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
2384 * by calling {@link #onBackPressed()}, though the behavior varies based
2385 * on the application compatibility mode: for
2386 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
2387 * it will set up the dispatch to call {@link #onKeyUp} where the action
2388 * will be performed; for earlier applications, it will perform the
2389 * action immediately in on-down, as those versions of the platform
2390 * behaved.
RoboErik55011652014-07-09 15:05:53 -07002391 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07002392 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002393 * if configured with {@link #setDefaultKeyMode}.
RoboErik55011652014-07-09 15:05:53 -07002394 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002396 * further, or <code>false</code> to indicate that you have not handled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 * this event and it should continue to be propagated.
2398 * @see #onKeyUp
2399 * @see android.view.KeyEvent
2400 */
2401 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002402 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002403 if (getApplicationInfo().targetSdkVersion
2404 >= Build.VERSION_CODES.ECLAIR) {
2405 event.startTracking();
2406 } else {
2407 onBackPressed();
2408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 return true;
2410 }
RoboErik55011652014-07-09 15:05:53 -07002411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
2413 return false;
2414 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Jose Lima7a22fc62015-01-23 17:24:22 -08002415 Window w = getWindow();
2416 if (w.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
2417 w.performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, keyCode, event,
2418 Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002419 return true;
2420 }
2421 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 } else {
2423 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
2424 boolean clearSpannable = false;
2425 boolean handled;
2426 if ((event.getRepeatCount() != 0) || event.isSystem()) {
2427 clearSpannable = true;
2428 handled = false;
2429 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002430 handled = TextKeyListener.getInstance().onKeyDown(
2431 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 if (handled && mDefaultKeySsb.length() > 0) {
2433 // something useable has been typed - dispatch it now.
2434
2435 final String str = mDefaultKeySsb.toString();
2436 clearSpannable = true;
RoboErik55011652014-07-09 15:05:53 -07002437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 switch (mDefaultKeyMode) {
2439 case DEFAULT_KEYS_DIALER:
2440 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
2441 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RoboErik55011652014-07-09 15:05:53 -07002442 startActivity(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 break;
2444 case DEFAULT_KEYS_SEARCH_LOCAL:
2445 startSearch(str, false, null, false);
2446 break;
2447 case DEFAULT_KEYS_SEARCH_GLOBAL:
2448 startSearch(str, false, null, true);
2449 break;
2450 }
2451 }
2452 }
2453 if (clearSpannable) {
2454 mDefaultKeySsb.clear();
2455 mDefaultKeySsb.clearSpans();
2456 Selection.setSelection(mDefaultKeySsb,0);
2457 }
2458 return handled;
2459 }
2460 }
2461
2462 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002463 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
2464 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
2465 * the event).
2466 */
2467 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2468 return false;
2469 }
2470
2471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 * Called when a key was released and not handled by any of the views
RoboErik55011652014-07-09 15:05:53 -07002473 * inside of the activity. So, for example, key presses while the cursor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 * is inside a TextView will not trigger the event (unless it is a navigation
2475 * to another object) because TextView handles its own key presses.
RoboErik55011652014-07-09 15:05:53 -07002476 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002477 * <p>The default implementation handles KEYCODE_BACK to stop the activity
2478 * and go back.
RoboErik55011652014-07-09 15:05:53 -07002479 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 * @return Return <code>true</code> to prevent this event from being propagated
RoboErik55011652014-07-09 15:05:53 -07002481 * further, or <code>false</code> to indicate that you have not handled
2482 * this event and it should continue to be propagated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 * @see #onKeyDown
2484 * @see KeyEvent
2485 */
2486 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002487 if (getApplicationInfo().targetSdkVersion
2488 >= Build.VERSION_CODES.ECLAIR) {
2489 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
2490 && !event.isCanceled()) {
2491 onBackPressed();
2492 return true;
2493 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 return false;
2496 }
2497
2498 /**
2499 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
2500 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
2501 * the event).
2502 */
2503 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2504 return false;
2505 }
RoboErik55011652014-07-09 15:05:53 -07002506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002508 * Called when the activity has detected the user's press of the back
2509 * key. The default implementation simply finishes the current activity,
2510 * but you can override this to do whatever you want.
2511 */
2512 public void onBackPressed() {
Adam Powell07a74542014-05-30 15:52:44 -07002513 if (mActionBar != null && mActionBar.collapseActionView()) {
2514 return;
2515 }
2516
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08002517 if (!mFragments.popBackStackImmediate()) {
Craig Mautner73f843d2014-05-19 09:42:28 -07002518 finishAfterTransition();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07002519 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002520 }
Jeff Brown64da12a2011-01-04 19:57:47 -08002521
2522 /**
2523 * Called when a key shortcut event is not handled by any of the views in the Activity.
2524 * Override this method to implement global key shortcuts for the Activity.
2525 * Key shortcuts can also be implemented by setting the
2526 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
2527 *
2528 * @param keyCode The value in event.getKeyCode().
2529 * @param event Description of the key event.
2530 * @return True if the key shortcut was handled.
2531 */
2532 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2533 return false;
2534 }
2535
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002536 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 * Called when a touch screen event was not handled by any of the views
2538 * under it. This is most useful to process touch events that happen
2539 * outside of your window bounds, where there is no view to receive it.
RoboErik55011652014-07-09 15:05:53 -07002540 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 * @param event The touch screen event being processed.
RoboErik55011652014-07-09 15:05:53 -07002542 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 * @return Return true if you have consumed the event, false if you haven't.
2544 * The default implementation always returns false.
2545 */
2546 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002547 if (mWindow.shouldCloseOnTouch(this, event)) {
2548 finish();
2549 return true;
2550 }
RoboErik55011652014-07-09 15:05:53 -07002551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 return false;
2553 }
RoboErik55011652014-07-09 15:05:53 -07002554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 /**
2556 * Called when the trackball was moved and not handled by any of the
2557 * views inside of the activity. So, for example, if the trackball moves
2558 * while focus is on a button, you will receive a call here because
2559 * buttons do not normally do anything with trackball events. The call
2560 * here happens <em>before</em> trackball movements are converted to
2561 * DPAD key events, which then get sent back to the view hierarchy, and
2562 * will be processed at the point for things like focus navigation.
RoboErik55011652014-07-09 15:05:53 -07002563 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 * @param event The trackball event being processed.
RoboErik55011652014-07-09 15:05:53 -07002565 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 * @return Return true if you have consumed the event, false if you haven't.
2567 * The default implementation always returns false.
2568 */
2569 public boolean onTrackballEvent(MotionEvent event) {
2570 return false;
2571 }
Jeff Browncb1404e2011-01-15 18:14:15 -08002572
2573 /**
2574 * Called when a generic motion event was not handled by any of the
2575 * views inside of the activity.
2576 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08002577 * Generic motion events describe joystick movements, mouse hovers, track pad
2578 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08002579 * {@link MotionEvent#getSource() source} of the motion event specifies
2580 * the class of input that was received. Implementations of this method
2581 * must examine the bits in the source before processing the event.
2582 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08002583 * </p><p>
2584 * Generic motion events with source class
2585 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
2586 * are delivered to the view under the pointer. All other generic motion events are
2587 * delivered to the focused view.
2588 * </p><p>
2589 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
2590 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08002591 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08002592 *
2593 * @param event The generic motion event being processed.
2594 *
2595 * @return Return true if you have consumed the event, false if you haven't.
2596 * The default implementation always returns false.
2597 */
2598 public boolean onGenericMotionEvent(MotionEvent event) {
2599 return false;
2600 }
2601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 /**
2603 * Called whenever a key, touch, or trackball event is dispatched to the
2604 * activity. Implement this method if you wish to know that the user has
2605 * interacted with the device in some way while your activity is running.
2606 * This callback and {@link #onUserLeaveHint} are intended to help
2607 * activities manage status bar notifications intelligently; specifically,
2608 * for helping activities determine the proper time to cancel a notfication.
RoboErik55011652014-07-09 15:05:53 -07002609 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
2611 * be accompanied by calls to {@link #onUserInteraction}. This
2612 * ensures that your activity will be told of relevant user activity such
2613 * as pulling down the notification pane and touching an item there.
RoboErik55011652014-07-09 15:05:53 -07002614 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 * <p>Note that this callback will be invoked for the touch down action
2616 * that begins a touch gesture, but may not be invoked for the touch-moved
2617 * and touch-up actions that follow.
RoboErik55011652014-07-09 15:05:53 -07002618 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 * @see #onUserLeaveHint()
2620 */
2621 public void onUserInteraction() {
2622 }
RoboErik55011652014-07-09 15:05:53 -07002623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
2625 // Update window manager if: we have a view, that view is
2626 // attached to its parent (which will be a RootView), and
2627 // this activity is not embedded.
2628 if (mParent == null) {
2629 View decor = mDecor;
2630 if (decor != null && decor.getParent() != null) {
2631 getWindowManager().updateViewLayout(decor, params);
2632 }
2633 }
2634 }
2635
2636 public void onContentChanged() {
2637 }
2638
2639 /**
2640 * Called when the current {@link Window} of the activity gains or loses
2641 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002642 * to the user. The default implementation clears the key tracking
2643 * state, so should always be called.
RoboErik55011652014-07-09 15:05:53 -07002644 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002645 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 * is managed independently of activity lifecycles. As such, while focus
2647 * changes will generally have some relation to lifecycle changes (an
2648 * activity that is stopped will not generally get window focus), you
2649 * should not rely on any particular order between the callbacks here and
2650 * those in the other lifecycle methods such as {@link #onResume}.
RoboErik55011652014-07-09 15:05:53 -07002651 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 * <p>As a general rule, however, a resumed activity will have window
2653 * focus... unless it has displayed other dialogs or popups that take
2654 * input focus, in which case the activity itself will not have focus
2655 * when the other windows have it. Likewise, the system may display
2656 * system-level windows (such as the status bar notification panel or
2657 * a system alert) which will temporarily take window input focus without
2658 * pausing the foreground activity.
2659 *
2660 * @param hasFocus Whether the window of this activity has focus.
RoboErik55011652014-07-09 15:05:53 -07002661 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 * @see #hasWindowFocus()
2663 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002664 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 */
2666 public void onWindowFocusChanged(boolean hasFocus) {
2667 }
RoboErik55011652014-07-09 15:05:53 -07002668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002670 * Called when the main window associated with the activity has been
2671 * attached to the window manager.
2672 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
2673 * for more information.
2674 * @see View#onAttachedToWindow
2675 */
2676 public void onAttachedToWindow() {
2677 }
RoboErik55011652014-07-09 15:05:53 -07002678
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002679 /**
2680 * Called when the main window associated with the activity has been
2681 * detached from the window manager.
2682 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
2683 * for more information.
2684 * @see View#onDetachedFromWindow
2685 */
2686 public void onDetachedFromWindow() {
2687 }
RoboErik55011652014-07-09 15:05:53 -07002688
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002689 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 * Returns true if this activity's <em>main</em> window currently has window focus.
2691 * Note that this is not the same as the view itself having focus.
RoboErik55011652014-07-09 15:05:53 -07002692 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 * @return True if this activity's main window currently has window focus.
RoboErik55011652014-07-09 15:05:53 -07002694 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
2696 */
2697 public boolean hasWindowFocus() {
2698 Window w = getWindow();
2699 if (w != null) {
2700 View d = w.getDecorView();
2701 if (d != null) {
2702 return d.hasWindowFocus();
2703 }
2704 }
2705 return false;
2706 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002707
2708 /**
2709 * Called when the main window associated with the activity has been dismissed.
Adam Powell117b6952014-05-05 18:14:56 -07002710 * @hide
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002711 */
Adam Powell117b6952014-05-05 18:14:56 -07002712 @Override
Will Haldean Brownca6234e2014-02-12 10:23:41 -08002713 public void onWindowDismissed() {
2714 finish();
2715 }
RoboErik55011652014-07-09 15:05:53 -07002716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 /**
RoboErik55011652014-07-09 15:05:53 -07002718 * Called to process key events. You can override this to intercept all
2719 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 * this implementation for key events that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07002721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 * @param event The key event.
RoboErik55011652014-07-09 15:05:53 -07002723 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 * @return boolean Return true if this event was consumed.
2725 */
2726 public boolean dispatchKeyEvent(KeyEvent event) {
2727 onUserInteraction();
Adam Powell07a74542014-05-30 15:52:44 -07002728
2729 // Let action bars open menus in response to the menu key prioritized over
2730 // the window handling it
2731 if (event.getKeyCode() == KeyEvent.KEYCODE_MENU &&
2732 mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
2733 return true;
2734 }
2735
Dianne Hackborn8d374262009-09-14 21:21:52 -07002736 Window win = getWindow();
2737 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 return true;
2739 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002740 View decor = mDecor;
2741 if (decor == null) decor = win.getDecorView();
2742 return event.dispatch(this, decor != null
2743 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
2745
2746 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08002747 * Called to process a key shortcut event.
2748 * You can override this to intercept all key shortcut events before they are
2749 * dispatched to the window. Be sure to call this implementation for key shortcut
2750 * events that should be handled normally.
2751 *
2752 * @param event The key shortcut event.
2753 * @return True if this event was consumed.
2754 */
2755 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
2756 onUserInteraction();
2757 if (getWindow().superDispatchKeyShortcutEvent(event)) {
2758 return true;
2759 }
2760 return onKeyShortcut(event.getKeyCode(), event);
2761 }
2762
2763 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 * Called to process touch screen events. You can override this to
2765 * intercept all touch screen events before they are dispatched to the
2766 * window. Be sure to call this implementation for touch screen events
2767 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07002768 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 * @param ev The touch screen event.
RoboErik55011652014-07-09 15:05:53 -07002770 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 * @return boolean Return true if this event was consumed.
2772 */
2773 public boolean dispatchTouchEvent(MotionEvent ev) {
2774 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
2775 onUserInteraction();
2776 }
2777 if (getWindow().superDispatchTouchEvent(ev)) {
2778 return true;
2779 }
2780 return onTouchEvent(ev);
2781 }
RoboErik55011652014-07-09 15:05:53 -07002782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 /**
2784 * Called to process trackball events. You can override this to
2785 * intercept all trackball events before they are dispatched to the
2786 * window. Be sure to call this implementation for trackball events
2787 * that should be handled normally.
RoboErik55011652014-07-09 15:05:53 -07002788 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 * @param ev The trackball event.
RoboErik55011652014-07-09 15:05:53 -07002790 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 * @return boolean Return true if this event was consumed.
2792 */
2793 public boolean dispatchTrackballEvent(MotionEvent ev) {
2794 onUserInteraction();
2795 if (getWindow().superDispatchTrackballEvent(ev)) {
2796 return true;
2797 }
2798 return onTrackballEvent(ev);
2799 }
svetoslavganov75986cf2009-05-14 22:28:01 -07002800
Jeff Browncb1404e2011-01-15 18:14:15 -08002801 /**
2802 * Called to process generic motion events. You can override this to
2803 * intercept all generic motion events before they are dispatched to the
2804 * window. Be sure to call this implementation for generic motion events
2805 * that should be handled normally.
2806 *
2807 * @param ev The generic motion event.
2808 *
2809 * @return boolean Return true if this event was consumed.
2810 */
2811 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
2812 onUserInteraction();
2813 if (getWindow().superDispatchGenericMotionEvent(ev)) {
2814 return true;
2815 }
2816 return onGenericMotionEvent(ev);
2817 }
2818
svetoslavganov75986cf2009-05-14 22:28:01 -07002819 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2820 event.setClassName(getClass().getName());
2821 event.setPackageName(getPackageName());
2822
2823 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08002824 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
2825 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07002826 event.setFullScreen(isFullScreen);
2827
2828 CharSequence title = getTitle();
2829 if (!TextUtils.isEmpty(title)) {
2830 event.getText().add(title);
2831 }
2832
2833 return true;
2834 }
2835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 /**
2837 * Default implementation of
2838 * {@link android.view.Window.Callback#onCreatePanelView}
2839 * for activities. This
2840 * simply returns null so that all panel sub-windows will have the default
2841 * menu behavior.
2842 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002843 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 public View onCreatePanelView(int featureId) {
2845 return null;
2846 }
2847
2848 /**
2849 * Default implementation of
2850 * {@link android.view.Window.Callback#onCreatePanelMenu}
2851 * for activities. This calls through to the new
2852 * {@link #onCreateOptionsMenu} method for the
2853 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
2854 * so that subclasses of Activity don't need to deal with feature codes.
2855 */
2856 public boolean onCreatePanelMenu(int featureId, Menu menu) {
2857 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002858 boolean show = onCreateOptionsMenu(menu);
2859 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
2860 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 }
2862 return false;
2863 }
2864
2865 /**
2866 * Default implementation of
2867 * {@link android.view.Window.Callback#onPreparePanel}
2868 * for activities. This
2869 * calls through to the new {@link #onPrepareOptionsMenu} method for the
2870 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
2871 * panel, so that subclasses of
2872 * Activity don't need to deal with feature codes.
2873 */
2874 public boolean onPreparePanel(int featureId, View view, Menu menu) {
2875 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
2876 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002877 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
Adam Powellef31e7c2012-05-08 09:59:40 -07002878 return goforit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 }
2880 return true;
2881 }
2882
2883 /**
2884 * {@inheritDoc}
RoboErik55011652014-07-09 15:05:53 -07002885 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 * @return The default implementation returns true.
2887 */
2888 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08002889 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powelle43340c2014-03-17 19:10:43 -07002890 initWindowDecorActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08002891 if (mActionBar != null) {
2892 mActionBar.dispatchMenuVisibilityChanged(true);
2893 } else {
2894 Log.e(TAG, "Tried to open action bar menu with no action bar");
2895 }
Adam Powell8515ee82010-11-30 14:09:55 -08002896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 return true;
2898 }
2899
2900 /**
2901 * Default implementation of
2902 * {@link android.view.Window.Callback#onMenuItemSelected}
2903 * for activities. This calls through to the new
2904 * {@link #onOptionsItemSelected} method for the
2905 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
2906 * panel, so that subclasses of
2907 * Activity don't need to deal with feature codes.
2908 */
2909 public boolean onMenuItemSelected(int featureId, MenuItem item) {
zobject686a8052012-12-14 21:11:08 +09002910 CharSequence titleCondensed = item.getTitleCondensed();
2911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 switch (featureId) {
2913 case Window.FEATURE_OPTIONS_PANEL:
2914 // Put event logging here so it gets called even if subclass
2915 // doesn't call through to superclass's implmeentation of each
2916 // of these methods below
zobject686a8052012-12-14 21:11:08 +09002917 if(titleCondensed != null) {
2918 EventLog.writeEvent(50000, 0, titleCondensed.toString());
2919 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002920 if (onOptionsItemSelected(item)) {
2921 return true;
2922 }
Adam Powell04d58112012-04-09 10:22:12 -07002923 if (mFragments.dispatchOptionsItemSelected(item)) {
2924 return true;
2925 }
2926 if (item.getItemId() == android.R.id.home && mActionBar != null &&
2927 (mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
2928 if (mParent == null) {
Adam Powell07304f5292012-05-07 17:32:44 -07002929 return onNavigateUp();
Adam Powell04d58112012-04-09 10:22:12 -07002930 } else {
Adam Powell07304f5292012-05-07 17:32:44 -07002931 return mParent.onNavigateUpFromChild(this);
Adam Powell04d58112012-04-09 10:22:12 -07002932 }
Adam Powell04d58112012-04-09 10:22:12 -07002933 }
2934 return false;
RoboErik55011652014-07-09 15:05:53 -07002935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 case Window.FEATURE_CONTEXT_MENU:
zobject686a8052012-12-14 21:11:08 +09002937 if(titleCondensed != null) {
2938 EventLog.writeEvent(50000, 1, titleCondensed.toString());
2939 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07002940 if (onContextItemSelected(item)) {
2941 return true;
2942 }
2943 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08002944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 default:
2946 return false;
2947 }
2948 }
RoboErik55011652014-07-09 15:05:53 -07002949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 /**
2951 * Default implementation of
2952 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
2953 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
2954 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
2955 * so that subclasses of Activity don't need to deal with feature codes.
2956 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
2957 * {@link #onContextMenuClosed(Menu)} will be called.
2958 */
2959 public void onPanelClosed(int featureId, Menu menu) {
2960 switch (featureId) {
2961 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002962 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 onOptionsMenuClosed(menu);
2964 break;
RoboErik55011652014-07-09 15:05:53 -07002965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 case Window.FEATURE_CONTEXT_MENU:
2967 onContextMenuClosed(menu);
2968 break;
Adam Powell8515ee82010-11-30 14:09:55 -08002969
2970 case Window.FEATURE_ACTION_BAR:
Adam Powelle43340c2014-03-17 19:10:43 -07002971 initWindowDecorActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08002972 mActionBar.dispatchMenuVisibilityChanged(false);
2973 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 }
2975 }
2976
2977 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002978 * Declare that the options menu has changed, so should be recreated.
2979 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
2980 * time it needs to be displayed.
2981 */
2982 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08002983 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
2984 (mActionBar == null || !mActionBar.invalidateOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07002985 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
2986 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002987 }
RoboErik55011652014-07-09 15:05:53 -07002988
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002989 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 * Initialize the contents of the Activity's standard options menu. You
2991 * should place your menu items in to <var>menu</var>.
RoboErik55011652014-07-09 15:05:53 -07002992 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 * <p>This is only called once, the first time the options menu is
2994 * displayed. To update the menu every time it is displayed, see
2995 * {@link #onPrepareOptionsMenu}.
RoboErik55011652014-07-09 15:05:53 -07002996 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 * <p>The default implementation populates the menu with standard system
RoboErik55011652014-07-09 15:05:53 -07002998 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
2999 * they will be correctly ordered with application-defined menu items.
3000 * Deriving classes should always call through to the base implementation.
3001 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 * <p>You can safely hold on to <var>menu</var> (and any items created
3003 * from it), making modifications to it as desired, until the next
3004 * time onCreateOptionsMenu() is called.
RoboErik55011652014-07-09 15:05:53 -07003005 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 * <p>When you add items to the menu, you can implement the Activity's
3007 * {@link #onOptionsItemSelected} method to handle them there.
RoboErik55011652014-07-09 15:05:53 -07003008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 * @param menu The options menu in which you place your items.
RoboErik55011652014-07-09 15:05:53 -07003010 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 * @return You must return true for the menu to be displayed;
3012 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003013 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 * @see #onPrepareOptionsMenu
3015 * @see #onOptionsItemSelected
3016 */
3017 public boolean onCreateOptionsMenu(Menu menu) {
3018 if (mParent != null) {
3019 return mParent.onCreateOptionsMenu(menu);
3020 }
3021 return true;
3022 }
3023
3024 /**
3025 * Prepare the Screen's standard options menu to be displayed. This is
3026 * called right before the menu is shown, every time it is shown. You can
3027 * use this method to efficiently enable/disable items or otherwise
3028 * dynamically modify the contents.
RoboErik55011652014-07-09 15:05:53 -07003029 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 * <p>The default implementation updates the system menu items based on the
3031 * activity's state. Deriving classes should always call through to the
3032 * base class implementation.
RoboErik55011652014-07-09 15:05:53 -07003033 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 * @param menu The options menu as last shown or first initialized by
3035 * onCreateOptionsMenu().
RoboErik55011652014-07-09 15:05:53 -07003036 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 * @return You must return true for the menu to be displayed;
3038 * if you return false it will not be shown.
RoboErik55011652014-07-09 15:05:53 -07003039 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 * @see #onCreateOptionsMenu
3041 */
3042 public boolean onPrepareOptionsMenu(Menu menu) {
3043 if (mParent != null) {
3044 return mParent.onPrepareOptionsMenu(menu);
3045 }
3046 return true;
3047 }
3048
3049 /**
3050 * This hook is called whenever an item in your options menu is selected.
3051 * The default implementation simply returns false to have the normal
3052 * processing happen (calling the item's Runnable or sending a message to
3053 * its Handler as appropriate). You can use this method for any items
3054 * for which you would like to do processing without those other
3055 * facilities.
RoboErik55011652014-07-09 15:05:53 -07003056 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 * <p>Derived classes should call through to the base class for it to
Adam Powelldd8fab22012-03-22 17:47:27 -07003058 * perform the default menu handling.</p>
RoboErik55011652014-07-09 15:05:53 -07003059 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 * @param item The menu item that was selected.
RoboErik55011652014-07-09 15:05:53 -07003061 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 * @return boolean Return false to allow normal menu processing to
3063 * proceed, true to consume it here.
RoboErik55011652014-07-09 15:05:53 -07003064 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 * @see #onCreateOptionsMenu
3066 */
3067 public boolean onOptionsItemSelected(MenuItem item) {
3068 if (mParent != null) {
3069 return mParent.onOptionsItemSelected(item);
3070 }
3071 return false;
3072 }
3073
3074 /**
Adam Powelldd8fab22012-03-22 17:47:27 -07003075 * This method is called whenever the user chooses to navigate Up within your application's
3076 * activity hierarchy from the action bar.
3077 *
3078 * <p>If the attribute {@link android.R.attr#parentActivityName parentActivityName}
3079 * was specified in the manifest for this activity or an activity-alias to it,
3080 * default Up navigation will be handled automatically. If any activity
3081 * along the parent chain requires extra Intent arguments, the Activity subclass
3082 * should override the method {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}
3083 * to supply those arguments.</p>
3084 *
3085 * <p>See <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
3086 * from the developer guide and <a href="{@docRoot}design/patterns/navigation.html">Navigation</a>
3087 * from the design guide for more information about navigating within your app.</p>
3088 *
3089 * <p>See the {@link TaskStackBuilder} class and the Activity methods
3090 * {@link #getParentActivityIntent()}, {@link #shouldUpRecreateTask(Intent)}, and
3091 * {@link #navigateUpTo(Intent)} for help implementing custom Up navigation.
3092 * The AppNavigation sample application in the Android SDK is also available for reference.</p>
3093 *
3094 * @return true if Up navigation completed successfully and this Activity was finished,
3095 * false otherwise.
3096 */
3097 public boolean onNavigateUp() {
3098 // Automatically handle hierarchical Up navigation if the proper
3099 // metadata is available.
3100 Intent upIntent = getParentActivityIntent();
3101 if (upIntent != null) {
Adam Powell0fc5b2b2012-07-18 18:20:29 -07003102 if (mActivityInfo.taskAffinity == null) {
3103 // Activities with a null affinity are special; they really shouldn't
3104 // specify a parent activity intent in the first place. Just finish
3105 // the current activity and call it a day.
3106 finish();
3107 } else if (shouldUpRecreateTask(upIntent)) {
Adam Powellf78a8442012-05-01 18:09:32 -07003108 TaskStackBuilder b = TaskStackBuilder.create(this);
Adam Powelldd8fab22012-03-22 17:47:27 -07003109 onCreateNavigateUpTaskStack(b);
3110 onPrepareNavigateUpTaskStack(b);
3111 b.startActivities();
Adam Powell3d193d92012-05-15 16:51:55 -07003112
3113 // We can't finishAffinity if we have a result.
3114 // Fall back and simply finish the current activity instead.
3115 if (mResultCode != RESULT_CANCELED || mResultData != null) {
3116 // Tell the developer what's going on to avoid hair-pulling.
3117 Log.i(TAG, "onNavigateUp only finishing topmost activity to return a result");
3118 finish();
3119 } else {
3120 finishAffinity();
3121 }
Adam Powelldd8fab22012-03-22 17:47:27 -07003122 } else {
3123 navigateUpTo(upIntent);
3124 }
3125 return true;
3126 }
3127 return false;
3128 }
3129
3130 /**
3131 * This is called when a child activity of this one attempts to navigate up.
3132 * The default implementation simply calls onNavigateUp() on this activity (the parent).
3133 *
3134 * @param child The activity making the call.
3135 */
3136 public boolean onNavigateUpFromChild(Activity child) {
3137 return onNavigateUp();
3138 }
3139
3140 /**
3141 * Define the synthetic task stack that will be generated during Up navigation from
3142 * a different task.
3143 *
3144 * <p>The default implementation of this method adds the parent chain of this activity
3145 * as specified in the manifest to the supplied {@link TaskStackBuilder}. Applications
3146 * may choose to override this method to construct the desired task stack in a different
3147 * way.</p>
3148 *
Adam Powellf0195952012-05-02 21:38:54 -07003149 * <p>This method will be invoked by the default implementation of {@link #onNavigateUp()}
3150 * if {@link #shouldUpRecreateTask(Intent)} returns true when supplied with the intent
3151 * returned by {@link #getParentActivityIntent()}.</p>
3152 *
Adam Powelldd8fab22012-03-22 17:47:27 -07003153 * <p>Applications that wish to supply extra Intent parameters to the parent stack defined
3154 * by the manifest should override {@link #onPrepareNavigateUpTaskStack(TaskStackBuilder)}.</p>
3155 *
3156 * @param builder An empty TaskStackBuilder - the application should add intents representing
3157 * the desired task stack
3158 */
3159 public void onCreateNavigateUpTaskStack(TaskStackBuilder builder) {
3160 builder.addParentStack(this);
3161 }
3162
3163 /**
3164 * Prepare the synthetic task stack that will be generated during Up navigation
3165 * from a different task.
3166 *
3167 * <p>This method receives the {@link TaskStackBuilder} with the constructed series of
3168 * Intents as generated by {@link #onCreateNavigateUpTaskStack(TaskStackBuilder)}.
3169 * If any extra data should be added to these intents before launching the new task,
3170 * the application should override this method and add that data here.</p>
3171 *
3172 * @param builder A TaskStackBuilder that has been populated with Intents by
3173 * onCreateNavigateUpTaskStack.
3174 */
3175 public void onPrepareNavigateUpTaskStack(TaskStackBuilder builder) {
3176 }
3177
3178 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 * This hook is called whenever the options menu is being closed (either by the user canceling
3180 * the menu with the back/menu button, or when an item is selected).
RoboErik55011652014-07-09 15:05:53 -07003181 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 * @param menu The options menu as last shown or first initialized by
3183 * onCreateOptionsMenu().
3184 */
3185 public void onOptionsMenuClosed(Menu menu) {
3186 if (mParent != null) {
3187 mParent.onOptionsMenuClosed(menu);
3188 }
3189 }
RoboErik55011652014-07-09 15:05:53 -07003190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 /**
3192 * Programmatically opens the options menu. If the options menu is already
3193 * open, this method does nothing.
3194 */
3195 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003196 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) &&
3197 (mActionBar == null || !mActionBar.openOptionsMenu())) {
Adam Powell07a74542014-05-30 15:52:44 -07003198 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
3199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 }
RoboErik55011652014-07-09 15:05:53 -07003201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 /**
3203 * Progammatically closes the options menu. If the options menu is already
3204 * closed, this method does nothing.
3205 */
3206 public void closeOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08003207 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
3208 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
3209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 }
3211
3212 /**
3213 * Called when a context menu for the {@code view} is about to be shown.
3214 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
3215 * time the context menu is about to be shown and should be populated for
3216 * the view (or item inside the view for {@link AdapterView} subclasses,
3217 * this can be found in the {@code menuInfo})).
3218 * <p>
3219 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
3220 * item has been selected.
3221 * <p>
3222 * It is not safe to hold onto the context menu after this method returns.
Scott Maincdf51062013-01-08 20:03:05 -08003223 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 */
3225 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
3226 }
3227
3228 /**
3229 * Registers a context menu to be shown for the given view (multiple views
3230 * can show the context menu). This method will set the
3231 * {@link OnCreateContextMenuListener} on the view to this activity, so
3232 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
3233 * called when it is time to show the context menu.
RoboErik55011652014-07-09 15:05:53 -07003234 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 * @see #unregisterForContextMenu(View)
3236 * @param view The view that should show a context menu.
3237 */
3238 public void registerForContextMenu(View view) {
3239 view.setOnCreateContextMenuListener(this);
3240 }
RoboErik55011652014-07-09 15:05:53 -07003241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 /**
3243 * Prevents a context menu to be shown for the given view. This method will remove the
3244 * {@link OnCreateContextMenuListener} on the view.
RoboErik55011652014-07-09 15:05:53 -07003245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 * @see #registerForContextMenu(View)
3247 * @param view The view that should stop showing a context menu.
3248 */
3249 public void unregisterForContextMenu(View view) {
3250 view.setOnCreateContextMenuListener(null);
3251 }
RoboErik55011652014-07-09 15:05:53 -07003252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 /**
3254 * Programmatically opens the context menu for a particular {@code view}.
3255 * The {@code view} should have been added via
3256 * {@link #registerForContextMenu(View)}.
RoboErik55011652014-07-09 15:05:53 -07003257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 * @param view The view to show the context menu for.
3259 */
3260 public void openContextMenu(View view) {
3261 view.showContextMenu();
3262 }
RoboErik55011652014-07-09 15:05:53 -07003263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 /**
3265 * Programmatically closes the most recently opened context menu, if showing.
3266 */
3267 public void closeContextMenu() {
Jose Limabd769a12015-01-28 10:43:15 -08003268 if (mWindow.hasFeature(Window.FEATURE_CONTEXT_MENU)) {
3269 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
3270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 }
RoboErik55011652014-07-09 15:05:53 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 /**
3274 * This hook is called whenever an item in a context menu is selected. The
3275 * default implementation simply returns false to have the normal processing
3276 * happen (calling the item's Runnable or sending a message to its Handler
3277 * as appropriate). You can use this method for any items for which you
3278 * would like to do processing without those other facilities.
3279 * <p>
3280 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
3281 * View that added this menu item.
3282 * <p>
3283 * Derived classes should call through to the base class for it to perform
3284 * the default menu handling.
RoboErik55011652014-07-09 15:05:53 -07003285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 * @param item The context menu item that was selected.
3287 * @return boolean Return false to allow normal context menu processing to
3288 * proceed, true to consume it here.
3289 */
3290 public boolean onContextItemSelected(MenuItem item) {
3291 if (mParent != null) {
3292 return mParent.onContextItemSelected(item);
3293 }
3294 return false;
3295 }
3296
3297 /**
3298 * This hook is called whenever the context menu is being closed (either by
3299 * the user canceling the menu with the back/menu button, or when an item is
3300 * selected).
RoboErik55011652014-07-09 15:05:53 -07003301 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 * @param menu The context menu that is being closed.
3303 */
3304 public void onContextMenuClosed(Menu menu) {
3305 if (mParent != null) {
3306 mParent.onContextMenuClosed(menu);
3307 }
3308 }
3309
3310 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003311 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003313 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 protected Dialog onCreateDialog(int id) {
3315 return null;
3316 }
3317
3318 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003319 * Callback for creating dialogs that are managed (saved and restored) for you
3320 * by the activity. The default implementation calls through to
3321 * {@link #onCreateDialog(int)} for compatibility.
3322 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003323 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3324 * or later, consider instead using a {@link DialogFragment} instead.</em>
3325 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003326 * <p>If you use {@link #showDialog(int)}, the activity will call through to
3327 * this method the first time, and hang onto it thereafter. Any dialog
3328 * that is created by this method will automatically be saved and restored
3329 * for you, including whether it is showing.
3330 *
3331 * <p>If you would like the activity to manage saving and restoring dialogs
3332 * for you, you should override this method and handle any ids that are
3333 * passed to {@link #showDialog}.
3334 *
3335 * <p>If you would like an opportunity to prepare your dialog before it is shown,
3336 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
3337 *
3338 * @param id The id of the dialog.
3339 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3340 * @return The dialog. If you return null, the dialog will not be created.
3341 *
3342 * @see #onPrepareDialog(int, Dialog, Bundle)
3343 * @see #showDialog(int, Bundle)
3344 * @see #dismissDialog(int)
3345 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003346 *
3347 * @deprecated Use the new {@link DialogFragment} class with
3348 * {@link FragmentManager} instead; this is also
3349 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003350 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003351 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003352 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003353 protected Dialog onCreateDialog(int id, Bundle args) {
3354 return onCreateDialog(id);
3355 }
3356
3357 /**
3358 * @deprecated Old no-arguments version of
3359 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
3360 */
3361 @Deprecated
3362 protected void onPrepareDialog(int id, Dialog dialog) {
3363 dialog.setOwnerActivity(this);
3364 }
3365
3366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003368 * shown. The default implementation calls through to
3369 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
RoboErik55011652014-07-09 15:05:53 -07003370 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 * <p>
3372 * Override this if you need to update a managed dialog based on the state
3373 * of the application each time it is shown. For example, a time picker
3374 * dialog might want to be updated with the current time. You should call
3375 * through to the superclass's implementation. The default implementation
3376 * will set this Activity as the owner activity on the Dialog.
RoboErik55011652014-07-09 15:05:53 -07003377 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 * @param id The id of the managed dialog.
3379 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003380 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
3381 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 * @see #showDialog(int)
3383 * @see #dismissDialog(int)
3384 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003385 *
3386 * @deprecated Use the new {@link DialogFragment} class with
3387 * {@link FragmentManager} instead; this is also
3388 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003390 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003391 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
3392 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 }
3394
3395 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003396 * Simple version of {@link #showDialog(int, Bundle)} that does not
3397 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
3398 * with null arguments.
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003399 *
3400 * @deprecated Use the new {@link DialogFragment} class with
3401 * {@link FragmentManager} instead; this is also
3402 * available on older platforms through the Android compatibility package.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003403 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003404 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003405 public final void showDialog(int id) {
3406 showDialog(id, null);
3407 }
3408
3409 /**
3410 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 * will be made with the same id the first time this is called for a given
3412 * id. From thereafter, the dialog will be automatically saved and restored.
3413 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07003414 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
3415 * or later, consider instead using a {@link DialogFragment} instead.</em>
3416 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003417 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 * be made to provide an opportunity to do any timely preparation.
3419 *
3420 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003421 * @param args Arguments to pass through to the dialog. These will be saved
3422 * and restored for you. Note that if the dialog is already created,
3423 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
3424 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08003425 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003426 * @return Returns true if the Dialog was created; false is returned if
3427 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
RoboErik55011652014-07-09 15:05:53 -07003428 *
Joe Onorato37296dc2009-07-31 17:58:55 -07003429 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003430 * @see #onCreateDialog(int, Bundle)
3431 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 * @see #dismissDialog(int)
3433 * @see #removeDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003434 *
3435 * @deprecated Use the new {@link DialogFragment} class with
3436 * {@link FragmentManager} instead; this is also
3437 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003439 @Nullable
Dianne Hackborn9567a662011-04-19 18:44:03 -07003440 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003441 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003443 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003445 ManagedDialog md = mManagedDialogs.get(id);
3446 if (md == null) {
3447 md = new ManagedDialog();
3448 md.mDialog = createDialog(id, null, args);
3449 if (md.mDialog == null) {
3450 return false;
3451 }
3452 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453 }
RoboErik55011652014-07-09 15:05:53 -07003454
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003455 md.mArgs = args;
3456 onPrepareDialog(id, md.mDialog, args);
3457 md.mDialog.show();
3458 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 }
3460
3461 /**
3462 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
3463 *
3464 * @param id The id of the managed dialog.
3465 *
3466 * @throws IllegalArgumentException if the id was not previously shown via
3467 * {@link #showDialog(int)}.
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 #removeDialog(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 dismissDialog(int id) {
3480 if (mManagedDialogs == null) {
3481 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 }
RoboErik55011652014-07-09 15:05:53 -07003483
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003484 final ManagedDialog md = mManagedDialogs.get(id);
3485 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 throw missingDialog(id);
3487 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003488 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 }
3490
3491 /**
3492 * Creates an exception to throw if a user passed in a dialog id that is
3493 * unexpected.
3494 */
3495 private IllegalArgumentException missingDialog(int id) {
3496 return new IllegalArgumentException("no dialog with id " + id + " was ever "
3497 + "shown via Activity#showDialog");
3498 }
3499
3500 /**
3501 * Removes any internal references to a dialog managed by this Activity.
3502 * If the dialog is showing, it will dismiss it as part of the clean up.
3503 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003504 * <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 -08003505 * want to avoid the overhead of saving and restoring it in the future.
3506 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003507 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
3508 * will not throw an exception if you try to remove an ID that does not
3509 * currently have an associated dialog.</p>
RoboErik55011652014-07-09 15:05:53 -07003510 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 * @param id The id of the managed dialog.
3512 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08003513 * @see #onCreateDialog(int, Bundle)
3514 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 * @see #showDialog(int)
3516 * @see #dismissDialog(int)
Dianne Hackborn271c2fe2011-08-09 19:35:13 -07003517 *
3518 * @deprecated Use the new {@link DialogFragment} class with
3519 * {@link FragmentManager} instead; this is also
3520 * available on older platforms through the Android compatibility package.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07003522 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07003524 if (mManagedDialogs != null) {
3525 final ManagedDialog md = mManagedDialogs.get(id);
3526 if (md != null) {
3527 md.mDialog.dismiss();
3528 mManagedDialogs.remove(id);
3529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 }
3532
3533 /**
3534 * This hook is called when the user signals the desire to start a search.
RoboErik55011652014-07-09 15:05:53 -07003535 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01003536 * <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 -07003537 * menu item, search button, or other widgets within your activity. Unless overidden,
Bjorn Bringert6266e402009-09-25 14:25:41 +01003538 * calling this function is the same as calling
3539 * {@link #startSearch startSearch(null, false, null, false)}, which launches
3540 * search for the current activity as specified in its manifest, see {@link SearchManager}.
RoboErik55011652014-07-09 15:05:53 -07003541 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 * <p>You can override this function to force global search, e.g. in response to a dedicated
3543 * search key, or to block search entirely (by simply returning false).
RoboErik55011652014-07-09 15:05:53 -07003544 *
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003545 * <p>Note: when running in a {@link Configuration#UI_MODE_TYPE_TELEVISION}, the default
3546 * implementation changes to simply return false and you must supply your own custom
3547 * implementation if you want to support search.</p>
3548 *
3549 * @return Returns {@code true} if search launched, and {@code false} if the activity does
3550 * not respond to search. The default implementation always returns {@code true}, except
3551 * when in {@link Configuration#UI_MODE_TYPE_TELEVISION} mode where it returns false.
RoboErik55011652014-07-09 15:05:53 -07003552 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 * @see android.app.SearchManager
3554 */
3555 public boolean onSearchRequested() {
Dianne Hackbornfdf5b352014-10-08 17:43:48 -07003556 if ((getResources().getConfiguration().uiMode&Configuration.UI_MODE_TYPE_MASK)
3557 != Configuration.UI_MODE_TYPE_TELEVISION) {
3558 startSearch(null, false, null, false);
3559 return true;
3560 } else {
3561 return false;
3562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 }
RoboErik55011652014-07-09 15:05:53 -07003564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 /**
3566 * This hook is called to launch the search UI.
RoboErik55011652014-07-09 15:05:53 -07003567 *
3568 * <p>It is typically called from onSearchRequested(), either directly from
3569 * Activity.onSearchRequested() or from an overridden version in any given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 * Activity. If your goal is simply to activate search, it is preferred to call
Tor Norbyed9273d62013-05-30 15:59:53 -07003571 * onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 * is to inject specific data such as context data, it is preferred to <i>override</i>
3573 * onSearchRequested(), so that any callers to it will benefit from the override.
RoboErik55011652014-07-09 15:05:53 -07003574 *
3575 * @param initialQuery Any non-null non-empty string will be inserted as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 * pre-entered text in the search query box.
Tor Norbyed9273d62013-05-30 15:59:53 -07003577 * @param selectInitialQuery If true, the initial query will be preselected, which means that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 * any further typing will replace it. This is useful for cases where an entire pre-formed
3579 * query is being inserted. If false, the selection point will be placed at the end of the
3580 * inserted query. This is useful when the inserted query is text that the user entered,
3581 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
3582 * if initialQuery is a non-empty string.</i>
RoboErik55011652014-07-09 15:05:53 -07003583 * @param appSearchData An application can insert application-specific
3584 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 * searches. This data will be returned with SEARCH intent(s). Null if
3586 * no extra data is required.
3587 * @param globalSearch If false, this will only launch the search that has been specifically
RoboErik55011652014-07-09 15:05:53 -07003588 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07003589 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 * If true, this will always launch a platform-global (e.g. web-based) search instead.
RoboErik55011652014-07-09 15:05:53 -07003591 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 * @see android.app.SearchManager
3593 * @see #onSearchRequested
3594 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003595 public void startSearch(@Nullable String initialQuery, boolean selectInitialQuery,
3596 @Nullable Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003597 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01003598 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
RoboErik55011652014-07-09 15:05:53 -07003599 appSearchData, globalSearch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 }
3601
3602 /**
krosaend2d60142009-08-17 08:56:48 -07003603 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
3604 * the search dialog. Made available for testing purposes.
3605 *
3606 * @param query The query to trigger. If empty, the request will be ignored.
3607 * @param appSearchData An application can insert application-specific
3608 * context here, in order to improve quality or specificity of its own
3609 * searches. This data will be returned with SEARCH intent(s). Null if
3610 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07003611 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003612 public void triggerSearch(String query, @Nullable Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07003613 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01003614 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07003615 }
3616
3617 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 * Request that key events come to this activity. Use this if your
3619 * activity has no views with focus, but the activity still wants
3620 * a chance to process key events.
RoboErik55011652014-07-09 15:05:53 -07003621 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 * @see android.view.Window#takeKeyEvents
3623 */
3624 public void takeKeyEvents(boolean get) {
3625 getWindow().takeKeyEvents(get);
3626 }
3627
3628 /**
3629 * Enable extended window features. This is a convenience for calling
3630 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
RoboErik55011652014-07-09 15:05:53 -07003631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 * @param featureId The desired feature as defined in
3633 * {@link android.view.Window}.
3634 * @return Returns true if the requested feature is supported and now
3635 * enabled.
RoboErik55011652014-07-09 15:05:53 -07003636 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 * @see android.view.Window#requestFeature
3638 */
3639 public final boolean requestWindowFeature(int featureId) {
3640 return getWindow().requestFeature(featureId);
3641 }
3642
3643 /**
3644 * Convenience for calling
3645 * {@link android.view.Window#setFeatureDrawableResource}.
3646 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003647 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 getWindow().setFeatureDrawableResource(featureId, resId);
3649 }
3650
3651 /**
3652 * Convenience for calling
3653 * {@link android.view.Window#setFeatureDrawableUri}.
3654 */
3655 public final void setFeatureDrawableUri(int featureId, Uri uri) {
3656 getWindow().setFeatureDrawableUri(featureId, uri);
3657 }
3658
3659 /**
3660 * Convenience for calling
3661 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
3662 */
3663 public final void setFeatureDrawable(int featureId, Drawable drawable) {
3664 getWindow().setFeatureDrawable(featureId, drawable);
3665 }
3666
3667 /**
3668 * Convenience for calling
3669 * {@link android.view.Window#setFeatureDrawableAlpha}.
3670 */
3671 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
3672 getWindow().setFeatureDrawableAlpha(featureId, alpha);
3673 }
3674
3675 /**
3676 * Convenience for calling
3677 * {@link android.view.Window#getLayoutInflater}.
3678 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003679 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 public LayoutInflater getLayoutInflater() {
3681 return getWindow().getLayoutInflater();
3682 }
3683
3684 /**
3685 * Returns a {@link MenuInflater} with this context.
3686 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003687 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07003689 // Make sure that action views can get an appropriate theme.
3690 if (mMenuInflater == null) {
Adam Powelle43340c2014-03-17 19:10:43 -07003691 initWindowDecorActionBar();
Adam Powell88ab6972011-07-28 11:25:01 -07003692 if (mActionBar != null) {
Dianne Hackborn92751972012-05-18 19:22:14 -07003693 mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this);
Adam Powell88ab6972011-07-28 11:25:01 -07003694 } else {
3695 mMenuInflater = new MenuInflater(this);
3696 }
3697 }
3698 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 }
3700
3701 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07003702 protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003703 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 if (mParent == null) {
3705 super.onApplyThemeResource(theme, resid, first);
3706 } else {
3707 try {
3708 theme.setTo(mParent.getTheme());
3709 } catch (Exception e) {
3710 // Empty
3711 }
3712 theme.applyStyle(resid, false);
3713 }
Winson Chung03a9bae2014-05-02 09:56:12 -07003714
Winson Chunga449dc02014-05-16 11:15:04 -07003715 // Get the primary color and update the TaskDescription for this activity
Winson Chung4d1a4342014-05-09 16:42:00 -07003716 if (theme != null) {
3717 TypedArray a = theme.obtainStyledAttributes(com.android.internal.R.styleable.Theme);
Winson Chungaca96022014-05-09 15:04:08 -07003718 int colorPrimary = a.getColor(com.android.internal.R.styleable.Theme_colorPrimary, 0);
3719 a.recycle();
3720 if (colorPrimary != 0) {
Winson Chunga449dc02014-05-16 11:15:04 -07003721 ActivityManager.TaskDescription v = new ActivityManager.TaskDescription(null, null,
3722 colorPrimary);
3723 setTaskDescription(v);
Winson Chungaca96022014-05-09 15:04:08 -07003724 }
Winson Chung03a9bae2014-05-02 09:56:12 -07003725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 }
3727
3728 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07003729 * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
3730 * with no options.
3731 *
3732 * @param intent The intent to start.
3733 * @param requestCode If >= 0, this code will be returned in
3734 * onActivityResult() when the activity exits.
3735 *
3736 * @throws android.content.ActivityNotFoundException
3737 *
George Mount0a778ed2013-12-13 13:35:36 -08003738 * @see #startActivity
Dianne Hackborna4972e92012-03-14 10:38:05 -07003739 */
3740 public void startActivityForResult(Intent intent, int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07003741 startActivityForResult(intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07003742 }
3743
3744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 * Launch an activity for which you would like a result when it finished.
3746 * When this activity exits, your
George Mount0a778ed2013-12-13 13:35:36 -08003747 * onActivityResult() method will be called with the given requestCode.
RoboErik55011652014-07-09 15:05:53 -07003748 * Using a negative requestCode is the same as calling
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 * {@link #startActivity} (the activity is not launched as a sub-activity).
Dianne Hackborna4972e92012-03-14 10:38:05 -07003750 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 * <p>Note that this method should only be used with Intent protocols
3752 * that are defined to return a result. In other protocols (such as
3753 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
3754 * not get the result when you expect. For example, if the activity you
3755 * are launching uses the singleTask launch mode, it will not run in your
3756 * task and thus you will immediately receive a cancel result.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003757 *
RoboErik55011652014-07-09 15:05:53 -07003758 * <p>As a special case, if you call startActivityForResult() with a requestCode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
George Mount0a778ed2013-12-13 13:35:36 -08003760 * activity, then your window will not be displayed until a result is
3761 * returned back from the started activity. This is to avoid visible
3762 * flickering when redirecting to another activity.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003763 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 * <p>This method throws {@link android.content.ActivityNotFoundException}
3765 * if there was no Activity found to run the given Intent.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003766 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 * @param intent The intent to start.
3768 * @param requestCode If >= 0, this code will be returned in
3769 * onActivityResult() when the activity exits.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003770 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003771 * See {@link android.content.Context#startActivity(Intent, Bundle)
3772 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003773 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07003775 *
George Mount0a778ed2013-12-13 13:35:36 -08003776 * @see #startActivity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003778 public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 if (mParent == null) {
3780 Instrumentation.ActivityResult ar =
3781 mInstrumentation.execStartActivity(
3782 this, mMainThread.getApplicationThread(), mToken, this,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003783 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003784 if (ar != null) {
3785 mMainThread.sendActivityResult(
3786 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
3787 ar.getResultData());
3788 }
3789 if (requestCode >= 0) {
3790 // If this start is requesting a result, we can avoid making
3791 // the activity visible until the result is received. Setting
3792 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
3793 // activity hidden during this time, to avoid flickering.
3794 // This can only be done when a result is requested because
3795 // that guarantees we will get information back when the
3796 // activity is finished, no matter what happens to it.
3797 mStartedActivity = true;
3798 }
Adam Powell14874662013-07-18 19:42:41 -07003799
3800 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
3801 if (decor != null) {
3802 decor.cancelPendingInputEvents();
3803 }
3804 // TODO Consider clearing/flushing other event sources and events for child windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07003806 if (options != null) {
3807 mParent.startActivityFromChild(this, intent, requestCode, options);
3808 } else {
3809 // Note we want to go through this method for compatibility with
3810 // existing applications that may have overridden it.
3811 mParent.startActivityFromChild(this, intent, requestCode);
3812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 }
George Mountff243282014-07-07 16:12:07 -07003814 if (options != null && !isTopOfTask()) {
3815 mActivityTransitionState.startExitOutTransition(this, options);
3816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 }
3818
3819 /**
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07003820 * @hide Implement to provide correct calling token.
3821 */
Alexandra Gherghina83231452014-07-04 12:05:20 +01003822 public void startActivityForResultAsUser(Intent intent, int requestCode, UserHandle user) {
3823 startActivityForResultAsUser(intent, requestCode, null, user);
3824 }
3825
3826 /**
3827 * @hide Implement to provide correct calling token.
3828 */
3829 public void startActivityForResultAsUser(Intent intent, int requestCode,
3830 @Nullable Bundle options, UserHandle user) {
3831 if (options != null) {
3832 mActivityTransitionState.startExitOutTransition(this, options);
3833 }
3834 if (mParent != null) {
3835 throw new RuntimeException("Can't be called from a child");
3836 }
3837 Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
3838 this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode,
3839 options, user);
3840 if (ar != null) {
3841 mMainThread.sendActivityResult(
3842 mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
3843 }
3844 if (requestCode >= 0) {
3845 // If this start is requesting a result, we can avoid making
3846 // the activity visible until the result is received. Setting
3847 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
3848 // activity hidden during this time, to avoid flickering.
3849 // This can only be done when a result is requested because
3850 // that guarantees we will get information back when the
3851 // activity is finished, no matter what happens to it.
3852 mStartedActivity = true;
3853 }
3854
3855 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
3856 if (decor != null) {
3857 decor.cancelPendingInputEvents();
3858 }
3859 }
3860
3861 /**
3862 * @hide Implement to provide correct calling token.
3863 */
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07003864 public void startActivityAsUser(Intent intent, UserHandle user) {
3865 startActivityAsUser(intent, null, user);
3866 }
3867
3868 /**
3869 * @hide Implement to provide correct calling token.
3870 */
3871 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
3872 if (mParent != null) {
Alexandra Gherghina83231452014-07-04 12:05:20 +01003873 throw new RuntimeException("Can't be called from a child");
Dianne Hackbornf1c26e22012-08-23 13:54:58 -07003874 }
3875 Instrumentation.ActivityResult ar =
3876 mInstrumentation.execStartActivity(
3877 this, mMainThread.getApplicationThread(), mToken, this,
3878 intent, -1, options, user);
3879 if (ar != null) {
3880 mMainThread.sendActivityResult(
3881 mToken, mEmbeddedID, -1, ar.getResultCode(),
3882 ar.getResultData());
3883 }
3884 }
3885
3886 /**
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07003887 * Start a new activity as if it was started by the activity that started our
3888 * current activity. This is for the resolver and chooser activities, which operate
3889 * as intermediaries that dispatch their intent to the target the user selects -- to
3890 * do this, they must perform all security checks including permission grants as if
3891 * their launch had come from the original activity.
3892 * @hide
3893 */
Jeff Sharkey97978802014-10-14 10:48:18 -07003894 public void startActivityAsCaller(Intent intent, @Nullable Bundle options, int userId) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07003895 if (mParent != null) {
3896 throw new RuntimeException("Can't be called from a child");
3897 }
3898 Instrumentation.ActivityResult ar =
3899 mInstrumentation.execStartActivityAsCaller(
3900 this, mMainThread.getApplicationThread(), mToken, this,
Jeff Sharkey97978802014-10-14 10:48:18 -07003901 intent, -1, options, userId);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07003902 if (ar != null) {
3903 mMainThread.sendActivityResult(
3904 mToken, mEmbeddedID, -1, ar.getResultCode(),
3905 ar.getResultData());
3906 }
3907 }
3908
3909 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07003910 * Same as calling {@link #startIntentSenderForResult(IntentSender, int,
3911 * Intent, int, int, int, Bundle)} with no options.
3912 *
3913 * @param intent The IntentSender to launch.
3914 * @param requestCode If >= 0, this code will be returned in
3915 * onActivityResult() when the activity exits.
3916 * @param fillInIntent If non-null, this will be provided as the
3917 * intent parameter to {@link IntentSender#sendIntent}.
3918 * @param flagsMask Intent flags in the original IntentSender that you
3919 * would like to change.
3920 * @param flagsValues Desired values for any bits set in
3921 * <var>flagsMask</var>
3922 * @param extraFlags Always set to 0.
3923 */
3924 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07003925 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackborna4972e92012-03-14 10:38:05 -07003926 throws IntentSender.SendIntentException {
3927 startIntentSenderForResult(intent, requestCode, fillInIntent, flagsMask,
3928 flagsValues, extraFlags, null);
3929 }
3930
3931 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003932 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003933 * to use a IntentSender to describe the activity to be started. If
3934 * the IntentSender is for an activity, that activity will be started
3935 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
3936 * here; otherwise, its associated action will be executed (such as
3937 * sending a broadcast) as if you had called
3938 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
RoboErik55011652014-07-09 15:05:53 -07003939 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003940 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003941 * @param requestCode If >= 0, this code will be returned in
3942 * onActivityResult() when the activity exits.
3943 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003944 * intent parameter to {@link IntentSender#sendIntent}.
3945 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003946 * would like to change.
3947 * @param flagsValues Desired values for any bits set in
3948 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003949 * @param extraFlags Always set to 0.
Dianne Hackborna4972e92012-03-14 10:38:05 -07003950 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07003951 * See {@link android.content.Context#startActivity(Intent, Bundle)
3952 * Context.startActivity(Intent, Bundle)} for more details. If options
3953 * have also been supplied by the IntentSender, options given here will
3954 * override any that conflict with those given by the IntentSender.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003955 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003956 public void startIntentSenderForResult(IntentSender intent, int requestCode,
Tor Norbyed9273d62013-05-30 15:59:53 -07003957 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003958 Bundle options) throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003959 if (mParent == null) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003960 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003961 flagsMask, flagsValues, this, options);
3962 } else if (options != null) {
3963 mParent.startIntentSenderFromChild(this, intent, requestCode,
3964 fillInIntent, flagsMask, flagsValues, extraFlags, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003965 } else {
Dianne Hackborna4972e92012-03-14 10:38:05 -07003966 // Note we want to go through this call for compatibility with
3967 // existing applications that may have overridden the method.
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003968 mParent.startIntentSenderFromChild(this, intent, requestCode,
3969 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003970 }
3971 }
3972
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003973 private void startIntentSenderForResultInner(IntentSender intent, int requestCode,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003974 Intent fillInIntent, int flagsMask, int flagsValues, Activity activity,
3975 Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003976 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003977 try {
3978 String resolvedType = null;
3979 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07003980 fillInIntent.migrateExtraStreamToClipData();
3981 fillInIntent.prepareToLeaveProcess();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003982 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
3983 }
3984 int result = ActivityManagerNative.getDefault()
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003985 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003986 fillInIntent, resolvedType, mToken, activity.mEmbeddedID,
Dianne Hackborna4972e92012-03-14 10:38:05 -07003987 requestCode, flagsMask, flagsValues, options);
3988 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003989 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003990 }
3991 Instrumentation.checkStartActivityResult(result, null);
3992 } catch (RemoteException e) {
3993 }
3994 if (requestCode >= 0) {
3995 // If this start is requesting a result, we can avoid making
3996 // the activity visible until the result is received. Setting
3997 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
3998 // activity hidden during this time, to avoid flickering.
3999 // This can only be done when a result is requested because
4000 // that guarantees we will get information back when the
4001 // activity is finished, no matter what happens to it.
4002 mStartedActivity = true;
4003 }
4004 }
4005
4006 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004007 * Same as {@link #startActivity(Intent, Bundle)} with no options
4008 * specified.
4009 *
4010 * @param intent The intent to start.
4011 *
4012 * @throws android.content.ActivityNotFoundException
4013 *
4014 * @see {@link #startActivity(Intent, Bundle)}
4015 * @see #startActivityForResult
4016 */
4017 @Override
4018 public void startActivity(Intent intent) {
George Mounte1803372014-02-26 19:00:52 +00004019 this.startActivity(intent, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004020 }
4021
4022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 * Launch a new activity. You will not receive any information about when
4024 * the activity exits. This implementation overrides the base version,
4025 * providing information about
4026 * the activity performing the launch. Because of this additional
4027 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4028 * required; if not specified, the new activity will be added to the
4029 * task of the caller.
RoboErik55011652014-07-09 15:05:53 -07004030 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 * <p>This method throws {@link android.content.ActivityNotFoundException}
4032 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004033 *
4034 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004035 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004036 * See {@link android.content.Context#startActivity(Intent, Bundle)
4037 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004038 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 * @throws android.content.ActivityNotFoundException
Dianne Hackborna4972e92012-03-14 10:38:05 -07004040 *
4041 * @see {@link #startActivity(Intent)}
RoboErik55011652014-07-09 15:05:53 -07004042 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 */
4044 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004045 public void startActivity(Intent intent, @Nullable Bundle options) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004046 if (options != null) {
4047 startActivityForResult(intent, -1, options);
4048 } else {
4049 // Note we want to go through this call for compatibility with
4050 // applications that may have overridden the method.
4051 startActivityForResult(intent, -1);
4052 }
4053 }
4054
4055 /**
4056 * Same as {@link #startActivities(Intent[], Bundle)} with no options
4057 * specified.
4058 *
4059 * @param intents The intents to start.
4060 *
4061 * @throws android.content.ActivityNotFoundException
4062 *
4063 * @see {@link #startActivities(Intent[], Bundle)}
4064 * @see #startActivityForResult
4065 */
4066 @Override
4067 public void startActivities(Intent[] intents) {
4068 startActivities(intents, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 }
4070
4071 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004072 * Launch a new activity. You will not receive any information about when
4073 * the activity exits. This implementation overrides the base version,
4074 * providing information about
4075 * the activity performing the launch. Because of this additional
4076 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
4077 * required; if not specified, the new activity will be added to the
4078 * task of the caller.
4079 *
4080 * <p>This method throws {@link android.content.ActivityNotFoundException}
4081 * if there was no Activity found to run the given Intent.
4082 *
4083 * @param intents The intents to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004084 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004085 * See {@link android.content.Context#startActivity(Intent, Bundle)
4086 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004087 *
4088 * @throws android.content.ActivityNotFoundException
4089 *
Dianne Hackborna4972e92012-03-14 10:38:05 -07004090 * @see {@link #startActivities(Intent[])}
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004091 * @see #startActivityForResult
4092 */
4093 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07004094 public void startActivities(Intent[] intents, @Nullable Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004095 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
Dianne Hackborna4972e92012-03-14 10:38:05 -07004096 mToken, this, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004097 }
4098
4099 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004100 * Same as calling {@link #startIntentSender(IntentSender, Intent, int, int, int, Bundle)}
4101 * with no options.
RoboErik55011652014-07-09 15:05:53 -07004102 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004103 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004104 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004105 * intent parameter to {@link IntentSender#sendIntent}.
4106 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004107 * would like to change.
4108 * @param flagsValues Desired values for any bits set in
4109 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004110 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004111 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004112 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004113 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004114 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004115 startIntentSender(intent, fillInIntent, flagsMask, flagsValues,
4116 extraFlags, null);
4117 }
4118
4119 /**
4120 * Like {@link #startActivity(Intent, Bundle)}, but taking a IntentSender
4121 * to start; see
4122 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)}
4123 * for more information.
4124 *
4125 * @param intent The IntentSender to launch.
4126 * @param fillInIntent If non-null, this will be provided as the
4127 * intent parameter to {@link IntentSender#sendIntent}.
4128 * @param flagsMask Intent flags in the original IntentSender that you
4129 * would like to change.
4130 * @param flagsValues Desired values for any bits set in
4131 * <var>flagsMask</var>
4132 * @param extraFlags Always set to 0.
4133 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004134 * See {@link android.content.Context#startActivity(Intent, Bundle)
4135 * Context.startActivity(Intent, Bundle)} for more details. If options
4136 * have also been supplied by the IntentSender, options given here will
4137 * override any that conflict with those given by the IntentSender.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004138 */
4139 public void startIntentSender(IntentSender intent,
Tor Norbyed9273d62013-05-30 15:59:53 -07004140 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004141 Bundle options) throws IntentSender.SendIntentException {
4142 if (options != null) {
4143 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4144 flagsValues, extraFlags, options);
4145 } else {
4146 // Note we want to go through this call for compatibility with
4147 // applications that may have overridden the method.
4148 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
4149 flagsValues, extraFlags);
4150 }
4151 }
4152
4153 /**
4154 * Same as calling {@link #startActivityIfNeeded(Intent, int, Bundle)}
4155 * with no options.
4156 *
4157 * @param intent The intent to start.
4158 * @param requestCode If >= 0, this code will be returned in
4159 * onActivityResult() when the activity exits, as described in
4160 * {@link #startActivityForResult}.
4161 *
4162 * @return If a new activity was launched then true is returned; otherwise
4163 * false is returned and you must handle the Intent yourself.
4164 *
4165 * @see #startActivity
4166 * @see #startActivityForResult
4167 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004168 public boolean startActivityIfNeeded(@NonNull Intent intent, int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004169 return startActivityIfNeeded(intent, requestCode, null);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004170 }
4171
4172 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 * A special variation to launch an activity only if a new activity
4174 * instance is needed to handle the given Intent. In other words, this is
RoboErik55011652014-07-09 15:05:53 -07004175 * just like {@link #startActivityForResult(Intent, int)} except: if you are
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
RoboErik55011652014-07-09 15:05:53 -07004177 * singleTask or singleTop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
RoboErik55011652014-07-09 15:05:53 -07004179 * and the activity
4180 * that handles <var>intent</var> is the same as your currently running
4181 * activity, then a new instance is not needed. In this case, instead of
4182 * the normal behavior of calling {@link #onNewIntent} this function will
4183 * return and you can handle the Intent yourself.
4184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 * <p>This function can only be called from a top-level activity; if it is
4186 * called from a child activity, a runtime exception will be thrown.
RoboErik55011652014-07-09 15:05:53 -07004187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 * @param intent The intent to start.
4189 * @param requestCode If >= 0, this code will be returned in
4190 * onActivityResult() when the activity exits, as described in
4191 * {@link #startActivityForResult}.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004192 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004193 * See {@link android.content.Context#startActivity(Intent, Bundle)
4194 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004195 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 * @return If a new activity was launched then true is returned; otherwise
4197 * false is returned and you must handle the Intent yourself.
RoboErik55011652014-07-09 15:05:53 -07004198 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 * @see #startActivity
4200 * @see #startActivityForResult
4201 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004202 public boolean startActivityIfNeeded(@NonNull Intent intent, int requestCode,
4203 @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 if (mParent == null) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004205 int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004207 intent.migrateExtraStreamToClipData();
4208 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 result = ActivityManagerNative.getDefault()
Dianne Hackbornf265ea92013-01-31 15:00:51 -08004210 .startActivity(mMainThread.getApplicationThread(), getBasePackageName(),
Jeff Hao1b012d32014-08-20 10:35:34 -07004211 intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
4212 mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED,
4213 null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 } catch (RemoteException e) {
4215 // Empty
4216 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08004217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 Instrumentation.checkStartActivityResult(result, intent);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 if (requestCode >= 0) {
4221 // If this start is requesting a result, we can avoid making
4222 // the activity visible until the result is received. Setting
4223 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
4224 // activity hidden during this time, to avoid flickering.
4225 // This can only be done when a result is requested because
4226 // that guarantees we will get information back when the
4227 // activity is finished, no matter what happens to it.
4228 mStartedActivity = true;
4229 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004230 return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 }
4232
4233 throw new UnsupportedOperationException(
4234 "startActivityIfNeeded can only be called from a top-level activity");
4235 }
4236
4237 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004238 * Same as calling {@link #startNextMatchingActivity(Intent, Bundle)} with
4239 * no options.
4240 *
4241 * @param intent The intent to dispatch to the next activity. For
4242 * correct behavior, this must be the same as the Intent that started
4243 * your own activity; the only changes you can make are to the extras
4244 * inside of it.
4245 *
4246 * @return Returns a boolean indicating whether there was another Activity
4247 * to start: true if there was a next activity to start, false if there
4248 * wasn't. In general, if true is returned you will then want to call
4249 * finish() on yourself.
4250 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004251 public boolean startNextMatchingActivity(@NonNull Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07004252 return startNextMatchingActivity(intent, null);
4253 }
4254
4255 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 * Special version of starting an activity, for use when you are replacing
4257 * other activity components. You can use this to hand the Intent off
4258 * to the next Activity that can handle it. You typically call this in
4259 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
RoboErik55011652014-07-09 15:05:53 -07004260 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 * @param intent The intent to dispatch to the next activity. For
4262 * correct behavior, this must be the same as the Intent that started
4263 * your own activity; the only changes you can make are to the extras
4264 * inside of it.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004265 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004266 * See {@link android.content.Context#startActivity(Intent, Bundle)
4267 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004268 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 * @return Returns a boolean indicating whether there was another Activity
4270 * to start: true if there was a next activity to start, false if there
4271 * wasn't. In general, if true is returned you will then want to call
4272 * finish() on yourself.
4273 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004274 public boolean startNextMatchingActivity(@NonNull Intent intent, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 if (mParent == null) {
4276 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004277 intent.migrateExtraStreamToClipData();
4278 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 return ActivityManagerNative.getDefault()
Dianne Hackborna4972e92012-03-14 10:38:05 -07004280 .startNextMatchingActivity(mToken, intent, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 } catch (RemoteException e) {
4282 // Empty
4283 }
4284 return false;
4285 }
4286
4287 throw new UnsupportedOperationException(
4288 "startNextMatchingActivity can only be called from a top-level activity");
4289 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004290
4291 /**
4292 * Same as calling {@link #startActivityFromChild(Activity, Intent, int, Bundle)}
4293 * with no options.
4294 *
4295 * @param child The activity making the call.
4296 * @param intent The intent to start.
4297 * @param requestCode Reply request code. < 0 if reply is not requested.
4298 *
4299 * @throws android.content.ActivityNotFoundException
4300 *
4301 * @see #startActivity
4302 * @see #startActivityForResult
4303 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004304 public void startActivityFromChild(@NonNull Activity child, Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004305 int requestCode) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004306 startActivityFromChild(child, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004307 }
4308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 /**
RoboErik55011652014-07-09 15:05:53 -07004310 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 * {@link #startActivity} or {@link #startActivityForResult} method.
RoboErik55011652014-07-09 15:05:53 -07004312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 * <p>This method throws {@link android.content.ActivityNotFoundException}
4314 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004315 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 * @param child The activity making the call.
4317 * @param intent The intent to start.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004318 * @param requestCode Reply request code. < 0 if reply is not requested.
4319 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004320 * See {@link android.content.Context#startActivity(Intent, Bundle)
4321 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004322 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004324 *
4325 * @see #startActivity
4326 * @see #startActivityForResult
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004328 public void startActivityFromChild(@NonNull Activity child, Intent intent,
4329 int requestCode, @Nullable Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 Instrumentation.ActivityResult ar =
4331 mInstrumentation.execStartActivity(
4332 this, mMainThread.getApplicationThread(), mToken, child,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004333 intent, requestCode, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 if (ar != null) {
4335 mMainThread.sendActivityResult(
4336 mToken, child.mEmbeddedID, requestCode,
4337 ar.getResultCode(), ar.getResultData());
4338 }
4339 }
4340
4341 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004342 * Same as calling {@link #startActivityFromFragment(Fragment, Intent, int, Bundle)}
4343 * with no options.
4344 *
4345 * @param fragment The fragment making the call.
4346 * @param intent The intent to start.
4347 * @param requestCode Reply request code. < 0 if reply is not requested.
4348 *
4349 * @throws android.content.ActivityNotFoundException
4350 *
4351 * @see Fragment#startActivity
4352 * @see Fragment#startActivityForResult
4353 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004354 public void startActivityFromFragment(@NonNull Fragment fragment, Intent intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004355 int requestCode) {
George Mountabb63cb2014-06-23 11:17:58 -07004356 startActivityFromFragment(fragment, intent, requestCode, null);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004357 }
4358
4359 /**
RoboErik55011652014-07-09 15:05:53 -07004360 * This is called when a Fragment in this activity calls its
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004361 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
4362 * method.
RoboErik55011652014-07-09 15:05:53 -07004363 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004364 * <p>This method throws {@link android.content.ActivityNotFoundException}
4365 * if there was no Activity found to run the given Intent.
RoboErik55011652014-07-09 15:05:53 -07004366 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004367 * @param fragment The fragment making the call.
4368 * @param intent The intent to start.
RoboErik55011652014-07-09 15:05:53 -07004369 * @param requestCode Reply request code. < 0 if reply is not requested.
Dianne Hackborna4972e92012-03-14 10:38:05 -07004370 * @param options Additional options for how the Activity should be started.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004371 * See {@link android.content.Context#startActivity(Intent, Bundle)
4372 * Context.startActivity(Intent, Bundle)} for more details.
RoboErik55011652014-07-09 15:05:53 -07004373 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004374 * @throws android.content.ActivityNotFoundException
RoboErik55011652014-07-09 15:05:53 -07004375 *
4376 * @see Fragment#startActivity
4377 * @see Fragment#startActivityForResult
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004378 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004379 public void startActivityFromFragment(@NonNull Fragment fragment, Intent intent,
4380 int requestCode, @Nullable Bundle options) {
George Mounta8bd9292014-05-21 10:54:17 -07004381 if (options != null) {
4382 mActivityTransitionState.startExitOutTransition(this, options);
4383 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004384 Instrumentation.ActivityResult ar =
4385 mInstrumentation.execStartActivity(
4386 this, mMainThread.getApplicationThread(), mToken, fragment,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004387 intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004388 if (ar != null) {
4389 mMainThread.sendActivityResult(
4390 mToken, fragment.mWho, requestCode,
4391 ar.getResultCode(), ar.getResultData());
4392 }
4393 }
4394
4395 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -07004396 * Same as calling {@link #startIntentSenderFromChild(Activity, IntentSender,
4397 * int, Intent, int, int, int, Bundle)} with no options.
4398 */
4399 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4400 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
4401 int extraFlags)
4402 throws IntentSender.SendIntentException {
4403 startIntentSenderFromChild(child, intent, requestCode, fillInIntent,
4404 flagsMask, flagsValues, extraFlags, null);
4405 }
4406
4407 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004408 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004409 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07004410 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004411 * for more information.
4412 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004413 public void startIntentSenderFromChild(Activity child, IntentSender intent,
4414 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
Tor Norbyed9273d62013-05-30 15:59:53 -07004415 int extraFlags, @Nullable Bundle options)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07004416 throws IntentSender.SendIntentException {
4417 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07004418 flagsMask, flagsValues, child, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07004419 }
4420
4421 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004422 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
4423 * or {@link #finish} to specify an explicit transition animation to
4424 * perform next.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004425 *
4426 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
4427 * to using this with starting activities is to supply the desired animation
4428 * information through a {@link ActivityOptions} bundle to
4429 * {@link #startActivity(Intent, Bundle) or a related function. This allows
4430 * you to specify a custom animation even when starting an activity from
4431 * outside the context of the current top activity.
4432 *
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004433 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004434 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004435 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07004436 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004437 */
4438 public void overridePendingTransition(int enterAnim, int exitAnim) {
4439 try {
4440 ActivityManagerNative.getDefault().overridePendingTransition(
4441 mToken, getPackageName(), enterAnim, exitAnim);
4442 } catch (RemoteException e) {
4443 }
4444 }
RoboErik55011652014-07-09 15:05:53 -07004445
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004446 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 * Call this to set the result that your activity will return to its
4448 * caller.
RoboErik55011652014-07-09 15:05:53 -07004449 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 * @param resultCode The result code to propagate back to the originating
4451 * activity, often RESULT_CANCELED or RESULT_OK
RoboErik55011652014-07-09 15:05:53 -07004452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 * @see #RESULT_CANCELED
4454 * @see #RESULT_OK
4455 * @see #RESULT_FIRST_USER
4456 * @see #setResult(int, Intent)
4457 */
4458 public final void setResult(int resultCode) {
4459 synchronized (this) {
4460 mResultCode = resultCode;
4461 mResultData = null;
4462 }
4463 }
4464
4465 /**
4466 * Call this to set the result that your activity will return to its
4467 * caller.
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004468 *
4469 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent
4470 * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
4471 * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
4472 * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the
4473 * Activity receiving the result access to the specific URIs in the Intent.
4474 * Access will remain until the Activity has finished (it will remain across the hosting
4475 * process being killed and other temporary destruction) and will be added
4476 * to any existing set of URI permissions it already holds.
4477 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 * @param resultCode The result code to propagate back to the originating
4479 * activity, often RESULT_CANCELED or RESULT_OK
4480 * @param data The data to propagate back to the originating activity.
RoboErik55011652014-07-09 15:05:53 -07004481 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 * @see #RESULT_CANCELED
4483 * @see #RESULT_OK
4484 * @see #RESULT_FIRST_USER
4485 * @see #setResult(int)
4486 */
4487 public final void setResult(int resultCode, Intent data) {
4488 synchronized (this) {
4489 mResultCode = resultCode;
4490 mResultData = data;
4491 }
4492 }
4493
4494 /**
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004495 * Return information about who launched this activity. If the launching Intent
4496 * contains an {@link android.content.Intent#EXTRA_REFERRER Intent.EXTRA_REFERRER},
4497 * that will be returned as-is; otherwise, if known, an
4498 * {@link Intent#URI_ANDROID_APP_SCHEME android-app:} referrer URI containing the
4499 * package name that started the Intent will be returned. This may return null if no
4500 * referrer can be identified -- it is neither explicitly specified, nor is it known which
4501 * application package was involved.
4502 *
4503 * <p>If called while inside the handling of {@link #onNewIntent}, this function will
4504 * return the referrer that submitted that new intent to the activity. Otherwise, it
4505 * always returns the referrer of the original Intent.</p>
4506 *
4507 * <p>Note that this is <em>not</em> a security feature -- you can not trust the
4508 * referrer information, applications can spoof it.</p>
4509 */
4510 @Nullable
4511 public Uri getReferrer() {
4512 Intent intent = getIntent();
4513 Uri referrer = intent.getParcelableExtra(Intent.EXTRA_REFERRER);
4514 if (referrer != null) {
4515 return referrer;
4516 }
4517 String referrerName = intent.getStringExtra(Intent.EXTRA_REFERRER_NAME);
4518 if (referrerName != null) {
4519 return Uri.parse(referrerName);
4520 }
4521 if (mReferrer != null) {
4522 return new Uri.Builder().scheme("android-app").authority(mReferrer).build();
4523 }
4524 return null;
4525 }
4526
4527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 * Return the name of the package that invoked this activity. This is who
4529 * the data in {@link #setResult setResult()} will be sent to. You can
4530 * use this information to validate that the recipient is allowed to
4531 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07004532 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07004533 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07004534 * did not use the {@link #startActivityForResult}
4535 * form that includes a request code), then the calling package will be
Dianne Hackborn7b924672013-04-11 18:08:42 -07004536 * null.</p>
4537 *
4538 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
4539 * the result from this method was unstable. If the process hosting the calling
4540 * package was no longer running, it would return null instead of the proper package
4541 * name. You can use {@link #getCallingActivity()} and retrieve the package name
4542 * from that instead.</p>
RoboErik55011652014-07-09 15:05:53 -07004543 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 * @return The package of the activity that will receive your
4545 * reply, or null if none.
4546 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004547 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 public String getCallingPackage() {
4549 try {
4550 return ActivityManagerNative.getDefault().getCallingPackage(mToken);
4551 } catch (RemoteException e) {
4552 return null;
4553 }
4554 }
4555
4556 /**
4557 * Return the name of the activity that invoked this activity. This is
4558 * who the data in {@link #setResult setResult()} will be sent to. You
4559 * can use this information to validate that the recipient is allowed to
4560 * receive the data.
RoboErik55011652014-07-09 15:05:53 -07004561 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07004562 * <p class="note">Note: if the calling activity is not expecting a result (that is it
RoboErik55011652014-07-09 15:05:53 -07004563 * did not use the {@link #startActivityForResult}
4564 * form that includes a request code), then the calling package will be
4565 * null.
4566 *
Dianne Hackborn7b924672013-04-11 18:08:42 -07004567 * @return The ComponentName of the activity that will receive your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 * reply, or null if none.
4569 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004570 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 public ComponentName getCallingActivity() {
4572 try {
4573 return ActivityManagerNative.getDefault().getCallingActivity(mToken);
4574 } catch (RemoteException e) {
4575 return null;
4576 }
4577 }
4578
4579 /**
4580 * Control whether this activity's main window is visible. This is intended
4581 * only for the special case of an activity that is not going to show a
4582 * UI itself, but can't just finish prior to onResume() because it needs
4583 * to wait for a service binding or such. Setting this to false allows
4584 * you to prevent your UI from being shown during that time.
RoboErik55011652014-07-09 15:05:53 -07004585 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 * <p>The default value for this is taken from the
4587 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
4588 */
4589 public void setVisible(boolean visible) {
4590 if (mVisibleFromClient != visible) {
4591 mVisibleFromClient = visible;
4592 if (mVisibleFromServer) {
4593 if (visible) makeVisible();
4594 else mDecor.setVisibility(View.INVISIBLE);
4595 }
4596 }
4597 }
RoboErik55011652014-07-09 15:05:53 -07004598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 void makeVisible() {
4600 if (!mWindowAdded) {
4601 ViewManager wm = getWindowManager();
4602 wm.addView(mDecor, getWindow().getAttributes());
4603 mWindowAdded = true;
4604 }
4605 mDecor.setVisibility(View.VISIBLE);
4606 }
RoboErik55011652014-07-09 15:05:53 -07004607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 /**
4609 * Check to see whether this activity is in the process of finishing,
4610 * either because you called {@link #finish} on it or someone else
4611 * has requested that it finished. This is often used in
4612 * {@link #onPause} to determine whether the activity is simply pausing or
4613 * completely finishing.
RoboErik55011652014-07-09 15:05:53 -07004614 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004615 * @return If the activity is finishing, returns true; else returns false.
RoboErik55011652014-07-09 15:05:53 -07004616 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 * @see #finish
4618 */
4619 public boolean isFinishing() {
4620 return mFinished;
4621 }
4622
4623 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07004624 * Returns true if the final {@link #onDestroy()} call has been made
4625 * on the Activity, so this instance is now dead.
4626 */
4627 public boolean isDestroyed() {
4628 return mDestroyed;
4629 }
4630
4631 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05004632 * Check to see whether this activity is in the process of being destroyed in order to be
4633 * recreated with a new configuration. This is often used in
4634 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
4635 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
RoboErik55011652014-07-09 15:05:53 -07004636 *
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05004637 * @return If the activity is being torn down in order to be recreated with a new configuration,
4638 * returns true; else returns false.
4639 */
4640 public boolean isChangingConfigurations() {
4641 return mChangingConfigurations;
4642 }
4643
4644 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004645 * Cause this Activity to be recreated with a new instance. This results
4646 * in essentially the same flow as when the Activity is created due to
4647 * a configuration change -- the current instance will go through its
4648 * lifecycle to {@link #onDestroy} and a new instance then created after it.
4649 */
4650 public void recreate() {
4651 if (mParent != null) {
4652 throw new IllegalStateException("Can only be called on top-level activity");
4653 }
4654 if (Looper.myLooper() != mMainThread.getLooper()) {
4655 throw new IllegalStateException("Must be called from main thread");
4656 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004657 mMainThread.requestRelaunchActivity(mToken, null, null, 0, false, null, null, false);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004658 }
4659
4660 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07004661 * Finishes the current activity and specifies whether to remove the task associated with this
4662 * activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 */
Winson Chung3b3f4642014-04-22 10:08:18 -07004664 private void finish(boolean finishTask) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004665 if (mParent == null) {
4666 int resultCode;
4667 Intent resultData;
4668 synchronized (this) {
4669 resultCode = mResultCode;
4670 resultData = mResultData;
4671 }
Joe Onorato43a17652011-04-06 19:22:23 -07004672 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004673 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04004674 if (resultData != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004675 resultData.prepareToLeaveProcess();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04004676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 if (ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07004678 .finishActivity(mToken, resultCode, resultData, finishTask)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004679 mFinished = true;
4680 }
4681 } catch (RemoteException e) {
4682 // Empty
4683 }
4684 } else {
4685 mParent.finishFromChild(this);
4686 }
4687 }
4688
4689 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07004690 * Call this when your activity is done and should be closed. The
4691 * ActivityResult is propagated back to whoever launched you via
4692 * onActivityResult().
4693 */
4694 public void finish() {
4695 finish(false);
4696 }
4697
4698 /**
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07004699 * Finish this activity as well as all activities immediately below it
4700 * in the current task that have the same affinity. This is typically
4701 * used when an application can be launched on to another task (such as
4702 * from an ACTION_VIEW of a content type it understands) and the user
4703 * has used the up navigation to switch out of the current task and in
4704 * to its own task. In this case, if the user has navigated down into
4705 * any other activities of the second application, all of those should
4706 * be removed from the original task as part of the task switch.
4707 *
4708 * <p>Note that this finish does <em>not</em> allow you to deliver results
4709 * to the previous activity, and an exception will be thrown if you are trying
4710 * to do so.</p>
4711 */
4712 public void finishAffinity() {
4713 if (mParent != null) {
4714 throw new IllegalStateException("Can not be called from an embedded activity");
4715 }
4716 if (mResultCode != RESULT_CANCELED || mResultData != null) {
4717 throw new IllegalStateException("Can not be called to deliver a result");
4718 }
4719 try {
4720 if (ActivityManagerNative.getDefault().finishActivityAffinity(mToken)) {
4721 mFinished = true;
4722 }
4723 } catch (RemoteException e) {
4724 // Empty
4725 }
4726 }
4727
4728 /**
RoboErik55011652014-07-09 15:05:53 -07004729 * This is called when a child activity of this one calls its
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004730 * {@link #finish} method. The default implementation simply calls
4731 * finish() on this activity (the parent), finishing the entire group.
RoboErik55011652014-07-09 15:05:53 -07004732 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004733 * @param child The activity making the call.
RoboErik55011652014-07-09 15:05:53 -07004734 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004735 * @see #finish
4736 */
4737 public void finishFromChild(Activity child) {
4738 finish();
4739 }
4740
4741 /**
George Mountcb4b7d92014-02-25 10:47:55 -08004742 * Reverses the Activity Scene entry Transition and triggers the calling Activity
4743 * to reverse its exit Transition. When the exit Transition completes,
4744 * {@link #finish()} is called. If no entry Transition was used, finish() is called
4745 * immediately and the Activity exit Transition is run.
George Mount62ab9b72014-05-02 13:51:17 -07004746 * @see android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, android.util.Pair[])
George Mountcb4b7d92014-02-25 10:47:55 -08004747 */
Craig Mautner73f843d2014-05-19 09:42:28 -07004748 public void finishAfterTransition() {
George Mount62ab9b72014-05-02 13:51:17 -07004749 if (!mActivityTransitionState.startExitBackTransition(this)) {
George Mount31a21722014-03-24 17:44:36 -07004750 finish();
4751 }
George Mountcb4b7d92014-02-25 10:47:55 -08004752 }
4753
4754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 * Force finish another activity that you had previously started with
4756 * {@link #startActivityForResult}.
RoboErik55011652014-07-09 15:05:53 -07004757 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 * @param requestCode The request code of the activity that you had
4759 * given to startActivityForResult(). If there are multiple
4760 * activities started with this request code, they
4761 * will all be finished.
4762 */
4763 public void finishActivity(int requestCode) {
4764 if (mParent == null) {
4765 try {
4766 ActivityManagerNative.getDefault()
4767 .finishSubActivity(mToken, mEmbeddedID, requestCode);
4768 } catch (RemoteException e) {
4769 // Empty
4770 }
4771 } else {
4772 mParent.finishActivityFromChild(this, requestCode);
4773 }
4774 }
4775
4776 /**
4777 * This is called when a child activity of this one calls its
4778 * finishActivity().
RoboErik55011652014-07-09 15:05:53 -07004779 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 * @param child The activity making the call.
4781 * @param requestCode Request code that had been used to start the
4782 * activity.
4783 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004784 public void finishActivityFromChild(@NonNull Activity child, int requestCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 try {
4786 ActivityManagerNative.getDefault()
4787 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
4788 } catch (RemoteException e) {
4789 // Empty
4790 }
4791 }
4792
4793 /**
Winson Chung3b3f4642014-04-22 10:08:18 -07004794 * Call this when your activity is done and should be closed and the task should be completely
4795 * removed as a part of finishing the Activity.
4796 */
4797 public void finishAndRemoveTask() {
4798 finish(true);
4799 }
4800
4801 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004802 * Ask that the local app instance of this activity be released to free up its memory.
4803 * This is asking for the activity to be destroyed, but does <b>not</b> finish the activity --
4804 * a new instance of the activity will later be re-created if needed due to the user
4805 * navigating back to it.
4806 *
4807 * @return Returns true if the activity was in a state that it has started the process
4808 * of destroying its current instance; returns false if for any reason this could not
4809 * be done: it is currently visible to the user, it is already being destroyed, it is
4810 * being finished, it hasn't yet saved its state, etc.
4811 */
4812 public boolean releaseInstance() {
4813 try {
4814 return ActivityManagerNative.getDefault().releaseActivityInstance(mToken);
4815 } catch (RemoteException e) {
4816 // Empty
4817 }
4818 return false;
4819 }
4820
4821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 * Called when an activity you launched exits, giving you the requestCode
4823 * you started it with, the resultCode it returned, and any additional
4824 * data from it. The <var>resultCode</var> will be
4825 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
4826 * didn't return any result, or crashed during its operation.
RoboErik55011652014-07-09 15:05:53 -07004827 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 * <p>You will receive this call immediately before onResume() when your
4829 * activity is re-starting.
RoboErik55011652014-07-09 15:05:53 -07004830 *
Ricardo Cervera92f6a742014-04-04 11:17:06 -07004831 * <p>This method is never invoked if your activity sets
4832 * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
4833 * <code>true</code>.
Ricardo Cervera93f94c22015-01-16 09:54:30 -08004834 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 * @param requestCode The integer request code originally supplied to
4836 * startActivityForResult(), allowing you to identify who this
4837 * result came from.
4838 * @param resultCode The integer result code returned by the child activity
4839 * through its setResult().
4840 * @param data An Intent, which can return result data to the caller
4841 * (various data can be attached to Intent "extras").
RoboErik55011652014-07-09 15:05:53 -07004842 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004843 * @see #startActivityForResult
4844 * @see #createPendingResult
4845 * @see #setResult(int)
4846 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004847 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 }
4849
4850 /**
George Mount62ab9b72014-05-02 13:51:17 -07004851 * Called when an activity you launched with an activity transition exposes this
4852 * Activity through a returning activity transition, giving you the resultCode
4853 * and any additional data from it. This method will only be called if the activity
4854 * set a result code other than {@link #RESULT_CANCELED} and it supports activity
George Mount9826f632014-09-11 08:50:09 -07004855 * transitions with {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount62ab9b72014-05-02 13:51:17 -07004856 *
4857 * <p>The purpose of this function is to let the called Activity send a hint about
4858 * its state so that this underlying Activity can prepare to be exposed. A call to
4859 * this method does not guarantee that the called Activity has or will be exiting soon.
4860 * It only indicates that it will expose this Activity's Window and it has
4861 * some data to pass to prepare it.</p>
4862 *
4863 * @param resultCode The integer result code returned by the child activity
4864 * through its setResult().
4865 * @param data An Intent, which can return result data to the caller
4866 * (various data can be attached to Intent "extras").
4867 */
Craig Mautner7a629c22014-09-04 14:57:04 -07004868 public void onActivityReenter(int resultCode, Intent data) {
George Mount62ab9b72014-05-02 13:51:17 -07004869 }
4870
4871 /**
RoboErik55011652014-07-09 15:05:53 -07004872 * Create a new PendingIntent object which you can hand to others
4873 * for them to use to send result data back to your
4874 * {@link #onActivityResult} callback. The created object will be either
4875 * one-shot (becoming invalid after a result is sent back) or multiple
4876 * (allowing any number of results to be sent through it).
4877 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 * @param requestCode Private request code for the sender that will be
4879 * associated with the result data when it is returned. The sender can not
4880 * modify this value, allowing you to identify incoming results.
4881 * @param data Default data to supply in the result, which may be modified
4882 * by the sender.
4883 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
4884 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
4885 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
4886 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
4887 * or any of the flags as supported by
4888 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
4889 * of the intent that can be supplied when the actual send happens.
RoboErik55011652014-07-09 15:05:53 -07004890 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004891 * @return Returns an existing or new PendingIntent matching the given
4892 * parameters. May return null only if
4893 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
4894 * supplied.
RoboErik55011652014-07-09 15:05:53 -07004895 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 * @see PendingIntent
4897 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004898 public PendingIntent createPendingResult(int requestCode, @NonNull Intent data,
4899 @PendingIntent.Flags int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004900 String packageName = getPackageName();
4901 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07004902 data.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 IIntentSender target =
4904 ActivityManagerNative.getDefault().getIntentSender(
Dianne Hackborna4972e92012-03-14 10:38:05 -07004905 ActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004906 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn41203752012-08-31 14:05:51 -07004907 mEmbeddedID, requestCode, new Intent[] { data }, null, flags, null,
4908 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 return target != null ? new PendingIntent(target) : null;
4910 } catch (RemoteException e) {
4911 // Empty
4912 }
4913 return null;
4914 }
4915
4916 /**
4917 * Change the desired orientation of this activity. If the activity
4918 * is currently in the foreground or otherwise impacting the screen
4919 * orientation, the screen will immediately be changed (possibly causing
4920 * the activity to be restarted). Otherwise, this will be used the next
4921 * time the activity is visible.
RoboErik55011652014-07-09 15:05:53 -07004922 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 * @param requestedOrientation An orientation constant as used in
4924 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
4925 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004926 public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 if (mParent == null) {
4928 try {
4929 ActivityManagerNative.getDefault().setRequestedOrientation(
4930 mToken, requestedOrientation);
4931 } catch (RemoteException e) {
4932 // Empty
4933 }
4934 } else {
4935 mParent.setRequestedOrientation(requestedOrientation);
4936 }
4937 }
RoboErik55011652014-07-09 15:05:53 -07004938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 /**
4940 * Return the current requested orientation of the activity. This will
4941 * either be the orientation requested in its component's manifest, or
4942 * the last requested orientation given to
4943 * {@link #setRequestedOrientation(int)}.
RoboErik55011652014-07-09 15:05:53 -07004944 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945 * @return Returns an orientation constant as used in
4946 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
4947 */
Tor Norbyed9273d62013-05-30 15:59:53 -07004948 @ActivityInfo.ScreenOrientation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949 public int getRequestedOrientation() {
4950 if (mParent == null) {
4951 try {
4952 return ActivityManagerNative.getDefault()
4953 .getRequestedOrientation(mToken);
4954 } catch (RemoteException e) {
4955 // Empty
4956 }
4957 } else {
4958 return mParent.getRequestedOrientation();
4959 }
4960 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
4961 }
RoboErik55011652014-07-09 15:05:53 -07004962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 /**
4964 * Return the identifier of the task this activity is in. This identifier
4965 * will remain the same for the lifetime of the activity.
RoboErik55011652014-07-09 15:05:53 -07004966 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004967 * @return Task identifier, an opaque integer.
4968 */
4969 public int getTaskId() {
4970 try {
4971 return ActivityManagerNative.getDefault()
4972 .getTaskForActivity(mToken, false);
4973 } catch (RemoteException e) {
4974 return -1;
4975 }
4976 }
4977
4978 /**
4979 * Return whether this activity is the root of a task. The root is the
4980 * first activity in a task.
RoboErik55011652014-07-09 15:05:53 -07004981 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982 * @return True if this is the root activity, else false.
4983 */
4984 public boolean isTaskRoot() {
4985 try {
4986 return ActivityManagerNative.getDefault()
4987 .getTaskForActivity(mToken, true) >= 0;
4988 } catch (RemoteException e) {
4989 return false;
4990 }
4991 }
4992
4993 /**
4994 * Move the task containing this activity to the back of the activity
4995 * stack. The activity's order within the task is unchanged.
RoboErik55011652014-07-09 15:05:53 -07004996 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997 * @param nonRoot If false then this only works if the activity is the root
4998 * of a task; if true it will work for any activity in
4999 * a task.
RoboErik55011652014-07-09 15:05:53 -07005000 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 * @return If the task was moved (or it was already at the
5002 * back) true is returned, else false.
5003 */
5004 public boolean moveTaskToBack(boolean nonRoot) {
5005 try {
5006 return ActivityManagerNative.getDefault().moveActivityTaskToBack(
5007 mToken, nonRoot);
5008 } catch (RemoteException e) {
5009 // Empty
5010 }
5011 return false;
5012 }
5013
5014 /**
5015 * Returns class name for this activity with the package prefix removed.
5016 * This is the default name used to read and write settings.
RoboErik55011652014-07-09 15:05:53 -07005017 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 * @return The local class name.
5019 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005020 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005021 public String getLocalClassName() {
5022 final String pkg = getPackageName();
5023 final String cls = mComponent.getClassName();
5024 int packageLen = pkg.length();
5025 if (!cls.startsWith(pkg) || cls.length() <= packageLen
5026 || cls.charAt(packageLen) != '.') {
5027 return cls;
5028 }
5029 return cls.substring(packageLen+1);
5030 }
RoboErik55011652014-07-09 15:05:53 -07005031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005032 /**
5033 * Returns complete component name of this activity.
RoboErik55011652014-07-09 15:05:53 -07005034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 * @return Returns the complete component name for this activity
5036 */
5037 public ComponentName getComponentName()
5038 {
5039 return mComponent;
5040 }
5041
5042 /**
5043 * Retrieve a {@link SharedPreferences} object for accessing preferences
5044 * that are private to this activity. This simply calls the underlying
5045 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
5046 * class name as the preferences name.
RoboErik55011652014-07-09 15:05:53 -07005047 *
5048 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
5049 * operation, {@link #MODE_WORLD_READABLE} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 * {@link #MODE_WORLD_WRITEABLE} to control permissions.
5051 *
5052 * @return Returns the single SharedPreferences instance that can be used
5053 * to retrieve and modify the preference values.
5054 */
5055 public SharedPreferences getPreferences(int mode) {
5056 return getSharedPreferences(getLocalClassName(), mode);
5057 }
RoboErik55011652014-07-09 15:05:53 -07005058
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005059 private void ensureSearchManager() {
5060 if (mSearchManager != null) {
5061 return;
5062 }
RoboErik55011652014-07-09 15:05:53 -07005063
Amith Yamasanie9ce3f02010-01-25 09:15:50 -08005064 mSearchManager = new SearchManager(this, null);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005065 }
Tor Norbyed9273d62013-05-30 15:59:53 -07005066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 @Override
Tor Norbyed9273d62013-05-30 15:59:53 -07005068 public Object getSystemService(@ServiceName @NonNull String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 if (getBaseContext() == null) {
5070 throw new IllegalStateException(
5071 "System services not available to Activities before onCreate()");
5072 }
5073
5074 if (WINDOW_SERVICE.equals(name)) {
5075 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005076 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005077 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01005078 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 }
5080 return super.getSystemService(name);
5081 }
5082
5083 /**
5084 * Change the title associated with this activity. If this is a
5085 * top-level activity, the title for its window will change. If it
5086 * is an embedded activity, the parent can do whatever it wants
5087 * with it.
5088 */
5089 public void setTitle(CharSequence title) {
5090 mTitle = title;
5091 onTitleChanged(title, mTitleColor);
5092
5093 if (mParent != null) {
5094 mParent.onChildTitleChanged(this, title);
5095 }
5096 }
5097
5098 /**
5099 * Change the title associated with this activity. If this is a
5100 * top-level activity, the title for its window will change. If it
5101 * is an embedded activity, the parent can do whatever it wants
5102 * with it.
5103 */
5104 public void setTitle(int titleId) {
5105 setTitle(getText(titleId));
5106 }
5107
Alan Viverette2525d9c2013-11-15 14:42:19 -08005108 /**
5109 * Change the color of the title associated with this activity.
5110 * <p>
5111 * This method is deprecated starting in API Level 11 and replaced by action
5112 * bar styles. For information on styling the Action Bar, read the <a
5113 * href="{@docRoot} guide/topics/ui/actionbar.html">Action Bar</a> developer
5114 * guide.
5115 *
5116 * @deprecated Use action bar styles instead.
5117 */
5118 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005119 public void setTitleColor(int textColor) {
5120 mTitleColor = textColor;
5121 onTitleChanged(mTitle, textColor);
5122 }
5123
5124 public final CharSequence getTitle() {
5125 return mTitle;
5126 }
5127
5128 public final int getTitleColor() {
5129 return mTitleColor;
5130 }
5131
5132 protected void onTitleChanged(CharSequence title, int color) {
5133 if (mTitleReady) {
5134 final Window win = getWindow();
5135 if (win != null) {
5136 win.setTitle(title);
5137 if (color != 0) {
5138 win.setTitleColor(color);
5139 }
5140 }
Adam Powellaf2d8592014-08-26 18:06:40 -07005141 if (mActionBar != null) {
5142 mActionBar.setWindowTitle(title);
5143 }
Adam Powella557fdc2014-08-21 18:05:53 -07005144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 }
5146
5147 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
5148 }
5149
5150 /**
Winson Chunga449dc02014-05-16 11:15:04 -07005151 * Sets information describing the task with this activity for presentation inside the Recents
5152 * System UI. When {@link ActivityManager#getRecentTasks} is called, the activities of each task
5153 * are traversed in order from the topmost activity to the bottommost. The traversal continues
5154 * for each property until a suitable value is found. For each task the taskDescription will be
5155 * returned in {@link android.app.ActivityManager.TaskDescription}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07005156 *
5157 * @see ActivityManager#getRecentTasks
Winson Chunga449dc02014-05-16 11:15:04 -07005158 * @see android.app.ActivityManager.TaskDescription
Craig Mautner2fbd7542014-03-21 09:34:07 -07005159 *
Winson Chunga449dc02014-05-16 11:15:04 -07005160 * @param taskDescription The TaskDescription properties that describe the task with this activity
Craig Mautner2fbd7542014-03-21 09:34:07 -07005161 */
Winson Chunga449dc02014-05-16 11:15:04 -07005162 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
5163 ActivityManager.TaskDescription td;
5164 // Scale the icon down to something reasonable if it is provided
Craig Mautner648f69b2014-09-18 14:16:26 -07005165 if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
Craig Mautner2fbd7542014-03-21 09:34:07 -07005166 final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
Winson Chunga449dc02014-05-16 11:15:04 -07005167 final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size, true);
5168 td = new ActivityManager.TaskDescription(taskDescription.getLabel(), icon,
5169 taskDescription.getPrimaryColor());
5170 } else {
5171 td = taskDescription;
Craig Mautner2fbd7542014-03-21 09:34:07 -07005172 }
5173 try {
Winson Chunga449dc02014-05-16 11:15:04 -07005174 ActivityManagerNative.getDefault().setTaskDescription(mToken, td);
Craig Mautner2fbd7542014-03-21 09:34:07 -07005175 } catch (RemoteException e) {
5176 }
5177 }
5178
5179 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 * Sets the visibility of the progress bar in the title.
5181 * <p>
5182 * In order for the progress bar to be shown, the feature must be requested
5183 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 * @param visible Whether to show the progress bars in the title.
5186 */
5187 public final void setProgressBarVisibility(boolean visible) {
5188 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
5189 Window.PROGRESS_VISIBILITY_OFF);
5190 }
5191
5192 /**
5193 * Sets the visibility of the indeterminate progress bar in the title.
5194 * <p>
5195 * In order for the progress bar to be shown, the feature must be requested
5196 * via {@link #requestWindowFeature(int)}.
5197 *
5198 * @param visible Whether to show the progress bars in the title.
5199 */
5200 public final void setProgressBarIndeterminateVisibility(boolean visible) {
5201 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
5202 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
5203 }
RoboErik55011652014-07-09 15:05:53 -07005204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 /**
5206 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
5207 * is always indeterminate).
5208 * <p>
5209 * In order for the progress bar to be shown, the feature must be requested
5210 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005211 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
5213 */
5214 public final void setProgressBarIndeterminate(boolean indeterminate) {
5215 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
George Mounte1803372014-02-26 19:00:52 +00005216 indeterminate ? Window.PROGRESS_INDETERMINATE_ON
5217 : Window.PROGRESS_INDETERMINATE_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 }
RoboErik55011652014-07-09 15:05:53 -07005219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 /**
5221 * Sets the progress for the progress bars in the title.
5222 * <p>
5223 * In order for the progress bar to be shown, the feature must be requested
5224 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005225 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 * @param progress The progress for the progress bar. Valid ranges are from
5227 * 0 to 10000 (both inclusive). If 10000 is given, the progress
5228 * bar will be completely filled and will fade out.
5229 */
5230 public final void setProgress(int progress) {
5231 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
5232 }
RoboErik55011652014-07-09 15:05:53 -07005233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 /**
5235 * Sets the secondary progress for the progress bar in the title. This
5236 * progress is drawn between the primary progress (set via
5237 * {@link #setProgress(int)} and the background. It can be ideal for media
5238 * scenarios such as showing the buffering progress while the default
5239 * progress shows the play progress.
5240 * <p>
5241 * In order for the progress bar to be shown, the feature must be requested
5242 * via {@link #requestWindowFeature(int)}.
RoboErik55011652014-07-09 15:05:53 -07005243 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
5245 * 0 to 10000 (both inclusive).
5246 */
5247 public final void setSecondaryProgress(int secondaryProgress) {
5248 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
5249 secondaryProgress + Window.PROGRESS_SECONDARY_START);
5250 }
5251
5252 /**
5253 * Suggests an audio stream whose volume should be changed by the hardware
5254 * volume controls.
5255 * <p>
5256 * The suggested audio stream will be tied to the window of this Activity.
RoboErik55011652014-07-09 15:05:53 -07005257 * Volume requests which are received while the Activity is in the
5258 * foreground will affect this stream.
5259 * <p>
5260 * It is not guaranteed that the hardware volume controls will always change
5261 * this stream's volume (for example, if a call is in progress, its stream's
5262 * volume may be changed instead). To reset back to the default, use
5263 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
5264 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005265 * @param streamType The type of the audio stream whose volume should be
RoboErik55011652014-07-09 15:05:53 -07005266 * changed by the hardware volume controls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 */
5268 public final void setVolumeControlStream(int streamType) {
5269 getWindow().setVolumeControlStream(streamType);
5270 }
5271
5272 /**
5273 * Gets the suggested audio stream whose volume should be changed by the
Tor Norbyed9273d62013-05-30 15:59:53 -07005274 * hardware volume controls.
RoboErik55011652014-07-09 15:05:53 -07005275 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 * @return The suggested audio stream type whose volume should be changed by
5277 * the hardware volume controls.
5278 * @see #setVolumeControlStream(int)
5279 */
5280 public final int getVolumeControlStream() {
5281 return getWindow().getVolumeControlStream();
5282 }
RoboErik55011652014-07-09 15:05:53 -07005283
5284 /**
5285 * Sets a {@link MediaController} to send media keys and volume changes to.
5286 * <p>
5287 * The controller will be tied to the window of this Activity. Media key and
5288 * volume events which are received while the Activity is in the foreground
5289 * will be forwarded to the controller and used to invoke transport controls
5290 * or adjust the volume. This may be used instead of or in addition to
5291 * {@link #setVolumeControlStream} to affect a specific session instead of a
5292 * specific stream.
5293 * <p>
5294 * It is not guaranteed that the hardware volume controls will always change
5295 * this session's volume (for example, if a call is in progress, its
5296 * stream's volume may be changed instead). To reset back to the default use
5297 * null as the controller.
5298 *
5299 * @param controller The controller for the session which should receive
5300 * media keys and volume changes.
5301 */
5302 public final void setMediaController(MediaController controller) {
5303 getWindow().setMediaController(controller);
5304 }
5305
5306 /**
5307 * Gets the controller which should be receiving media key and volume events
5308 * while this activity is in the foreground.
5309 *
5310 * @return The controller which should receive events.
5311 * @see #setMediaController(android.media.session.MediaController)
5312 */
5313 public final MediaController getMediaController() {
5314 return getWindow().getMediaController();
5315 }
5316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 /**
5318 * Runs the specified action on the UI thread. If the current thread is the UI
5319 * thread, then the action is executed immediately. If the current thread is
5320 * not the UI thread, the action is posted to the event queue of the UI thread.
5321 *
5322 * @param action the action to run on the UI thread
5323 */
5324 public final void runOnUiThread(Runnable action) {
5325 if (Thread.currentThread() != mUiThread) {
5326 mHandler.post(action);
5327 } else {
5328 action.run();
5329 }
5330 }
5331
5332 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005333 * Standard implementation of
5334 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
5335 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005336 * This implementation does nothing and is for
5337 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
5338 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
5339 *
5340 * @see android.view.LayoutInflater#createView
5341 * @see android.view.Window#getLayoutInflater
5342 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005343 @Nullable
Dianne Hackborn625ac272010-09-17 18:29:22 -07005344 public View onCreateView(String name, Context context, AttributeSet attrs) {
5345 return null;
5346 }
5347
5348 /**
5349 * Standard implementation of
5350 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
5351 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005352 * This implementation handles <fragment> tags to embed fragments inside
5353 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 *
5355 * @see android.view.LayoutInflater#createView
5356 * @see android.view.Window#getLayoutInflater
5357 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07005358 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005359 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07005360 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07005361 }
RoboErik55011652014-07-09 15:05:53 -07005362
Adam Powell371a8092014-06-20 12:51:12 -07005363 return mFragments.onCreateView(parent, name, context, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 }
5365
Daniel Sandler69a48172010-06-23 16:29:36 -04005366 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07005367 * Print the Activity's state into the given stream. This gets invoked if
Jeff Sharkey5554b702012-04-11 18:30:51 -07005368 * you run "adb shell dumpsys activity &lt;activity_component_name&gt;".
Dianne Hackborn625ac272010-09-17 18:29:22 -07005369 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08005370 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07005371 * @param fd The raw file descriptor that the dump is being sent to.
5372 * @param writer The PrintWriter to which you should dump your state. This will be
5373 * closed for you after you return.
5374 * @param args additional arguments to the dump request.
5375 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08005376 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07005377 dumpInner(prefix, fd, writer, args);
5378 }
5379
5380 void dumpInner(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08005381 writer.print(prefix); writer.print("Local Activity ");
5382 writer.print(Integer.toHexString(System.identityHashCode(this)));
5383 writer.println(" State:");
5384 String innerPrefix = prefix + " ";
5385 writer.print(innerPrefix); writer.print("mResumed=");
5386 writer.print(mResumed); writer.print(" mStopped=");
5387 writer.print(mStopped); writer.print(" mFinished=");
5388 writer.println(mFinished);
5389 writer.print(innerPrefix); writer.print("mLoadersStarted=");
5390 writer.println(mLoadersStarted);
5391 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
5392 writer.println(mChangingConfigurations);
5393 writer.print(innerPrefix); writer.print("mCurrentConfig=");
5394 writer.println(mCurrentConfig);
Jeff Brown5182c782013-10-15 20:31:52 -07005395
Dianne Hackborn30d71892010-12-11 10:37:55 -08005396 if (mLoaderManager != null) {
5397 writer.print(prefix); writer.print("Loader Manager ");
5398 writer.print(Integer.toHexString(System.identityHashCode(mLoaderManager)));
5399 writer.println(":");
5400 mLoaderManager.dump(prefix + " ", fd, writer, args);
5401 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005402
Jeff Brown5182c782013-10-15 20:31:52 -07005403 mFragments.dump(prefix, fd, writer, args);
5404
Michael Wright5f48dc72013-11-01 12:42:49 -07005405 if (getWindow() != null &&
5406 getWindow().peekDecorView() != null &&
5407 getWindow().peekDecorView().getViewRootImpl() != null) {
5408 getWindow().peekDecorView().getViewRootImpl().dump(prefix, fd, writer, args);
5409 }
Jeff Brown5182c782013-10-15 20:31:52 -07005410
5411 mHandler.getLooper().dump(new PrintWriterPrinter(writer), prefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07005412 }
5413
5414 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04005415 * Bit indicating that this activity is "immersive" and should not be
5416 * interrupted by notifications if possible.
5417 *
5418 * This value is initially set by the manifest property
5419 * <code>android:immersive</code> but may be changed at runtime by
5420 * {@link #setImmersive}.
5421 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07005422 * @see #setImmersive(boolean)
Daniel Sandler69a48172010-06-23 16:29:36 -04005423 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5424 */
5425 public boolean isImmersive() {
5426 try {
5427 return ActivityManagerNative.getDefault().isImmersive(mToken);
5428 } catch (RemoteException e) {
5429 return false;
5430 }
5431 }
5432
5433 /**
Craig Mautnerd61dc202014-07-07 11:09:11 -07005434 * Indication of whether this is the highest level activity in this task. Can be used to
5435 * determine whether an activity launched by this activity was placed in the same task or
5436 * another task.
5437 *
5438 * @return true if this is the topmost, non-finishing activity in its task.
Craig Mautnerd61dc202014-07-07 11:09:11 -07005439 */
George Mountff243282014-07-07 16:12:07 -07005440 private boolean isTopOfTask() {
Craig Mautnerd61dc202014-07-07 11:09:11 -07005441 try {
5442 return ActivityManagerNative.getDefault().isTopOfTask(mToken);
5443 } catch (RemoteException e) {
5444 return false;
5445 }
5446 }
5447
5448 /**
Craig Mautner4addfc52013-06-25 08:05:45 -07005449 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a
5450 * fullscreen opaque Activity.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005451 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005452 * Call this whenever the background of a translucent Activity has changed to become opaque.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005453 * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released.
5454 * <p>
Craig Mautner4addfc52013-06-25 08:05:45 -07005455 * This call has no effect on non-translucent activities or on activities with the
5456 * {@link android.R.attr#windowIsFloating} attribute.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005457 *
George Mount62ab9b72014-05-02 13:51:17 -07005458 * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener,
5459 * ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07005460 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005461 *
5462 * @hide
Craig Mautner4addfc52013-06-25 08:05:45 -07005463 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005464 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07005465 public void convertFromTranslucent() {
Craig Mautner4addfc52013-06-25 08:05:45 -07005466 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07005467 mTranslucentCallback = null;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005468 if (ActivityManagerNative.getDefault().convertFromTranslucent(mToken)) {
5469 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
5470 }
Craig Mautner4addfc52013-06-25 08:05:45 -07005471 } catch (RemoteException e) {
5472 // pass
5473 }
5474 }
5475
5476 /**
Craig Mautner5eda9b32013-07-02 11:58:16 -07005477 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
5478 * opaque to translucent following a call to {@link #convertFromTranslucent()}.
5479 * <p>
5480 * Calling this allows the Activity behind this one to be seen again. Once all such Activities
5481 * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will
5482 * be called indicating that it is safe to make this activity translucent again. Until
5483 * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
5484 * behind the frontmost Activity will be indeterminate.
5485 * <p>
5486 * This call has no effect on non-translucent activities or on activities with the
5487 * {@link android.R.attr#windowIsFloating} attribute.
5488 *
5489 * @param callback the method to call when all visible Activities behind this one have been
5490 * drawn and it is safe to make this Activity translucent again.
Craig Mautner233ceee2014-05-09 17:05:11 -07005491 * @param options activity options delivered to the activity below this one. The options
5492 * are retrieved using {@link #getActivityOptions}.
George Mount3cc716c2014-06-12 16:35:35 -07005493 * @return <code>true</code> if Window was opaque and will become translucent or
5494 * <code>false</code> if window was translucent and no change needed to be made.
Craig Mautner5eda9b32013-07-02 11:58:16 -07005495 *
5496 * @see #convertFromTranslucent()
5497 * @see TranslucentConversionListener
Chet Haaseabd3d772013-09-11 14:33:05 -07005498 *
5499 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07005500 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005501 @SystemApi
George Mount3cc716c2014-06-12 16:35:35 -07005502 public boolean convertToTranslucent(TranslucentConversionListener callback,
Jose Lima5517ea72014-06-10 12:31:43 -07005503 ActivityOptions options) {
Craig Mautner233ceee2014-05-09 17:05:11 -07005504 boolean drawComplete;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005505 try {
5506 mTranslucentCallback = callback;
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005507 mChangeCanvasToTranslucent =
Craig Mautner233ceee2014-05-09 17:05:11 -07005508 ActivityManagerNative.getDefault().convertToTranslucent(mToken, options);
George Mount9e183972014-09-03 12:35:09 -07005509 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
Craig Mautner233ceee2014-05-09 17:05:11 -07005510 drawComplete = true;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005511 } catch (RemoteException e) {
Craig Mautner233ceee2014-05-09 17:05:11 -07005512 // Make callback return as though it timed out.
5513 mChangeCanvasToTranslucent = false;
5514 drawComplete = false;
5515 }
5516 if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
5517 // Window is already translucent.
5518 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
Craig Mautner5eda9b32013-07-02 11:58:16 -07005519 }
George Mount3cc716c2014-06-12 16:35:35 -07005520 return mChangeCanvasToTranslucent;
Craig Mautner5eda9b32013-07-02 11:58:16 -07005521 }
5522
5523 /** @hide */
5524 void onTranslucentConversionComplete(boolean drawComplete) {
5525 if (mTranslucentCallback != null) {
5526 mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
5527 mTranslucentCallback = null;
5528 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07005529 if (mChangeCanvasToTranslucent) {
5530 WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
5531 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07005532 }
5533
Craig Mautnereb8abf72014-07-02 15:04:09 -07005534 /** @hide */
5535 public void onNewActivityOptions(ActivityOptions options) {
5536 mActivityTransitionState.setEnterActivityOptions(this, options);
5537 if (!mStopped) {
5538 mActivityTransitionState.enterReady(this);
5539 }
5540 }
5541
Craig Mautner5eda9b32013-07-02 11:58:16 -07005542 /**
Craig Mautner233ceee2014-05-09 17:05:11 -07005543 * Retrieve the ActivityOptions passed in from the launching activity or passed back
5544 * from an activity launched by this activity in its call to {@link
5545 * #convertToTranslucent(TranslucentConversionListener, ActivityOptions)}
5546 *
5547 * @return The ActivityOptions passed to {@link #convertToTranslucent}.
5548 * @hide
5549 */
5550 ActivityOptions getActivityOptions() {
5551 try {
5552 return ActivityManagerNative.getDefault().getActivityOptions(mToken);
5553 } catch (RemoteException e) {
5554 }
5555 return null;
5556 }
5557
5558 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07005559 * Activities that want to remain visible behind a translucent activity above them must call
Craig Mautner64ccb702014-10-01 09:38:40 -07005560 * this method anytime between the start of {@link #onResume()} and the return from
5561 * {@link #onPause()}. If this call is successful then the activity will remain visible after
5562 * {@link #onPause()} is called, and is allowed to continue playing media in the background.
5563 *
5564 * <p>The actions of this call are reset each time that this activity is brought to the
5565 * front. That is, every time {@link #onResume()} is called the activity will be assumed
5566 * to not have requested visible behind. Therefore, if you want this activity to continue to
5567 * be visible in the background you must call this method again.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005568 *
5569 * <p>Only fullscreen opaque activities may make this call. I.e. this call is a nop
5570 * for dialog and translucent activities.
5571 *
Craig Mautner64ccb702014-10-01 09:38:40 -07005572 * <p>Under all circumstances, the activity must stop playing and release resources prior to or
5573 * within a call to {@link #onVisibleBehindCanceled()} or if this call returns false.
5574 *
5575 * <p>False will be returned any time this method is called between the return of onPause and
Craig Mautneree2e45a2014-06-27 12:10:03 -07005576 * the next call to onResume.
5577 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005578 * @param visible true to notify the system that the activity wishes to be visible behind other
5579 * translucent activities, false to indicate otherwise. Resources must be
5580 * released when passing false to this method.
Craig Mautner64ccb702014-10-01 09:38:40 -07005581 * @return the resulting visibiity state. If true the activity will remain visible beyond
5582 * {@link #onPause()} if the next activity is translucent or not fullscreen. If false
5583 * then the activity may not count on being visible behind other translucent activities,
5584 * and must stop any media playback and release resources.
5585 * Returning false may occur in lieu of a call to {@link #onVisibleBehindCanceled()} so
5586 * the return value must be checked.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005587 *
Jose Limafcf70832014-08-27 23:09:05 -07005588 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07005589 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07005590 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005591 public boolean requestVisibleBehind(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07005592 if (!mResumed) {
Jose Lima4b6c6692014-08-12 17:41:12 -07005593 // Do not permit paused or stopped activities to do this.
5594 visible = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005595 }
5596 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07005597 mVisibleBehind = ActivityManagerNative.getDefault()
5598 .requestVisibleBehind(mToken, visible) && visible;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005599 } catch (RemoteException e) {
Jose Lima4b6c6692014-08-12 17:41:12 -07005600 mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005601 }
Jose Lima4b6c6692014-08-12 17:41:12 -07005602 return mVisibleBehind;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005603 }
5604
5605 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07005606 * Called when a translucent activity over this activity is becoming opaque or another
5607 * activity is being launched. Activities that override this method must call
Jose Limafcf70832014-08-27 23:09:05 -07005608 * <code>super.onVisibleBehindCanceled()</code> or a SuperNotCalledException will be thrown.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005609 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005610 * <p>When this method is called the activity has 500 msec to release any resources it may be
5611 * using while visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005612 * If the activity has not returned from this method in 500 msec the system will destroy
Jose Lima4b6c6692014-08-12 17:41:12 -07005613 * the activity and kill the process in order to recover the resources for another
Craig Mautneree2e45a2014-06-27 12:10:03 -07005614 * process. Otherwise {@link #onStop()} will be called following return.
5615 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005616 * @see #requestVisibleBehind(boolean)
5617 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07005618 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08005619 @CallSuper
Jose Limafcf70832014-08-27 23:09:05 -07005620 public void onVisibleBehindCanceled() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07005621 mCalled = true;
5622 }
5623
5624 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07005625 * Translucent activities may call this to determine if there is an activity below them that
5626 * is currently set to be visible in the background.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005627 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005628 * @return true if an activity below is set to visible according to the most recent call to
5629 * {@link #requestVisibleBehind(boolean)}, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005630 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005631 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07005632 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07005633 * @see #onBackgroundVisibleBehindChanged(boolean)
Craig Mautneree2e45a2014-06-27 12:10:03 -07005634 * @hide
5635 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005636 @SystemApi
5637 public boolean isBackgroundVisibleBehind() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07005638 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07005639 return ActivityManagerNative.getDefault().isBackgroundVisibleBehind(mToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07005640 } catch (RemoteException e) {
5641 }
5642 return false;
5643 }
5644
5645 /**
Jose Lima4b6c6692014-08-12 17:41:12 -07005646 * The topmost foreground activity will receive this call when the background visibility state
5647 * of the activity below it changes.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005648 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005649 * This call may be a consequence of {@link #requestVisibleBehind(boolean)} or might be
Craig Mautneree2e45a2014-06-27 12:10:03 -07005650 * due to a background activity finishing itself.
5651 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005652 * @param visible true if a background activity is visible, false otherwise.
Craig Mautneree2e45a2014-06-27 12:10:03 -07005653 *
Jose Lima4b6c6692014-08-12 17:41:12 -07005654 * @see #requestVisibleBehind(boolean)
Jose Limafcf70832014-08-27 23:09:05 -07005655 * @see #onVisibleBehindCanceled()
Jose Lima4b6c6692014-08-12 17:41:12 -07005656 * @hide
Craig Mautneree2e45a2014-06-27 12:10:03 -07005657 */
Jose Lima4b6c6692014-08-12 17:41:12 -07005658 @SystemApi
5659 public void onBackgroundVisibleBehindChanged(boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07005660 }
5661
5662 /**
Craig Mautner8746a472014-07-24 15:12:54 -07005663 * Activities cannot draw during the period that their windows are animating in. In order
5664 * to know when it is safe to begin drawing they can override this method which will be
5665 * called when the entering animation has completed.
5666 */
5667 public void onEnterAnimationComplete() {
5668 }
5669
5670 /**
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08005671 * @hide
5672 */
5673 public void dispatchEnterAnimationComplete() {
5674 onEnterAnimationComplete();
5675 if (getWindow() != null && getWindow().getDecorView() != null) {
5676 getWindow().getDecorView().getViewTreeObserver().dispatchOnEnterAnimationComplete();
5677 }
5678 }
5679
5680 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04005681 * Adjust the current immersive mode setting.
Christopher Tate73c2aee2012-03-15 16:27:14 -07005682 *
Daniel Sandler69a48172010-06-23 16:29:36 -04005683 * Note that changing this value will have no effect on the activity's
5684 * {@link android.content.pm.ActivityInfo} structure; that is, if
5685 * <code>android:immersive</code> is set to <code>true</code>
5686 * in the application's manifest entry for this activity, the {@link
5687 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
5688 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5689 * FLAG_IMMERSIVE} bit set.
5690 *
Christopher Tate73c2aee2012-03-15 16:27:14 -07005691 * @see #isImmersive()
Daniel Sandler69a48172010-06-23 16:29:36 -04005692 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
5693 */
5694 public void setImmersive(boolean i) {
5695 try {
5696 ActivityManagerNative.getDefault().setImmersive(mToken, i);
5697 } catch (RemoteException e) {
5698 // pass
5699 }
5700 }
5701
Adam Powell6e346362010-07-23 10:18:23 -07005702 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00005703 * Start an action mode of the default type {@link ActionMode#TYPE_PRIMARY}.
Adam Powell6e346362010-07-23 10:18:23 -07005704 *
Clara Bayarri4423d912015-03-02 19:42:48 +00005705 * @param callback Callback that will manage lifecycle events for this action mode
5706 * @return The ActionMode that was started, or null if it was canceled
Adam Powell6e346362010-07-23 10:18:23 -07005707 *
5708 * @see ActionMode
5709 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005710 @Nullable
Adam Powell5d279772010-07-27 16:34:07 -07005711 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07005712 return mWindow.getDecorView().startActionMode(callback);
5713 }
5714
Adam Powelldebf3be2010-11-15 18:58:48 -08005715 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00005716 * Start an action mode of the given type.
5717 *
5718 * @param callback Callback that will manage lifecycle events for this action mode
5719 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}.
5720 * @return The ActionMode that was started, or null if it was canceled
5721 *
5722 * @see ActionMode
5723 */
5724 @Nullable
5725 public ActionMode startActionMode(ActionMode.Callback callback, int type) {
5726 return mWindow.getDecorView().startActionMode(callback, type);
5727 }
5728
5729 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08005730 * Give the Activity a chance to control the UI for an action mode requested
5731 * by the system.
5732 *
5733 * <p>Note: If you are looking for a notification callback that an action mode
5734 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
5735 *
5736 * @param callback The callback that should control the new action mode
5737 * @return The new action mode, or <code>null</code> if the activity does not want to
5738 * provide special handling for this action mode. (It will be handled by the system.)
5739 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005740 @Nullable
Craig Mautner5eda9b32013-07-02 11:58:16 -07005741 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08005742 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00005743 // Only Primary ActionModes are represented in the ActionBar.
5744 if (mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
5745 initWindowDecorActionBar();
5746 if (mActionBar != null) {
5747 return mActionBar.startActionMode(callback);
5748 }
Adam Powell6e346362010-07-23 10:18:23 -07005749 }
5750 return null;
5751 }
5752
Adam Powelldebf3be2010-11-15 18:58:48 -08005753 /**
Clara Bayarri4423d912015-03-02 19:42:48 +00005754 * {@inheritDoc}
5755 */
5756 @Nullable
5757 @Override
5758 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
5759 try {
5760 mActionModeTypeStarting = type;
5761 return onWindowStartingActionMode(callback);
5762 } finally {
5763 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
5764 }
5765 }
5766
5767 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08005768 * Notifies the Activity that an action mode has been started.
5769 * Activity subclasses overriding this method should call the superclass implementation.
5770 *
5771 * @param mode The new action mode.
5772 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08005773 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07005774 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08005775 public void onActionModeStarted(ActionMode mode) {
5776 }
5777
5778 /**
5779 * Notifies the activity that an action mode has finished.
5780 * Activity subclasses overriding this method should call the superclass implementation.
5781 *
5782 * @param mode The action mode that just finished.
5783 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08005784 @CallSuper
Craig Mautner5eda9b32013-07-02 11:58:16 -07005785 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08005786 public void onActionModeFinished(ActionMode mode) {
5787 }
5788
Adam Powelldd8fab22012-03-22 17:47:27 -07005789 /**
5790 * Returns true if the app should recreate the task when navigating 'up' from this activity
5791 * by using targetIntent.
5792 *
5793 * <p>If this method returns false the app can trivially call
5794 * {@link #navigateUpTo(Intent)} using the same parameters to correctly perform
5795 * up navigation. If this method returns false, the app should synthesize a new task stack
5796 * by using {@link TaskStackBuilder} or another similar mechanism to perform up navigation.</p>
5797 *
5798 * @param targetIntent An intent representing the target destination for up navigation
5799 * @return true if navigating up should recreate a new task stack, false if the same task
5800 * should be used for the destination
5801 */
5802 public boolean shouldUpRecreateTask(Intent targetIntent) {
5803 try {
5804 PackageManager pm = getPackageManager();
5805 ComponentName cn = targetIntent.getComponent();
5806 if (cn == null) {
5807 cn = targetIntent.resolveActivity(pm);
5808 }
5809 ActivityInfo info = pm.getActivityInfo(cn, 0);
5810 if (info.taskAffinity == null) {
5811 return false;
5812 }
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07005813 return ActivityManagerNative.getDefault()
5814 .shouldUpRecreateTask(mToken, info.taskAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -07005815 } catch (RemoteException e) {
5816 return false;
5817 } catch (NameNotFoundException e) {
5818 return false;
5819 }
5820 }
5821
5822 /**
5823 * Navigate from this activity to the activity specified by upIntent, finishing this activity
5824 * in the process. If the activity indicated by upIntent already exists in the task's history,
5825 * this activity and all others before the indicated activity in the history stack will be
Adam Powell35c064b2012-05-02 11:37:15 -07005826 * finished.
5827 *
5828 * <p>If the indicated activity does not appear in the history stack, this will finish
5829 * each activity in this task until the root activity of the task is reached, resulting in
5830 * an "in-app home" behavior. This can be useful in apps with a complex navigation hierarchy
5831 * when an activity may be reached by a path not passing through a canonical parent
5832 * activity.</p>
Adam Powelldd8fab22012-03-22 17:47:27 -07005833 *
5834 * <p>This method should be used when performing up navigation from within the same task
5835 * as the destination. If up navigation should cross tasks in some cases, see
5836 * {@link #shouldUpRecreateTask(Intent)}.</p>
5837 *
5838 * @param upIntent An intent representing the target destination for up navigation
5839 *
5840 * @return true if up navigation successfully reached the activity indicated by upIntent and
5841 * upIntent was delivered to it. false if an instance of the indicated activity could
5842 * not be found and this activity was simply finished normally.
5843 */
5844 public boolean navigateUpTo(Intent upIntent) {
5845 if (mParent == null) {
5846 ComponentName destInfo = upIntent.getComponent();
5847 if (destInfo == null) {
5848 destInfo = upIntent.resolveActivity(getPackageManager());
5849 if (destInfo == null) {
5850 return false;
5851 }
5852 upIntent = new Intent(upIntent);
5853 upIntent.setComponent(destInfo);
5854 }
5855 int resultCode;
5856 Intent resultData;
5857 synchronized (this) {
5858 resultCode = mResultCode;
5859 resultData = mResultData;
5860 }
5861 if (resultData != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005862 resultData.prepareToLeaveProcess();
Adam Powelldd8fab22012-03-22 17:47:27 -07005863 }
5864 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07005865 upIntent.prepareToLeaveProcess();
Adam Powelldd8fab22012-03-22 17:47:27 -07005866 return ActivityManagerNative.getDefault().navigateUpTo(mToken, upIntent,
5867 resultCode, resultData);
5868 } catch (RemoteException e) {
5869 return false;
5870 }
5871 } else {
5872 return mParent.navigateUpToFromChild(this, upIntent);
5873 }
5874 }
5875
5876 /**
5877 * This is called when a child activity of this one calls its
5878 * {@link #navigateUpTo} method. The default implementation simply calls
5879 * navigateUpTo(upIntent) on this activity (the parent).
5880 *
5881 * @param child The activity making the call.
5882 * @param upIntent An intent representing the target destination for up navigation
5883 *
5884 * @return true if up navigation successfully reached the activity indicated by upIntent and
5885 * upIntent was delivered to it. false if an instance of the indicated activity could
5886 * not be found and this activity was simply finished normally.
5887 */
5888 public boolean navigateUpToFromChild(Activity child, Intent upIntent) {
5889 return navigateUpTo(upIntent);
5890 }
5891
5892 /**
5893 * Obtain an {@link Intent} that will launch an explicit target activity specified by
5894 * this activity's logical parent. The logical parent is named in the application's manifest
5895 * by the {@link android.R.attr#parentActivityName parentActivityName} attribute.
Adam Powell04d58112012-04-09 10:22:12 -07005896 * Activity subclasses may override this method to modify the Intent returned by
5897 * super.getParentActivityIntent() or to implement a different mechanism of retrieving
5898 * the parent intent entirely.
Adam Powelldd8fab22012-03-22 17:47:27 -07005899 *
Adam Powell04d58112012-04-09 10:22:12 -07005900 * @return a new Intent targeting the defined parent of this activity or null if
5901 * there is no valid parent.
Adam Powelldd8fab22012-03-22 17:47:27 -07005902 */
Tor Norbyed9273d62013-05-30 15:59:53 -07005903 @Nullable
Adam Powelldd8fab22012-03-22 17:47:27 -07005904 public Intent getParentActivityIntent() {
Adam Powell04d58112012-04-09 10:22:12 -07005905 final String parentName = mActivityInfo.parentActivityName;
5906 if (TextUtils.isEmpty(parentName)) {
5907 return null;
5908 }
Adam Powell5a4010c2012-09-16 15:14:05 -07005909
5910 // If the parent itself has no parent, generate a main activity intent.
5911 final ComponentName target = new ComponentName(this, parentName);
5912 try {
5913 final ActivityInfo parentInfo = getPackageManager().getActivityInfo(target, 0);
5914 final String parentActivity = parentInfo.parentActivityName;
5915 final Intent parentIntent = parentActivity == null
5916 ? Intent.makeMainActivity(target)
5917 : new Intent().setComponent(target);
5918 return parentIntent;
5919 } catch (NameNotFoundException e) {
5920 Log.e(TAG, "getParentActivityIntent: bad parentActivityName '" + parentName +
5921 "' in manifest");
5922 return null;
5923 }
Adam Powelldd8fab22012-03-22 17:47:27 -07005924 }
5925
George Mount31a21722014-03-24 17:44:36 -07005926 /**
George Mount62ab9b72014-05-02 13:51:17 -07005927 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07005928 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07005929 * will be called to handle shared elements on the <i>launched</i> Activity. This requires
George Mount9826f632014-09-11 08:50:09 -07005930 * {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount31a21722014-03-24 17:44:36 -07005931 *
George Mount65580562014-08-29 08:15:48 -07005932 * @param callback Used to manipulate shared element transitions on the launched Activity.
George Mount31a21722014-03-24 17:44:36 -07005933 */
George Mount65580562014-08-29 08:15:48 -07005934 public void setEnterSharedElementCallback(SharedElementCallback callback) {
5935 if (callback == null) {
5936 callback = SharedElementCallback.NULL_CALLBACK;
George Mount31a21722014-03-24 17:44:36 -07005937 }
George Mount65580562014-08-29 08:15:48 -07005938 mEnterTransitionListener = callback;
George Mount800d72b2014-05-19 07:09:00 -07005939 }
5940
5941 /**
5942 * When {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
George Mount65580562014-08-29 08:15:48 -07005943 * android.view.View, String)} was used to start an Activity, <var>callback</var>
George Mount800d72b2014-05-19 07:09:00 -07005944 * will be called to handle shared elements on the <i>launching</i> Activity. Most
5945 * calls will only come when returning from the started Activity.
George Mount9826f632014-09-11 08:50:09 -07005946 * This requires {@link Window#FEATURE_ACTIVITY_TRANSITIONS}.
George Mount800d72b2014-05-19 07:09:00 -07005947 *
George Mount65580562014-08-29 08:15:48 -07005948 * @param callback Used to manipulate shared element transitions on the launching Activity.
George Mount800d72b2014-05-19 07:09:00 -07005949 */
George Mount65580562014-08-29 08:15:48 -07005950 public void setExitSharedElementCallback(SharedElementCallback callback) {
5951 if (callback == null) {
5952 callback = SharedElementCallback.NULL_CALLBACK;
George Mount800d72b2014-05-19 07:09:00 -07005953 }
George Mount65580562014-08-29 08:15:48 -07005954 mExitTransitionListener = callback;
George Mount31a21722014-03-24 17:44:36 -07005955 }
5956
George Mount8c2614c2014-06-10 11:12:01 -07005957 /**
5958 * Postpone the entering activity transition when Activity was started with
5959 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
5960 * android.util.Pair[])}.
5961 * <p>This method gives the Activity the ability to delay starting the entering and
5962 * shared element transitions until all data is loaded. Until then, the Activity won't
5963 * draw into its window, leaving the window transparent. This may also cause the
5964 * returning animation to be delayed until data is ready. This method should be
5965 * called in {@link #onCreate(android.os.Bundle)} or in
5966 * {@link #onActivityReenter(int, android.content.Intent)}.
5967 * {@link #startPostponedEnterTransition()} must be called to allow the Activity to
5968 * start the transitions. If the Activity did not use
5969 * {@link android.app.ActivityOptions#makeSceneTransitionAnimation(Activity,
5970 * android.util.Pair[])}, then this method does nothing.</p>
5971 */
5972 public void postponeEnterTransition() {
5973 mActivityTransitionState.postponeEnterTransition();
5974 }
5975
5976 /**
5977 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
5978 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
5979 * to have your Activity start drawing.
5980 */
5981 public void startPostponedEnterTransition() {
5982 mActivityTransitionState.startPostponedEnterTransition();
5983 }
5984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 // ------------------ Internal API ------------------
RoboErik55011652014-07-09 15:05:53 -07005986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005987 final void setParent(Activity parent) {
5988 mParent = parent;
5989 }
5990
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005991 final void attach(Context context, ActivityThread aThread,
5992 Instrumentation instr, IBinder token, int ident,
5993 Application application, Intent intent, ActivityInfo info,
5994 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005995 NonConfigurationInstances lastNonConfigurationInstances,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08005996 Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 attachBaseContext(context);
5998
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07005999 mFragments.attachActivity(this, mContainer, null);
RoboErik55011652014-07-09 15:05:53 -07006000
Jorim Jaggib10e33f2015-02-04 21:57:40 +01006001 mWindow = new PhoneWindow(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 mWindow.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -07006003 mWindow.setOnWindowDismissedCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08006004 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
6006 mWindow.setSoftInputMode(info.softInputMode);
6007 }
Adam Powell269248d2011-08-02 10:26:54 -07006008 if (info.uiOptions != 0) {
6009 mWindow.setUiOptions(info.uiOptions);
6010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 mUiThread = Thread.currentThread();
George Mount0a778ed2013-12-13 13:35:36 -08006012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006013 mMainThread = aThread;
6014 mInstrumentation = instr;
6015 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006016 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 mApplication = application;
6018 mIntent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08006019 mReferrer = referrer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006020 mComponent = intent.getComponent();
6021 mActivityInfo = info;
6022 mTitle = title;
6023 mParent = parent;
6024 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006025 mLastNonConfigurationInstances = lastNonConfigurationInstances;
Dianne Hackborn20d94742014-05-29 18:35:45 -07006026 if (voiceInteractor != null) {
6027 if (lastNonConfigurationInstances != null) {
6028 mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
6029 } else {
6030 mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
6031 Looper.myLooper());
6032 }
6033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034
Jeff Brown98365d72012-08-19 20:30:52 -07006035 mWindow.setWindowManager(
6036 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
6037 mToken, mComponent.flattenToString(),
Romain Guy529b60a2010-08-03 18:05:47 -07006038 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 if (mParent != null) {
6040 mWindow.setContainer(mParent.getWindow());
6041 }
6042 mWindowManager = mWindow.getWindowManager();
6043 mCurrentConfig = config;
6044 }
6045
Dianne Hackborn5320eb82012-05-18 12:05:04 -07006046 /** @hide */
6047 public final IBinder getActivityToken() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 return mParent != null ? mParent.getActivityToken() : mToken;
6049 }
6050
Craig Mautnera0026042014-04-23 11:45:37 -07006051 final void performCreateCommon() {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08006052 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
6053 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07006054 mFragments.dispatchActivityCreated();
George Mount62ab9b72014-05-02 13:51:17 -07006055 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006056 }
George Mount0a778ed2013-12-13 13:35:36 -08006057
Craig Mautnera0026042014-04-23 11:45:37 -07006058 final void performCreate(Bundle icicle) {
6059 onCreate(icicle);
George Mount62ab9b72014-05-02 13:51:17 -07006060 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006061 performCreateCommon();
6062 }
6063
6064 final void performCreate(Bundle icicle, PersistableBundle persistentState) {
6065 onCreate(icicle, persistentState);
George Mount62ab9b72014-05-02 13:51:17 -07006066 mActivityTransitionState.readState(icicle);
Craig Mautnera0026042014-04-23 11:45:37 -07006067 performCreateCommon();
6068 }
6069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070 final void performStart() {
George Mount62ab9b72014-05-02 13:51:17 -07006071 mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006072 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07006074 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 mInstrumentation.callActivityOnStart(this);
6076 if (!mCalled) {
6077 throw new SuperNotCalledException(
6078 "Activity " + mComponent.toShortString() +
6079 " did not call through to super.onStart()");
6080 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006081 mFragments.dispatchStart();
Dianne Hackborn2707d602010-07-09 18:01:20 -07006082 if (mAllLoaderManagers != null) {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -07006083 final int N = mAllLoaderManagers.size();
6084 LoaderManagerImpl loaders[] = new LoaderManagerImpl[N];
6085 for (int i=N-1; i>=0; i--) {
6086 loaders[i] = mAllLoaderManagers.valueAt(i);
6087 }
6088 for (int i=0; i<N; i++) {
6089 LoaderManagerImpl lm = loaders[i];
6090 lm.finishRetain();
6091 lm.doReportStart();
Dianne Hackborn2707d602010-07-09 18:01:20 -07006092 }
6093 }
George Mount62ab9b72014-05-02 13:51:17 -07006094 mActivityTransitionState.enterReady(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006095 }
RoboErik55011652014-07-09 15:05:53 -07006096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006097 final void performRestart() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006098 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07006099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 if (mStopped) {
6101 mStopped = false;
Dianne Hackbornce418e62011-03-01 14:31:38 -08006102 if (mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07006103 WindowManagerGlobal.getInstance().setStoppedState(mToken, false);
Dianne Hackbornce418e62011-03-01 14:31:38 -08006104 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006105
6106 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 || mc.mUpdated) {
6111 if (!mc.mCursor.requery()) {
Dianne Hackborna5445d32011-09-01 14:38:24 -07006112 if (getApplicationInfo().targetSdkVersion
6113 >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
6114 throw new IllegalStateException(
6115 "trying to requery an already closed cursor "
6116 + mc.mCursor);
6117 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07006118 }
6119 mc.mReleased = false;
6120 mc.mUpdated = false;
6121 }
6122 }
6123 }
6124
6125 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 mInstrumentation.callActivityOnRestart(this);
6127 if (!mCalled) {
6128 throw new SuperNotCalledException(
6129 "Activity " + mComponent.toShortString() +
6130 " did not call through to super.onRestart()");
6131 }
6132 performStart();
6133 }
6134 }
RoboErik55011652014-07-09 15:05:53 -07006135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006136 final void performResume() {
6137 performRestart();
RoboErik55011652014-07-09 15:05:53 -07006138
Dianne Hackborn445646c2010-06-25 15:52:59 -07006139 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006140
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006141 mLastNonConfigurationInstances = null;
RoboErik55011652014-07-09 15:05:53 -07006142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006143 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06006144 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 mInstrumentation.callActivityOnResume(this);
6146 if (!mCalled) {
6147 throw new SuperNotCalledException(
6148 "Activity " + mComponent.toShortString() +
6149 " did not call through to super.onResume()");
6150 }
6151
Todd Kennedyee8c9c62014-12-10 14:22:59 -08006152 // invisible activities must be finished before onResume() completes
6153 if (!mVisibleFromClient && !mFinished) {
6154 Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");
6155 if (getApplicationInfo().targetSdkVersion
6156 > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
6157 throw new IllegalStateException(
6158 "Activity " + mComponent.toShortString() +
6159 " did not call finish() prior to onResume() completing");
6160 }
6161 }
6162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 mCalled = false;
RoboErik55011652014-07-09 15:05:53 -07006165
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006166 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07006167 mFragments.execPendingActions();
RoboErik55011652014-07-09 15:05:53 -07006168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 onPostResume();
6170 if (!mCalled) {
6171 throw new SuperNotCalledException(
6172 "Activity " + mComponent.toShortString() +
6173 " did not call through to super.onPostResume()");
6174 }
6175 }
6176
6177 final void performPause() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006178 mDoReportFullyDrawn = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006179 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006180 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006181 onPause();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006182 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07006183 if (!mCalled && getApplicationInfo().targetSdkVersion
6184 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
6185 throw new SuperNotCalledException(
6186 "Activity " + mComponent.toShortString() +
6187 " did not call through to super.onPause()");
6188 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06006189 mResumed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 }
RoboErik55011652014-07-09 15:05:53 -07006191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006192 final void performUserLeaving() {
6193 onUserInteraction();
6194 onUserLeaveHint();
6195 }
RoboErik55011652014-07-09 15:05:53 -07006196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 final void performStop() {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07006198 mDoReportFullyDrawn = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006199 if (mLoadersStarted) {
6200 mLoadersStarted = false;
Dianne Hackborn2707d602010-07-09 18:01:20 -07006201 if (mLoaderManager != null) {
6202 if (!mChangingConfigurations) {
6203 mLoaderManager.doStop();
6204 } else {
6205 mLoaderManager.doRetain();
6206 }
6207 }
6208 }
RoboErik55011652014-07-09 15:05:53 -07006209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006210 if (!mStopped) {
6211 if (mWindow != null) {
6212 mWindow.closeAllPanels();
6213 }
6214
Dianne Hackbornce418e62011-03-01 14:31:38 -08006215 if (mToken != null && mParent == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07006216 WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
Dianne Hackbornce418e62011-03-01 14:31:38 -08006217 }
RoboErik55011652014-07-09 15:05:53 -07006218
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006219 mFragments.dispatchStop();
RoboErik55011652014-07-09 15:05:53 -07006220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006221 mCalled = false;
6222 mInstrumentation.callActivityOnStop(this);
6223 if (!mCalled) {
6224 throw new SuperNotCalledException(
6225 "Activity " + mComponent.toShortString() +
6226 " did not call through to super.onStop()");
6227 }
RoboErik55011652014-07-09 15:05:53 -07006228
Makoto Onuki2f6a0182010-02-22 13:26:59 -08006229 synchronized (mManagedCursors) {
6230 final int N = mManagedCursors.size();
6231 for (int i=0; i<N; i++) {
6232 ManagedCursor mc = mManagedCursors.get(i);
6233 if (!mc.mReleased) {
6234 mc.mCursor.deactivate();
6235 mc.mReleased = true;
6236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 }
6238 }
George Mount0a778ed2013-12-13 13:35:36 -08006239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006240 mStopped = true;
6241 }
6242 mResumed = false;
6243 }
6244
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006245 final void performDestroy() {
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -07006246 mDestroyed = true;
Dianne Hackborn291905e2010-08-17 15:17:15 -07006247 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006248 mFragments.dispatchDestroy();
6249 onDestroy();
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07006250 if (mLoaderManager != null) {
6251 mLoaderManager.doDestroy();
6252 }
Dianne Hackborn20d94742014-05-29 18:35:45 -07006253 if (mVoiceInteractor != null) {
6254 mVoiceInteractor.detachActivity();
6255 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07006256 }
George Mount0a778ed2013-12-13 13:35:36 -08006257
6258 /**
Jeff Hamilton52d32032011-01-08 15:31:26 -06006259 * @hide
6260 */
6261 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 return mResumed;
6263 }
6264
George Mount0a778ed2013-12-13 13:35:36 -08006265 void dispatchActivityResult(String who, int requestCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 int resultCode, Intent data) {
Joe Onorato43a17652011-04-06 19:22:23 -07006267 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
6269 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07006270 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 if (who == null) {
6272 onActivityResult(requestCode, resultCode, data);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07006273 } else {
6274 Fragment frag = mFragments.findFragmentByWho(who);
6275 if (frag != null) {
6276 frag.onActivityResult(requestCode, resultCode, data);
6277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006278 }
6279 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07006280
Jason Monka57e5e02014-05-07 10:06:48 -04006281 /**
Jason Monk62515be2014-05-21 16:06:19 -04006282 * Request to put this Activity in a mode where the user is locked to the
Jason Monka57e5e02014-05-07 10:06:48 -04006283 * current task.
6284 *
6285 * This will prevent the user from launching other apps, going to settings,
6286 * or reaching the home screen.
6287 *
Jason Monk62515be2014-05-21 16:06:19 -04006288 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true
6289 * for this component then the app will go directly into Lock Task mode. The user
6290 * will not be able to exit this mode until {@link Activity#stopLockTask()} is called.
6291 *
6292 * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false
6293 * then the system will prompt the user with a dialog requesting permission to enter
Jason Monk8863b572014-07-29 13:49:32 -04006294 * this mode. When entered through this method the user can exit at any time through
6295 * an action described by the request dialog. Calling stopLockTask will also exit the
6296 * mode.
Jason Monka57e5e02014-05-07 10:06:48 -04006297 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006298 public void startLockTask() {
6299 try {
6300 ActivityManagerNative.getDefault().startLockTaskMode(mToken);
6301 } catch (RemoteException e) {
6302 }
6303 }
6304
Jason Monka57e5e02014-05-07 10:06:48 -04006305 /**
6306 * Allow the user to switch away from the current task.
6307 *
6308 * Called to end the mode started by {@link Activity#startLockTask}. This
6309 * can only be called by activities that have successfully called
6310 * startLockTask previously.
6311 *
6312 * This will allow the user to exit this app and move onto other activities.
6313 */
Craig Mautneraea74a52014-03-08 14:23:10 -08006314 public void stopLockTask() {
6315 try {
6316 ActivityManagerNative.getDefault().stopLockTaskMode();
6317 } catch (RemoteException e) {
6318 }
6319 }
6320
Craig Mautner5eda9b32013-07-02 11:58:16 -07006321 /**
6322 * Interface for informing a translucent {@link Activity} once all visible activities below it
6323 * have completed drawing. This is necessary only after an {@link Activity} has been made
6324 * opaque using {@link Activity#convertFromTranslucent()} and before it has been drawn
6325 * translucent again following a call to {@link
George Mount800d72b2014-05-19 07:09:00 -07006326 * Activity#convertToTranslucent(android.app.Activity.TranslucentConversionListener,
6327 * ActivityOptions)}
Chet Haaseabd3d772013-09-11 14:33:05 -07006328 *
6329 * @hide
Craig Mautner5eda9b32013-07-02 11:58:16 -07006330 */
Jose Lima14914852014-08-14 09:14:12 -07006331 @SystemApi
Craig Mautner5eda9b32013-07-02 11:58:16 -07006332 public interface TranslucentConversionListener {
6333 /**
6334 * Callback made following {@link Activity#convertToTranslucent} once all visible Activities
6335 * below the top one have been redrawn. Following this callback it is safe to make the top
6336 * Activity translucent because the underlying Activity has been drawn.
6337 *
6338 * @param drawComplete True if the background Activity has drawn itself. False if a timeout
6339 * occurred waiting for the Activity to complete drawing.
6340 *
6341 * @see Activity#convertFromTranslucent()
Craig Mautner233ceee2014-05-09 17:05:11 -07006342 * @see Activity#convertToTranslucent(TranslucentConversionListener, ActivityOptions)
Craig Mautner5eda9b32013-07-02 11:58:16 -07006343 */
6344 public void onTranslucentConversionComplete(boolean drawComplete);
6345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346}