blob: fb1570ea0162d6e0e0918b49ccdc6b69a5e0544c [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
Adam Powell6e346362010-07-23 10:18:23 -070019import com.android.internal.app.ActionBarImpl;
20import com.android.internal.policy.PolicyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ComponentCallbacks;
23import android.content.ComponentName;
24import android.content.ContentResolver;
25import android.content.Context;
Jason parks6ed50de2010-08-25 10:18:50 -050026import android.content.CursorLoader;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070027import android.content.IIntentSender;
Adam Powell33b97432010-04-20 10:01:14 -070028import android.content.Intent;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070029import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.SharedPreferences;
31import android.content.pm.ActivityInfo;
32import android.content.res.Configuration;
33import android.content.res.Resources;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070034import android.content.res.TypedArray;
Adam Powell88ab6972011-07-28 11:25:01 -070035import android.content.res.Resources.Theme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.database.Cursor;
37import android.graphics.Bitmap;
38import android.graphics.Canvas;
39import android.graphics.drawable.Drawable;
40import android.media.AudioManager;
41import android.net.Uri;
Dianne Hackborn8d374262009-09-14 21:21:52 -070042import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Handler;
45import android.os.IBinder;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -080046import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070047import android.os.Parcelable;
svetoslavganov75986cf2009-05-14 22:28:01 -070048import android.os.RemoteException;
Brad Fitzpatrick75803572011-01-13 14:21:03 -080049import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.text.Selection;
51import android.text.SpannableStringBuilder;
svetoslavganov75986cf2009-05-14 22:28:01 -070052import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.text.method.TextKeyListener;
54import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.EventLog;
56import android.util.Log;
57import android.util.SparseArray;
Adam Powell88ab6972011-07-28 11:25:01 -070058import android.util.TypedValue;
Adam Powell6e346362010-07-23 10:18:23 -070059import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070061import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.view.ContextThemeWrapper;
63import android.view.KeyEvent;
64import android.view.LayoutInflater;
65import android.view.Menu;
66import android.view.MenuInflater;
67import android.view.MenuItem;
68import android.view.MotionEvent;
69import android.view.View;
Dianne Hackbornce418e62011-03-01 14:31:38 -080070import android.view.WindowManagerImpl;
Adam Powell6e346362010-07-23 10:18:23 -070071import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -070073import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.ViewManager;
75import android.view.Window;
76import android.view.WindowManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070077import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.widget.AdapterView;
79
Dianne Hackborn625ac272010-09-17 18:29:22 -070080import java.io.FileDescriptor;
81import java.io.PrintWriter;
Adam Powell6e346362010-07-23 10:18:23 -070082import java.util.ArrayList;
83import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85/**
86 * An activity is a single, focused thing that the user can do. Almost all
87 * activities interact with the user, so the Activity class takes care of
88 * creating a window for you in which you can place your UI with
89 * {@link #setContentView}. While activities are often presented to the user
90 * as full-screen windows, they can also be used in other ways: as floating
91 * windows (via a theme with {@link android.R.attr#windowIsFloating} set)
92 * or embedded inside of another activity (using {@link ActivityGroup}).
93 *
94 * There are two methods almost all subclasses of Activity will implement:
95 *
96 * <ul>
97 * <li> {@link #onCreate} is where you initialize your activity. Most
98 * importantly, here you will usually call {@link #setContentView(int)}
99 * with a layout resource defining your UI, and using {@link #findViewById}
100 * to retrieve the widgets in that UI that you need to interact with
101 * programmatically.
102 *
103 * <li> {@link #onPause} is where you deal with the user leaving your
104 * activity. Most importantly, any changes made by the user should at this
105 * point be committed (usually to the
106 * {@link android.content.ContentProvider} holding the data).
107 * </ul>
108 *
109 * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
110 * activity classes must have a corresponding
111 * {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
112 * declaration in their package's <code>AndroidManifest.xml</code>.</p>
113 *
114 * <p>The Activity class is an important part of an application's overall lifecycle,
115 * and the way activities are launched and put together is a fundamental
Scott Main7aee61f2011-02-08 11:25:01 -0800116 * part of the platform's application model. For a detailed perspective on the structure of an
117 * Android application and how activities behave, please read the
118 * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> and
119 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back Stack</a>
120 * documents.</p>
121 *
122 * <p>You can also find a detailed discussion about how to create activities in the
123 * <a href="{@docRoot}guide/topics/fundamentals/activities.html">Activities</a>
124 * document.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 *
126 * <p>Topics covered here:
127 * <ol>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700128 * <li><a href="#Fragments">Fragments</a>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 * <li><a href="#ActivityLifecycle">Activity Lifecycle</a>
130 * <li><a href="#ConfigurationChanges">Configuration Changes</a>
131 * <li><a href="#StartingActivities">Starting Activities and Getting Results</a>
132 * <li><a href="#SavingPersistentState">Saving Persistent State</a>
133 * <li><a href="#Permissions">Permissions</a>
134 * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
135 * </ol>
136 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700137 * <a name="Fragments"></a>
138 * <h3>Fragments</h3>
139 *
140 * <p>Starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}, Activity
141 * implementations can make use of the {@link Fragment} class to better
142 * modularize their code, build more sophisticated user interfaces for larger
143 * screens, and help scale their application between small and large screens.
144 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 * <a name="ActivityLifecycle"></a>
146 * <h3>Activity Lifecycle</h3>
147 *
148 * <p>Activities in the system are managed as an <em>activity stack</em>.
149 * When a new activity is started, it is placed on the top of the stack
150 * and becomes the running activity -- the previous activity always remains
151 * below it in the stack, and will not come to the foreground again until
152 * the new activity exits.</p>
153 *
154 * <p>An activity has essentially four states:</p>
155 * <ul>
156 * <li> If an activity in the foreground of the screen (at the top of
157 * the stack),
158 * it is <em>active</em> or <em>running</em>. </li>
159 * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized
160 * or transparent activity has focus on top of your activity), it
161 * is <em>paused</em>. A paused activity is completely alive (it
162 * maintains all state and member information and remains attached to
163 * the window manager), but can be killed by the system in extreme
164 * low memory situations.
165 * <li>If an activity is completely obscured by another activity,
166 * it is <em>stopped</em>. It still retains all state and member information,
167 * however, it is no longer visible to the user so its window is hidden
168 * and it will often be killed by the system when memory is needed
169 * elsewhere.</li>
170 * <li>If an activity is paused or stopped, the system can drop the activity
171 * from memory by either asking it to finish, or simply killing its
172 * process. When it is displayed again to the user, it must be
173 * completely restarted and restored to its previous state.</li>
174 * </ul>
175 *
176 * <p>The following diagram shows the important state paths of an Activity.
177 * The square rectangles represent callback methods you can implement to
178 * perform operations when the Activity moves between states. The colored
179 * ovals are major states the Activity can be in.</p>
180 *
181 * <p><img src="../../../images/activity_lifecycle.png"
182 * alt="State diagram for an Android Activity Lifecycle." border="0" /></p>
183 *
184 * <p>There are three key loops you may be interested in monitoring within your
185 * activity:
186 *
187 * <ul>
188 * <li>The <b>entire lifetime</b> of an activity happens between the first call
189 * to {@link android.app.Activity#onCreate} through to a single final call
190 * to {@link android.app.Activity#onDestroy}. An activity will do all setup
191 * of "global" state in onCreate(), and release all remaining resources in
192 * onDestroy(). For example, if it has a thread running in the background
193 * to download data from the network, it may create that thread in onCreate()
194 * and then stop the thread in onDestroy().
195 *
196 * <li>The <b>visible lifetime</b> of an activity happens between a call to
197 * {@link android.app.Activity#onStart} until a corresponding call to
198 * {@link android.app.Activity#onStop}. During this time the user can see the
199 * activity on-screen, though it may not be in the foreground and interacting
200 * with the user. Between these two methods you can maintain resources that
201 * are needed to show the activity to the user. For example, you can register
202 * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes
203 * that impact your UI, and unregister it in onStop() when the user an no
204 * longer see what you are displaying. The onStart() and onStop() methods
205 * can be called multiple times, as the activity becomes visible and hidden
206 * to the user.
207 *
208 * <li>The <b>foreground lifetime</b> of an activity happens between a call to
209 * {@link android.app.Activity#onResume} until a corresponding call to
210 * {@link android.app.Activity#onPause}. During this time the activity is
211 * in front of all other activities and interacting with the user. An activity
212 * can frequently go between the resumed and paused states -- for example when
213 * the device goes to sleep, when an activity result is delivered, when a new
214 * intent is delivered -- so the code in these methods should be fairly
215 * lightweight.
216 * </ul>
217 *
218 * <p>The entire lifecycle of an activity is defined by the following
219 * Activity methods. All of these are hooks that you can override
220 * to do appropriate work when the activity changes state. All
221 * activities will implement {@link android.app.Activity#onCreate}
222 * to do their initial setup; many will also implement
223 * {@link android.app.Activity#onPause} to commit changes to data and
224 * otherwise prepare to stop interacting with the user. You should always
225 * call up to your superclass when implementing these methods.</p>
226 *
227 * </p>
228 * <pre class="prettyprint">
229 * public class Activity extends ApplicationContext {
230 * protected void onCreate(Bundle savedInstanceState);
231 *
232 * protected void onStart();
233 *
234 * protected void onRestart();
235 *
236 * protected void onResume();
237 *
238 * protected void onPause();
239 *
240 * protected void onStop();
241 *
242 * protected void onDestroy();
243 * }
244 * </pre>
245 *
246 * <p>In general the movement through an activity's lifecycle looks like
247 * this:</p>
248 *
249 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
250 * <colgroup align="left" span="3" />
251 * <colgroup align="left" />
252 * <colgroup align="center" />
253 * <colgroup align="center" />
254 *
255 * <thead>
256 * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr>
257 * </thead>
258 *
259 * <tbody>
260 * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onCreate onCreate()}</th>
261 * <td>Called when the activity is first created.
262 * This is where you should do all of your normal static set up:
263 * create views, bind data to lists, etc. This method also
264 * provides you with a Bundle containing the activity's previously
265 * frozen state, if there was one.
266 * <p>Always followed by <code>onStart()</code>.</td>
267 * <td align="center">No</td>
268 * <td align="center"><code>onStart()</code></td>
269 * </tr>
270 *
271 * <tr><td rowspan="5" style="border-left: none; border-right: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
272 * <th colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</th>
273 * <td>Called after your activity has been stopped, prior to it being
274 * started again.
275 * <p>Always followed by <code>onStart()</code></td>
276 * <td align="center">No</td>
277 * <td align="center"><code>onStart()</code></td>
278 * </tr>
279 *
280 * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStart onStart()}</th>
281 * <td>Called when the activity is becoming visible to the user.
282 * <p>Followed by <code>onResume()</code> if the activity comes
283 * to the foreground, or <code>onStop()</code> if it becomes hidden.</td>
284 * <td align="center">No</td>
285 * <td align="center"><code>onResume()</code> or <code>onStop()</code></td>
286 * </tr>
287 *
288 * <tr><td rowspan="2" style="border-left: none;">&nbsp;&nbsp;&nbsp;&nbsp;</td>
289 * <th align="left" border="0">{@link android.app.Activity#onResume onResume()}</th>
290 * <td>Called when the activity will start
291 * interacting with the user. At this point your activity is at
292 * the top of the activity stack, with user input going to it.
293 * <p>Always followed by <code>onPause()</code>.</td>
294 * <td align="center">No</td>
295 * <td align="center"><code>onPause()</code></td>
296 * </tr>
297 *
298 * <tr><th align="left" border="0">{@link android.app.Activity#onPause onPause()}</th>
299 * <td>Called when the system is about to start resuming a previous
300 * activity. This is typically used to commit unsaved changes to
301 * persistent data, stop animations and other things that may be consuming
302 * CPU, etc. Implementations of this method must be very quick because
303 * the next activity will not be resumed until this method returns.
304 * <p>Followed by either <code>onResume()</code> if the activity
305 * returns back to the front, or <code>onStop()</code> if it becomes
306 * invisible to the user.</td>
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800307 * <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 -0800308 * <td align="center"><code>onResume()</code> or<br>
309 * <code>onStop()</code></td>
310 * </tr>
311 *
312 * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</th>
313 * <td>Called when the activity is no longer visible to the user, because
314 * another activity has been resumed and is covering this one. This
315 * may happen either because a new activity is being started, an existing
316 * one is being brought in front of this one, or this one is being
317 * destroyed.
318 * <p>Followed by either <code>onRestart()</code> if
319 * this activity is coming back to interact with the user, or
320 * <code>onDestroy()</code> if this activity is going away.</td>
321 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
322 * <td align="center"><code>onRestart()</code> or<br>
323 * <code>onDestroy()</code></td>
324 * </tr>
325 *
326 * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onDestroy onDestroy()}</th>
327 * <td>The final call you receive before your
328 * activity is destroyed. This can happen either because the
329 * activity is finishing (someone called {@link Activity#finish} on
330 * it, or because the system is temporarily destroying this
331 * instance of the activity to save space. You can distinguish
332 * between these two scenarios with the {@link
333 * Activity#isFinishing} method.</td>
334 * <td align="center"><font color="#800000"><strong>Yes</strong></font></td>
335 * <td align="center"><em>nothing</em></td>
336 * </tr>
337 * </tbody>
338 * </table>
339 *
340 * <p>Note the "Killable" column in the above table -- for those methods that
341 * are marked as being killable, after that method returns the process hosting the
342 * activity may killed by the system <em>at any time</em> without another line
343 * of its code being executed. Because of this, you should use the
344 * {@link #onPause} method to write any persistent data (such as user edits)
345 * to storage. In addition, the method
346 * {@link #onSaveInstanceState(Bundle)} is called before placing the activity
347 * in such a background state, allowing you to save away any dynamic instance
348 * state in your activity into the given Bundle, to be later received in
349 * {@link #onCreate} if the activity needs to be re-created.
350 * See the <a href="#ProcessLifecycle">Process Lifecycle</a>
351 * section for more information on how the lifecycle of a process is tied
352 * to the activities it is hosting. Note that it is important to save
353 * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState}
Daisuke Miyakawa5c40f3f2011-02-15 13:24:36 -0800354 * because the latter is not part of the lifecycle callbacks, so will not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * be called in every situation as described in its documentation.</p>
356 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800357 * <p class="note">Be aware that these semantics will change slightly between
358 * applications targeting platforms starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}
359 * vs. those targeting prior platforms. Starting with Honeycomb, an application
360 * is not in the killable state until its {@link #onStop} has returned. This
361 * impacts when {@link #onSaveInstanceState(Bundle)} may be called (it may be
362 * safely called after {@link #onPause()} and allows and application to safely
363 * wait until {@link #onStop()} to save persistent state.</p>
364 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 * <p>For those methods that are not marked as being killable, the activity's
366 * process will not be killed by the system starting from the time the method
367 * is called and continuing after it returns. Thus an activity is in the killable
368 * state, for example, between after <code>onPause()</code> to the start of
369 * <code>onResume()</code>.</p>
370 *
371 * <a name="ConfigurationChanges"></a>
372 * <h3>Configuration Changes</h3>
373 *
374 * <p>If the configuration of the device (as defined by the
375 * {@link Configuration Resources.Configuration} class) changes,
376 * then anything displaying a user interface will need to update to match that
377 * configuration. Because Activity is the primary mechanism for interacting
378 * with the user, it includes special support for handling configuration
379 * changes.</p>
380 *
381 * <p>Unless you specify otherwise, a configuration change (such as a change
382 * in screen orientation, language, input devices, etc) will cause your
383 * current activity to be <em>destroyed</em>, going through the normal activity
384 * lifecycle process of {@link #onPause},
385 * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity
386 * had been in the foreground or visible to the user, once {@link #onDestroy} is
387 * called in that instance then a new instance of the activity will be
388 * created, with whatever savedInstanceState the previous instance had generated
389 * from {@link #onSaveInstanceState}.</p>
390 *
391 * <p>This is done because any application resource,
392 * including layout files, can change based on any configuration value. Thus
393 * the only safe way to handle a configuration change is to re-retrieve all
394 * resources, including layouts, drawables, and strings. Because activities
395 * must already know how to save their state and re-create themselves from
396 * that state, this is a convenient way to have an activity restart itself
397 * with a new configuration.</p>
398 *
399 * <p>In some special cases, you may want to bypass restarting of your
400 * activity based on one or more types of configuration changes. This is
401 * done with the {@link android.R.attr#configChanges android:configChanges}
402 * attribute in its manifest. For any types of configuration changes you say
403 * that you handle there, you will receive a call to your current activity's
404 * {@link #onConfigurationChanged} method instead of being restarted. If
405 * a configuration change involves any that you do not handle, however, the
406 * activity will still be restarted and {@link #onConfigurationChanged}
407 * will not be called.</p>
408 *
409 * <a name="StartingActivities"></a>
410 * <h3>Starting Activities and Getting Results</h3>
411 *
412 * <p>The {@link android.app.Activity#startActivity}
413 * method is used to start a
414 * new activity, which will be placed at the top of the activity stack. It
415 * takes a single argument, an {@link android.content.Intent Intent},
416 * which describes the activity
417 * to be executed.</p>
418 *
419 * <p>Sometimes you want to get a result back from an activity when it
420 * ends. For example, you may start an activity that lets the user pick
421 * a person in a list of contacts; when it ends, it returns the person
422 * that was selected. To do this, you call the
423 * {@link android.app.Activity#startActivityForResult(Intent, int)}
424 * version with a second integer parameter identifying the call. The result
425 * will come back through your {@link android.app.Activity#onActivityResult}
426 * method.</p>
427 *
428 * <p>When an activity exits, it can call
429 * {@link android.app.Activity#setResult(int)}
430 * to return data back to its parent. It must always supply a result code,
431 * which can be the standard results RESULT_CANCELED, RESULT_OK, or any
432 * custom values starting at RESULT_FIRST_USER. In addition, it can optionally
433 * return back an Intent containing any additional data it wants. All of this
434 * information appears back on the
435 * parent's <code>Activity.onActivityResult()</code>, along with the integer
436 * identifier it originally supplied.</p>
437 *
438 * <p>If a child activity fails for any reason (such as crashing), the parent
439 * activity will receive a result with the code RESULT_CANCELED.</p>
440 *
441 * <pre class="prettyprint">
442 * public class MyActivity extends Activity {
443 * ...
444 *
445 * static final int PICK_CONTACT_REQUEST = 0;
446 *
447 * protected boolean onKeyDown(int keyCode, KeyEvent event) {
448 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
449 * // When the user center presses, let them pick a contact.
450 * startActivityForResult(
451 * new Intent(Intent.ACTION_PICK,
452 * new Uri("content://contacts")),
453 * PICK_CONTACT_REQUEST);
454 * return true;
455 * }
456 * return false;
457 * }
458 *
459 * protected void onActivityResult(int requestCode, int resultCode,
460 * Intent data) {
461 * if (requestCode == PICK_CONTACT_REQUEST) {
462 * if (resultCode == RESULT_OK) {
463 * // A contact was picked. Here we will just display it
464 * // to the user.
465 * startActivity(new Intent(Intent.ACTION_VIEW, data));
466 * }
467 * }
468 * }
469 * }
470 * </pre>
471 *
472 * <a name="SavingPersistentState"></a>
473 * <h3>Saving Persistent State</h3>
474 *
475 * <p>There are generally two kinds of persistent state than an activity
476 * will deal with: shared document-like data (typically stored in a SQLite
477 * database using a {@linkplain android.content.ContentProvider content provider})
478 * and internal state such as user preferences.</p>
479 *
480 * <p>For content provider data, we suggest that activities use a
481 * "edit in place" user model. That is, any edits a user makes are effectively
482 * made immediately without requiring an additional confirmation step.
483 * Supporting this model is generally a simple matter of following two rules:</p>
484 *
485 * <ul>
486 * <li> <p>When creating a new document, the backing database entry or file for
487 * it is created immediately. For example, if the user chooses to write
488 * a new e-mail, a new entry for that e-mail is created as soon as they
489 * start entering data, so that if they go to any other activity after
490 * that point this e-mail will now appear in the list of drafts.</p>
491 * <li> <p>When an activity's <code>onPause()</code> method is called, it should
492 * commit to the backing content provider or file any changes the user
493 * has made. This ensures that those changes will be seen by any other
494 * activity that is about to run. You will probably want to commit
495 * your data even more aggressively at key times during your
496 * activity's lifecycle: for example before starting a new
497 * activity, before finishing your own activity, when the user
498 * switches between input fields, etc.</p>
499 * </ul>
500 *
501 * <p>This model is designed to prevent data loss when a user is navigating
502 * between activities, and allows the system to safely kill an activity (because
503 * system resources are needed somewhere else) at any time after it has been
504 * paused. Note this implies
505 * that the user pressing BACK from your activity does <em>not</em>
506 * mean "cancel" -- it means to leave the activity with its current contents
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800507 * saved away. Canceling edits in an activity must be provided through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 * some other mechanism, such as an explicit "revert" or "undo" option.</p>
509 *
510 * <p>See the {@linkplain android.content.ContentProvider content package} for
511 * more information about content providers. These are a key aspect of how
512 * different activities invoke and propagate data between themselves.</p>
513 *
514 * <p>The Activity class also provides an API for managing internal persistent state
515 * associated with an activity. This can be used, for example, to remember
516 * the user's preferred initial display in a calendar (day view or week view)
517 * or the user's default home page in a web browser.</p>
518 *
519 * <p>Activity persistent state is managed
520 * with the method {@link #getPreferences},
521 * allowing you to retrieve and
522 * modify a set of name/value pairs associated with the activity. To use
523 * preferences that are shared across multiple application components
524 * (activities, receivers, services, providers), you can use the underlying
525 * {@link Context#getSharedPreferences Context.getSharedPreferences()} method
526 * to retrieve a preferences
527 * object stored under a specific name.
528 * (Note that it is not possible to share settings data across application
529 * packages -- for that you will need a content provider.)</p>
530 *
531 * <p>Here is an excerpt from a calendar activity that stores the user's
532 * preferred view mode in its persistent settings:</p>
533 *
534 * <pre class="prettyprint">
535 * public class CalendarActivity extends Activity {
536 * ...
537 *
538 * static final int DAY_VIEW_MODE = 0;
539 * static final int WEEK_VIEW_MODE = 1;
540 *
541 * private SharedPreferences mPrefs;
542 * private int mCurViewMode;
543 *
544 * protected void onCreate(Bundle savedInstanceState) {
545 * super.onCreate(savedInstanceState);
546 *
547 * SharedPreferences mPrefs = getSharedPreferences();
548 * mCurViewMode = mPrefs.getInt("view_mode" DAY_VIEW_MODE);
549 * }
550 *
551 * protected void onPause() {
552 * super.onPause();
553 *
554 * SharedPreferences.Editor ed = mPrefs.edit();
555 * ed.putInt("view_mode", mCurViewMode);
556 * ed.commit();
557 * }
558 * }
559 * </pre>
560 *
561 * <a name="Permissions"></a>
562 * <h3>Permissions</h3>
563 *
564 * <p>The ability to start a particular Activity can be enforced when it is
565 * declared in its
566 * manifest's {@link android.R.styleable#AndroidManifestActivity &lt;activity&gt;}
567 * tag. By doing so, other applications will need to declare a corresponding
568 * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
569 * element in their own manifest to be able to start that activity.
570 *
571 * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
572 * document for more information on permissions and security in general.
573 *
574 * <a name="ProcessLifecycle"></a>
575 * <h3>Process Lifecycle</h3>
576 *
577 * <p>The Android system attempts to keep application process around for as
578 * long as possible, but eventually will need to remove old processes when
579 * memory runs low. As described in <a href="#ActivityLifecycle">Activity
580 * Lifecycle</a>, the decision about which process to remove is intimately
581 * tied to the state of the user's interaction with it. In general, there
582 * are four states a process can be in based on the activities running in it,
583 * listed here in order of importance. The system will kill less important
584 * processes (the last ones) before it resorts to killing more important
585 * processes (the first ones).
586 *
587 * <ol>
588 * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen
589 * that the user is currently interacting with) is considered the most important.
590 * Its process will only be killed as a last resort, if it uses more memory
591 * than is available on the device. Generally at this point the device has
592 * reached a memory paging state, so this is required in order to keep the user
593 * interface responsive.
594 * <li> <p>A <b>visible activity</b> (an activity that is visible to the user
595 * but not in the foreground, such as one sitting behind a foreground dialog)
596 * is considered extremely important and will not be killed unless that is
597 * required to keep the foreground activity running.
598 * <li> <p>A <b>background activity</b> (an activity that is not visible to
599 * the user and has been paused) is no longer critical, so the system may
600 * safely kill its process to reclaim memory for other foreground or
601 * visible processes. If its process needs to be killed, when the user navigates
602 * back to the activity (making it visible on the screen again), its
603 * {@link #onCreate} method will be called with the savedInstanceState it had previously
604 * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same
605 * state as the user last left it.
606 * <li> <p>An <b>empty process</b> is one hosting no activities or other
607 * application components (such as {@link Service} or
608 * {@link android.content.BroadcastReceiver} classes). These are killed very
609 * quickly by the system as memory becomes low. For this reason, any
610 * background operation you do outside of an activity must be executed in the
611 * context of an activity BroadcastReceiver or Service to ensure that the system
612 * knows it needs to keep your process around.
613 * </ol>
614 *
615 * <p>Sometimes an Activity may need to do a long-running operation that exists
616 * independently of the activity lifecycle itself. An example may be a camera
617 * application that allows you to upload a picture to a web site. The upload
618 * may take a long time, and the application should allow the user to leave
619 * the application will it is executing. To accomplish this, your Activity
620 * should start a {@link Service} in which the upload takes place. This allows
621 * the system to properly prioritize your process (considering it to be more
622 * important than other non-visible applications) for the duration of the
623 * upload, independent of whether the original activity is paused, stopped,
624 * or finished.
625 */
626public class Activity extends ContextThemeWrapper
Dianne Hackborn625ac272010-09-17 18:29:22 -0700627 implements LayoutInflater.Factory2,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 Window.Callback, KeyEvent.Callback,
629 OnCreateContextMenuListener, ComponentCallbacks {
630 private static final String TAG = "Activity";
631
632 /** Standard activity result: operation canceled. */
633 public static final int RESULT_CANCELED = 0;
634 /** Standard activity result: operation succeeded. */
635 public static final int RESULT_OK = -1;
636 /** Start of user-defined activity results. */
637 public static final int RESULT_FIRST_USER = 1;
638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700640 private static final String FRAGMENTS_TAG = "android:fragments";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";
642 private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";
643 private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800644 private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800646 private static class ManagedDialog {
647 Dialog mDialog;
648 Bundle mArgs;
649 }
650 private SparseArray<ManagedDialog> mManagedDialogs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651
652 // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.
653 private Instrumentation mInstrumentation;
654 private IBinder mToken;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700655 private int mIdent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 /*package*/ String mEmbeddedID;
657 private Application mApplication;
Christopher Tateb70f3df2009-04-07 16:07:59 -0700658 /*package*/ Intent mIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 private ComponentName mComponent;
660 /*package*/ ActivityInfo mActivityInfo;
661 /*package*/ ActivityThread mMainThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 Activity mParent;
663 boolean mCalled;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700664 boolean mCheckedForLoaderManager;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700665 boolean mLoadersStarted;
Jeff Hamilton52d32032011-01-08 15:31:26 -0600666 /*package*/ boolean mResumed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 private boolean mStopped;
668 boolean mFinished;
669 boolean mStartedActivity;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700670 /** true if the activity is going through a transient pause */
671 /*package*/ boolean mTemporaryPause = false;
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -0500672 /** true if the activity is being destroyed in order to recreate it with a new configuration */
673 /*package*/ boolean mChangingConfigurations = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 /*package*/ int mConfigChangeFlags;
675 /*package*/ Configuration mCurrentConfig;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100676 private SearchManager mSearchManager;
Adam Powell88ab6972011-07-28 11:25:01 -0700677 private MenuInflater mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700679 static final class NonConfigurationInstances {
680 Object activity;
681 HashMap<String, Object> children;
682 ArrayList<Fragment> fragments;
Dianne Hackborn4911b782010-07-15 12:54:39 -0700683 SparseArray<LoaderManagerImpl> loaders;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700684 }
685 /* package */ NonConfigurationInstances mLastNonConfigurationInstances;
686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 private Window mWindow;
688
689 private WindowManager mWindowManager;
690 /*package*/ View mDecor = null;
691 /*package*/ boolean mWindowAdded = false;
692 /*package*/ boolean mVisibleFromServer = false;
693 /*package*/ boolean mVisibleFromClient = true;
Adam Powellac695c62010-07-20 18:19:27 -0700694 /*package*/ ActionBarImpl mActionBar = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695
696 private CharSequence mTitle;
697 private int mTitleColor = 0;
698
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700699 final FragmentManagerImpl mFragments = new FragmentManagerImpl();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700700
Dianne Hackborn4911b782010-07-15 12:54:39 -0700701 SparseArray<LoaderManagerImpl> mAllLoaderManagers;
702 LoaderManagerImpl mLoaderManager;
Dianne Hackbornc8017682010-07-06 13:34:38 -0700703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 private static final class ManagedCursor {
705 ManagedCursor(Cursor cursor) {
706 mCursor = cursor;
707 mReleased = false;
708 mUpdated = false;
709 }
710
711 private final Cursor mCursor;
712 private boolean mReleased;
713 private boolean mUpdated;
714 }
715 private final ArrayList<ManagedCursor> mManagedCursors =
716 new ArrayList<ManagedCursor>();
717
718 // protected by synchronized (this)
719 int mResultCode = RESULT_CANCELED;
720 Intent mResultData = null;
721
722 private boolean mTitleReady = false;
723
724 private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
725 private SpannableStringBuilder mDefaultKeySsb = null;
726
727 protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
728
Jeff Brown7e442832011-06-10 18:00:16 -0700729 private final Object mInstanceTracker = StrictMode.trackActivity(this);
730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 private Thread mUiThread;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700732 final Handler mHandler = new Handler();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 /** Return the intent that started this activity. */
735 public Intent getIntent() {
736 return mIntent;
737 }
738
739 /**
740 * Change the intent returned by {@link #getIntent}. This holds a
741 * reference to the given intent; it does not copy it. Often used in
742 * conjunction with {@link #onNewIntent}.
743 *
744 * @param newIntent The new Intent object to return from getIntent
745 *
746 * @see #getIntent
747 * @see #onNewIntent
748 */
749 public void setIntent(Intent newIntent) {
750 mIntent = newIntent;
751 }
752
753 /** Return the application that owns this activity. */
754 public final Application getApplication() {
755 return mApplication;
756 }
757
758 /** Is this activity embedded inside of another activity? */
759 public final boolean isChild() {
760 return mParent != null;
761 }
762
763 /** Return the parent activity if this view is an embedded child. */
764 public final Activity getParent() {
765 return mParent;
766 }
767
768 /** Retrieve the window manager for showing custom windows. */
769 public WindowManager getWindowManager() {
770 return mWindowManager;
771 }
772
773 /**
774 * Retrieve the current {@link android.view.Window} for the activity.
775 * This can be used to directly access parts of the Window API that
776 * are not available through Activity/Screen.
777 *
778 * @return Window The current window, or null if the activity is not
779 * visual.
780 */
781 public Window getWindow() {
782 return mWindow;
783 }
784
785 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -0700786 * Return the LoaderManager for this fragment, creating it if needed.
787 */
788 public LoaderManager getLoaderManager() {
789 if (mLoaderManager != null) {
790 return mLoaderManager;
791 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700792 mCheckedForLoaderManager = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700793 mLoaderManager = getLoaderManager(-1, mLoadersStarted, true);
Dianne Hackbornc8017682010-07-06 13:34:38 -0700794 return mLoaderManager;
795 }
796
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700797 LoaderManagerImpl getLoaderManager(int index, boolean started, boolean create) {
Dianne Hackbornc8017682010-07-06 13:34:38 -0700798 if (mAllLoaderManagers == null) {
Dianne Hackborn4911b782010-07-15 12:54:39 -0700799 mAllLoaderManagers = new SparseArray<LoaderManagerImpl>();
Dianne Hackbornc8017682010-07-06 13:34:38 -0700800 }
Dianne Hackborn4911b782010-07-15 12:54:39 -0700801 LoaderManagerImpl lm = mAllLoaderManagers.get(index);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700802 if (lm == null) {
803 if (create) {
804 lm = new LoaderManagerImpl(this, started);
805 mAllLoaderManagers.put(index, lm);
806 }
807 } else {
808 lm.updateActivity(this);
Dianne Hackbornc8017682010-07-06 13:34:38 -0700809 }
810 return lm;
811 }
812
813 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 * Calls {@link android.view.Window#getCurrentFocus} on the
815 * Window of this Activity to return the currently focused view.
816 *
817 * @return View The current View with focus or null.
818 *
819 * @see #getWindow
820 * @see android.view.Window#getCurrentFocus
821 */
822 public View getCurrentFocus() {
823 return mWindow != null ? mWindow.getCurrentFocus() : null;
824 }
825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 /**
827 * Called when the activity is starting. This is where most initialization
828 * should go: calling {@link #setContentView(int)} to inflate the
829 * activity's UI, using {@link #findViewById} to programmatically interact
830 * with widgets in the UI, calling
831 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
832 * cursors for data being displayed, etc.
833 *
834 * <p>You can call {@link #finish} from within this function, in
835 * which case onDestroy() will be immediately called without any of the rest
836 * of the activity lifecycle ({@link #onStart}, {@link #onResume},
837 * {@link #onPause}, etc) executing.
838 *
839 * <p><em>Derived classes must call through to the super class's
840 * implementation of this method. If they do not, an exception will be
841 * thrown.</em></p>
842 *
843 * @param savedInstanceState If the activity is being re-initialized after
844 * previously being shut down then this Bundle contains the data it most
845 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
846 *
847 * @see #onStart
848 * @see #onSaveInstanceState
849 * @see #onRestoreInstanceState
850 * @see #onPostCreate
851 */
852 protected void onCreate(Bundle savedInstanceState) {
Dianne Hackborn2707d602010-07-09 18:01:20 -0700853 if (mLastNonConfigurationInstances != null) {
854 mAllLoaderManagers = mLastNonConfigurationInstances.loaders;
855 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700856 if (savedInstanceState != null) {
857 Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
858 mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
859 ? mLastNonConfigurationInstances.fragments : null);
860 }
861 mFragments.dispatchCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 mCalled = true;
863 }
864
865 /**
866 * The hook for {@link ActivityThread} to restore the state of this activity.
867 *
868 * Calls {@link #onSaveInstanceState(android.os.Bundle)} and
869 * {@link #restoreManagedDialogs(android.os.Bundle)}.
870 *
871 * @param savedInstanceState contains the saved state
872 */
873 final void performRestoreInstanceState(Bundle savedInstanceState) {
874 onRestoreInstanceState(savedInstanceState);
875 restoreManagedDialogs(savedInstanceState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 }
877
878 /**
879 * This method is called after {@link #onStart} when the activity is
880 * being re-initialized from a previously saved state, given here in
Mike LeBeau305de9d2010-03-11 09:21:08 -0800881 * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 * to restore their state, but it is sometimes convenient to do it here
883 * after all of the initialization has been done or to allow subclasses to
884 * decide whether to use your default implementation. The default
885 * implementation of this method performs a restore of any view state that
886 * had previously been frozen by {@link #onSaveInstanceState}.
887 *
888 * <p>This method is called between {@link #onStart} and
889 * {@link #onPostCreate}.
890 *
891 * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}.
892 *
893 * @see #onCreate
894 * @see #onPostCreate
895 * @see #onResume
896 * @see #onSaveInstanceState
897 */
898 protected void onRestoreInstanceState(Bundle savedInstanceState) {
899 if (mWindow != null) {
900 Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG);
901 if (windowState != null) {
902 mWindow.restoreHierarchyState(windowState);
903 }
904 }
905 }
906
907 /**
908 * Restore the state of any saved managed dialogs.
909 *
910 * @param savedInstanceState The bundle to restore from.
911 */
912 private void restoreManagedDialogs(Bundle savedInstanceState) {
913 final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
914 if (b == null) {
915 return;
916 }
917
918 final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
919 final int numDialogs = ids.length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800920 mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 for (int i = 0; i < numDialogs; i++) {
922 final Integer dialogId = ids[i];
923 Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
924 if (dialogState != null) {
Romain Guye35c2352009-06-19 13:18:12 -0700925 // Calling onRestoreInstanceState() below will invoke dispatchOnCreate
926 // so tell createDialog() not to do it, otherwise we get an exception
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800927 final ManagedDialog md = new ManagedDialog();
928 md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
929 md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
930 if (md.mDialog != null) {
931 mManagedDialogs.put(dialogId, md);
932 onPrepareDialog(dialogId, md.mDialog, md.mArgs);
933 md.mDialog.onRestoreInstanceState(dialogState);
934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
936 }
937 }
938
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800939 private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
940 final Dialog dialog = onCreateDialog(dialogId, args);
Romain Guy764d5332009-06-17 16:52:22 -0700941 if (dialog == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800942 return null;
Romain Guy764d5332009-06-17 16:52:22 -0700943 }
Romain Guy6de4aed2009-07-08 10:54:45 -0700944 dialog.dispatchOnCreate(state);
Romain Guy764d5332009-06-17 16:52:22 -0700945 return dialog;
946 }
947
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800948 private static String savedDialogKeyFor(int key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 return SAVED_DIALOG_KEY_PREFIX + key;
950 }
951
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800952 private static String savedDialogArgsKeyFor(int key) {
953 return SAVED_DIALOG_ARGS_KEY_PREFIX + key;
954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955
956 /**
957 * Called when activity start-up is complete (after {@link #onStart}
958 * and {@link #onRestoreInstanceState} have been called). Applications will
959 * generally not implement this method; it is intended for system
960 * classes to do final initialization after application code has run.
961 *
962 * <p><em>Derived classes must call through to the super class's
963 * implementation of this method. If they do not, an exception will be
964 * thrown.</em></p>
965 *
966 * @param savedInstanceState If the activity is being re-initialized after
967 * previously being shut down then this Bundle contains the data it most
968 * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b>
969 * @see #onCreate
970 */
971 protected void onPostCreate(Bundle savedInstanceState) {
972 if (!isChild()) {
973 mTitleReady = true;
974 onTitleChanged(getTitle(), getTitleColor());
975 }
976 mCalled = true;
977 }
978
979 /**
980 * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
981 * the activity had been stopped, but is now again being displayed to the
982 * user. It will be followed by {@link #onResume}.
983 *
984 * <p><em>Derived classes must call through to the super class's
985 * implementation of this method. If they do not, an exception will be
986 * thrown.</em></p>
987 *
988 * @see #onCreate
989 * @see #onStop
990 * @see #onResume
991 */
992 protected void onStart() {
993 mCalled = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700994
995 if (!mLoadersStarted) {
996 mLoadersStarted = true;
997 if (mLoaderManager != null) {
998 mLoaderManager.doStart();
999 } else if (!mCheckedForLoaderManager) {
1000 mLoaderManager = getLoaderManager(-1, mLoadersStarted, false);
1001 }
1002 mCheckedForLoaderManager = true;
Dianne Hackborn2707d602010-07-09 18:01:20 -07001003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 }
1005
1006 /**
1007 * Called after {@link #onStop} when the current activity is being
1008 * re-displayed to the user (the user has navigated back to it). It will
1009 * be followed by {@link #onStart} and then {@link #onResume}.
1010 *
1011 * <p>For activities that are using raw {@link Cursor} objects (instead of
1012 * creating them through
1013 * {@link #managedQuery(android.net.Uri , String[], String, String[], String)},
1014 * this is usually the place
1015 * where the cursor should be requeried (because you had deactivated it in
1016 * {@link #onStop}.
1017 *
1018 * <p><em>Derived classes must call through to the super class's
1019 * implementation of this method. If they do not, an exception will be
1020 * thrown.</em></p>
1021 *
1022 * @see #onStop
1023 * @see #onStart
1024 * @see #onResume
1025 */
1026 protected void onRestart() {
1027 mCalled = true;
1028 }
1029
1030 /**
1031 * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
1032 * {@link #onPause}, for your activity to start interacting with the user.
1033 * This is a good place to begin animations, open exclusive-access devices
1034 * (such as the camera), etc.
1035 *
1036 * <p>Keep in mind that onResume is not the best indicator that your activity
1037 * is visible to the user; a system window such as the keyguard may be in
1038 * front. Use {@link #onWindowFocusChanged} to know for certain that your
1039 * activity is visible to the user (for example, to resume a game).
1040 *
1041 * <p><em>Derived classes must call through to the super class's
1042 * implementation of this method. If they do not, an exception will be
1043 * thrown.</em></p>
1044 *
1045 * @see #onRestoreInstanceState
1046 * @see #onRestart
1047 * @see #onPostResume
1048 * @see #onPause
1049 */
1050 protected void onResume() {
1051 mCalled = true;
1052 }
1053
1054 /**
1055 * Called when activity resume is complete (after {@link #onResume} has
1056 * been called). Applications will generally not implement this method;
1057 * it is intended for system classes to do final setup after application
1058 * resume code has run.
1059 *
1060 * <p><em>Derived classes must call through to the super class's
1061 * implementation of this method. If they do not, an exception will be
1062 * thrown.</em></p>
1063 *
1064 * @see #onResume
1065 */
1066 protected void onPostResume() {
1067 final Window win = getWindow();
1068 if (win != null) win.makeActive();
Adam Powell50efbed2011-02-08 16:20:15 -08001069 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 mCalled = true;
1071 }
1072
1073 /**
1074 * This is called for activities that set launchMode to "singleTop" in
1075 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
1076 * flag when calling {@link #startActivity}. In either case, when the
1077 * activity is re-launched while at the top of the activity stack instead
1078 * of a new instance of the activity being started, onNewIntent() will be
1079 * called on the existing instance with the Intent that was used to
1080 * re-launch it.
1081 *
1082 * <p>An activity will always be paused before receiving a new intent, so
1083 * you can count on {@link #onResume} being called after this method.
1084 *
1085 * <p>Note that {@link #getIntent} still returns the original Intent. You
1086 * can use {@link #setIntent} to update it to this new Intent.
1087 *
1088 * @param intent The new intent that was started for the activity.
1089 *
1090 * @see #getIntent
1091 * @see #setIntent
1092 * @see #onResume
1093 */
1094 protected void onNewIntent(Intent intent) {
1095 }
1096
1097 /**
1098 * The hook for {@link ActivityThread} to save the state of this activity.
1099 *
1100 * Calls {@link #onSaveInstanceState(android.os.Bundle)}
1101 * and {@link #saveManagedDialogs(android.os.Bundle)}.
1102 *
1103 * @param outState The bundle to save the state to.
1104 */
1105 final void performSaveInstanceState(Bundle outState) {
1106 onSaveInstanceState(outState);
1107 saveManagedDialogs(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 }
1109
1110 /**
1111 * Called to retrieve per-instance state from an activity before being killed
1112 * so that the state can be restored in {@link #onCreate} or
1113 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
1114 * will be passed to both).
1115 *
1116 * <p>This method is called before an activity may be killed so that when it
1117 * comes back some time in the future it can restore its state. For example,
1118 * if activity B is launched in front of activity A, and at some point activity
1119 * A is killed to reclaim resources, activity A will have a chance to save the
1120 * current state of its user interface via this method so that when the user
1121 * returns to activity A, the state of the user interface can be restored
1122 * via {@link #onCreate} or {@link #onRestoreInstanceState}.
1123 *
1124 * <p>Do not confuse this method with activity lifecycle callbacks such as
1125 * {@link #onPause}, which is always called when an activity is being placed
1126 * in the background or on its way to destruction, or {@link #onStop} which
1127 * is called before destruction. One example of when {@link #onPause} and
1128 * {@link #onStop} is called and not this method is when a user navigates back
1129 * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
1130 * on B because that particular instance will never be restored, so the
1131 * system avoids calling it. An example when {@link #onPause} is called and
1132 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
1133 * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
1134 * killed during the lifetime of B since the state of the user interface of
1135 * A will stay intact.
1136 *
1137 * <p>The default implementation takes care of most of the UI per-instance
1138 * state for you by calling {@link android.view.View#onSaveInstanceState()} on each
1139 * view in the hierarchy that has an id, and by saving the id of the currently
1140 * focused view (all of which is restored by the default implementation of
1141 * {@link #onRestoreInstanceState}). If you override this method to save additional
1142 * information not captured by each individual view, you will likely want to
1143 * call through to the default implementation, otherwise be prepared to save
1144 * all of the state of each view yourself.
1145 *
1146 * <p>If called, this method will occur before {@link #onStop}. There are
1147 * no guarantees about whether it will occur before or after {@link #onPause}.
1148 *
1149 * @param outState Bundle in which to place your saved state.
1150 *
1151 * @see #onCreate
1152 * @see #onRestoreInstanceState
1153 * @see #onPause
1154 */
1155 protected void onSaveInstanceState(Bundle outState) {
1156 outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001157 Parcelable p = mFragments.saveAllState();
1158 if (p != null) {
1159 outState.putParcelable(FRAGMENTS_TAG, p);
1160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162
1163 /**
1164 * Save the state of any managed dialogs.
1165 *
1166 * @param outState place to store the saved state.
1167 */
1168 private void saveManagedDialogs(Bundle outState) {
1169 if (mManagedDialogs == null) {
1170 return;
1171 }
1172
1173 final int numDialogs = mManagedDialogs.size();
1174 if (numDialogs == 0) {
1175 return;
1176 }
1177
1178 Bundle dialogState = new Bundle();
1179
1180 int[] ids = new int[mManagedDialogs.size()];
1181
1182 // save each dialog's bundle, gather the ids
1183 for (int i = 0; i < numDialogs; i++) {
1184 final int key = mManagedDialogs.keyAt(i);
1185 ids[i] = key;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001186 final ManagedDialog md = mManagedDialogs.valueAt(i);
1187 dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());
1188 if (md.mArgs != null) {
1189 dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
1190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192
1193 dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
1194 outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
1195 }
1196
1197
1198 /**
1199 * Called as part of the activity lifecycle when an activity is going into
1200 * the background, but has not (yet) been killed. The counterpart to
1201 * {@link #onResume}.
1202 *
1203 * <p>When activity B is launched in front of activity A, this callback will
1204 * be invoked on A. B will not be created until A's {@link #onPause} returns,
1205 * so be sure to not do anything lengthy here.
1206 *
1207 * <p>This callback is mostly used for saving any persistent state the
1208 * activity is editing, to present a "edit in place" model to the user and
1209 * making sure nothing is lost if there are not enough resources to start
1210 * the new activity without first killing this one. This is also a good
1211 * place to do things like stop animations and other things that consume a
1212 * noticeable mount of CPU in order to make the switch to the next activity
1213 * as fast as possible, or to close resources that are exclusive access
1214 * such as the camera.
1215 *
1216 * <p>In situations where the system needs more memory it may kill paused
1217 * processes to reclaim resources. Because of this, you should be sure
1218 * that all of your state is saved by the time you return from
1219 * this function. In general {@link #onSaveInstanceState} is used to save
1220 * per-instance state in the activity and this method is used to store
1221 * global persistent data (in content providers, files, etc.)
1222 *
1223 * <p>After receiving this call you will usually receive a following call
1224 * to {@link #onStop} (after the next activity has been resumed and
1225 * displayed), however in some cases there will be a direct call back to
1226 * {@link #onResume} without going through the stopped state.
1227 *
1228 * <p><em>Derived classes must call through to the super class's
1229 * implementation of this method. If they do not, an exception will be
1230 * thrown.</em></p>
1231 *
1232 * @see #onResume
1233 * @see #onSaveInstanceState
1234 * @see #onStop
1235 */
1236 protected void onPause() {
1237 mCalled = true;
1238 }
1239
1240 /**
1241 * Called as part of the activity lifecycle when an activity is about to go
1242 * into the background as the result of user choice. For example, when the
1243 * user presses the Home key, {@link #onUserLeaveHint} will be called, but
1244 * when an incoming phone call causes the in-call Activity to be automatically
1245 * brought to the foreground, {@link #onUserLeaveHint} will not be called on
1246 * the activity being interrupted. In cases when it is invoked, this method
1247 * is called right before the activity's {@link #onPause} callback.
1248 *
1249 * <p>This callback and {@link #onUserInteraction} are intended to help
1250 * activities manage status bar notifications intelligently; specifically,
1251 * for helping activities determine the proper time to cancel a notfication.
1252 *
1253 * @see #onUserInteraction()
1254 */
1255 protected void onUserLeaveHint() {
1256 }
1257
1258 /**
1259 * Generate a new thumbnail for this activity. This method is called before
1260 * pausing the activity, and should draw into <var>outBitmap</var> the
1261 * imagery for the desired thumbnail in the dimensions of that bitmap. It
1262 * can use the given <var>canvas</var>, which is configured to draw into the
1263 * bitmap, for rendering if desired.
1264 *
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001265 * <p>The default implementation returns fails and does not draw a thumbnail;
1266 * this will result in the platform creating its own thumbnail if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 *
1268 * @param outBitmap The bitmap to contain the thumbnail.
1269 * @param canvas Can be used to render into the bitmap.
1270 *
1271 * @return Return true if you have drawn into the bitmap; otherwise after
1272 * you return it will be filled with a default thumbnail.
1273 *
1274 * @see #onCreateDescription
1275 * @see #onSaveInstanceState
1276 * @see #onPause
1277 */
1278 public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08001279 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
1281
1282 /**
1283 * Generate a new description for this activity. This method is called
1284 * before pausing the activity and can, if desired, return some textual
1285 * description of its current state to be displayed to the user.
1286 *
1287 * <p>The default implementation returns null, which will cause you to
1288 * inherit the description from the previous activity. If all activities
1289 * return null, generally the label of the top activity will be used as the
1290 * description.
1291 *
1292 * @return A description of what the user is doing. It should be short and
1293 * sweet (only a few words).
1294 *
1295 * @see #onCreateThumbnail
1296 * @see #onSaveInstanceState
1297 * @see #onPause
1298 */
1299 public CharSequence onCreateDescription() {
1300 return null;
1301 }
1302
1303 /**
1304 * Called when you are no longer visible to the user. You will next
1305 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
1306 * depending on later user activity.
1307 *
1308 * <p>Note that this method may never be called, in low memory situations
1309 * where the system does not have enough memory to keep your activity's
1310 * process running after its {@link #onPause} method is called.
1311 *
1312 * <p><em>Derived classes must call through to the super class's
1313 * implementation of this method. If they do not, an exception will be
1314 * thrown.</em></p>
1315 *
1316 * @see #onRestart
1317 * @see #onResume
1318 * @see #onSaveInstanceState
1319 * @see #onDestroy
1320 */
1321 protected void onStop() {
Adam Powell50efbed2011-02-08 16:20:15 -08001322 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 mCalled = true;
1324 }
1325
1326 /**
1327 * Perform any final cleanup before an activity is destroyed. This can
1328 * happen either because the activity is finishing (someone called
1329 * {@link #finish} on it, or because the system is temporarily destroying
1330 * this instance of the activity to save space. You can distinguish
1331 * between these two scenarios with the {@link #isFinishing} method.
1332 *
1333 * <p><em>Note: do not count on this method being called as a place for
1334 * saving data! For example, if an activity is editing data in a content
1335 * provider, those edits should be committed in either {@link #onPause} or
1336 * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
1337 * free resources like threads that are associated with an activity, so
1338 * that a destroyed activity does not leave such things around while the
1339 * rest of its application is still running. There are situations where
1340 * the system will simply kill the activity's hosting process without
1341 * calling this method (or any others) in it, so it should not be used to
1342 * do things that are intended to remain around after the process goes
1343 * away.
1344 *
1345 * <p><em>Derived classes must call through to the super class's
1346 * implementation of this method. If they do not, an exception will be
1347 * thrown.</em></p>
1348 *
1349 * @see #onPause
1350 * @see #onStop
1351 * @see #finish
1352 * @see #isFinishing
1353 */
1354 protected void onDestroy() {
1355 mCalled = true;
1356
1357 // dismiss any dialogs we are managing.
1358 if (mManagedDialogs != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 final int numDialogs = mManagedDialogs.size();
1360 for (int i = 0; i < numDialogs; i++) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001361 final ManagedDialog md = mManagedDialogs.valueAt(i);
1362 if (md.mDialog.isShowing()) {
1363 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
1365 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001366 mManagedDialogs = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368
1369 // close any cursors we are managing.
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001370 synchronized (mManagedCursors) {
1371 int numCursors = mManagedCursors.size();
1372 for (int i = 0; i < numCursors; i++) {
1373 ManagedCursor c = mManagedCursors.get(i);
1374 if (c != null) {
1375 c.mCursor.close();
1376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 }
Makoto Onuki2f6a0182010-02-22 13:26:59 -08001378 mManagedCursors.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
Amith Yamasani49860442010-03-17 20:54:10 -07001380
1381 // Close any open search dialog
1382 if (mSearchManager != null) {
1383 mSearchManager.stopSearch();
1384 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
1386
1387 /**
1388 * Called by the system when the device configuration changes while your
1389 * activity is running. Note that this will <em>only</em> be called if
1390 * you have selected configurations you would like to handle with the
1391 * {@link android.R.attr#configChanges} attribute in your manifest. If
1392 * any configuration change occurs that is not selected to be reported
1393 * by that attribute, then instead of reporting it the system will stop
1394 * and restart the activity (to have it launched with the new
1395 * configuration).
1396 *
1397 * <p>At the time that this function has been called, your Resources
1398 * object will have been updated to return resource values matching the
1399 * new configuration.
1400 *
1401 * @param newConfig The new device configuration.
1402 */
1403 public void onConfigurationChanged(Configuration newConfig) {
1404 mCalled = true;
Bjorn Bringert444c7272009-07-06 21:32:50 +01001405
Dianne Hackborn9d071802010-12-08 14:49:15 -08001406 mFragments.dispatchConfigurationChanged(newConfig);
1407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 if (mWindow != null) {
1409 // Pass the configuration changed event to the window
1410 mWindow.onConfigurationChanged(newConfig);
1411 }
Adam Powell45c0b192011-07-28 15:11:57 -07001412
1413 if (mActionBar != null) {
1414 // Do this last; the action bar will need to access
1415 // view changes from above.
1416 mActionBar.onConfigurationChanged(newConfig);
1417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
1419
1420 /**
1421 * If this activity is being destroyed because it can not handle a
1422 * configuration parameter being changed (and thus its
1423 * {@link #onConfigurationChanged(Configuration)} method is
1424 * <em>not</em> being called), then you can use this method to discover
1425 * the set of changes that have occurred while in the process of being
1426 * destroyed. Note that there is no guarantee that these will be
1427 * accurate (other changes could have happened at any time), so you should
1428 * only use this as an optimization hint.
1429 *
1430 * @return Returns a bit field of the configuration parameters that are
1431 * changing, as defined by the {@link android.content.res.Configuration}
1432 * class.
1433 */
1434 public int getChangingConfigurations() {
1435 return mConfigChangeFlags;
1436 }
1437
1438 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -07001439 * @deprecated Use the new {@link Fragment} API
1440 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
1441 * available on older platforms through the Android compatibility package.
1442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 * Retrieve the non-configuration instance data that was previously
1444 * returned by {@link #onRetainNonConfigurationInstance()}. This will
1445 * be available from the initial {@link #onCreate} and
1446 * {@link #onStart} calls to the new instance, allowing you to extract
1447 * any useful dynamic state from the previous instance.
1448 *
1449 * <p>Note that the data you retrieve here should <em>only</em> be used
1450 * as an optimization for handling configuration changes. You should always
1451 * be able to handle getting a null pointer back, and an activity must
1452 * still be able to restore itself to its previous state (through the
1453 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
1454 * function returns null.
1455 *
1456 * @return Returns the object previously returned by
1457 * {@link #onRetainNonConfigurationInstance()}.
1458 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07001459 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 public Object getLastNonConfigurationInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001461 return mLastNonConfigurationInstances != null
1462 ? mLastNonConfigurationInstances.activity : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 }
1464
1465 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -07001466 * @deprecated Use the new {@link Fragment} API
1467 * {@link Fragment#setRetainInstance(boolean)} instead; this is also
1468 * available on older platforms through the Android compatibility package.
1469 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 * Called by the system, as part of destroying an
1471 * activity due to a configuration change, when it is known that a new
1472 * instance will immediately be created for the new configuration. You
1473 * can return any object you like here, including the activity instance
1474 * itself, which can later be retrieved by calling
1475 * {@link #getLastNonConfigurationInstance()} in the new activity
1476 * instance.
1477 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001478 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1479 * or later, consider instead using a {@link Fragment} with
1480 * {@link Fragment#setRetainInstance(boolean)
1481 * Fragment.setRetainInstance(boolean}.</em>
1482 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 * <p>This function is called purely as an optimization, and you must
1484 * not rely on it being called. When it is called, a number of guarantees
1485 * will be made to help optimize configuration switching:
1486 * <ul>
1487 * <li> The function will be called between {@link #onStop} and
1488 * {@link #onDestroy}.
1489 * <li> A new instance of the activity will <em>always</em> be immediately
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001490 * created after this one's {@link #onDestroy()} is called. In particular,
1491 * <em>no</em> messages will be dispatched during this time (when the returned
1492 * object does not have an activity to be associated with).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 * <li> The object you return here will <em>always</em> be available from
1494 * the {@link #getLastNonConfigurationInstance()} method of the following
1495 * activity instance as described there.
1496 * </ul>
1497 *
1498 * <p>These guarantees are designed so that an activity can use this API
1499 * to propagate extensive state from the old to new activity instance, from
1500 * loaded bitmaps, to network connections, to evenly actively running
1501 * threads. Note that you should <em>not</em> propagate any data that
1502 * may change based on the configuration, including any data loaded from
1503 * resources such as strings, layouts, or drawables.
1504 *
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001505 * <p>The guarantee of no message handling during the switch to the next
1506 * activity simplifies use with active objects. For example if your retained
1507 * state is an {@link android.os.AsyncTask} you are guaranteed that its
1508 * call back functions (like {@link android.os.AsyncTask#onPostExecute}) will
1509 * not be called from the call here until you execute the next instance's
1510 * {@link #onCreate(Bundle)}. (Note however that there is of course no such
1511 * guarantee for {@link android.os.AsyncTask#doInBackground} since that is
1512 * running in a separate thread.)
1513 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 * @return Return any Object holding the desired state to propagate to the
1515 * next activity instance.
1516 */
1517 public Object onRetainNonConfigurationInstance() {
1518 return null;
1519 }
1520
1521 /**
1522 * Retrieve the non-configuration instance data that was previously
1523 * returned by {@link #onRetainNonConfigurationChildInstances()}. This will
1524 * be available from the initial {@link #onCreate} and
1525 * {@link #onStart} calls to the new instance, allowing you to extract
1526 * any useful dynamic state from the previous instance.
1527 *
1528 * <p>Note that the data you retrieve here should <em>only</em> be used
1529 * as an optimization for handling configuration changes. You should always
1530 * be able to handle getting a null pointer back, and an activity must
1531 * still be able to restore itself to its previous state (through the
1532 * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this
1533 * function returns null.
1534 *
1535 * @return Returns the object previously returned by
1536 * {@link #onRetainNonConfigurationChildInstances()}
1537 */
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001538 HashMap<String, Object> getLastNonConfigurationChildInstances() {
1539 return mLastNonConfigurationInstances != null
1540 ? mLastNonConfigurationInstances.children : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
1542
1543 /**
1544 * This method is similar to {@link #onRetainNonConfigurationInstance()} except that
1545 * it should return either a mapping from child activity id strings to arbitrary objects,
1546 * or null. This method is intended to be used by Activity framework subclasses that control a
1547 * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply
1548 * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null.
1549 */
1550 HashMap<String,Object> onRetainNonConfigurationChildInstances() {
1551 return null;
1552 }
1553
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001554 NonConfigurationInstances retainNonConfigurationInstances() {
1555 Object activity = onRetainNonConfigurationInstance();
1556 HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
1557 ArrayList<Fragment> fragments = mFragments.retainNonConfig();
Dianne Hackborn2707d602010-07-09 18:01:20 -07001558 boolean retainLoaders = false;
1559 if (mAllLoaderManagers != null) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001560 // prune out any loader managers that were already stopped and so
Dianne Hackborn2707d602010-07-09 18:01:20 -07001561 // have nothing useful to retain.
1562 for (int i=mAllLoaderManagers.size()-1; i>=0; i--) {
Dianne Hackborn4911b782010-07-15 12:54:39 -07001563 LoaderManagerImpl lm = mAllLoaderManagers.valueAt(i);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001564 if (lm.mRetaining) {
1565 retainLoaders = true;
1566 } else {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001567 lm.doDestroy();
Dianne Hackborn2707d602010-07-09 18:01:20 -07001568 mAllLoaderManagers.removeAt(i);
1569 }
1570 }
1571 }
1572 if (activity == null && children == null && fragments == null && !retainLoaders) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001573 return null;
1574 }
1575
1576 NonConfigurationInstances nci = new NonConfigurationInstances();
1577 nci.activity = activity;
1578 nci.children = children;
1579 nci.fragments = fragments;
Dianne Hackborn2707d602010-07-09 18:01:20 -07001580 nci.loaders = mAllLoaderManagers;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001581 return nci;
1582 }
1583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 public void onLowMemory() {
1585 mCalled = true;
Dianne Hackborn9d071802010-12-08 14:49:15 -08001586 mFragments.dispatchLowMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 }
1588
1589 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001590 * Return the FragmentManager for interacting with fragments associated
1591 * with this activity.
1592 */
1593 public FragmentManager getFragmentManager() {
1594 return mFragments;
1595 }
1596
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07001597 void invalidateFragmentIndex(int index) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001598 //Log.v(TAG, "invalidateFragmentIndex: index=" + index);
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07001599 if (mAllLoaderManagers != null) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001600 LoaderManagerImpl lm = mAllLoaderManagers.get(index);
Dianne Hackbornd04ad542011-07-25 16:16:15 -07001601 if (lm != null && !lm.mRetaining) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001602 lm.doDestroy();
Dianne Hackbornd04ad542011-07-25 16:16:15 -07001603 mAllLoaderManagers.remove(index);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001604 }
Dianne Hackborn9e14e9f32010-07-14 11:07:38 -07001605 }
1606 }
1607
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001608 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001609 * Called when a Fragment is being attached to this activity, immediately
1610 * after the call to its {@link Fragment#onAttach Fragment.onAttach()}
1611 * method and before {@link Fragment#onCreate Fragment.onCreate()}.
1612 */
1613 public void onAttachFragment(Fragment fragment) {
1614 }
1615
1616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 * Wrapper around
1618 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
1619 * that gives the resulting {@link Cursor} to call
1620 * {@link #startManagingCursor} so that the activity will manage its
1621 * lifecycle for you.
1622 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001623 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1624 * or later, consider instead using {@link LoaderManager} instead, available
1625 * via {@link #getLoaderManager()}.</em>
1626 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 * @param uri The URI of the content provider to query.
1628 * @param projection List of columns to return.
1629 * @param selection SQL WHERE clause.
1630 * @param sortOrder SQL ORDER BY clause.
1631 *
1632 * @return The Cursor that was returned by query().
1633 *
1634 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
1635 * @see #startManagingCursor
1636 * @hide
Jason parks6ed50de2010-08-25 10:18:50 -05001637 *
1638 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 */
Jason parks6ed50de2010-08-25 10:18:50 -05001640 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07001641 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
1642 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder);
1644 if (c != null) {
1645 startManagingCursor(c);
1646 }
1647 return c;
1648 }
1649
1650 /**
1651 * Wrapper around
1652 * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)}
1653 * that gives the resulting {@link Cursor} to call
1654 * {@link #startManagingCursor} so that the activity will manage its
1655 * lifecycle for you.
1656 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001657 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1658 * or later, consider instead using {@link LoaderManager} instead, available
1659 * via {@link #getLoaderManager()}.</em>
1660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 * @param uri The URI of the content provider to query.
1662 * @param projection List of columns to return.
1663 * @param selection SQL WHERE clause.
1664 * @param selectionArgs The arguments to selection, if any ?s are pesent
1665 * @param sortOrder SQL ORDER BY clause.
1666 *
1667 * @return The Cursor that was returned by query().
1668 *
1669 * @see ContentResolver#query(android.net.Uri , String[], String, String[], String)
1670 * @see #startManagingCursor
Jason parks6ed50de2010-08-25 10:18:50 -05001671 *
1672 * @deprecated Use {@link CursorLoader} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 */
Jason parks6ed50de2010-08-25 10:18:50 -05001674 @Deprecated
Dianne Hackborn291905e2010-08-17 15:17:15 -07001675 public final Cursor managedQuery(Uri uri, String[] projection, String selection,
1676 String[] selectionArgs, String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
1678 if (c != null) {
1679 startManagingCursor(c);
1680 }
1681 return c;
1682 }
1683
1684 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -07001685 * @deprecated Use the new {@link android.content.CursorLoader} class with
1686 * {@link LoaderManager} instead; this is also
1687 * available on older platforms through the Android compatibility package.
1688 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 * This method allows the activity to take care of managing the given
1690 * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.
1691 * That is, when the activity is stopped it will automatically call
1692 * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted
1693 * it will call {@link Cursor#requery} for you. When the activity is
1694 * destroyed, all managed Cursors will be closed automatically.
1695 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07001696 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
1697 * or later, consider instead using {@link LoaderManager} instead, available
1698 * via {@link #getLoaderManager()}.</em>
1699 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 * @param c The Cursor to be managed.
1701 *
1702 * @see #managedQuery(android.net.Uri , String[], String, String[], String)
1703 * @see #stopManagingCursor
1704 */
Jason parks6ed50de2010-08-25 10:18:50 -05001705 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 public void startManagingCursor(Cursor c) {
1707 synchronized (mManagedCursors) {
1708 mManagedCursors.add(new ManagedCursor(c));
1709 }
1710 }
1711
1712 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -07001713 * @deprecated Use the new {@link android.content.CursorLoader} class with
1714 * {@link LoaderManager} instead; this is also
1715 * available on older platforms through the Android compatibility package.
1716 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 * Given a Cursor that was previously given to
1718 * {@link #startManagingCursor}, stop the activity's management of that
1719 * cursor.
1720 *
1721 * @param c The Cursor that was being managed.
1722 *
1723 * @see #startManagingCursor
1724 */
Jason parks6ed50de2010-08-25 10:18:50 -05001725 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 public void stopManagingCursor(Cursor c) {
1727 synchronized (mManagedCursors) {
1728 final int N = mManagedCursors.size();
1729 for (int i=0; i<N; i++) {
1730 ManagedCursor mc = mManagedCursors.get(i);
1731 if (mc.mCursor == c) {
1732 mManagedCursors.remove(i);
1733 break;
1734 }
1735 }
1736 }
1737 }
1738
1739 /**
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07001740 * @deprecated As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}
1741 * this is a no-op.
Dianne Hackborn4f3867e2010-12-14 22:09:51 -08001742 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 */
Dianne Hackbornd3efa392010-09-01 17:34:12 -07001744 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 public void setPersistent(boolean isPersistent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
1747
1748 /**
1749 * Finds a view that was identified by the id attribute from the XML that
1750 * was processed in {@link #onCreate}.
1751 *
1752 * @return The view if found or null otherwise.
1753 */
1754 public View findViewById(int id) {
1755 return getWindow().findViewById(id);
1756 }
Adam Powell33b97432010-04-20 10:01:14 -07001757
1758 /**
1759 * Retrieve a reference to this activity's ActionBar.
Adam Powell42c0fe82010-08-10 16:36:56 -07001760 *
Adam Powell33b97432010-04-20 10:01:14 -07001761 * @return The Activity's ActionBar, or null if it does not have one.
1762 */
1763 public ActionBar getActionBar() {
Adam Powell42c0fe82010-08-10 16:36:56 -07001764 initActionBar();
Adam Powell33b97432010-04-20 10:01:14 -07001765 return mActionBar;
1766 }
1767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 /**
Adam Powell33b97432010-04-20 10:01:14 -07001769 * Creates a new ActionBar, locates the inflated ActionBarView,
1770 * initializes the ActionBar with the view, and sets mActionBar.
1771 */
1772 private void initActionBar() {
Adam Powell89e06452010-06-23 20:24:52 -07001773 Window window = getWindow();
Adam Powella593d982011-05-13 14:09:54 -07001774
1775 // Initializing the window decor can change window feature flags.
1776 // Make sure that we have the correct set before performing the test below.
1777 window.getDecorView();
1778
Adam Powell9b4c8042010-08-10 15:36:44 -07001779 if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
Adam Powell33b97432010-04-20 10:01:14 -07001780 return;
1781 }
1782
Adam Powell661c9082010-07-02 10:09:44 -07001783 mActionBar = new ActionBarImpl(this);
Adam Powell33b97432010-04-20 10:01:14 -07001784 }
1785
1786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 * Set the activity content from a layout resource. The resource will be
1788 * inflated, adding all top-level views to the activity.
Romain Guy482b34a62011-01-20 10:59:28 -08001789 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 * @param layoutResID Resource ID to be inflated.
Romain Guy482b34a62011-01-20 10:59:28 -08001791 *
1792 * @see #setContentView(android.view.View)
1793 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 */
1795 public void setContentView(int layoutResID) {
1796 getWindow().setContentView(layoutResID);
Adam Powell33b97432010-04-20 10:01:14 -07001797 initActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
1799
1800 /**
1801 * Set the activity content to an explicit view. This view is placed
1802 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08001803 * view hierarchy. When calling this method, the layout parameters of the
1804 * specified view are ignored. Both the width and the height of the view are
1805 * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
1806 * your own layout parameters, invoke
1807 * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
1808 * instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 *
1810 * @param view The desired content to display.
Romain Guy482b34a62011-01-20 10:59:28 -08001811 *
1812 * @see #setContentView(int)
1813 * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 */
1815 public void setContentView(View view) {
1816 getWindow().setContentView(view);
Adam Powell33b97432010-04-20 10:01:14 -07001817 initActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 }
1819
1820 /**
1821 * Set the activity content to an explicit view. This view is placed
1822 * directly into the activity's view hierarchy. It can itself be a complex
Romain Guy482b34a62011-01-20 10:59:28 -08001823 * view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 *
1825 * @param view The desired content to display.
1826 * @param params Layout parameters for the view.
Romain Guy482b34a62011-01-20 10:59:28 -08001827 *
1828 * @see #setContentView(android.view.View)
1829 * @see #setContentView(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 */
1831 public void setContentView(View view, ViewGroup.LayoutParams params) {
1832 getWindow().setContentView(view, params);
Adam Powell33b97432010-04-20 10:01:14 -07001833 initActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 }
1835
1836 /**
1837 * Add an additional content view to the activity. Added after any existing
1838 * ones in the activity -- existing views are NOT removed.
1839 *
1840 * @param view The desired content to display.
1841 * @param params Layout parameters for the view.
1842 */
1843 public void addContentView(View view, ViewGroup.LayoutParams params) {
1844 getWindow().addContentView(view, params);
Adam Powell33b97432010-04-20 10:01:14 -07001845 initActionBar();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
1847
1848 /**
Dianne Hackborncfaf8872011-01-18 13:57:54 -08001849 * Sets whether this activity is finished when touched outside its window's
1850 * bounds.
1851 */
1852 public void setFinishOnTouchOutside(boolean finish) {
1853 mWindow.setCloseOnTouchOutside(finish);
1854 }
1855
1856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 * Use with {@link #setDefaultKeyMode} to turn off default handling of
1858 * keys.
1859 *
1860 * @see #setDefaultKeyMode
1861 */
1862 static public final int DEFAULT_KEYS_DISABLE = 0;
1863 /**
1864 * Use with {@link #setDefaultKeyMode} to launch the dialer during default
1865 * key handling.
1866 *
1867 * @see #setDefaultKeyMode
1868 */
1869 static public final int DEFAULT_KEYS_DIALER = 1;
1870 /**
1871 * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in
1872 * default key handling.
1873 *
1874 * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts.
1875 *
1876 * @see #setDefaultKeyMode
1877 */
1878 static public final int DEFAULT_KEYS_SHORTCUT = 2;
1879 /**
1880 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
1881 * will start an application-defined search. (If the application or activity does not
1882 * actually define a search, the the keys will be ignored.)
1883 *
1884 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
1885 *
1886 * @see #setDefaultKeyMode
1887 */
1888 static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3;
1889
1890 /**
1891 * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes
1892 * will start a global search (typically web search, but some platforms may define alternate
1893 * methods for global search)
1894 *
1895 * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details.
1896 *
1897 * @see #setDefaultKeyMode
1898 */
1899 static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4;
1900
1901 /**
1902 * Select the default key handling for this activity. This controls what
1903 * will happen to key events that are not otherwise handled. The default
1904 * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the
1905 * floor. Other modes allow you to launch the dialer
1906 * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options
1907 * menu without requiring the menu key be held down
1908 * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL}
1909 * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}).
1910 *
1911 * <p>Note that the mode selected here does not impact the default
1912 * handling of system keys, such as the "back" and "menu" keys, and your
1913 * activity and its views always get a first chance to receive and handle
1914 * all application keys.
1915 *
1916 * @param mode The desired default key mode constant.
1917 *
1918 * @see #DEFAULT_KEYS_DISABLE
1919 * @see #DEFAULT_KEYS_DIALER
1920 * @see #DEFAULT_KEYS_SHORTCUT
1921 * @see #DEFAULT_KEYS_SEARCH_LOCAL
1922 * @see #DEFAULT_KEYS_SEARCH_GLOBAL
1923 * @see #onKeyDown
1924 */
1925 public final void setDefaultKeyMode(int mode) {
1926 mDefaultKeyMode = mode;
1927
1928 // Some modes use a SpannableStringBuilder to track & dispatch input events
1929 // This list must remain in sync with the switch in onKeyDown()
1930 switch (mode) {
1931 case DEFAULT_KEYS_DISABLE:
1932 case DEFAULT_KEYS_SHORTCUT:
1933 mDefaultKeySsb = null; // not used in these modes
1934 break;
1935 case DEFAULT_KEYS_DIALER:
1936 case DEFAULT_KEYS_SEARCH_LOCAL:
1937 case DEFAULT_KEYS_SEARCH_GLOBAL:
1938 mDefaultKeySsb = new SpannableStringBuilder();
1939 Selection.setSelection(mDefaultKeySsb,0);
1940 break;
1941 default:
1942 throw new IllegalArgumentException();
1943 }
1944 }
1945
1946 /**
1947 * Called when a key was pressed down and not handled by any of the views
1948 * inside of the activity. So, for example, key presses while the cursor
1949 * is inside a TextView will not trigger the event (unless it is a navigation
1950 * to another object) because TextView handles its own key presses.
1951 *
1952 * <p>If the focused view didn't want this event, this method is called.
1953 *
Dianne Hackborn8d374262009-09-14 21:21:52 -07001954 * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
1955 * by calling {@link #onBackPressed()}, though the behavior varies based
1956 * on the application compatibility mode: for
1957 * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
1958 * it will set up the dispatch to call {@link #onKeyUp} where the action
1959 * will be performed; for earlier applications, it will perform the
1960 * action immediately in on-down, as those versions of the platform
1961 * behaved.
1962 *
1963 * <p>Other additional default key handling may be performed
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001964 * if configured with {@link #setDefaultKeyMode}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 *
1966 * @return Return <code>true</code> to prevent this event from being propagated
1967 * further, or <code>false</code> to indicate that you have not handled
1968 * this event and it should continue to be propagated.
1969 * @see #onKeyUp
1970 * @see android.view.KeyEvent
1971 */
1972 public boolean onKeyDown(int keyCode, KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001973 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07001974 if (getApplicationInfo().targetSdkVersion
1975 >= Build.VERSION_CODES.ECLAIR) {
1976 event.startTracking();
1977 } else {
1978 onBackPressed();
1979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 return true;
1981 }
1982
1983 if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) {
1984 return false;
1985 } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001986 if (getWindow().performPanelShortcut(Window.FEATURE_OPTIONS_PANEL,
1987 keyCode, event, Menu.FLAG_ALWAYS_PERFORM_CLOSE)) {
1988 return true;
1989 }
1990 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 } else {
1992 // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_*
1993 boolean clearSpannable = false;
1994 boolean handled;
1995 if ((event.getRepeatCount() != 0) || event.isSystem()) {
1996 clearSpannable = true;
1997 handled = false;
1998 } else {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001999 handled = TextKeyListener.getInstance().onKeyDown(
2000 null, mDefaultKeySsb, keyCode, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 if (handled && mDefaultKeySsb.length() > 0) {
2002 // something useable has been typed - dispatch it now.
2003
2004 final String str = mDefaultKeySsb.toString();
2005 clearSpannable = true;
2006
2007 switch (mDefaultKeyMode) {
2008 case DEFAULT_KEYS_DIALER:
2009 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
2010 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2011 startActivity(intent);
2012 break;
2013 case DEFAULT_KEYS_SEARCH_LOCAL:
2014 startSearch(str, false, null, false);
2015 break;
2016 case DEFAULT_KEYS_SEARCH_GLOBAL:
2017 startSearch(str, false, null, true);
2018 break;
2019 }
2020 }
2021 }
2022 if (clearSpannable) {
2023 mDefaultKeySsb.clear();
2024 mDefaultKeySsb.clearSpans();
2025 Selection.setSelection(mDefaultKeySsb,0);
2026 }
2027 return handled;
2028 }
2029 }
2030
2031 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002032 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
2033 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
2034 * the event).
2035 */
2036 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2037 return false;
2038 }
2039
2040 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 * Called when a key was released and not handled by any of the views
2042 * inside of the activity. So, for example, key presses while the cursor
2043 * is inside a TextView will not trigger the event (unless it is a navigation
2044 * to another object) because TextView handles its own key presses.
2045 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002046 * <p>The default implementation handles KEYCODE_BACK to stop the activity
2047 * and go back.
2048 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 * @return Return <code>true</code> to prevent this event from being propagated
2050 * further, or <code>false</code> to indicate that you have not handled
2051 * this event and it should continue to be propagated.
2052 * @see #onKeyDown
2053 * @see KeyEvent
2054 */
2055 public boolean onKeyUp(int keyCode, KeyEvent event) {
Dianne Hackborn8d374262009-09-14 21:21:52 -07002056 if (getApplicationInfo().targetSdkVersion
2057 >= Build.VERSION_CODES.ECLAIR) {
2058 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
2059 && !event.isCanceled()) {
2060 onBackPressed();
2061 return true;
2062 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 return false;
2065 }
2066
2067 /**
2068 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
2069 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
2070 * the event).
2071 */
2072 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2073 return false;
2074 }
2075
2076 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002077 * Called when the activity has detected the user's press of the back
2078 * key. The default implementation simply finishes the current activity,
2079 * but you can override this to do whatever you want.
2080 */
2081 public void onBackPressed() {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08002082 if (!mFragments.popBackStackImmediate()) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07002083 finish();
2084 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002085 }
Jeff Brown64da12a2011-01-04 19:57:47 -08002086
2087 /**
2088 * Called when a key shortcut event is not handled by any of the views in the Activity.
2089 * Override this method to implement global key shortcuts for the Activity.
2090 * Key shortcuts can also be implemented by setting the
2091 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
2092 *
2093 * @param keyCode The value in event.getKeyCode().
2094 * @param event Description of the key event.
2095 * @return True if the key shortcut was handled.
2096 */
2097 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2098 return false;
2099 }
2100
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 * Called when a touch screen event was not handled by any of the views
2103 * under it. This is most useful to process touch events that happen
2104 * outside of your window bounds, where there is no view to receive it.
2105 *
2106 * @param event The touch screen event being processed.
2107 *
2108 * @return Return true if you have consumed the event, false if you haven't.
2109 * The default implementation always returns false.
2110 */
2111 public boolean onTouchEvent(MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -08002112 if (mWindow.shouldCloseOnTouch(this, event)) {
2113 finish();
2114 return true;
2115 }
2116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 return false;
2118 }
2119
2120 /**
2121 * Called when the trackball was moved and not handled by any of the
2122 * views inside of the activity. So, for example, if the trackball moves
2123 * while focus is on a button, you will receive a call here because
2124 * buttons do not normally do anything with trackball events. The call
2125 * here happens <em>before</em> trackball movements are converted to
2126 * DPAD key events, which then get sent back to the view hierarchy, and
2127 * will be processed at the point for things like focus navigation.
2128 *
2129 * @param event The trackball event being processed.
2130 *
2131 * @return Return true if you have consumed the event, false if you haven't.
2132 * The default implementation always returns false.
2133 */
2134 public boolean onTrackballEvent(MotionEvent event) {
2135 return false;
2136 }
Jeff Browncb1404e2011-01-15 18:14:15 -08002137
2138 /**
2139 * Called when a generic motion event was not handled by any of the
2140 * views inside of the activity.
2141 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08002142 * Generic motion events describe joystick movements, mouse hovers, track pad
2143 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08002144 * {@link MotionEvent#getSource() source} of the motion event specifies
2145 * the class of input that was received. Implementations of this method
2146 * must examine the bits in the source before processing the event.
2147 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08002148 * </p><p>
2149 * Generic motion events with source class
2150 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
2151 * are delivered to the view under the pointer. All other generic motion events are
2152 * delivered to the focused view.
2153 * </p><p>
2154 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
2155 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -08002156 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08002157 *
2158 * @param event The generic motion event being processed.
2159 *
2160 * @return Return true if you have consumed the event, false if you haven't.
2161 * The default implementation always returns false.
2162 */
2163 public boolean onGenericMotionEvent(MotionEvent event) {
2164 return false;
2165 }
2166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 /**
2168 * Called whenever a key, touch, or trackball event is dispatched to the
2169 * activity. Implement this method if you wish to know that the user has
2170 * interacted with the device in some way while your activity is running.
2171 * This callback and {@link #onUserLeaveHint} are intended to help
2172 * activities manage status bar notifications intelligently; specifically,
2173 * for helping activities determine the proper time to cancel a notfication.
2174 *
2175 * <p>All calls to your activity's {@link #onUserLeaveHint} callback will
2176 * be accompanied by calls to {@link #onUserInteraction}. This
2177 * ensures that your activity will be told of relevant user activity such
2178 * as pulling down the notification pane and touching an item there.
2179 *
2180 * <p>Note that this callback will be invoked for the touch down action
2181 * that begins a touch gesture, but may not be invoked for the touch-moved
2182 * and touch-up actions that follow.
2183 *
2184 * @see #onUserLeaveHint()
2185 */
2186 public void onUserInteraction() {
2187 }
2188
2189 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
2190 // Update window manager if: we have a view, that view is
2191 // attached to its parent (which will be a RootView), and
2192 // this activity is not embedded.
2193 if (mParent == null) {
2194 View decor = mDecor;
2195 if (decor != null && decor.getParent() != null) {
2196 getWindowManager().updateViewLayout(decor, params);
2197 }
2198 }
2199 }
2200
2201 public void onContentChanged() {
2202 }
2203
2204 /**
2205 * Called when the current {@link Window} of the activity gains or loses
2206 * focus. This is the best indicator of whether this activity is visible
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002207 * to the user. The default implementation clears the key tracking
2208 * state, so should always be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002210 * <p>Note that this provides information about global focus state, which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 * is managed independently of activity lifecycles. As such, while focus
2212 * changes will generally have some relation to lifecycle changes (an
2213 * activity that is stopped will not generally get window focus), you
2214 * should not rely on any particular order between the callbacks here and
2215 * those in the other lifecycle methods such as {@link #onResume}.
2216 *
2217 * <p>As a general rule, however, a resumed activity will have window
2218 * focus... unless it has displayed other dialogs or popups that take
2219 * input focus, in which case the activity itself will not have focus
2220 * when the other windows have it. Likewise, the system may display
2221 * system-level windows (such as the status bar notification panel or
2222 * a system alert) which will temporarily take window input focus without
2223 * pausing the foreground activity.
2224 *
2225 * @param hasFocus Whether the window of this activity has focus.
2226 *
2227 * @see #hasWindowFocus()
2228 * @see #onResume
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002229 * @see View#onWindowFocusChanged(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 */
2231 public void onWindowFocusChanged(boolean hasFocus) {
2232 }
2233
2234 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -07002235 * Called when the main window associated with the activity has been
2236 * attached to the window manager.
2237 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
2238 * for more information.
2239 * @see View#onAttachedToWindow
2240 */
2241 public void onAttachedToWindow() {
2242 }
2243
2244 /**
2245 * Called when the main window associated with the activity has been
2246 * detached from the window manager.
2247 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()}
2248 * for more information.
2249 * @see View#onDetachedFromWindow
2250 */
2251 public void onDetachedFromWindow() {
2252 }
2253
2254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 * Returns true if this activity's <em>main</em> window currently has window focus.
2256 * Note that this is not the same as the view itself having focus.
2257 *
2258 * @return True if this activity's main window currently has window focus.
2259 *
2260 * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams)
2261 */
2262 public boolean hasWindowFocus() {
2263 Window w = getWindow();
2264 if (w != null) {
2265 View d = w.getDecorView();
2266 if (d != null) {
2267 return d.hasWindowFocus();
2268 }
2269 }
2270 return false;
2271 }
2272
2273 /**
2274 * Called to process key events. You can override this to intercept all
2275 * key events before they are dispatched to the window. Be sure to call
2276 * this implementation for key events that should be handled normally.
2277 *
2278 * @param event The key event.
2279 *
2280 * @return boolean Return true if this event was consumed.
2281 */
2282 public boolean dispatchKeyEvent(KeyEvent event) {
2283 onUserInteraction();
Dianne Hackborn8d374262009-09-14 21:21:52 -07002284 Window win = getWindow();
2285 if (win.superDispatchKeyEvent(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 return true;
2287 }
Dianne Hackborn8d374262009-09-14 21:21:52 -07002288 View decor = mDecor;
2289 if (decor == null) decor = win.getDecorView();
2290 return event.dispatch(this, decor != null
2291 ? decor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 }
2293
2294 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08002295 * Called to process a key shortcut event.
2296 * You can override this to intercept all key shortcut events before they are
2297 * dispatched to the window. Be sure to call this implementation for key shortcut
2298 * events that should be handled normally.
2299 *
2300 * @param event The key shortcut event.
2301 * @return True if this event was consumed.
2302 */
2303 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
2304 onUserInteraction();
2305 if (getWindow().superDispatchKeyShortcutEvent(event)) {
2306 return true;
2307 }
2308 return onKeyShortcut(event.getKeyCode(), event);
2309 }
2310
2311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 * Called to process touch screen events. You can override this to
2313 * intercept all touch screen events before they are dispatched to the
2314 * window. Be sure to call this implementation for touch screen events
2315 * that should be handled normally.
2316 *
2317 * @param ev The touch screen event.
2318 *
2319 * @return boolean Return true if this event was consumed.
2320 */
2321 public boolean dispatchTouchEvent(MotionEvent ev) {
2322 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
2323 onUserInteraction();
2324 }
2325 if (getWindow().superDispatchTouchEvent(ev)) {
2326 return true;
2327 }
2328 return onTouchEvent(ev);
2329 }
2330
2331 /**
2332 * Called to process trackball events. You can override this to
2333 * intercept all trackball events before they are dispatched to the
2334 * window. Be sure to call this implementation for trackball events
2335 * that should be handled normally.
2336 *
2337 * @param ev The trackball event.
2338 *
2339 * @return boolean Return true if this event was consumed.
2340 */
2341 public boolean dispatchTrackballEvent(MotionEvent ev) {
2342 onUserInteraction();
2343 if (getWindow().superDispatchTrackballEvent(ev)) {
2344 return true;
2345 }
2346 return onTrackballEvent(ev);
2347 }
svetoslavganov75986cf2009-05-14 22:28:01 -07002348
Jeff Browncb1404e2011-01-15 18:14:15 -08002349 /**
2350 * Called to process generic motion events. You can override this to
2351 * intercept all generic motion events before they are dispatched to the
2352 * window. Be sure to call this implementation for generic motion events
2353 * that should be handled normally.
2354 *
2355 * @param ev The generic motion event.
2356 *
2357 * @return boolean Return true if this event was consumed.
2358 */
2359 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
2360 onUserInteraction();
2361 if (getWindow().superDispatchGenericMotionEvent(ev)) {
2362 return true;
2363 }
2364 return onGenericMotionEvent(ev);
2365 }
2366
svetoslavganov75986cf2009-05-14 22:28:01 -07002367 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2368 event.setClassName(getClass().getName());
2369 event.setPackageName(getPackageName());
2370
2371 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -08002372 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
2373 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -07002374 event.setFullScreen(isFullScreen);
2375
2376 CharSequence title = getTitle();
2377 if (!TextUtils.isEmpty(title)) {
2378 event.getText().add(title);
2379 }
2380
2381 return true;
2382 }
2383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 /**
2385 * Default implementation of
2386 * {@link android.view.Window.Callback#onCreatePanelView}
2387 * for activities. This
2388 * simply returns null so that all panel sub-windows will have the default
2389 * menu behavior.
2390 */
2391 public View onCreatePanelView(int featureId) {
2392 return null;
2393 }
2394
2395 /**
2396 * Default implementation of
2397 * {@link android.view.Window.Callback#onCreatePanelMenu}
2398 * for activities. This calls through to the new
2399 * {@link #onCreateOptionsMenu} method for the
2400 * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
2401 * so that subclasses of Activity don't need to deal with feature codes.
2402 */
2403 public boolean onCreatePanelMenu(int featureId, Menu menu) {
2404 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002405 boolean show = onCreateOptionsMenu(menu);
2406 show |= mFragments.dispatchCreateOptionsMenu(menu, getMenuInflater());
2407 return show;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 }
2409 return false;
2410 }
2411
2412 /**
2413 * Default implementation of
2414 * {@link android.view.Window.Callback#onPreparePanel}
2415 * for activities. This
2416 * calls through to the new {@link #onPrepareOptionsMenu} method for the
2417 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
2418 * panel, so that subclasses of
2419 * Activity don't need to deal with feature codes.
2420 */
2421 public boolean onPreparePanel(int featureId, View view, Menu menu) {
2422 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
2423 boolean goforit = onPrepareOptionsMenu(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002424 goforit |= mFragments.dispatchPrepareOptionsMenu(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 return goforit && menu.hasVisibleItems();
2426 }
2427 return true;
2428 }
2429
2430 /**
2431 * {@inheritDoc}
2432 *
2433 * @return The default implementation returns true.
2434 */
2435 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -08002436 if (featureId == Window.FEATURE_ACTION_BAR) {
Adam Powell763cbb12011-03-01 10:45:34 -08002437 initActionBar();
Adam Powell049dd3d2010-12-02 13:43:59 -08002438 if (mActionBar != null) {
2439 mActionBar.dispatchMenuVisibilityChanged(true);
2440 } else {
2441 Log.e(TAG, "Tried to open action bar menu with no action bar");
2442 }
Adam Powell8515ee82010-11-30 14:09:55 -08002443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 return true;
2445 }
2446
2447 /**
2448 * Default implementation of
2449 * {@link android.view.Window.Callback#onMenuItemSelected}
2450 * for activities. This calls through to the new
2451 * {@link #onOptionsItemSelected} method for the
2452 * {@link android.view.Window#FEATURE_OPTIONS_PANEL}
2453 * panel, so that subclasses of
2454 * Activity don't need to deal with feature codes.
2455 */
2456 public boolean onMenuItemSelected(int featureId, MenuItem item) {
2457 switch (featureId) {
2458 case Window.FEATURE_OPTIONS_PANEL:
2459 // Put event logging here so it gets called even if subclass
2460 // doesn't call through to superclass's implmeentation of each
2461 // of these methods below
2462 EventLog.writeEvent(50000, 0, item.getTitleCondensed());
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002463 if (onOptionsItemSelected(item)) {
2464 return true;
2465 }
2466 return mFragments.dispatchOptionsItemSelected(item);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467
2468 case Window.FEATURE_CONTEXT_MENU:
2469 EventLog.writeEvent(50000, 1, item.getTitleCondensed());
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07002470 if (onContextItemSelected(item)) {
2471 return true;
2472 }
2473 return mFragments.dispatchContextItemSelected(item);
Adam Powell8515ee82010-11-30 14:09:55 -08002474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 default:
2476 return false;
2477 }
2478 }
2479
2480 /**
2481 * Default implementation of
2482 * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for
2483 * activities. This calls through to {@link #onOptionsMenuClosed(Menu)}
2484 * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel,
2485 * so that subclasses of Activity don't need to deal with feature codes.
2486 * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the
2487 * {@link #onContextMenuClosed(Menu)} will be called.
2488 */
2489 public void onPanelClosed(int featureId, Menu menu) {
2490 switch (featureId) {
2491 case Window.FEATURE_OPTIONS_PANEL:
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002492 mFragments.dispatchOptionsMenuClosed(menu);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 onOptionsMenuClosed(menu);
2494 break;
2495
2496 case Window.FEATURE_CONTEXT_MENU:
2497 onContextMenuClosed(menu);
2498 break;
Adam Powell8515ee82010-11-30 14:09:55 -08002499
2500 case Window.FEATURE_ACTION_BAR:
Adam Powell9b4bee02011-04-27 19:24:47 -07002501 initActionBar();
Adam Powell8515ee82010-11-30 14:09:55 -08002502 mActionBar.dispatchMenuVisibilityChanged(false);
2503 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 }
2505 }
2506
2507 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002508 * Declare that the options menu has changed, so should be recreated.
2509 * The {@link #onCreateOptionsMenu(Menu)} method will be called the next
2510 * time it needs to be displayed.
2511 */
2512 public void invalidateOptionsMenu() {
2513 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
2514 }
2515
2516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 * Initialize the contents of the Activity's standard options menu. You
2518 * should place your menu items in to <var>menu</var>.
2519 *
2520 * <p>This is only called once, the first time the options menu is
2521 * displayed. To update the menu every time it is displayed, see
2522 * {@link #onPrepareOptionsMenu}.
2523 *
2524 * <p>The default implementation populates the menu with standard system
2525 * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
2526 * they will be correctly ordered with application-defined menu items.
2527 * Deriving classes should always call through to the base implementation.
2528 *
2529 * <p>You can safely hold on to <var>menu</var> (and any items created
2530 * from it), making modifications to it as desired, until the next
2531 * time onCreateOptionsMenu() is called.
2532 *
2533 * <p>When you add items to the menu, you can implement the Activity's
2534 * {@link #onOptionsItemSelected} method to handle them there.
2535 *
2536 * @param menu The options menu in which you place your items.
2537 *
2538 * @return You must return true for the menu to be displayed;
2539 * if you return false it will not be shown.
2540 *
2541 * @see #onPrepareOptionsMenu
2542 * @see #onOptionsItemSelected
2543 */
2544 public boolean onCreateOptionsMenu(Menu menu) {
2545 if (mParent != null) {
2546 return mParent.onCreateOptionsMenu(menu);
2547 }
2548 return true;
2549 }
2550
2551 /**
2552 * Prepare the Screen's standard options menu to be displayed. This is
2553 * called right before the menu is shown, every time it is shown. You can
2554 * use this method to efficiently enable/disable items or otherwise
2555 * dynamically modify the contents.
2556 *
2557 * <p>The default implementation updates the system menu items based on the
2558 * activity's state. Deriving classes should always call through to the
2559 * base class implementation.
2560 *
2561 * @param menu The options menu as last shown or first initialized by
2562 * onCreateOptionsMenu().
2563 *
2564 * @return You must return true for the menu to be displayed;
2565 * if you return false it will not be shown.
2566 *
2567 * @see #onCreateOptionsMenu
2568 */
2569 public boolean onPrepareOptionsMenu(Menu menu) {
2570 if (mParent != null) {
2571 return mParent.onPrepareOptionsMenu(menu);
2572 }
2573 return true;
2574 }
2575
2576 /**
2577 * This hook is called whenever an item in your options menu is selected.
2578 * The default implementation simply returns false to have the normal
2579 * processing happen (calling the item's Runnable or sending a message to
2580 * its Handler as appropriate). You can use this method for any items
2581 * for which you would like to do processing without those other
2582 * facilities.
2583 *
2584 * <p>Derived classes should call through to the base class for it to
2585 * perform the default menu handling.
2586 *
2587 * @param item The menu item that was selected.
2588 *
2589 * @return boolean Return false to allow normal menu processing to
2590 * proceed, true to consume it here.
2591 *
2592 * @see #onCreateOptionsMenu
2593 */
2594 public boolean onOptionsItemSelected(MenuItem item) {
2595 if (mParent != null) {
2596 return mParent.onOptionsItemSelected(item);
2597 }
2598 return false;
2599 }
2600
2601 /**
2602 * This hook is called whenever the options menu is being closed (either by the user canceling
2603 * the menu with the back/menu button, or when an item is selected).
2604 *
2605 * @param menu The options menu as last shown or first initialized by
2606 * onCreateOptionsMenu().
2607 */
2608 public void onOptionsMenuClosed(Menu menu) {
2609 if (mParent != null) {
2610 mParent.onOptionsMenuClosed(menu);
2611 }
2612 }
2613
2614 /**
2615 * Programmatically opens the options menu. If the options menu is already
2616 * open, this method does nothing.
2617 */
2618 public void openOptionsMenu() {
2619 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
2620 }
2621
2622 /**
2623 * Progammatically closes the options menu. If the options menu is already
2624 * closed, this method does nothing.
2625 */
2626 public void closeOptionsMenu() {
2627 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
2628 }
2629
2630 /**
2631 * Called when a context menu for the {@code view} is about to be shown.
2632 * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every
2633 * time the context menu is about to be shown and should be populated for
2634 * the view (or item inside the view for {@link AdapterView} subclasses,
2635 * this can be found in the {@code menuInfo})).
2636 * <p>
2637 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
2638 * item has been selected.
2639 * <p>
2640 * It is not safe to hold onto the context menu after this method returns.
2641 * {@inheritDoc}
2642 */
2643 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
2644 }
2645
2646 /**
2647 * Registers a context menu to be shown for the given view (multiple views
2648 * can show the context menu). This method will set the
2649 * {@link OnCreateContextMenuListener} on the view to this activity, so
2650 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
2651 * called when it is time to show the context menu.
2652 *
2653 * @see #unregisterForContextMenu(View)
2654 * @param view The view that should show a context menu.
2655 */
2656 public void registerForContextMenu(View view) {
2657 view.setOnCreateContextMenuListener(this);
2658 }
2659
2660 /**
2661 * Prevents a context menu to be shown for the given view. This method will remove the
2662 * {@link OnCreateContextMenuListener} on the view.
2663 *
2664 * @see #registerForContextMenu(View)
2665 * @param view The view that should stop showing a context menu.
2666 */
2667 public void unregisterForContextMenu(View view) {
2668 view.setOnCreateContextMenuListener(null);
2669 }
2670
2671 /**
2672 * Programmatically opens the context menu for a particular {@code view}.
2673 * The {@code view} should have been added via
2674 * {@link #registerForContextMenu(View)}.
2675 *
2676 * @param view The view to show the context menu for.
2677 */
2678 public void openContextMenu(View view) {
2679 view.showContextMenu();
2680 }
2681
2682 /**
2683 * Programmatically closes the most recently opened context menu, if showing.
2684 */
2685 public void closeContextMenu() {
2686 mWindow.closePanel(Window.FEATURE_CONTEXT_MENU);
2687 }
2688
2689 /**
2690 * This hook is called whenever an item in a context menu is selected. The
2691 * default implementation simply returns false to have the normal processing
2692 * happen (calling the item's Runnable or sending a message to its Handler
2693 * as appropriate). You can use this method for any items for which you
2694 * would like to do processing without those other facilities.
2695 * <p>
2696 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
2697 * View that added this menu item.
2698 * <p>
2699 * Derived classes should call through to the base class for it to perform
2700 * the default menu handling.
2701 *
2702 * @param item The context menu item that was selected.
2703 * @return boolean Return false to allow normal context menu processing to
2704 * proceed, true to consume it here.
2705 */
2706 public boolean onContextItemSelected(MenuItem item) {
2707 if (mParent != null) {
2708 return mParent.onContextItemSelected(item);
2709 }
2710 return false;
2711 }
2712
2713 /**
2714 * This hook is called whenever the context menu is being closed (either by
2715 * the user canceling the menu with the back/menu button, or when an item is
2716 * selected).
2717 *
2718 * @param menu The context menu that is being closed.
2719 */
2720 public void onContextMenuClosed(Menu menu) {
2721 if (mParent != null) {
2722 mParent.onContextMenuClosed(menu);
2723 }
2724 }
2725
2726 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002727 * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002729 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 protected Dialog onCreateDialog(int id) {
2731 return null;
2732 }
2733
2734 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -07002735 * @deprecated Use the new {@link DialogFragment} class with
2736 * {@link FragmentManager} instead; this is also
2737 * available on older platforms through the Android compatibility package.
2738 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002739 * Callback for creating dialogs that are managed (saved and restored) for you
2740 * by the activity. The default implementation calls through to
2741 * {@link #onCreateDialog(int)} for compatibility.
2742 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002743 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2744 * or later, consider instead using a {@link DialogFragment} instead.</em>
2745 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002746 * <p>If you use {@link #showDialog(int)}, the activity will call through to
2747 * this method the first time, and hang onto it thereafter. Any dialog
2748 * that is created by this method will automatically be saved and restored
2749 * for you, including whether it is showing.
2750 *
2751 * <p>If you would like the activity to manage saving and restoring dialogs
2752 * for you, you should override this method and handle any ids that are
2753 * passed to {@link #showDialog}.
2754 *
2755 * <p>If you would like an opportunity to prepare your dialog before it is shown,
2756 * override {@link #onPrepareDialog(int, Dialog, Bundle)}.
2757 *
2758 * @param id The id of the dialog.
2759 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
2760 * @return The dialog. If you return null, the dialog will not be created.
2761 *
2762 * @see #onPrepareDialog(int, Dialog, Bundle)
2763 * @see #showDialog(int, Bundle)
2764 * @see #dismissDialog(int)
2765 * @see #removeDialog(int)
2766 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07002767 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002768 protected Dialog onCreateDialog(int id, Bundle args) {
2769 return onCreateDialog(id);
2770 }
2771
2772 /**
2773 * @deprecated Old no-arguments version of
2774 * {@link #onPrepareDialog(int, Dialog, Bundle)}.
2775 */
2776 @Deprecated
2777 protected void onPrepareDialog(int id, Dialog dialog) {
2778 dialog.setOwnerActivity(this);
2779 }
2780
2781 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -07002782 * @deprecated Use the new {@link DialogFragment} class with
2783 * {@link FragmentManager} instead; this is also
2784 * available on older platforms through the Android compatibility package.
2785 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 * Provides an opportunity to prepare a managed dialog before it is being
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002787 * shown. The default implementation calls through to
2788 * {@link #onPrepareDialog(int, Dialog)} for compatibility.
2789 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 * <p>
2791 * Override this if you need to update a managed dialog based on the state
2792 * of the application each time it is shown. For example, a time picker
2793 * dialog might want to be updated with the current time. You should call
2794 * through to the superclass's implementation. The default implementation
2795 * will set this Activity as the owner activity on the Dialog.
2796 *
2797 * @param id The id of the managed dialog.
2798 * @param dialog The dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002799 * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}.
2800 * @see #onCreateDialog(int, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 * @see #showDialog(int)
2802 * @see #dismissDialog(int)
2803 * @see #removeDialog(int)
2804 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07002805 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002806 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
2807 onPrepareDialog(id, dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 }
2809
2810 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -07002811 * @deprecated Use the new {@link DialogFragment} class with
2812 * {@link FragmentManager} instead; this is also
2813 * available on older platforms through the Android compatibility package.
2814 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002815 * Simple version of {@link #showDialog(int, Bundle)} that does not
2816 * take any arguments. Simply calls {@link #showDialog(int, Bundle)}
2817 * with null arguments.
2818 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07002819 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002820 public final void showDialog(int id) {
2821 showDialog(id, null);
2822 }
2823
2824 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -07002825 * @deprecated Use the new {@link DialogFragment} class with
2826 * {@link FragmentManager} instead; this is also
2827 * available on older platforms through the Android compatibility package.
2828 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002829 * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 * will be made with the same id the first time this is called for a given
2831 * id. From thereafter, the dialog will be automatically saved and restored.
2832 *
Dianne Hackborn291905e2010-08-17 15:17:15 -07002833 * <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB}
2834 * or later, consider instead using a {@link DialogFragment} instead.</em>
2835 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002836 * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 * be made to provide an opportunity to do any timely preparation.
2838 *
2839 * @param id The id of the managed dialog.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002840 * @param args Arguments to pass through to the dialog. These will be saved
2841 * and restored for you. Note that if the dialog is already created,
2842 * {@link #onCreateDialog(int, Bundle)} will not be called with the new
2843 * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08002844 * If you need to rebuild the dialog, call {@link #removeDialog(int)} first.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002845 * @return Returns true if the Dialog was created; false is returned if
2846 * it is not created because {@link #onCreateDialog(int, Bundle)} returns false.
2847 *
Joe Onorato37296dc2009-07-31 17:58:55 -07002848 * @see Dialog
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002849 * @see #onCreateDialog(int, Bundle)
2850 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 * @see #dismissDialog(int)
2852 * @see #removeDialog(int)
2853 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07002854 @Deprecated
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002855 public final boolean showDialog(int id, Bundle args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 if (mManagedDialogs == null) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002857 mManagedDialogs = new SparseArray<ManagedDialog>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002859 ManagedDialog md = mManagedDialogs.get(id);
2860 if (md == null) {
2861 md = new ManagedDialog();
2862 md.mDialog = createDialog(id, null, args);
2863 if (md.mDialog == null) {
2864 return false;
2865 }
2866 mManagedDialogs.put(id, md);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 }
2868
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002869 md.mArgs = args;
2870 onPrepareDialog(id, md.mDialog, args);
2871 md.mDialog.show();
2872 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 }
2874
2875 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -07002876 * @deprecated Use the new {@link DialogFragment} class with
2877 * {@link FragmentManager} instead; this is also
2878 * available on older platforms through the Android compatibility package.
2879 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 * Dismiss a dialog that was previously shown via {@link #showDialog(int)}.
2881 *
2882 * @param id The id of the managed dialog.
2883 *
2884 * @throws IllegalArgumentException if the id was not previously shown via
2885 * {@link #showDialog(int)}.
2886 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002887 * @see #onCreateDialog(int, Bundle)
2888 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 * @see #showDialog(int)
2890 * @see #removeDialog(int)
2891 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07002892 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 public final void dismissDialog(int id) {
2894 if (mManagedDialogs == null) {
2895 throw missingDialog(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002897
2898 final ManagedDialog md = mManagedDialogs.get(id);
2899 if (md == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 throw missingDialog(id);
2901 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002902 md.mDialog.dismiss();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 }
2904
2905 /**
2906 * Creates an exception to throw if a user passed in a dialog id that is
2907 * unexpected.
2908 */
2909 private IllegalArgumentException missingDialog(int id) {
2910 return new IllegalArgumentException("no dialog with id " + id + " was ever "
2911 + "shown via Activity#showDialog");
2912 }
2913
2914 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -07002915 * @deprecated Use the new {@link DialogFragment} class with
2916 * {@link FragmentManager} instead; this is also
2917 * available on older platforms through the Android compatibility package.
2918 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 * Removes any internal references to a dialog managed by this Activity.
2920 * If the dialog is showing, it will dismiss it as part of the clean up.
2921 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002922 * <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 -08002923 * want to avoid the overhead of saving and restoring it in the future.
2924 *
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07002925 * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, this function
2926 * will not throw an exception if you try to remove an ID that does not
2927 * currently have an associated dialog.</p>
2928 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 * @param id The id of the managed dialog.
2930 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08002931 * @see #onCreateDialog(int, Bundle)
2932 * @see #onPrepareDialog(int, Dialog, Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 * @see #showDialog(int)
2934 * @see #dismissDialog(int)
2935 */
Dianne Hackborn9567a662011-04-19 18:44:03 -07002936 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 public final void removeDialog(int id) {
Dianne Hackbornd2ce8bbb2010-10-06 16:46:05 -07002938 if (mManagedDialogs != null) {
2939 final ManagedDialog md = mManagedDialogs.get(id);
2940 if (md != null) {
2941 md.mDialog.dismiss();
2942 mManagedDialogs.remove(id);
2943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 }
2946
2947 /**
2948 * This hook is called when the user signals the desire to start a search.
2949 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01002950 * <p>You can use this function as a simple way to launch the search UI, in response to a
2951 * menu item, search button, or other widgets within your activity. Unless overidden,
2952 * calling this function is the same as calling
2953 * {@link #startSearch startSearch(null, false, null, false)}, which launches
2954 * search for the current activity as specified in its manifest, see {@link SearchManager}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 *
2956 * <p>You can override this function to force global search, e.g. in response to a dedicated
2957 * search key, or to block search entirely (by simply returning false).
2958 *
Bjorn Bringert6266e402009-09-25 14:25:41 +01002959 * @return Returns {@code true} if search launched, and {@code false} if activity blocks it.
2960 * The default implementation always returns {@code true}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 *
2962 * @see android.app.SearchManager
2963 */
2964 public boolean onSearchRequested() {
2965 startSearch(null, false, null, false);
2966 return true;
2967 }
2968
2969 /**
2970 * This hook is called to launch the search UI.
2971 *
2972 * <p>It is typically called from onSearchRequested(), either directly from
2973 * Activity.onSearchRequested() or from an overridden version in any given
2974 * Activity. If your goal is simply to activate search, it is preferred to call
2975 * onSearchRequested(), which may have been overriden elsewhere in your Activity. If your goal
2976 * is to inject specific data such as context data, it is preferred to <i>override</i>
2977 * onSearchRequested(), so that any callers to it will benefit from the override.
2978 *
2979 * @param initialQuery Any non-null non-empty string will be inserted as
2980 * pre-entered text in the search query box.
2981 * @param selectInitialQuery If true, the intial query will be preselected, which means that
2982 * any further typing will replace it. This is useful for cases where an entire pre-formed
2983 * query is being inserted. If false, the selection point will be placed at the end of the
2984 * inserted query. This is useful when the inserted query is text that the user entered,
2985 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
2986 * if initialQuery is a non-empty string.</i>
2987 * @param appSearchData An application can insert application-specific
2988 * context here, in order to improve quality or specificity of its own
2989 * searches. This data will be returned with SEARCH intent(s). Null if
2990 * no extra data is required.
2991 * @param globalSearch If false, this will only launch the search that has been specifically
2992 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -07002993 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 * If true, this will always launch a platform-global (e.g. web-based) search instead.
2995 *
2996 * @see android.app.SearchManager
2997 * @see #onSearchRequested
2998 */
2999 public void startSearch(String initialQuery, boolean selectInitialQuery,
3000 Bundle appSearchData, boolean globalSearch) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003001 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01003002 mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 appSearchData, globalSearch);
3004 }
3005
3006 /**
krosaend2d60142009-08-17 08:56:48 -07003007 * Similar to {@link #startSearch}, but actually fires off the search query after invoking
3008 * the search dialog. Made available for testing purposes.
3009 *
3010 * @param query The query to trigger. If empty, the request will be ignored.
3011 * @param appSearchData An application can insert application-specific
3012 * context here, in order to improve quality or specificity of its own
3013 * searches. This data will be returned with SEARCH intent(s). Null if
3014 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -07003015 */
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01003016 public void triggerSearch(String query, Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -07003017 ensureSearchManager();
Bjorn Bringertb782a2f2009-10-01 09:57:33 +01003018 mSearchManager.triggerSearch(query, getComponentName(), appSearchData);
krosaend2d60142009-08-17 08:56:48 -07003019 }
3020
3021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 * Request that key events come to this activity. Use this if your
3023 * activity has no views with focus, but the activity still wants
3024 * a chance to process key events.
3025 *
3026 * @see android.view.Window#takeKeyEvents
3027 */
3028 public void takeKeyEvents(boolean get) {
3029 getWindow().takeKeyEvents(get);
3030 }
3031
3032 /**
3033 * Enable extended window features. This is a convenience for calling
3034 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
3035 *
3036 * @param featureId The desired feature as defined in
3037 * {@link android.view.Window}.
3038 * @return Returns true if the requested feature is supported and now
3039 * enabled.
3040 *
3041 * @see android.view.Window#requestFeature
3042 */
3043 public final boolean requestWindowFeature(int featureId) {
3044 return getWindow().requestFeature(featureId);
3045 }
3046
3047 /**
3048 * Convenience for calling
3049 * {@link android.view.Window#setFeatureDrawableResource}.
3050 */
3051 public final void setFeatureDrawableResource(int featureId, int resId) {
3052 getWindow().setFeatureDrawableResource(featureId, resId);
3053 }
3054
3055 /**
3056 * Convenience for calling
3057 * {@link android.view.Window#setFeatureDrawableUri}.
3058 */
3059 public final void setFeatureDrawableUri(int featureId, Uri uri) {
3060 getWindow().setFeatureDrawableUri(featureId, uri);
3061 }
3062
3063 /**
3064 * Convenience for calling
3065 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
3066 */
3067 public final void setFeatureDrawable(int featureId, Drawable drawable) {
3068 getWindow().setFeatureDrawable(featureId, drawable);
3069 }
3070
3071 /**
3072 * Convenience for calling
3073 * {@link android.view.Window#setFeatureDrawableAlpha}.
3074 */
3075 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
3076 getWindow().setFeatureDrawableAlpha(featureId, alpha);
3077 }
3078
3079 /**
3080 * Convenience for calling
3081 * {@link android.view.Window#getLayoutInflater}.
3082 */
3083 public LayoutInflater getLayoutInflater() {
3084 return getWindow().getLayoutInflater();
3085 }
3086
3087 /**
3088 * Returns a {@link MenuInflater} with this context.
3089 */
3090 public MenuInflater getMenuInflater() {
Adam Powell88ab6972011-07-28 11:25:01 -07003091 // Make sure that action views can get an appropriate theme.
3092 if (mMenuInflater == null) {
3093 initActionBar();
3094 if (mActionBar != null) {
3095 mMenuInflater = new MenuInflater(mActionBar.getThemedContext());
3096 } else {
3097 mMenuInflater = new MenuInflater(this);
3098 }
3099 }
3100 return mMenuInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 }
3102
3103 @Override
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003104 protected void onApplyThemeResource(Resources.Theme theme, int resid,
3105 boolean first) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 if (mParent == null) {
3107 super.onApplyThemeResource(theme, resid, first);
3108 } else {
3109 try {
3110 theme.setTo(mParent.getTheme());
3111 } catch (Exception e) {
3112 // Empty
3113 }
3114 theme.applyStyle(resid, false);
3115 }
3116 }
3117
3118 /**
3119 * Launch an activity for which you would like a result when it finished.
3120 * When this activity exits, your
3121 * onActivityResult() method will be called with the given requestCode.
3122 * Using a negative requestCode is the same as calling
3123 * {@link #startActivity} (the activity is not launched as a sub-activity).
3124 *
3125 * <p>Note that this method should only be used with Intent protocols
3126 * that are defined to return a result. In other protocols (such as
3127 * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
3128 * not get the result when you expect. For example, if the activity you
3129 * are launching uses the singleTask launch mode, it will not run in your
3130 * task and thus you will immediately receive a cancel result.
3131 *
3132 * <p>As a special case, if you call startActivityForResult() with a requestCode
3133 * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
3134 * activity, then your window will not be displayed until a result is
3135 * returned back from the started activity. This is to avoid visible
3136 * flickering when redirecting to another activity.
3137 *
3138 * <p>This method throws {@link android.content.ActivityNotFoundException}
3139 * if there was no Activity found to run the given Intent.
3140 *
3141 * @param intent The intent to start.
3142 * @param requestCode If >= 0, this code will be returned in
3143 * onActivityResult() when the activity exits.
3144 *
3145 * @throws android.content.ActivityNotFoundException
3146 *
3147 * @see #startActivity
3148 */
3149 public void startActivityForResult(Intent intent, int requestCode) {
3150 if (mParent == null) {
3151 Instrumentation.ActivityResult ar =
3152 mInstrumentation.execStartActivity(
3153 this, mMainThread.getApplicationThread(), mToken, this,
3154 intent, requestCode);
3155 if (ar != null) {
3156 mMainThread.sendActivityResult(
3157 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
3158 ar.getResultData());
3159 }
3160 if (requestCode >= 0) {
3161 // If this start is requesting a result, we can avoid making
3162 // the activity visible until the result is received. Setting
3163 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
3164 // activity hidden during this time, to avoid flickering.
3165 // This can only be done when a result is requested because
3166 // that guarantees we will get information back when the
3167 // activity is finished, no matter what happens to it.
3168 mStartedActivity = true;
3169 }
3170 } else {
3171 mParent.startActivityFromChild(this, intent, requestCode);
3172 }
3173 }
3174
3175 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003176 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003177 * to use a IntentSender to describe the activity to be started. If
3178 * the IntentSender is for an activity, that activity will be started
3179 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
3180 * here; otherwise, its associated action will be executed (such as
3181 * sending a broadcast) as if you had called
3182 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003183 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003184 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003185 * @param requestCode If >= 0, this code will be returned in
3186 * onActivityResult() when the activity exits.
3187 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003188 * intent parameter to {@link IntentSender#sendIntent}.
3189 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003190 * would like to change.
3191 * @param flagsValues Desired values for any bits set in
3192 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003193 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003194 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003195 public void startIntentSenderForResult(IntentSender intent, int requestCode,
3196 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
3197 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003198 if (mParent == null) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003199 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003200 flagsMask, flagsValues, this);
3201 } else {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003202 mParent.startIntentSenderFromChild(this, intent, requestCode,
3203 fillInIntent, flagsMask, flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003204 }
3205 }
3206
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003207 private void startIntentSenderForResultInner(IntentSender intent, int requestCode,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003208 Intent fillInIntent, int flagsMask, int flagsValues, Activity activity)
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003209 throws IntentSender.SendIntentException {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003210 try {
3211 String resolvedType = null;
3212 if (fillInIntent != null) {
3213 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
3214 }
3215 int result = ActivityManagerNative.getDefault()
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003216 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003217 fillInIntent, resolvedType, mToken, activity.mEmbeddedID,
3218 requestCode, flagsMask, flagsValues);
3219 if (result == IActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003220 throw new IntentSender.SendIntentException();
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003221 }
3222 Instrumentation.checkStartActivityResult(result, null);
3223 } catch (RemoteException e) {
3224 }
3225 if (requestCode >= 0) {
3226 // If this start is requesting a result, we can avoid making
3227 // the activity visible until the result is received. Setting
3228 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
3229 // activity hidden during this time, to avoid flickering.
3230 // This can only be done when a result is requested because
3231 // that guarantees we will get information back when the
3232 // activity is finished, no matter what happens to it.
3233 mStartedActivity = true;
3234 }
3235 }
3236
3237 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 * Launch a new activity. You will not receive any information about when
3239 * the activity exits. This implementation overrides the base version,
3240 * providing information about
3241 * the activity performing the launch. Because of this additional
3242 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
3243 * required; if not specified, the new activity will be added to the
3244 * task of the caller.
3245 *
3246 * <p>This method throws {@link android.content.ActivityNotFoundException}
3247 * if there was no Activity found to run the given Intent.
3248 *
3249 * @param intent The intent to start.
3250 *
3251 * @throws android.content.ActivityNotFoundException
3252 *
3253 * @see #startActivityForResult
3254 */
3255 @Override
3256 public void startActivity(Intent intent) {
3257 startActivityForResult(intent, -1);
3258 }
3259
3260 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003261 * Launch a new activity. You will not receive any information about when
3262 * the activity exits. This implementation overrides the base version,
3263 * providing information about
3264 * the activity performing the launch. Because of this additional
3265 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
3266 * required; if not specified, the new activity will be added to the
3267 * task of the caller.
3268 *
3269 * <p>This method throws {@link android.content.ActivityNotFoundException}
3270 * if there was no Activity found to run the given Intent.
3271 *
3272 * @param intents The intents to start.
3273 *
3274 * @throws android.content.ActivityNotFoundException
3275 *
3276 * @see #startActivityForResult
3277 */
3278 @Override
3279 public void startActivities(Intent[] intents) {
3280 mInstrumentation.execStartActivities(this, mMainThread.getApplicationThread(),
3281 mToken, this, intents);
3282 }
3283
3284 /**
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003285 * Like {@link #startActivity(Intent)}, but taking a IntentSender
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003286 * to start; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07003287 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003288 * for more information.
3289 *
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003290 * @param intent The IntentSender to launch.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003291 * @param fillInIntent If non-null, this will be provided as the
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003292 * intent parameter to {@link IntentSender#sendIntent}.
3293 * @param flagsMask Intent flags in the original IntentSender that you
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003294 * would like to change.
3295 * @param flagsValues Desired values for any bits set in
3296 * <var>flagsMask</var>
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003297 * @param extraFlags Always set to 0.
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003298 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003299 public void startIntentSender(IntentSender intent,
3300 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
3301 throws IntentSender.SendIntentException {
3302 startIntentSenderForResult(intent, -1, fillInIntent, flagsMask,
3303 flagsValues, extraFlags);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003304 }
3305
3306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 * A special variation to launch an activity only if a new activity
3308 * instance is needed to handle the given Intent. In other words, this is
3309 * just like {@link #startActivityForResult(Intent, int)} except: if you are
3310 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
3311 * singleTask or singleTop
3312 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
3313 * and the activity
3314 * that handles <var>intent</var> is the same as your currently running
3315 * activity, then a new instance is not needed. In this case, instead of
3316 * the normal behavior of calling {@link #onNewIntent} this function will
3317 * return and you can handle the Intent yourself.
3318 *
3319 * <p>This function can only be called from a top-level activity; if it is
3320 * called from a child activity, a runtime exception will be thrown.
3321 *
3322 * @param intent The intent to start.
3323 * @param requestCode If >= 0, this code will be returned in
3324 * onActivityResult() when the activity exits, as described in
3325 * {@link #startActivityForResult}.
3326 *
3327 * @return If a new activity was launched then true is returned; otherwise
3328 * false is returned and you must handle the Intent yourself.
3329 *
3330 * @see #startActivity
3331 * @see #startActivityForResult
3332 */
3333 public boolean startActivityIfNeeded(Intent intent, int requestCode) {
3334 if (mParent == null) {
3335 int result = IActivityManager.START_RETURN_INTENT_TO_CALLER;
3336 try {
3337 result = ActivityManagerNative.getDefault()
3338 .startActivity(mMainThread.getApplicationThread(),
3339 intent, intent.resolveTypeIfNeeded(
3340 getContentResolver()),
3341 null, 0,
3342 mToken, mEmbeddedID, requestCode, true, false);
3343 } catch (RemoteException e) {
3344 // Empty
3345 }
3346
3347 Instrumentation.checkStartActivityResult(result, intent);
3348
3349 if (requestCode >= 0) {
3350 // If this start is requesting a result, we can avoid making
3351 // the activity visible until the result is received. Setting
3352 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
3353 // activity hidden during this time, to avoid flickering.
3354 // This can only be done when a result is requested because
3355 // that guarantees we will get information back when the
3356 // activity is finished, no matter what happens to it.
3357 mStartedActivity = true;
3358 }
3359 return result != IActivityManager.START_RETURN_INTENT_TO_CALLER;
3360 }
3361
3362 throw new UnsupportedOperationException(
3363 "startActivityIfNeeded can only be called from a top-level activity");
3364 }
3365
3366 /**
3367 * Special version of starting an activity, for use when you are replacing
3368 * other activity components. You can use this to hand the Intent off
3369 * to the next Activity that can handle it. You typically call this in
3370 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
3371 *
3372 * @param intent The intent to dispatch to the next activity. For
3373 * correct behavior, this must be the same as the Intent that started
3374 * your own activity; the only changes you can make are to the extras
3375 * inside of it.
3376 *
3377 * @return Returns a boolean indicating whether there was another Activity
3378 * to start: true if there was a next activity to start, false if there
3379 * wasn't. In general, if true is returned you will then want to call
3380 * finish() on yourself.
3381 */
3382 public boolean startNextMatchingActivity(Intent intent) {
3383 if (mParent == null) {
3384 try {
3385 return ActivityManagerNative.getDefault()
3386 .startNextMatchingActivity(mToken, intent);
3387 } catch (RemoteException e) {
3388 // Empty
3389 }
3390 return false;
3391 }
3392
3393 throw new UnsupportedOperationException(
3394 "startNextMatchingActivity can only be called from a top-level activity");
3395 }
3396
3397 /**
3398 * This is called when a child activity of this one calls its
3399 * {@link #startActivity} or {@link #startActivityForResult} method.
3400 *
3401 * <p>This method throws {@link android.content.ActivityNotFoundException}
3402 * if there was no Activity found to run the given Intent.
3403 *
3404 * @param child The activity making the call.
3405 * @param intent The intent to start.
3406 * @param requestCode Reply request code. < 0 if reply is not requested.
3407 *
3408 * @throws android.content.ActivityNotFoundException
3409 *
3410 * @see #startActivity
3411 * @see #startActivityForResult
3412 */
3413 public void startActivityFromChild(Activity child, Intent intent,
3414 int requestCode) {
3415 Instrumentation.ActivityResult ar =
3416 mInstrumentation.execStartActivity(
3417 this, mMainThread.getApplicationThread(), mToken, child,
3418 intent, requestCode);
3419 if (ar != null) {
3420 mMainThread.sendActivityResult(
3421 mToken, child.mEmbeddedID, requestCode,
3422 ar.getResultCode(), ar.getResultData());
3423 }
3424 }
3425
3426 /**
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07003427 * This is called when a Fragment in this activity calls its
3428 * {@link Fragment#startActivity} or {@link Fragment#startActivityForResult}
3429 * method.
3430 *
3431 * <p>This method throws {@link android.content.ActivityNotFoundException}
3432 * if there was no Activity found to run the given Intent.
3433 *
3434 * @param fragment The fragment making the call.
3435 * @param intent The intent to start.
3436 * @param requestCode Reply request code. < 0 if reply is not requested.
3437 *
3438 * @throws android.content.ActivityNotFoundException
3439 *
3440 * @see Fragment#startActivity
3441 * @see Fragment#startActivityForResult
3442 */
3443 public void startActivityFromFragment(Fragment fragment, Intent intent,
3444 int requestCode) {
3445 Instrumentation.ActivityResult ar =
3446 mInstrumentation.execStartActivity(
3447 this, mMainThread.getApplicationThread(), mToken, fragment,
3448 intent, requestCode);
3449 if (ar != null) {
3450 mMainThread.sendActivityResult(
3451 mToken, fragment.mWho, requestCode,
3452 ar.getResultCode(), ar.getResultData());
3453 }
3454 }
3455
3456 /**
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003457 * Like {@link #startActivityFromChild(Activity, Intent, int)}, but
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003458 * taking a IntentSender; see
Dianne Hackbornae22c052009-09-17 18:46:22 -07003459 * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003460 * for more information.
3461 */
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003462 public void startIntentSenderFromChild(Activity child, IntentSender intent,
3463 int requestCode, Intent fillInIntent, int flagsMask, int flagsValues,
3464 int extraFlags)
3465 throws IntentSender.SendIntentException {
3466 startIntentSenderForResultInner(intent, requestCode, fillInIntent,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003467 flagsMask, flagsValues, child);
3468 }
3469
3470 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003471 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
3472 * or {@link #finish} to specify an explicit transition animation to
3473 * perform next.
3474 * @param enterAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003475 * the incoming activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003476 * @param exitAnim A resource ID of the animation resource to use for
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003477 * the outgoing activity. Use 0 for no animation.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003478 */
3479 public void overridePendingTransition(int enterAnim, int exitAnim) {
3480 try {
3481 ActivityManagerNative.getDefault().overridePendingTransition(
3482 mToken, getPackageName(), enterAnim, exitAnim);
3483 } catch (RemoteException e) {
3484 }
3485 }
3486
3487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 * Call this to set the result that your activity will return to its
3489 * caller.
3490 *
3491 * @param resultCode The result code to propagate back to the originating
3492 * activity, often RESULT_CANCELED or RESULT_OK
3493 *
3494 * @see #RESULT_CANCELED
3495 * @see #RESULT_OK
3496 * @see #RESULT_FIRST_USER
3497 * @see #setResult(int, Intent)
3498 */
3499 public final void setResult(int resultCode) {
3500 synchronized (this) {
3501 mResultCode = resultCode;
3502 mResultData = null;
3503 }
3504 }
3505
3506 /**
3507 * Call this to set the result that your activity will return to its
3508 * caller.
3509 *
3510 * @param resultCode The result code to propagate back to the originating
3511 * activity, often RESULT_CANCELED or RESULT_OK
3512 * @param data The data to propagate back to the originating activity.
3513 *
3514 * @see #RESULT_CANCELED
3515 * @see #RESULT_OK
3516 * @see #RESULT_FIRST_USER
3517 * @see #setResult(int)
3518 */
3519 public final void setResult(int resultCode, Intent data) {
3520 synchronized (this) {
3521 mResultCode = resultCode;
3522 mResultData = data;
3523 }
3524 }
3525
3526 /**
3527 * Return the name of the package that invoked this activity. This is who
3528 * the data in {@link #setResult setResult()} will be sent to. You can
3529 * use this information to validate that the recipient is allowed to
3530 * receive the data.
3531 *
3532 * <p>Note: if the calling activity is not expecting a result (that is it
3533 * did not use the {@link #startActivityForResult}
3534 * form that includes a request code), then the calling package will be
3535 * null.
3536 *
3537 * @return The package of the activity that will receive your
3538 * reply, or null if none.
3539 */
3540 public String getCallingPackage() {
3541 try {
3542 return ActivityManagerNative.getDefault().getCallingPackage(mToken);
3543 } catch (RemoteException e) {
3544 return null;
3545 }
3546 }
3547
3548 /**
3549 * Return the name of the activity that invoked this activity. This is
3550 * who the data in {@link #setResult setResult()} will be sent to. You
3551 * can use this information to validate that the recipient is allowed to
3552 * receive the data.
3553 *
3554 * <p>Note: if the calling activity is not expecting a result (that is it
3555 * did not use the {@link #startActivityForResult}
3556 * form that includes a request code), then the calling package will be
3557 * null.
3558 *
3559 * @return String The full name of the activity that will receive your
3560 * reply, or null if none.
3561 */
3562 public ComponentName getCallingActivity() {
3563 try {
3564 return ActivityManagerNative.getDefault().getCallingActivity(mToken);
3565 } catch (RemoteException e) {
3566 return null;
3567 }
3568 }
3569
3570 /**
3571 * Control whether this activity's main window is visible. This is intended
3572 * only for the special case of an activity that is not going to show a
3573 * UI itself, but can't just finish prior to onResume() because it needs
3574 * to wait for a service binding or such. Setting this to false allows
3575 * you to prevent your UI from being shown during that time.
3576 *
3577 * <p>The default value for this is taken from the
3578 * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme.
3579 */
3580 public void setVisible(boolean visible) {
3581 if (mVisibleFromClient != visible) {
3582 mVisibleFromClient = visible;
3583 if (mVisibleFromServer) {
3584 if (visible) makeVisible();
3585 else mDecor.setVisibility(View.INVISIBLE);
3586 }
3587 }
3588 }
3589
3590 void makeVisible() {
3591 if (!mWindowAdded) {
3592 ViewManager wm = getWindowManager();
3593 wm.addView(mDecor, getWindow().getAttributes());
3594 mWindowAdded = true;
3595 }
3596 mDecor.setVisibility(View.VISIBLE);
3597 }
3598
3599 /**
3600 * Check to see whether this activity is in the process of finishing,
3601 * either because you called {@link #finish} on it or someone else
3602 * has requested that it finished. This is often used in
3603 * {@link #onPause} to determine whether the activity is simply pausing or
3604 * completely finishing.
3605 *
3606 * @return If the activity is finishing, returns true; else returns false.
3607 *
3608 * @see #finish
3609 */
3610 public boolean isFinishing() {
3611 return mFinished;
3612 }
3613
3614 /**
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05003615 * Check to see whether this activity is in the process of being destroyed in order to be
3616 * recreated with a new configuration. This is often used in
3617 * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed
3618 * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}.
3619 *
3620 * @return If the activity is being torn down in order to be recreated with a new configuration,
3621 * returns true; else returns false.
3622 */
3623 public boolean isChangingConfigurations() {
3624 return mChangingConfigurations;
3625 }
3626
3627 /**
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003628 * Cause this Activity to be recreated with a new instance. This results
3629 * in essentially the same flow as when the Activity is created due to
3630 * a configuration change -- the current instance will go through its
3631 * lifecycle to {@link #onDestroy} and a new instance then created after it.
3632 */
3633 public void recreate() {
3634 if (mParent != null) {
3635 throw new IllegalStateException("Can only be called on top-level activity");
3636 }
3637 if (Looper.myLooper() != mMainThread.getLooper()) {
3638 throw new IllegalStateException("Must be called from main thread");
3639 }
3640 mMainThread.requestRelaunchActivity(mToken, null, null, 0, false, null, false);
3641 }
3642
3643 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 * Call this when your activity is done and should be closed. The
3645 * ActivityResult is propagated back to whoever launched you via
3646 * onActivityResult().
3647 */
3648 public void finish() {
3649 if (mParent == null) {
3650 int resultCode;
3651 Intent resultData;
3652 synchronized (this) {
3653 resultCode = mResultCode;
3654 resultData = mResultData;
3655 }
Joe Onorato43a17652011-04-06 19:22:23 -07003656 if (false) Log.v(TAG, "Finishing self: token=" + mToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 try {
3658 if (ActivityManagerNative.getDefault()
3659 .finishActivity(mToken, resultCode, resultData)) {
3660 mFinished = true;
3661 }
3662 } catch (RemoteException e) {
3663 // Empty
3664 }
3665 } else {
3666 mParent.finishFromChild(this);
3667 }
3668 }
3669
3670 /**
3671 * This is called when a child activity of this one calls its
3672 * {@link #finish} method. The default implementation simply calls
3673 * finish() on this activity (the parent), finishing the entire group.
3674 *
3675 * @param child The activity making the call.
3676 *
3677 * @see #finish
3678 */
3679 public void finishFromChild(Activity child) {
3680 finish();
3681 }
3682
3683 /**
3684 * Force finish another activity that you had previously started with
3685 * {@link #startActivityForResult}.
3686 *
3687 * @param requestCode The request code of the activity that you had
3688 * given to startActivityForResult(). If there are multiple
3689 * activities started with this request code, they
3690 * will all be finished.
3691 */
3692 public void finishActivity(int requestCode) {
3693 if (mParent == null) {
3694 try {
3695 ActivityManagerNative.getDefault()
3696 .finishSubActivity(mToken, mEmbeddedID, requestCode);
3697 } catch (RemoteException e) {
3698 // Empty
3699 }
3700 } else {
3701 mParent.finishActivityFromChild(this, requestCode);
3702 }
3703 }
3704
3705 /**
3706 * This is called when a child activity of this one calls its
3707 * finishActivity().
3708 *
3709 * @param child The activity making the call.
3710 * @param requestCode Request code that had been used to start the
3711 * activity.
3712 */
3713 public void finishActivityFromChild(Activity child, int requestCode) {
3714 try {
3715 ActivityManagerNative.getDefault()
3716 .finishSubActivity(mToken, child.mEmbeddedID, requestCode);
3717 } catch (RemoteException e) {
3718 // Empty
3719 }
3720 }
3721
3722 /**
3723 * Called when an activity you launched exits, giving you the requestCode
3724 * you started it with, the resultCode it returned, and any additional
3725 * data from it. The <var>resultCode</var> will be
3726 * {@link #RESULT_CANCELED} if the activity explicitly returned that,
3727 * didn't return any result, or crashed during its operation.
3728 *
3729 * <p>You will receive this call immediately before onResume() when your
3730 * activity is re-starting.
3731 *
3732 * @param requestCode The integer request code originally supplied to
3733 * startActivityForResult(), allowing you to identify who this
3734 * result came from.
3735 * @param resultCode The integer result code returned by the child activity
3736 * through its setResult().
3737 * @param data An Intent, which can return result data to the caller
3738 * (various data can be attached to Intent "extras").
3739 *
3740 * @see #startActivityForResult
3741 * @see #createPendingResult
3742 * @see #setResult(int)
3743 */
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07003744 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 }
3746
3747 /**
3748 * Create a new PendingIntent object which you can hand to others
3749 * for them to use to send result data back to your
3750 * {@link #onActivityResult} callback. The created object will be either
3751 * one-shot (becoming invalid after a result is sent back) or multiple
3752 * (allowing any number of results to be sent through it).
3753 *
3754 * @param requestCode Private request code for the sender that will be
3755 * associated with the result data when it is returned. The sender can not
3756 * modify this value, allowing you to identify incoming results.
3757 * @param data Default data to supply in the result, which may be modified
3758 * by the sender.
3759 * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT},
3760 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE},
3761 * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT},
3762 * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT},
3763 * or any of the flags as supported by
3764 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
3765 * of the intent that can be supplied when the actual send happens.
3766 *
3767 * @return Returns an existing or new PendingIntent matching the given
3768 * parameters. May return null only if
3769 * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been
3770 * supplied.
3771 *
3772 * @see PendingIntent
3773 */
3774 public PendingIntent createPendingResult(int requestCode, Intent data,
3775 int flags) {
3776 String packageName = getPackageName();
3777 try {
3778 IIntentSender target =
3779 ActivityManagerNative.getDefault().getIntentSender(
3780 IActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName,
3781 mParent == null ? mToken : mParent.mToken,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003782 mEmbeddedID, requestCode, new Intent[] { data }, null, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 return target != null ? new PendingIntent(target) : null;
3784 } catch (RemoteException e) {
3785 // Empty
3786 }
3787 return null;
3788 }
3789
3790 /**
3791 * Change the desired orientation of this activity. If the activity
3792 * is currently in the foreground or otherwise impacting the screen
3793 * orientation, the screen will immediately be changed (possibly causing
3794 * the activity to be restarted). Otherwise, this will be used the next
3795 * time the activity is visible.
3796 *
3797 * @param requestedOrientation An orientation constant as used in
3798 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
3799 */
3800 public void setRequestedOrientation(int requestedOrientation) {
3801 if (mParent == null) {
3802 try {
3803 ActivityManagerNative.getDefault().setRequestedOrientation(
3804 mToken, requestedOrientation);
3805 } catch (RemoteException e) {
3806 // Empty
3807 }
3808 } else {
3809 mParent.setRequestedOrientation(requestedOrientation);
3810 }
3811 }
3812
3813 /**
3814 * Return the current requested orientation of the activity. This will
3815 * either be the orientation requested in its component's manifest, or
3816 * the last requested orientation given to
3817 * {@link #setRequestedOrientation(int)}.
3818 *
3819 * @return Returns an orientation constant as used in
3820 * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
3821 */
3822 public int getRequestedOrientation() {
3823 if (mParent == null) {
3824 try {
3825 return ActivityManagerNative.getDefault()
3826 .getRequestedOrientation(mToken);
3827 } catch (RemoteException e) {
3828 // Empty
3829 }
3830 } else {
3831 return mParent.getRequestedOrientation();
3832 }
3833 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3834 }
3835
3836 /**
3837 * Return the identifier of the task this activity is in. This identifier
3838 * will remain the same for the lifetime of the activity.
3839 *
3840 * @return Task identifier, an opaque integer.
3841 */
3842 public int getTaskId() {
3843 try {
3844 return ActivityManagerNative.getDefault()
3845 .getTaskForActivity(mToken, false);
3846 } catch (RemoteException e) {
3847 return -1;
3848 }
3849 }
3850
3851 /**
3852 * Return whether this activity is the root of a task. The root is the
3853 * first activity in a task.
3854 *
3855 * @return True if this is the root activity, else false.
3856 */
3857 public boolean isTaskRoot() {
3858 try {
3859 return ActivityManagerNative.getDefault()
3860 .getTaskForActivity(mToken, true) >= 0;
3861 } catch (RemoteException e) {
3862 return false;
3863 }
3864 }
3865
3866 /**
3867 * Move the task containing this activity to the back of the activity
3868 * stack. The activity's order within the task is unchanged.
3869 *
3870 * @param nonRoot If false then this only works if the activity is the root
3871 * of a task; if true it will work for any activity in
3872 * a task.
3873 *
3874 * @return If the task was moved (or it was already at the
3875 * back) true is returned, else false.
3876 */
3877 public boolean moveTaskToBack(boolean nonRoot) {
3878 try {
3879 return ActivityManagerNative.getDefault().moveActivityTaskToBack(
3880 mToken, nonRoot);
3881 } catch (RemoteException e) {
3882 // Empty
3883 }
3884 return false;
3885 }
3886
3887 /**
3888 * Returns class name for this activity with the package prefix removed.
3889 * This is the default name used to read and write settings.
3890 *
3891 * @return The local class name.
3892 */
3893 public String getLocalClassName() {
3894 final String pkg = getPackageName();
3895 final String cls = mComponent.getClassName();
3896 int packageLen = pkg.length();
3897 if (!cls.startsWith(pkg) || cls.length() <= packageLen
3898 || cls.charAt(packageLen) != '.') {
3899 return cls;
3900 }
3901 return cls.substring(packageLen+1);
3902 }
3903
3904 /**
3905 * Returns complete component name of this activity.
3906 *
3907 * @return Returns the complete component name for this activity
3908 */
3909 public ComponentName getComponentName()
3910 {
3911 return mComponent;
3912 }
3913
3914 /**
3915 * Retrieve a {@link SharedPreferences} object for accessing preferences
3916 * that are private to this activity. This simply calls the underlying
3917 * {@link #getSharedPreferences(String, int)} method by passing in this activity's
3918 * class name as the preferences name.
3919 *
3920 * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default
3921 * operation, {@link #MODE_WORLD_READABLE} and
3922 * {@link #MODE_WORLD_WRITEABLE} to control permissions.
3923 *
3924 * @return Returns the single SharedPreferences instance that can be used
3925 * to retrieve and modify the preference values.
3926 */
3927 public SharedPreferences getPreferences(int mode) {
3928 return getSharedPreferences(getLocalClassName(), mode);
3929 }
3930
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003931 private void ensureSearchManager() {
3932 if (mSearchManager != null) {
3933 return;
3934 }
3935
Amith Yamasanie9ce3f02010-01-25 09:15:50 -08003936 mSearchManager = new SearchManager(this, null);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003937 }
3938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 @Override
3940 public Object getSystemService(String name) {
3941 if (getBaseContext() == null) {
3942 throw new IllegalStateException(
3943 "System services not available to Activities before onCreate()");
3944 }
3945
3946 if (WINDOW_SERVICE.equals(name)) {
3947 return mWindowManager;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01003948 } else if (SEARCH_SERVICE.equals(name)) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003949 ensureSearchManager();
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +01003950 return mSearchManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 }
3952 return super.getSystemService(name);
3953 }
3954
3955 /**
3956 * Change the title associated with this activity. If this is a
3957 * top-level activity, the title for its window will change. If it
3958 * is an embedded activity, the parent can do whatever it wants
3959 * with it.
3960 */
3961 public void setTitle(CharSequence title) {
3962 mTitle = title;
3963 onTitleChanged(title, mTitleColor);
3964
3965 if (mParent != null) {
3966 mParent.onChildTitleChanged(this, title);
3967 }
3968 }
3969
3970 /**
3971 * Change the title associated with this activity. If this is a
3972 * top-level activity, the title for its window will change. If it
3973 * is an embedded activity, the parent can do whatever it wants
3974 * with it.
3975 */
3976 public void setTitle(int titleId) {
3977 setTitle(getText(titleId));
3978 }
3979
3980 public void setTitleColor(int textColor) {
3981 mTitleColor = textColor;
3982 onTitleChanged(mTitle, textColor);
3983 }
3984
3985 public final CharSequence getTitle() {
3986 return mTitle;
3987 }
3988
3989 public final int getTitleColor() {
3990 return mTitleColor;
3991 }
3992
3993 protected void onTitleChanged(CharSequence title, int color) {
3994 if (mTitleReady) {
3995 final Window win = getWindow();
3996 if (win != null) {
3997 win.setTitle(title);
3998 if (color != 0) {
3999 win.setTitleColor(color);
4000 }
4001 }
4002 }
4003 }
4004
4005 protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
4006 }
4007
4008 /**
4009 * Sets the visibility of the progress bar in the title.
4010 * <p>
4011 * In order for the progress bar to be shown, the feature must be requested
4012 * via {@link #requestWindowFeature(int)}.
4013 *
4014 * @param visible Whether to show the progress bars in the title.
4015 */
4016 public final void setProgressBarVisibility(boolean visible) {
4017 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON :
4018 Window.PROGRESS_VISIBILITY_OFF);
4019 }
4020
4021 /**
4022 * Sets the visibility of the indeterminate progress bar in the title.
4023 * <p>
4024 * In order for the progress bar to be shown, the feature must be requested
4025 * via {@link #requestWindowFeature(int)}.
4026 *
4027 * @param visible Whether to show the progress bars in the title.
4028 */
4029 public final void setProgressBarIndeterminateVisibility(boolean visible) {
4030 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
4031 visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF);
4032 }
4033
4034 /**
4035 * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
4036 * is always indeterminate).
4037 * <p>
4038 * In order for the progress bar to be shown, the feature must be requested
4039 * via {@link #requestWindowFeature(int)}.
4040 *
4041 * @param indeterminate Whether the horizontal progress bar should be indeterminate.
4042 */
4043 public final void setProgressBarIndeterminate(boolean indeterminate) {
4044 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
4045 indeterminate ? Window.PROGRESS_INDETERMINATE_ON : Window.PROGRESS_INDETERMINATE_OFF);
4046 }
4047
4048 /**
4049 * Sets the progress for the progress bars in the title.
4050 * <p>
4051 * In order for the progress bar to be shown, the feature must be requested
4052 * via {@link #requestWindowFeature(int)}.
4053 *
4054 * @param progress The progress for the progress bar. Valid ranges are from
4055 * 0 to 10000 (both inclusive). If 10000 is given, the progress
4056 * bar will be completely filled and will fade out.
4057 */
4058 public final void setProgress(int progress) {
4059 getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START);
4060 }
4061
4062 /**
4063 * Sets the secondary progress for the progress bar in the title. This
4064 * progress is drawn between the primary progress (set via
4065 * {@link #setProgress(int)} and the background. It can be ideal for media
4066 * scenarios such as showing the buffering progress while the default
4067 * progress shows the play progress.
4068 * <p>
4069 * In order for the progress bar to be shown, the feature must be requested
4070 * via {@link #requestWindowFeature(int)}.
4071 *
4072 * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from
4073 * 0 to 10000 (both inclusive).
4074 */
4075 public final void setSecondaryProgress(int secondaryProgress) {
4076 getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
4077 secondaryProgress + Window.PROGRESS_SECONDARY_START);
4078 }
4079
4080 /**
4081 * Suggests an audio stream whose volume should be changed by the hardware
4082 * volume controls.
4083 * <p>
4084 * The suggested audio stream will be tied to the window of this Activity.
4085 * If the Activity is switched, the stream set here is no longer the
4086 * suggested stream. The client does not need to save and restore the old
4087 * suggested stream value in onPause and onResume.
4088 *
4089 * @param streamType The type of the audio stream whose volume should be
4090 * changed by the hardware volume controls. It is not guaranteed that
4091 * the hardware volume controls will always change this stream's
4092 * volume (for example, if a call is in progress, its stream's volume
4093 * may be changed instead). To reset back to the default, use
4094 * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
4095 */
4096 public final void setVolumeControlStream(int streamType) {
4097 getWindow().setVolumeControlStream(streamType);
4098 }
4099
4100 /**
4101 * Gets the suggested audio stream whose volume should be changed by the
4102 * harwdare volume controls.
4103 *
4104 * @return The suggested audio stream type whose volume should be changed by
4105 * the hardware volume controls.
4106 * @see #setVolumeControlStream(int)
4107 */
4108 public final int getVolumeControlStream() {
4109 return getWindow().getVolumeControlStream();
4110 }
4111
4112 /**
4113 * Runs the specified action on the UI thread. If the current thread is the UI
4114 * thread, then the action is executed immediately. If the current thread is
4115 * not the UI thread, the action is posted to the event queue of the UI thread.
4116 *
4117 * @param action the action to run on the UI thread
4118 */
4119 public final void runOnUiThread(Runnable action) {
4120 if (Thread.currentThread() != mUiThread) {
4121 mHandler.post(action);
4122 } else {
4123 action.run();
4124 }
4125 }
4126
4127 /**
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07004128 * Standard implementation of
4129 * {@link android.view.LayoutInflater.Factory#onCreateView} used when
4130 * inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackborn625ac272010-09-17 18:29:22 -07004131 * This implementation does nothing and is for
4132 * pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} apps. Newer apps
4133 * should use {@link #onCreateView(View, String, Context, AttributeSet)}.
4134 *
4135 * @see android.view.LayoutInflater#createView
4136 * @see android.view.Window#getLayoutInflater
4137 */
4138 public View onCreateView(String name, Context context, AttributeSet attrs) {
4139 return null;
4140 }
4141
4142 /**
4143 * Standard implementation of
4144 * {@link android.view.LayoutInflater.Factory2#onCreateView(View, String, Context, AttributeSet)}
4145 * used when inflating with the LayoutInflater returned by {@link #getSystemService}.
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07004146 * This implementation handles <fragment> tags to embed fragments inside
4147 * of the activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 *
4149 * @see android.view.LayoutInflater#createView
4150 * @see android.view.Window#getLayoutInflater
4151 */
Dianne Hackborn625ac272010-09-17 18:29:22 -07004152 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07004153 if (!"fragment".equals(name)) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07004154 return onCreateView(name, context, attrs);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07004155 }
4156
Dianne Hackborndef15372010-08-15 12:43:52 -07004157 String fname = attrs.getAttributeValue(null, "class");
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07004158 TypedArray a =
4159 context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
Dianne Hackborndef15372010-08-15 12:43:52 -07004160 if (fname == null) {
4161 fname = a.getString(com.android.internal.R.styleable.Fragment_name);
4162 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07004163 int id = a.getResourceId(com.android.internal.R.styleable.Fragment_id, View.NO_ID);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07004164 String tag = a.getString(com.android.internal.R.styleable.Fragment_tag);
4165 a.recycle();
4166
Dianne Hackborn625ac272010-09-17 18:29:22 -07004167 int containerId = parent != null ? parent.getId() : 0;
4168 if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004169 throw new IllegalArgumentException(attrs.getPositionDescription()
Dianne Hackborn625ac272010-09-17 18:29:22 -07004170 + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004171 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07004172
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07004173 // If we restored from a previous state, we may already have
4174 // instantiated this fragment from the state and should use
4175 // that instance instead of making a new one.
Dianne Hackborn625ac272010-09-17 18:29:22 -07004176 Fragment fragment = id != View.NO_ID ? mFragments.findFragmentById(id) : null;
4177 if (fragment == null && tag != null) {
4178 fragment = mFragments.findFragmentByTag(tag);
4179 }
4180 if (fragment == null && containerId != View.NO_ID) {
4181 fragment = mFragments.findFragmentById(containerId);
4182 }
4183
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07004184 if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x"
4185 + Integer.toHexString(id) + " fname=" + fname
4186 + " existing=" + fragment);
4187 if (fragment == null) {
4188 fragment = Fragment.instantiate(this, fname);
4189 fragment.mFromLayout = true;
Dianne Hackborn625ac272010-09-17 18:29:22 -07004190 fragment.mFragmentId = id != 0 ? id : containerId;
4191 fragment.mContainerId = containerId;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07004192 fragment.mTag = tag;
Dianne Hackborn625ac272010-09-17 18:29:22 -07004193 fragment.mInLayout = true;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07004194 fragment.mImmediateActivity = this;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07004195 fragment.mFragmentManager = mFragments;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08004196 fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
Dianne Hackborn625ac272010-09-17 18:29:22 -07004197 mFragments.addFragment(fragment, true);
4198
4199 } else if (fragment.mInLayout) {
4200 // A fragment already exists and it is not one we restored from
4201 // previous state.
4202 throw new IllegalArgumentException(attrs.getPositionDescription()
4203 + ": Duplicate id 0x" + Integer.toHexString(id)
4204 + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId)
4205 + " with another fragment for " + fname);
4206 } else {
4207 // This fragment was retained from a previous instance; get it
4208 // going now.
4209 fragment.mInLayout = true;
4210 fragment.mImmediateActivity = this;
Dianne Hackborndef15372010-08-15 12:43:52 -07004211 // If this fragment is newly instantiated (either right now, or
4212 // from last saved state), then give it the attributes to
4213 // initialize itself.
4214 if (!fragment.mRetaining) {
Dianne Hackborne3a7f622011-03-03 21:48:24 -08004215 fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
Dianne Hackborndef15372010-08-15 12:43:52 -07004216 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07004217 mFragments.moveToState(fragment);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07004218 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07004219
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07004220 if (fragment.mView == null) {
4221 throw new IllegalStateException("Fragment " + fname
4222 + " did not create a view.");
4223 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07004224 if (id != 0) {
4225 fragment.mView.setId(id);
4226 }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07004227 if (fragment.mView.getTag() == null) {
4228 fragment.mView.setTag(tag);
4229 }
4230 return fragment.mView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 }
4232
Daniel Sandler69a48172010-06-23 16:29:36 -04004233 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -07004234 * Print the Activity's state into the given stream. This gets invoked if
Dianne Hackborn30d71892010-12-11 10:37:55 -08004235 * you run "adb shell dumpsys activity <activity_component_name>".
Dianne Hackborn625ac272010-09-17 18:29:22 -07004236 *
Dianne Hackborn30d71892010-12-11 10:37:55 -08004237 * @param prefix Desired prefix to prepend at each line of output.
Dianne Hackborn625ac272010-09-17 18:29:22 -07004238 * @param fd The raw file descriptor that the dump is being sent to.
4239 * @param writer The PrintWriter to which you should dump your state. This will be
4240 * closed for you after you return.
4241 * @param args additional arguments to the dump request.
4242 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08004243 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4244 writer.print(prefix); writer.print("Local Activity ");
4245 writer.print(Integer.toHexString(System.identityHashCode(this)));
4246 writer.println(" State:");
4247 String innerPrefix = prefix + " ";
4248 writer.print(innerPrefix); writer.print("mResumed=");
4249 writer.print(mResumed); writer.print(" mStopped=");
4250 writer.print(mStopped); writer.print(" mFinished=");
4251 writer.println(mFinished);
4252 writer.print(innerPrefix); writer.print("mLoadersStarted=");
4253 writer.println(mLoadersStarted);
4254 writer.print(innerPrefix); writer.print("mChangingConfigurations=");
4255 writer.println(mChangingConfigurations);
4256 writer.print(innerPrefix); writer.print("mCurrentConfig=");
4257 writer.println(mCurrentConfig);
4258 if (mLoaderManager != null) {
4259 writer.print(prefix); writer.print("Loader Manager ");
4260 writer.print(Integer.toHexString(System.identityHashCode(mLoaderManager)));
4261 writer.println(":");
4262 mLoaderManager.dump(prefix + " ", fd, writer, args);
4263 }
4264 mFragments.dump(prefix, fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -07004265 }
4266
4267 /**
Daniel Sandler69a48172010-06-23 16:29:36 -04004268 * Bit indicating that this activity is "immersive" and should not be
4269 * interrupted by notifications if possible.
4270 *
4271 * This value is initially set by the manifest property
4272 * <code>android:immersive</code> but may be changed at runtime by
4273 * {@link #setImmersive}.
4274 *
4275 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
Dianne Hackborn02486b12010-08-26 14:18:37 -07004276 * @hide
Daniel Sandler69a48172010-06-23 16:29:36 -04004277 */
4278 public boolean isImmersive() {
4279 try {
4280 return ActivityManagerNative.getDefault().isImmersive(mToken);
4281 } catch (RemoteException e) {
4282 return false;
4283 }
4284 }
4285
4286 /**
4287 * Adjust the current immersive mode setting.
4288 *
4289 * Note that changing this value will have no effect on the activity's
4290 * {@link android.content.pm.ActivityInfo} structure; that is, if
4291 * <code>android:immersive</code> is set to <code>true</code>
4292 * in the application's manifest entry for this activity, the {@link
4293 * android.content.pm.ActivityInfo#flags ActivityInfo.flags} member will
4294 * always have its {@link android.content.pm.ActivityInfo#FLAG_IMMERSIVE
4295 * FLAG_IMMERSIVE} bit set.
4296 *
4297 * @see #isImmersive
4298 * @see android.content.pm.ActivityInfo#FLAG_IMMERSIVE
Dianne Hackborn02486b12010-08-26 14:18:37 -07004299 * @hide
Daniel Sandler69a48172010-06-23 16:29:36 -04004300 */
4301 public void setImmersive(boolean i) {
4302 try {
4303 ActivityManagerNative.getDefault().setImmersive(mToken, i);
4304 } catch (RemoteException e) {
4305 // pass
4306 }
4307 }
4308
Adam Powell6e346362010-07-23 10:18:23 -07004309 /**
Adam Powelldebf3be2010-11-15 18:58:48 -08004310 * Start an action mode.
Adam Powell6e346362010-07-23 10:18:23 -07004311 *
4312 * @param callback Callback that will manage lifecycle events for this context mode
4313 * @return The ContextMode that was started, or null if it was canceled
4314 *
4315 * @see ActionMode
4316 */
Adam Powell5d279772010-07-27 16:34:07 -07004317 public ActionMode startActionMode(ActionMode.Callback callback) {
Adam Powell6e346362010-07-23 10:18:23 -07004318 return mWindow.getDecorView().startActionMode(callback);
4319 }
4320
Adam Powelldebf3be2010-11-15 18:58:48 -08004321 /**
4322 * Give the Activity a chance to control the UI for an action mode requested
4323 * by the system.
4324 *
4325 * <p>Note: If you are looking for a notification callback that an action mode
4326 * has been started for this activity, see {@link #onActionModeStarted(ActionMode)}.</p>
4327 *
4328 * @param callback The callback that should control the new action mode
4329 * @return The new action mode, or <code>null</code> if the activity does not want to
4330 * provide special handling for this action mode. (It will be handled by the system.)
4331 */
4332 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Adam Powell42c0fe82010-08-10 16:36:56 -07004333 initActionBar();
Adam Powell6e346362010-07-23 10:18:23 -07004334 if (mActionBar != null) {
Adam Powell5d279772010-07-27 16:34:07 -07004335 return mActionBar.startActionMode(callback);
Adam Powell6e346362010-07-23 10:18:23 -07004336 }
4337 return null;
4338 }
4339
Adam Powelldebf3be2010-11-15 18:58:48 -08004340 /**
4341 * Notifies the Activity that an action mode has been started.
4342 * Activity subclasses overriding this method should call the superclass implementation.
4343 *
4344 * @param mode The new action mode.
4345 */
4346 public void onActionModeStarted(ActionMode mode) {
4347 }
4348
4349 /**
4350 * Notifies the activity that an action mode has finished.
4351 * Activity subclasses overriding this method should call the superclass implementation.
4352 *
4353 * @param mode The action mode that just finished.
4354 */
4355 public void onActionModeFinished(ActionMode mode) {
4356 }
4357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 // ------------------ Internal API ------------------
4359
4360 final void setParent(Activity parent) {
4361 mParent = parent;
4362 }
4363
4364 final void attach(Context context, ActivityThread aThread, Instrumentation instr, IBinder token,
4365 Application application, Intent intent, ActivityInfo info, CharSequence title,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004366 Activity parent, String id, NonConfigurationInstances lastNonConfigurationInstances,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 Configuration config) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004368 attach(context, aThread, instr, token, 0, application, intent, info, title, parent, id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004369 lastNonConfigurationInstances, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 }
4371
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004372 final void attach(Context context, ActivityThread aThread,
4373 Instrumentation instr, IBinder token, int ident,
4374 Application application, Intent intent, ActivityInfo info,
4375 CharSequence title, Activity parent, String id,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004376 NonConfigurationInstances lastNonConfigurationInstances,
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004377 Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 attachBaseContext(context);
4379
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004380 mFragments.attachActivity(this);
4381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 mWindow = PolicyManager.makeNewWindow(this);
4383 mWindow.setCallback(this);
Dianne Hackborn420829e2011-01-28 11:30:35 -08004384 mWindow.getLayoutInflater().setPrivateFactory(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004385 if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
4386 mWindow.setSoftInputMode(info.softInputMode);
4387 }
4388 mUiThread = Thread.currentThread();
Romain Guy529b60a2010-08-03 18:05:47 -07004389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 mMainThread = aThread;
4391 mInstrumentation = instr;
4392 mToken = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004393 mIdent = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 mApplication = application;
4395 mIntent = intent;
4396 mComponent = intent.getComponent();
4397 mActivityInfo = info;
4398 mTitle = title;
4399 mParent = parent;
4400 mEmbeddedID = id;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004401 mLastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402
Romain Guy529b60a2010-08-03 18:05:47 -07004403 mWindow.setWindowManager(null, mToken, mComponent.flattenToString(),
4404 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 if (mParent != null) {
4406 mWindow.setContainer(mParent.getWindow());
4407 }
4408 mWindowManager = mWindow.getWindowManager();
4409 mCurrentConfig = config;
4410 }
4411
4412 final IBinder getActivityToken() {
4413 return mParent != null ? mParent.getActivityToken() : mToken;
4414 }
4415
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004416 final void performCreate(Bundle icicle) {
4417 onCreate(icicle);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004418 mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
4419 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Dianne Hackbornc8017682010-07-06 13:34:38 -07004420 mFragments.dispatchActivityCreated();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004421 }
4422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 final void performStart() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004424 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 mCalled = false;
Dianne Hackborn445646c2010-06-25 15:52:59 -07004426 mFragments.execPendingActions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 mInstrumentation.callActivityOnStart(this);
4428 if (!mCalled) {
4429 throw new SuperNotCalledException(
4430 "Activity " + mComponent.toShortString() +
4431 " did not call through to super.onStart()");
4432 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004433 mFragments.dispatchStart();
Dianne Hackborn2707d602010-07-09 18:01:20 -07004434 if (mAllLoaderManagers != null) {
4435 for (int i=mAllLoaderManagers.size()-1; i>=0; i--) {
Dianne Hackbornafc4b282011-06-10 17:03:42 -07004436 LoaderManagerImpl lm = mAllLoaderManagers.valueAt(i);
4437 lm.finishRetain();
4438 lm.doReportStart();
Dianne Hackborn2707d602010-07-09 18:01:20 -07004439 }
4440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 }
4442
4443 final void performRestart() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004444 mFragments.noteStateNotSaved();
Dianne Hackborna21e3da2010-09-12 19:27:46 -07004445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004446 if (mStopped) {
4447 mStopped = false;
Dianne Hackbornce418e62011-03-01 14:31:38 -08004448 if (mToken != null && mParent == null) {
4449 WindowManagerImpl.getDefault().setStoppedState(mToken, false);
4450 }
Dianne Hackborn185e3e22011-06-03 15:26:01 -07004451
4452 synchronized (mManagedCursors) {
4453 final int N = mManagedCursors.size();
4454 for (int i=0; i<N; i++) {
4455 ManagedCursor mc = mManagedCursors.get(i);
4456 if (mc.mReleased || mc.mUpdated) {
4457 if (!mc.mCursor.requery()) {
4458 throw new IllegalStateException(
4459 "trying to requery an already closed cursor "
4460 + mc.mCursor);
4461 }
4462 mc.mReleased = false;
4463 mc.mUpdated = false;
4464 }
4465 }
4466 }
4467
4468 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 mInstrumentation.callActivityOnRestart(this);
4470 if (!mCalled) {
4471 throw new SuperNotCalledException(
4472 "Activity " + mComponent.toShortString() +
4473 " did not call through to super.onRestart()");
4474 }
4475 performStart();
4476 }
4477 }
4478
4479 final void performResume() {
4480 performRestart();
4481
Dianne Hackborn445646c2010-06-25 15:52:59 -07004482 mFragments.execPendingActions();
4483
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004484 mLastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 mCalled = false;
Jeff Hamilton52d32032011-01-08 15:31:26 -06004487 // mResumed is set by the instrumentation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 mInstrumentation.callActivityOnResume(this);
4489 if (!mCalled) {
4490 throw new SuperNotCalledException(
4491 "Activity " + mComponent.toShortString() +
4492 " did not call through to super.onResume()");
4493 }
4494
4495 // Now really resume, and install the current status bar and menu.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004496 mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004497
4498 mFragments.dispatchResume();
Dianne Hackborn445646c2010-06-25 15:52:59 -07004499 mFragments.execPendingActions();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 onPostResume();
4502 if (!mCalled) {
4503 throw new SuperNotCalledException(
4504 "Activity " + mComponent.toShortString() +
4505 " did not call through to super.onPostResume()");
4506 }
4507 }
4508
4509 final void performPause() {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004510 mFragments.dispatchPause();
Dianne Hackborne794e9f2010-08-24 12:32:10 -07004511 mCalled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 onPause();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004513 mResumed = false;
Dianne Hackborne794e9f2010-08-24 12:32:10 -07004514 if (!mCalled && getApplicationInfo().targetSdkVersion
4515 >= android.os.Build.VERSION_CODES.GINGERBREAD) {
4516 throw new SuperNotCalledException(
4517 "Activity " + mComponent.toShortString() +
4518 " did not call through to super.onPause()");
4519 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06004520 mResumed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 }
4522
4523 final void performUserLeaving() {
4524 onUserInteraction();
4525 onUserLeaveHint();
4526 }
4527
4528 final void performStop() {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004529 if (mLoadersStarted) {
4530 mLoadersStarted = false;
Dianne Hackborn2707d602010-07-09 18:01:20 -07004531 if (mLoaderManager != null) {
4532 if (!mChangingConfigurations) {
4533 mLoaderManager.doStop();
4534 } else {
4535 mLoaderManager.doRetain();
4536 }
4537 }
4538 }
4539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 if (!mStopped) {
4541 if (mWindow != null) {
4542 mWindow.closeAllPanels();
4543 }
4544
Dianne Hackbornce418e62011-03-01 14:31:38 -08004545 if (mToken != null && mParent == null) {
4546 WindowManagerImpl.getDefault().setStoppedState(mToken, true);
4547 }
4548
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004549 mFragments.dispatchStop();
4550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 mCalled = false;
4552 mInstrumentation.callActivityOnStop(this);
4553 if (!mCalled) {
4554 throw new SuperNotCalledException(
4555 "Activity " + mComponent.toShortString() +
4556 " did not call through to super.onStop()");
4557 }
4558
Makoto Onuki2f6a0182010-02-22 13:26:59 -08004559 synchronized (mManagedCursors) {
4560 final int N = mManagedCursors.size();
4561 for (int i=0; i<N; i++) {
4562 ManagedCursor mc = mManagedCursors.get(i);
4563 if (!mc.mReleased) {
4564 mc.mCursor.deactivate();
4565 mc.mReleased = true;
4566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 }
4568 }
4569
4570 mStopped = true;
4571 }
4572 mResumed = false;
4573 }
4574
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004575 final void performDestroy() {
Dianne Hackborn291905e2010-08-17 15:17:15 -07004576 mWindow.destroy();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004577 mFragments.dispatchDestroy();
4578 onDestroy();
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07004579 if (mLoaderManager != null) {
4580 mLoaderManager.doDestroy();
4581 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004582 }
4583
Jeff Hamilton52d32032011-01-08 15:31:26 -06004584 /**
4585 * @hide
4586 */
4587 public final boolean isResumed() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 return mResumed;
4589 }
4590
4591 void dispatchActivityResult(String who, int requestCode,
4592 int resultCode, Intent data) {
Joe Onorato43a17652011-04-06 19:22:23 -07004593 if (false) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
4595 + ", resCode=" + resultCode + ", data=" + data);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004596 mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 if (who == null) {
4598 onActivityResult(requestCode, resultCode, data);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07004599 } else {
4600 Fragment frag = mFragments.findFragmentByWho(who);
4601 if (frag != null) {
4602 frag.onActivityResult(requestCode, resultCode, data);
4603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 }
4605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004606}