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