1 page.title= 2 page.tags=activity lifecycle 3 helpoutsWidget=true 4 5 trainingnavtop=true 6 7 @jd:body 8 9 10 <div id="tb-wrapper"> 11 <div id="tb"> 12 13 <h2> </h2> 14 <ol> 15 <li><a href="#lifecycle-states"> </a></li> 16 <li><a href="#launching-activity"> </a></li> 17 <li><a href="#Create"> </a></li> 18 <li><a href="#Destroy"> </a></li> 19 </ol> 20 21 <h2> </h2> 22 <ul> 23 <li><a href="{@docRoot}guide/components/activities.html"></a></li> 24 </ul> 25 26 <h2></h2> 27 28 <div class="download-box"> 29 <a href="http://developer.android.com/shareables/training/ActivityLifecycle.zip" class="button"> </a> 30 <p class="filename">ActivityLifecycle.zip</p> 31 </div> 32 33 </div> 34 </div> 35 36 <p>{@code main()} , 37 Android {@link android.app.Activity} 38 39 . , 40 .</p> 41 42 <p> , 43 .</p> 44 45 46 47 <h2 id="lifecycle-states"> </h2> 48 49 <p> , 50 . , 51 . , 52 . 53 .</p> 54 55 <p> , 56 . , 57 (: ). , 58 ( ), 59 .</p> 60 61 62 <img src="{@docRoot}images/training/basics/basic-lifecycle.png" /> 63 <p class="img-caption"><strong> 1.</strong> 64 . 65 (Resumed) 66 . 67 (Paused) (Stopped) .</p> 68 69 70 <p> , 71 . , 72 . , 73 .</p> 74 <ul> 75 <li> 76 .</li> 77 <li> , 78 .</li> 79 <li> 80 .</li> 81 <li> , 82 .</li> 83 </ul> 84 85 <!-- 86 <p class="table-caption"><strong>Table 1.</strong> Activity lifecycle state pairs and callback 87 methods.</p> 88 <table> 89 <tr> 90 <th scope="col">Lifecycle State</th> 91 <th scope="col">Startup Method</th> 92 <th scope="col">Teardown Method</th> 93 </tr> 94 <tr> 95 <td>Created / Destroyed</td> 96 <td>{@link android.app.Activity#onCreate onCreate()}</td> 97 <td>{@link android.app.Activity#onDestroy()}</td> 98 </tr> 99 <tr> 100 <td>Started / Stopped</td> 101 <td>{@link android.app.Activity#onStart()}</td> 102 <td>{@link android.app.Activity#onStop()}</td> 103 </tr> 104 <tr> 105 <td>Resumed / Resumed</td> 106 <td>{@link android.app.Activity#onResume()}</td> 107 <td>{@link android.app.Activity#onPause()}</td> 108 </tr> 109 </table> 110 --> 111 112 <p> , 1 113 . , 114 . , 115 .</p> 116 <dl> 117 <dt>(Resumed)</dt> 118 <dd> 119 ( " " ).</dd> 120 <dt>(Paused)</dt> 121 <dd> . , 122 . 123 , . 124 <dt>(Stopped)</dt> 125 <dd> . 126 . 127 , .</dd> 128 </dl> 129 130 <p> ((Created) (Started)) , 131 . , 132 {@link android.app.Activity#onCreate onCreate()} {@link 133 android.app.Activity#onStart()} , {@link 134 android.app.Activity#onResume()} .</p> 135 136 <p> . , 137 .</p> 138 139 140 141 <h2 id="launching-activity"> </h2> 142 143 <p> , " "( "") {@link android.app.Activity} {@link 144 android.app.Activity#onCreate onCreate()} 145 . 146 .</p> 147 148 <p>Android <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">{@code AndroidManifest.xml}</a> . 149 .</p> 150 151 <p> {@link 152 android.content.Intent#ACTION_MAIN MAIN} {@link android.content.Intent#CATEGORY_LAUNCHER LAUNCHER} <a href="{@docRoot}guide/topics/manifest/intent-filter-element.html">{@code 153 <intent-filter>}</a> 154 . .</p> 155 156 <pre> 157 <activity android:name=".MainActivity" android:label="@string/app_name"> 158 <intent-filter> 159 <action android:name="android.intent.action.MAIN" /> 160 <category android:name="android.intent.category.LAUNCHER" /> 161 </intent-filter> 162 </activity> 163 </pre> 164 165 <p class="note"><strong>:</strong> Android SDK 166 Tools Android , 167 {@link android.app.Activity} .</p> 168 169 <p>{@link android.content.Intent#ACTION_MAIN MAIN} 170 {@link android.content.Intent#CATEGORY_LAUNCHER LAUNCHER} 171 , .</p> 172 173 174 175 <h2 id="Create"> </h2> 176 177 <p> . 178 , 179 , 180 {@link 181 android.app.Activity#onCreate onCreate()} {@link android.app.Activity} .</p> 182 183 <p> 184 {@link android.app.Activity#onCreate onCreate()} . 185 {@link android.app.Activity#onCreate onCreate()} 186 , .</p> 187 188 <p> {@link android.app.Activity#onCreate onCreate()} 189 190 (XML ), , 191 UI .</p> 192 193 <pre> 194 TextView mTextView; // Member variable for text view in the layout 195 196 @Override 197 public void onCreate(Bundle savedInstanceState) { 198 super.onCreate(savedInstanceState); 199 200 // Set the user interface layout for this Activity 201 // The layout file is defined in the project res/layout/main_activity.xml file 202 setContentView(R.layout.main_activity); 203 204 // Initialize member TextView so we can manipulate it later 205 mTextView = (TextView) findViewById(R.id.text_message); 206 207 // Make sure we're running on Honeycomb or higher to use ActionBar APIs 208 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 209 // For the main activity, make sure the app icon in the action bar 210 // does not behave as a button 211 ActionBar actionBar = getActionBar(); 212 actionBar.setHomeButtonEnabled(false); 213 } 214 } 215 </pre> 216 217 <p class="caution"><strong>:</strong> {@link android.os.Build.VERSION#SDK_INT} 218 API Android 2.0(API 219 5) . .</p> 220 221 <p>{@link android.app.Activity#onCreate onCreate()} 222 {@link android.app.Activity#onStart()} {@link android.app.Activity#onResume()} 223 . (Created) (Started) . 224 {@link android.app.Activity#onStart()} , 225 {@link android.app.Activity#onResume()} , (: , 226 , ) 227 (Resumed) .</p> 228 229 <p> {@link 230 android.app.Activity#onStart()} {@link android.app.Activity#onResume()} (Paused) (Stopped) 231 .</p> 232 233 <p class="note"><strong>:</strong> {@link android.app.Activity#onCreate onCreate()} 234 <code>savedInstanceState</code> . <a href="recreating.html"> </a> 235 .</p> 236 237 238 <img src="{@docRoot}images/training/basics/basic-lifecycle-create.png" /> 239 <p class="img-caption"><strong> 2.</strong> 240 {@link android.app.Activity#onCreate onCreate()}, {@link android.app.Activity#onStart()} {@link android.app.Activity#onResume()} 241 242 . 243 , (Resumed) . 244 .</p> 245 246 247 248 249 250 251 252 <h2 id="Destroy"> </h2> 253 254 <p> {@link android.app.Activity#onCreate 255 onCreate()}, {@link android.app.Activity#onDestroy}. 256 257 .</p> 258 259 <p> . 260 , {@link 261 android.app.Activity#onPause} {@link android.app.Activity#onStop} . 262 {@link 263 android.app.Activity#onCreate onCreate()} 264 , {@link 265 android.app.Activity#onDestroy} .</p> 266 267 <pre> 268 @Override 269 public void onDestroy() { 270 super.onDestroy(); // Always call the superclass 271 272 // Stop method tracing that the activity started during onCreate() 273 android.os.Debug.stopMethodTracing(); 274 } 275 </pre> 276 277 <p class="note"><strong>:</strong> , 278 {@link android.app.Activity#onCreate onCreate()} {@link 279 android.app.Activity#finish()} , {@link android.app.Activity#onPause} {@link 280 android.app.Activity#onStop} {@link android.app.Activity#onDestroy} 281 . (: 282 ), {@link 283 android.app.Activity#onCreate onCreate()} {@link android.app.Activity#finish()} . , 284 285 {@link android.app.Activity#onDestroy} .</p> 286