1 page.title=Cc hot ng 2 page.tags=hot ng, nh 3 @jd:body 4 5 <div id="qv-wrapper"> 6 <div id="qv"> 7 <h2>Trong ti liu ny</h2> 8 <ol> 9 <li><a href="#Creating">To mt Hot ng</a> 10 <ol> 11 <li><a href="#UI">Trin khai mt giao din ngi dng</a></li> 12 <li><a href="#Declaring">Khai bo hot ng trong bn k khai</a></li> 13 </ol> 14 </li> 15 <li><a href="#StartingAnActivity">Bt u mt Hot ng</a> 16 <ol> 17 <li><a href="#StartingAnActivityForResult">Bt u mt hot ng cho mt kt qu</a></li> 18 </ol> 19 </li> 20 <li><a href="#ShuttingDown">Tt mt Hot ng</a></li> 21 <li><a href="#Lifecycle">Qun l Vng i ca Hot ng</a> 22 <ol> 23 <li><a href="#ImplementingLifecycleCallbacks">Trin khai gi li vng i</a></li> 24 <li><a href="#SavingActivityState">Lu trng thi ca hot ng</a></li> 25 <li><a href="#ConfigurationChanges">X l thay i v cu hnh</a></li> 26 <li><a href="#CoordinatingActivities">iu phi hot ng</a></li> 27 </ol> 28 </li> 29 </ol> 30 31 <h2>Lp kha</h2> 32 <ol> 33 <li>{@link android.app.Activity}</li> 34 </ol> 35 36 <h2>Xem thm</h2> 37 <ol> 38 <li><a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tc v v Ngn 39 Xp</a></li> 40 </ol> 41 42 </div> 43 </div> 44 45 46 47 <p>{@link android.app.Activity} l mt thnh phn ng dng cung cp mt mn hnh m vi n 48 ngi dng c th tng tc thc hin mt iu g , chng hn nh quay s in thoi, chp nh, gi e-mail hoc 49 xem bn . Mi hot ng c cho trong mt ca s l ni v giao din ngi dng ca n. Ca s ny 50 thng lp y mn hnh, nhng c th nh hn mn hnh v ni bn trn cc ca s 51 khc.</p> 52 53 <p> ng dng thng bao gm nhiu hot ng c lin kt lng lo 54 vi nhau. Thng th mt hot ng trong mt ng dng s c quy nh l hot ng "chnh", n c 55 trnh by trc ngi dng khi khi chy ng dng ln u. Sau , mi 56 hot ng c th bt u mt hot ng khc thc hin cc hnh ng khc nhau. Mi khi mt hot ng 57 mi bt u, hot ng trc s b dng li, nhng h thng vn gi nguyn hot ng 58 trong mt ngn xp ("back stack"). Khi mt hot ng mi bt u, n c y ln ngn xp v 59 chim ly tiu im ca ngi dng. Ngn xp s tun theo c ch xp chng c bn "vo cui, ra u", 60 v th, khi ngi dng kt thc hot ng hin ti v nhn nt <em>Quay li</em>, n 61 s c y ra khi ngn xp (v b hy) v hot ng trc s tip tc. (Ngn xp c 62 cp k hn trong ti liu <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tc v 63 v Ngn Xp</a>.)</p> 64 65 <p>Khi mt hot ng b dng v mt hot ng mi bt u, n c thng bo v s thay i trng thi ny 66 qua cc phng php gi li vng i ca hot ng. 67 C mt vi phng php gi li vng i m mt hot ng c th nhn, do mt thay i v 68 trng thi ca n—d h thng ang to, dng hay tip tc n, hay hy n—v 69 mi ln gi li cho bn c hi thc hin cng vic c th 70 ph hp vi s thay i trng thi . V d, khi b dng, hot ng ca bn s gii phng mi 71 i tng ln, chng hn nh cc kt ni mng hoc c s d liu. Khi hot ng tip tc, bn c th 72 thu li nhng ti nguyn cn thit v tip tc nhng hnh ng b gin on. Nhng chuyn tip trng thi ny 73 u l mt phn ca vng i hot ng.</p> 74 75 <p>Phn cn li ca ti liu ny bn n nhng ni dung c bn v cch xy dng v s dng mt hot ng, 76 bao gm mt ni dung cp y v cch vn hnh ca vng i hot ng, bn c th qun l tt 77 s chuyn tip gia cc trng thi hot ng khc nhau.</p> 78 79 80 81 <h2 id="Creating">To mt Hot ng</h2> 82 83 <p> to mt hot ng, bn phi to mt lp con ca {@link android.app.Activity} (hoc 84 mt lp con hin ti ca n). Trong lp con ca mnh, bn cn trin khai cc phng php gi li m h thng 85 gi khi hot ng chuyn tip gia cc trng thi khc nhau trong vng i, chng hn nh khi 86 hot ng ang c to, dng, tip tc, hoc hy. Hai phng php gi li quan trng nht 87 l:</p> 88 89 <dl> 90 <dt>{@link android.app.Activity#onCreate onCreate()}</dt> 91 <dd>Bn phi trin khai phng php ny. H thng gi phng php ny khi to hot ng 92 ca bn. Trong qu trnh thc hin ca mnh, bn nn khi chy nhng thnh phn thit yu cho hot ng 93 ca mnh. 94 Quan trng nht, y l lc bn phi gi {@link android.app.Activity#setContentView 95 setContentView()} nh ngha b tr cho giao din ngi dng ca hot ng.</dd> 96 <dt>{@link android.app.Activity#onPause onPause()}</dt> 97 <dd>H thng gi phng php ny l du hiu u tin v vic ngi dng ang ri khi hot ng 98 ca bn (mc d khng phi lc no cng c ngha rng hot ng ang b hy). Trng hp ny thng l khi bn 99 nh thc hin bt k thay i no vn cn c hiu lc ngoi phin ca ngi dng hin thi (v 100 ngi dng c th khng quay li).</dd> 101 </dl> 102 103 <p>C mt vi phng php gi li vng i khc m bn nn s dng em n 104 mt tri nghim ngi dng mt m gia cc hot ng v x l nhng gin on bt ng khin hot ng ca bn 105 b dng v thm ch b hy. Tt c phng php gi li vng i c bn sau trong phn 106 ni v <a href="#Lifecycle">Qun l Vng i ca Hot ng</a>.</p> 107 108 109 110 <h3 id="UI">Trin khai mt giao din ngi dng</h3> 111 112 <p> Giao din ngi dng cho mt hot ng s c cung cp theo phn cp dng xem—i tng c suy ra 113 t lp {@link android.view.View}. Mi ch xem kim sot mt khng gian ch nht ring 114 trong ca s ca hot ng v c th phn hi trc tng tc ca ngi dng. V d, ch xem c th l 115 mt nt khi xng mt hnh ng khi ngi dng chm vo n.</p> 116 117 <p>Android cung cp nhiu ch xem sn c m bn c th s dng thit k v t chc cho b tr 118 ca mnh. "Widget" l nhng ch xem cung cp nhng phn t trc quan (v tng tc) cho mn hnh, chng hn nh 119 nt, trng vn bn, hp kim, hay ch l mt hnh nh. "B tr" l nhng ch xem c suy ra t {@link 120 android.view.ViewGroup} cung cp mt m hnh b tr duy nht cho cc ch xem con ca n, chng hn nh b tr 121 tuyn tnh, b tr li, hoc b tr tng i. Bn cng c th chia thnh lp con {@link android.view.View} v cc lp 122 {@link android.view.ViewGroup} (hoc cc lp con hin ti) to widget v 123 b tr ca chnh mnh v p dng chng vo b tr hot ng ca bn.</p> 124 125 <p>Cch ph bin nht nh ngha mt b tr bng cch s dng cc ch xem l dng mt tp b tr XML c lu trong ti nguyn ng dng 126 ca bn. Bng cch ny, bn c th duy tr thit k giao din ngi dng ca mnh c lp vi 127 m ngun nh ngha hnh vi ca hot ng. Bn c th t b tr lm UI cho hot ng 128 ca mnh bng {@link android.app.Activity#setContentView(int) setContentView()}, chuyn 129 ID ti nguyn cho b tr. Tuy nhin, bn cng c th to {@link android.view.View} mi trong m hot ng 130 ca mnh v xy dng mt cp bc ch xem bng cch chn cc {@link 131 android.view.View} mi vo mt {@link android.view.ViewGroup}, sau s dng b tr bng cch chuyn root 132 {@link android.view.ViewGroup} sang {@link android.app.Activity#setContentView(View) 133 setContentView()}.</p> 134 135 <p> bit thng tin v vic to mt giao din ngi dng, hy xem ti liu <a href="{@docRoot}guide/topics/ui/index.html">Giao din Ngi dng</a>.</p> 136 137 138 139 <h3 id="Declaring">Khai bo hot ng trong bn k khai</h3> 140 141 <p>Bn phi khai bo hot ng ca mnh trong tp bn k khai hot ng 142 c th truy cp c vo h thng. khai bo hot ng ca mnh, hy m tp bn k khai ca bn v thm mt phn t <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> 143 lm con ca phn t <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code <application>}</a> 144 . V d:</p> 145 146 <pre> 147 <manifest ... > 148 <application ... > 149 <activity android:name=".ExampleActivity" /> 150 ... 151 </application ... > 152 ... 153 </manifest > 154 </pre> 155 156 <p>C vi thuc tnh khc m bn c th nu trong phn t ny, nh ngha cc thuc tnh 157 nh nhn cho hot ng, biu tng cho hot ng, hoc ch m t kiu UI ca 158 hot ng. Thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html#nm">{@code android:name}</a> 159 l thuc tnh bt buc duy nht—n quy nh tn lp ca hot ng. Mt khi 160 bn pht hnh ng dng ca mnh, bn khng nn thay i tn ny, v nu bn lm vy, bn c th lm hng 161 mt s tnh nng, chng hn nh cc li tt ca ng dng (hy c bi ng trn blog, <a href="http://android-developers.blogspot.com/2011/06/things-that-cannot-change.html">Nhng iu 162 Khng Thay i c</a>).</p> 163 164 <p>Xem ti liu tham kho phn t <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> 165 bit thm thng tin v vic khai bo hot ng ca bn trong bn k khai.</p> 166 167 168 <h4>S dng cc b lc nh</h4> 169 170 <p>Mt phn t <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code 171 <activity>}</a> cng c th quy nh cc b lc nh khc nhau—bng cch s dng phn t <a href="{@docRoot}guide/topics/manifest/intent-filter-element.html">{@code 172 <intent-filter>}</a> — khai bo cch thc m cc thnh phn khc ca ng dng c th 173 kch hot n.</p> 174 175 <p>Khi bn to mt ng dng mi bng cch s dng cc cng c SDK ca Android, hot ng chng trnh nh 176 c to cho bn s t ng bao gm mt b lc nh khai bo hot ng 177 phn hi li hnh ng "chnh" v nn c t trong th loi "trnh khi chy". B lc nh 178 trng nh th ny:</p> 179 180 <pre> 181 <activity android:name=".ExampleActivity" android:icon="@drawable/app_icon"> 182 <intent-filter> 183 <action android:name="android.intent.action.MAIN" /> 184 <category android:name="android.intent.category.LAUNCHER" /> 185 </intent-filter> 186 </activity> 187 </pre> 188 189 <p>Phn t <a href="{@docRoot}guide/topics/manifest/action-element.html">{@code 190 <action>}</a> quy nh rng y l im mc nhp "chnh" i vi ng dng. Phn t <a href="{@docRoot}guide/topics/manifest/category-element.html">{@code 191 <category>}</a> quy nh rng hot ng ny nn c lit k trong trnh khi chy ng dng ca h thng 192 ( cho php ngi dng khi chy hot ng ny).</p> 193 194 <p>Nu bn c nh cho ng dng ca mnh c c lp v khng cho php cc ng dng khc 195 kch hot cc hot ng ca n, vy bn khng cn bt k b lc nh no khc. Ch mt hot ng nn c 196 hnh ng "chnh" v th loi "trnh khi chy" nh trong v d trc. Nhng hot ng m 197 bn khng mun cung cp sn cho cc ng dng khc khng nn c b lc nh v bn c th 198 t mnh bt u chng bng cch s dng cc nh r rng (nh c cp trong phn sau).</p> 199 200 <p>Tuy nhin, nu bn mun hot ng ca mnh phn hi li nhng nh ngm m c chuyn giao t 201 cc ng dng khc (v chnh bn), th bn phi nh ngha cc b lc nh b sung cho hot ng 202 ca mnh. Vi mi loi nh m bn mun phn hi, bn phi nu mt <a href="{@docRoot}guide/topics/manifest/intent-filter-element.html">{@code 203 <intent-filter>}</a> bao gm mt phn t 204 <a href="{@docRoot}guide/topics/manifest/action-element.html">{@code 205 <action>}</a> v, khng bt buc, mt phn t <a href="{@docRoot}guide/topics/manifest/category-element.html">{@code 206 <category>}</a> v/hoc mt phn t <a href="{@docRoot}guide/topics/manifest/data-element.html">{@code 207 <data>}</a>. Nhng phn t ny quy nh loi nh m hot ng ca bn c th 208 phn hi.</p> 209 210 <p> bit thm thng tin v cch thc cc hot ng ca bn c th phn hi li nh, hy xem ti liu <a href="{@docRoot}guide/components/intents-filters.html"> nh v B lc nh</a> 211 .</p> 212 213 214 215 <h2 id="StartingAnActivity">Bt u mt Hot ng</h2> 216 217 <p>Bn c th bt u mt hot ng khc bng cch gi {@link android.app.Activity#startActivity 218 startActivity()}, chuyn cho n mt {@link android.content.Intent} m m t hot ng bn 219 mun bt u. nh ny s quy nh hoc hot ng chnh xc m bn mun bt u hoc m t 220 loi hnh ng m bn mun thc hin (v h thng la chn hot ng ph hp cho bn, 221 thm ch 222 c th t mt ng dng khc). Mt nh cng c th mang theo lng nh d liu s c 223 s dng bi hot ng c bt u.</p> 224 225 <p>Khi ang lm vic trong ng dng ca chnh mnh, bn thng s cn khi chy mt hot ng bit. 226 Bn c th lm vy bng cch to mt nh trong quy nh r hot ng bn mun bt u, 227 s dng tn lp . V d, sau y l cch mt hot ng bt u mt hot ng khc c tn {@code 228 SignInActivity}:</p> 229 230 <pre> 231 Intent intent = new Intent(this, SignInActivity.class); 232 startActivity(intent); 233 </pre> 234 235 <p>Tuy nhin, ng dng ca bn cng c th mun thc hin mt s hnh ng, chng hn nh gi mt e-mail, tin nhn 236 vn bn, hoc cp nht trng thi, bng cch s dng d liu t hot ng ca bn. Trong trng hp ny, ng dng ca bn c th 237 khng c cc hot ng ca chnh n thc hin nhng hnh ng , v vy, thay vo , bn c th tn dng nhng hot ng 238 c cung cp bi cc ng dng khc trn thit b m c th thc hin hnh ng cho bn. y l lc 239 nh thc s c gi tr—bn c th to mt nh m t mt hnh ng bn mun 240 thc hin v h thng 241 s khi chy hot ng ph hp t mt ng dng khc. Nu c 242 nhiu hot ng m c th x l nh, vy ngi dng c th chn hot ng no s s dng. V 243 d, nu bn mun cho php ngi dng gi e-mail, bn c th to 244 nh sau:</p> 245 246 <pre> 247 Intent intent = new Intent(Intent.ACTION_SEND); 248 intent.putExtra(Intent.EXTRA_EMAIL, recipientArray); 249 startActivity(intent); 250 </pre> 251 252 <p>{@link android.content.Intent#EXTRA_EMAIL} ph c thm vo nh l mt mng xu ca 253 cc a ch e-mail m e-mail s c gi ti. Khi mt ng dng e-mail phn hi 254 nh ny, n c mng xu c cung cp trong phn ph v t n vo trng "n" ca mu son tho 255 e-mail. Trong trng hp ny, hot ng ca ng dng e-mail bt u v khi ngi dng 256 lm xong, hot ng ca bn s tip tc.</p> 257 258 259 260 261 <h3 id="StartingAnActivityForResult">Bt u mt hot ng cho mt kt qu</h3> 262 263 <p>i khi bn c th mun nhn c mt kt qu t hot ng m bn bt u. Trong trng hp , 264 hy bt u hot ng bng cch gi {@link android.app.Activity#startActivityForResult 265 startActivityForResult()} (thay v {@link android.app.Activity#startActivity 266 startActivity()}). Ri nhn c kt qu t hot ng 267 sau , hy trin khai phng php gi li {@link android.app.Activity#onActivityResult onActivityResult()} 268 . Khi hot ng sau din ra xong, n tr v mt kt qu trong mt {@link 269 android.content.Intent} cho phng php {@link android.app.Activity#onActivityResult onActivityResult()} 270 ca bn.</p> 271 272 <p>V d, bn c th mun ngi dng chn mt trong cc lin lc ca h, v vy hot ng ca bn c th 273 lm g vi thng tin trong lin lc . y l cch bn c th to mt nh nh vy v 274 x l kt qu:</p> 275 276 <pre> 277 private void pickContact() { 278 // Create an intent to "pick" a contact, as defined by the content provider URI 279 Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); 280 startActivityForResult(intent, PICK_CONTACT_REQUEST); 281 } 282 283 @Override 284 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 285 // If the request went well (OK) and the request was PICK_CONTACT_REQUEST 286 if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) { 287 // Perform a query to the contact's content provider for the contact's name 288 Cursor cursor = getContentResolver().query(data.getData(), 289 new String[] {Contacts.DISPLAY_NAME}, null, null, null); 290 if (cursor.moveToFirst()) { // True if the cursor is not empty 291 int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME); 292 String name = cursor.getString(columnIndex); 293 // Do something with the selected contact's name... 294 } 295 } 296 } 297 </pre> 298 299 <p>V d ny th hin l-gic c bn m bn s s dng trong phng php {@link 300 android.app.Activity#onActivityResult onActivityResult()} ca mnh x l mt 301 kt qu hot ng. iu kin u tin kim tra xem yu cu c thnh cng khng—nu c th 302 {@code resultCode} s l {@link android.app.Activity#RESULT_OK}—v liu yu cu 303 m kim tra ny ang phn hi c c bit hay khng—trong trng hp ny, {@code requestCode} ph hp vi 304 tham s th hai c gi bng {@link android.app.Activity#startActivityForResult 305 startActivityForResult()}. T , m x l kt qu hot ng bng cch truy vn 306 d liu c tr v trong {@link android.content.Intent} (tham s {@code data}).</p> 307 308 <p>iu xy ra l, {@link 309 android.content.ContentResolver} s thc hin mt truy vn i vi nha cung cp ni dung, truy vn ny tr v mt 310 {@link android.database.Cursor} cho php c d liu c truy vn. bit thm thng tin, hy xem ti liu 311 <a href="{@docRoot}guide/topics/providers/content-providers.html">Trnh cung cp Ni dung</a>.</p> 312 313 <p> bit thm thng tin v vic s dng nh, hy xem ti liu <a href="{@docRoot}guide/components/intents-filters.html"> nh v B lc 314 nh</a>.</p> 315 316 317 <h2 id="ShuttingDown">Tt mt Hot ng</h2> 318 319 <p>Bn c th tt mt hot ng bng cch gi phng php {@link android.app.Activity#finish 320 finish()} ca n. Bn cng c th tt mt hot ng ring m trc bn bt u bng cch gi 321 {@link android.app.Activity#finishActivity finishActivity()}.</p> 322 323 <p class="note"><strong>Lu :</strong> Trong hu ht trng hp, bn khng nn kt thc mt hot ng mt cch r rng 324 bng cch s dng nhng phng php ny. Nh cp trong phn sau v vng i ca hot ng, h thng 325 Android qun l tui th ca mt hot ng cho bn, v vy bn khng cn kt thc cc hot ng 326 ca chnh mnh. Vic gi nhng phng php ny c th nh hng tiu cc ti tri nghim ngi dng 327 k vng v ch nn c s dng khi bn tuyt i khng mun ngi dng quay li thc th ny ca 328 hot ng.</p> 329 330 331 <h2 id="Lifecycle">Qun l Vng i ca Hot ng</h2> 332 333 <p>Vic qun l vng i cc hot ng ca bn bng cch trin khai cc phng php gi li 334 rt quan trng i vi vic xy dng mt ng dng mnh 335 v linh hot. Vng i ca mt hot ng trc tip b nh hng bi s lin kt gia n vi 336 cc hot ng khc, tc v ca n v ngn xp (back stack).</p> 337 338 <p>V c bn, mt hot ng c th tn ti ba trng thi:</p> 339 340 <dl> 341 <dt><i>Tip tc</i></dt> 342 <dd>Hot ng tin cnh ca mn hnh v c tiu im ca ngi dng. (Trng thi ny 343 i khi cng c gi l "ang chy".)</dd> 344 345 <dt><i>Tm dng</i></dt> 346 <dd>Mt hot ng khc tin cnh v c tiu im, nhng hot ng ny vn hin th. C th, 347 mt hot ng khc hin th trn hot ng ny v hot ng trong sut mt phn hoc khng 348 che ton b mn hnh. Trng thi tm dng hon ton ang hot ng (i tng {@link android.app.Activity} 349 c gi li trong b nh, n duy tr tt c thng tin v trng thi v thnh vin, v vn gn vi 350 trnh qun l ca s), nhng c th b h thng tt b trong trng hp b nh cc k thp.</dd> 351 352 <dt><i>Dng</i></dt> 353 <dd>Hot ng b che khut hon ton bi mt hot ng khc (hot ng hin ang 354 di nn"). Hot ng dng cng vn ang hot ng ({@link android.app.Activity} 355 i tng c gi li trong b nh, n duy tr tt c thng tin v trng thi v thnh vin, nhng <em>khng</em> 356 gn vi trnh qun l ca s). Tuy nhin, hot ng khng cn hin th vi ngi dng na v h thng 357 c th tt b hot ng ny khi cn b nh ni khc.</dd> 358 </dl> 359 360 <p>Nu mt hot ng b tm dng hoc dng, h thng c th b n khi b nh hoc bng cch yu cu n 361 kt thc (gi phng php {@link android.app.Activity#finish finish()} ca n), hoc n gin l tt b tin trnh 362 ca hot ng. Khi hot ng c m li (sau khi b kt thc hoc tt b), n phi c to 363 li hon ton.</p> 364 365 366 367 <h3 id="ImplementingLifecycleCallbacks">Trin khai gi li vng i</h3> 368 369 <p>Khi mt hot ng chuyn tip vo ra cc trng thi khc nhau nu trn, n c thng bo 370 thng qua cc phng php gi li. Tt c phng php gi li u l nhng mc (hook) m bn 371 c th khng ch lm cng vic ph hp khi trng thi hot ng ca bn thay i. Hot ng khung sau 372 bao gm tng phng php trong cc phng php vng i c bn:</p> 373 374 375 <pre> 376 public class ExampleActivity extends Activity { 377 @Override 378 public void {@link android.app.Activity#onCreate onCreate}(Bundle savedInstanceState) { 379 super.onCreate(savedInstanceState); 380 // The activity is being created. 381 } 382 @Override 383 protected void {@link android.app.Activity#onStart onStart()} { 384 super.onStart(); 385 // The activity is about to become visible. 386 } 387 @Override 388 protected void {@link android.app.Activity#onResume onResume()} { 389 super.onResume(); 390 // The activity has become visible (it is now "resumed"). 391 } 392 @Override 393 protected void {@link android.app.Activity#onPause onPause()} { 394 super.onPause(); 395 // Another activity is taking focus (this activity is about to be "paused"). 396 } 397 @Override 398 protected void {@link android.app.Activity#onStop onStop()} { 399 super.onStop(); 400 // The activity is no longer visible (it is now "stopped") 401 } 402 @Override 403 protected void {@link android.app.Activity#onDestroy onDestroy()} { 404 super.onDestroy(); 405 // The activity is about to be destroyed. 406 } 407 } 408 </pre> 409 410 <p class="note"><strong>Lu :</strong> Vic bn trin khai nhng phng php vng i ny phi lun 411 gi trin khai siu lp trc khi lm bt k cng vic no, nh minh ha trong cc v d bn trn.</p> 412 413 <p>Cng nhau, nhng phng php ny nh ngha ton b vng i ca mt hot ng. Bng vic trin khai nhng phng php 414 ny, bn c th theo di ba vng lp lng nhau trong vng i ca hot ng: </p> 415 416 <ul> 417 <li><b>Ton b vng i</b> ca mt hot ng s xy ra t thi im lnh gi n {@link 418 android.app.Activity#onCreate onCreate()} cho ti thi im lnh gi n {@link 419 android.app.Activity#onDestroy}. Hot ng ca bn nn thc hin thit lp 420 trng thi "chung" (chng hn nh nh ngha b tr) trong {@link android.app.Activity#onCreate onCreate()}, v 421 gii phng tt c ti nguyn cn li trong {@link android.app.Activity#onDestroy}. V d, nu hot ng ca bn 422 c mt lung ang chy ngm ti xung d liu t mng, n c th to 423 lung trong {@link android.app.Activity#onCreate onCreate()} ri dng lung trong {@link 424 android.app.Activity#onDestroy}.</li> 425 426 <li><p><b>Vng i hin th</b> ca mt hot ng xy ra t thi im lnh gi n {@link 427 android.app.Activity#onStart onStart()} cho ti lnh gi n {@link 428 android.app.Activity#onStop onStop()}. Trong thi gian ny, ngi dng c th thy hot ng 429 trn mn hnh v tng tc vi n. V d, {@link android.app.Activity#onStop onStop()} c gi 430 khi mt hot ng mi bt u v khng cn hin th na. Gia hai phng php ny, bn c th 431 duy tr cc ti nguyn cn cho ngi dng thy hot ng. V d, bn c th ng k mt 432 {@link android.content.BroadcastReceiver} trong {@link 433 android.app.Activity#onStart onStart()} theo di cc thay i tc ng ti UI ca mnh, v b ng k 434 n trong {@link android.app.Activity#onStop onStop()} khi ngi dng khng cn thy th bn ang 435 hin th na. H thng c th gi {@link android.app.Activity#onStart onStart()} v {@link 436 android.app.Activity#onStop onStop()} nhiu ln trong sut vng i ca hot ng, khi 437 hot ng lun chuyn gia trng thi hin th v n vi ngi dng.</p></li> 438 439 <li><p><b>Vng i tin cnh</b> ca mt hot ng xy ra t thi im lnh gi n {@link 440 android.app.Activity#onResume onResume()} cho ti thi im lnh gi n {@link android.app.Activity#onPause 441 onPause()}. Trong thi gian ny, hot ng s pha trc tt c hot ng khc trn mn hnh v c 442 tiu im u vo ca ngi dng. Hot ng c th thng xuyn chuyn tip vo v ra tin cnh—v 443 d, {@link android.app.Activity#onPause onPause()} c gi khi thit b vo trng thi ng hoc 444 khi mt hp thoi xut hin. V trng thi ny c th chuyn tip thng xuyn, m trong hai phng php ny nn 445 tng i nh trnh chuyn tip chm khin ngi dng phi i.</p></li> 446 </ul> 447 448 <p>Hnh 1 minh ha nhng vng lp ny v cc ng dn m mt hot ng c th din ra gia cc trng thi. 449 Hnh ch nht i din cho cc phng php gi li bn c th trin khai thc hin thao tc khi 450 hot ng chuyn tip gia nhng trng thi ny. <p> 451 452 <img src="{@docRoot}images/activity_lifecycle.png" alt="" /> 453 <p class="img-caption"><strong>Hnh 1.</strong> Vng i ca hot ng.</p> 454 455 <p>Nhng phng php gi li vng i ny cng c lit k trong bng 1, trong m t tng phng php 456 gi li mt cch chi tit hn v xc nh tng phng php 457 trong vng i tng th ca hot ng, bao gm vic h thng c th tt b hot ng hay khng sau khi 458 phng php gi li hon tt.</p> 459 460 <p class="table-caption"><strong>Bng 1.</strong> Tm tt cc phng php gi li 461 trong vng i ca hot ng.</p> 462 463 <table border="2" width="85%" frame="hsides" rules="rows"> 464 <colgroup align="left" span="3"></colgroup> 465 <colgroup align="left"></colgroup> 466 <colgroup align="center"></colgroup> 467 <colgroup align="center"></colgroup> 468 469 <thead> 470 <tr><th colspan="3">Phng php</th> <th>M t</th> <th>C th tt b sau?</th> <th>Tip theo</th></tr> 471 </thead> 472 473 <tbody> 474 <tr> 475 <td colspan="3" align="left"><code>{@link android.app.Activity#onCreate onCreate()}</code></td> 476 <td>c gi khi hot ng mi c to. 477 y l lc bn nn thc hin tt c thit lp c nh thng thng ca mnh — 478 to ch xem, kt ghp d liu vi danh sch, v.v. Phng php ny c chuyn cho 479 mt i tng Gi cha trng thi trc y ca hot ng, nu trng thi 480 c thu li (xem phn <a href="#actstate">Lu Trng thi Hot ng</a>, 481 on sau). 482 <p>Lun c theo sau bi {@code onStart()}.</p></td> 483 <td align="center">Khng</td> 484 <td align="center">{@code onStart()}</td> 485 </tr> 486 487 <tr> 488 <td rowspan="5" style="border-left: none; border-right: none;"> </td> 489 <td colspan="2" align="left"><code>{@link android.app.Activity#onRestart 490 onRestart()}</code></td> 491 <td>c gi sau khi hot ng c dng, ngay trc khi hot ng c 492 bt u li. 493 <p>Lun c theo sau bi {@code onStart()}</p></td> 494 <td align="center">Khng</td> 495 <td align="center">{@code onStart()}</td> 496 </tr> 497 498 <tr> 499 <td colspan="2" align="left"><code>{@link android.app.Activity#onStart onStart()}</code></td> 500 <td>c gi ngay trc khi hot ng hin th trc ngi dng. 501 <p>c theo sau bi {@code onResume()} nu hot ng vo 502 tin cnh, hoc {@code onStop()} nu hot ng b n.</p></td> 503 <td align="center">Khng</td> 504 <td align="center">{@code onResume()} <br/>hoc<br/> {@code onStop()}</td> 505 </tr> 506 507 <tr> 508 <td rowspan="2" style="border-left: none;"> </td> 509 <td align="left"><code>{@link android.app.Activity#onResume onResume()}</code></td> 510 <td>c gi ngay trc khi hot ng bt u 511 tng tc vi ngi dng. Ti im ny, hot ng nm 512 trn cng ca chng hot ng, trong mc nhp ca ngi dng s n hot ng ny. 513 <p>Lun c theo sau bi {@code onPause()}.</p></td> 514 <td align="center">Khng</td> 515 <td align="center">{@code onPause()}</td> 516 </tr> 517 518 <tr> 519 <td align="left"><code>{@link android.app.Activity#onPause onPause()}</code></td> 520 <td>c gi khi h thng sp bt u tip tc mt hot ng 521 khc. Phng php ny thng c s dng thc hin cc thay i cha lu cho 522 d liu lin tc, dng cc hot nh v nhng vic khc m c th tiu tn cng sut 523 CPU, v.v. N s thc hin rt nhanh, v 524 hot ng tip theo s khng c tip tc ti khi n tr li. 525 <p>c theo sau hoc bi {@code onResume()} nu hot ng 526 tr li pha trc, hoc bi {@code onStop()} nu n 527 khng hin th vi ngi dng.</td> 528 <td align="center"><strong style="color:#800000">C</strong></td> 529 <td align="center">{@code onResume()} <br/>hoc<br/> {@code onStop()}</td> 530 </tr> 531 532 <tr> 533 <td colspan="2" align="left"><code>{@link android.app.Activity#onStop onStop()}</code></td> 534 <td>c gi khi hot ng khng cn hin th vi ngi dng. iu ny 535 c th xy ra v n ang b hy, hoc v mt hot ng khc 536 (ang tn ti hoc mi) c tip tc v ang che khut n. 537 <p>c theo sau hoc bi {@code onRestart()} nu 538 hot ng ang quay li tng tc vi ngi dng, hoc bi 539 {@code onDestroy()} nu hot ng ny s i mt.</p></td> 540 <td align="center"><strong style="color:#800000">C</strong></td> 541 <td align="center">{@code onRestart()} <br/>hoc<br/> {@code onDestroy()}</td> 542 </tr> 543 544 <tr> 545 <td colspan="3" align="left"><code>{@link android.app.Activity#onDestroy 546 onDestroy()}</code></td> 547 <td>c gi trc khi hot ng b hy. y l ln gi cui cng 548 m hot ng s nhn c. Nn gi n hoc v 549 hot ng ang kt thc (ai gi <code>{@link android.app.Activity#finish 550 finish()}</code> trn n), hoc v h thng ang tm thi hy thc th ny ca 551 hot ng tit kim b nh trng. Bn c th phn bit 552 nhng nhng kch bn ny bng phng php <code>{@link 553 android.app.Activity#isFinishing isFinishing()}</code>.</td> 554 <td align="center"><strong style="color:#800000">C</strong></td> 555 <td align="center"><em>khng c g</em></td> 556 </tr> 557 </tbody> 558 </table> 559 560 <p>Ct ghi "C th tt b sau?" cho bit liu h thng c th 561 tt b tin trnh ang lu tr hot ng vo bt c lc no <em>sau khi phng php tr v</em>, m khng 562 thc hin mt dng m khc ca hot ng hay khng. Ba phng php c ghi l "c": ({@link 563 android.app.Activity#onPause 564 onPause()}, {@link android.app.Activity#onStop onStop()}, v {@link android.app.Activity#onDestroy 565 onDestroy()}). V {@link android.app.Activity#onPause onPause()} l phng php u tin 566 trong ba phng php, sau khi hot ng c to, {@link android.app.Activity#onPause onPause()} l 567 phng php cui cng c bo m s c gi trc khi tin trnh <em>c th</em> b tt b—nu 568 h thng phi khi phc b nh trong mt tnh hung khn cp, khi {@link 569 android.app.Activity#onStop onStop()} v {@link android.app.Activity#onDestroy onDestroy()} c th 570 khng c gi. V th, bn nn s dng {@link android.app.Activity#onPause onPause()} ghi 571 d liu c nh quan trng (chng hn nh nhng chnh sa ca ngi dng) vo thit b lu tr. Tuy nhin, bn nn chn lc 572 thng tin no phi c gi li trong {@link android.app.Activity#onPause onPause()}, v bt k 573 th tc chn no trong phng php ny cng chn chuyn tip sang hot ng k tip v lm chm tri nghim 574 ca ngi dng.</p> 575 576 <p> Nhng phng php c ghi "Khng" trong ct <b>C th tt b</b> s bo v tin trnh ang lu tr 577 hot ng khi b tt b t thi im chng c gi. V th, mt hot ng c th tt b c 578 t thi im {@link android.app.Activity#onPause onPause()} tr v ti thi im 579 {@link android.app.Activity#onResume onResume()} s c gi. N s khng th li tt b c ti khi 580 {@link android.app.Activity#onPause onPause()} li c gi v tr v. </p> 581 582 <p class="note"><strong>Lu :</strong> Mt hot ng m khng th "tt b c" v mt k thut bi 583 nh ngha ny trong bng 1 vn c th b h thng tt b—nhng iu ch xy ra trong 584 nhng hon cnh cc oan khi khng cn gii php no khc. Thi im mt hot ng c th b tt b c 585 cp k hn trong ti liu <a href="{@docRoot}guide/components/processes-and-threads.html">Tin trnh v 586 Lung</a>.</p> 587 588 589 <h3 id="SavingActivityState">Lu trng thi ca hot ng</h3> 590 591 <p>Phn gii thiu v <a href="#Lifecycle">Qun l Vng i ca Hot ng</a> c cp s qua 592 rng 593 khi mt hot ng b tm dng hoc dng, trng thi ca hot ng s c gi li. iu ny ng v 594 i tng {@link android.app.Activity} vn c gi trong b nh khi n b tm dng hoc 595 dng—tt c thng tin v cc thnh vin v trng thi hin ti ca n vn hot ng. V th, bt k thay i no 596 m ngi dng thc hin trong hot ng u c gi li sao cho khi hot ng tr v 597 tin cnh (khi n "tip tc"), th nhng thay i ny vn cn .</p> 598 599 <p>Tuy nhin, khi h thng hy mt hot ng khi phc b nh, i tng {@link 600 android.app.Activity} b hy, v vy h thng khng th n thun tip tc hot ng vi trng thi 601 khng b nh hng. Thay vo , h thng phi to li i tng {@link android.app.Activity} nu ngi dng 602 iu hng tr li n. Tuy vy, ngi dng khng bit 603 rng h thng hy hot ng v to li n v, v th, c th 604 cho rng hot ng s vn nguyn nh c. Trong tnh hung ny, bn c th m bo rng 605 thng tin quan trng v trng thi ca hot ng c gi nguyn bng cch trin khai mt phng php gi li 606 b sung cho php bn lu thng tin v trng thi ca hot ng ca mnh: {@link 607 android.app.Activity#onSaveInstanceState onSaveInstanceState()}.</p> 608 609 <p>H thng gi {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} 610 trc khi khin hot ng d b hy. H thng chuyn cho phng php ny 611 mt {@link android.os.Bundle} trong bn c th lu 612 thng tin trng thi v hot ng nh cp tn gi tr, bng cch s dng cc phng php nh {@link 613 android.os.Bundle#putString putString()} v {@link 614 android.os.Bundle#putInt putInt()}. Sau , nu h thng tt b tin trnh ng dng ca bn 615 v ngi dng iu hng tr li hot ng ca bn, h thng s to li hot ng v 616 chuyn {@link android.os.Bundle} cho c {@link android.app.Activity#onCreate onCreate()} v {@link 617 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()}. S dng mt trong 618 hai phng php ny, bn c th trch xut trng thi lu ca mnh t {@link android.os.Bundle} v khi phc 619 trng thi ca hot ng. Nu khng c thng tin trng thi khi phc, khi {@link 620 android.os.Bundle} c chuyn cho bn s rng (l trng hp khi hot ng c to 621 ln u).</p> 622 623 <img src="{@docRoot}images/fundamentals/restore_instance.png" alt="" /> 624 <p class="img-caption"><strong>Hnh 2.</strong> Hai cch m theo mt hot ng tr v tiu im 625 ca ngi dng vi trng thi khng thay i: hoc hot ng b hy, ri to li v hot ng phi khi phc 626 trng thi lu trc , hoc hot ng b dng, ri tip tc v trng thi ca hot ng 627 gi nguyn khng i.</p> 628 629 <p class="note"><strong>Lu :</strong> Khng c g bo m rng {@link 630 android.app.Activity#onSaveInstanceState onSaveInstanceState()} s c gi trc khi hot ng 631 ca bn b hy, v c nhng trng hp m s khng cn lu trng thi 632 (chng hn nh khi ngi dng ri b hot ng ca bn bng cch s dng nt <em>Quay li</em>, v ngi dng 633 r rng 634 ang ng hot ng). Nu h thng gi {@link android.app.Activity#onSaveInstanceState 635 onSaveInstanceState()}, n lm vy trc {@link 636 android.app.Activity#onStop onStop()} v c th trc c {@link android.app.Activity#onPause 637 onPause()}.</p> 638 639 <p>Tuy nhin, ngay c khi bn khng lm g v khng trin khai {@link 640 android.app.Activity#onSaveInstanceState onSaveInstanceState()}, mt phn trng thi ca hot ng c khi phc 641 bi vic lp {@link android.app.Activity} trin khai mc nh {@link 642 android.app.Activity#onSaveInstanceState onSaveInstanceState()}. C th, trin khai 643 mc nh s gi phng php {@link 644 android.view.View#onSaveInstanceState onSaveInstanceState()} tng ng cho mi {@link 645 android.view.View} trong b tr, n cho php mi ch xem cung cp thng tin v chnh n 646 m s c lu. Gn nh mi widget trong khun kh Android u trin khai phng php ny nu 647 ph hp, sao cho mi thay i hin th i vi UI u t ng c lu v khi phc khi hot ng 648 ca bn c to li. V d, widget {@link android.widget.EditText} lu mi vn bn 649 do ngi dng in vo v widget {@link android.widget.CheckBox} lu s thng tin cho d c kim tra 650 hay cha. Vic duy nht bn cn lm l cung cp mt ID duy nht (vi thuc tnh <a href="{@docRoot}guide/topics/resources/layout-resource.html#idvalue">{@code android:id}</a> 651 ) cho mi widget bn mun lu trng thi ca n. Nu mt widget khng c ID th h thng 652 khng th lu trng thi ca n.</p> 653 654 <div class="sidebox-wrapper"> 655 <div class="sidebox"> 656 <p>Bn cng c th r rng dng mt ch xem trong b tr ca mnh khi vic lu trng thi ca n bng cch t thuc tnh 657 {@link android.R.attr#saveEnabled android:saveEnabled} thnh {@code "false"} hoc bng cch gi 658 phng php {@link android.view.View#setSaveEnabled setSaveEnabled()}. Thng th bn khng nn 659 v hiu ha iu ny, nhng c th lm nu bn mun khi phc trng thi ca UI hot ng khc i.</p> 660 </div> 661 </div> 662 663 <p>Mc d vic trin khai mc nh {@link 664 android.app.Activity#onSaveInstanceState onSaveInstanceState()} lu thng tin hu ch v 665 UI hot ng ca bn, bn c th vn cn khng ch n lu thm thng tin. 666 V d, bn c th cn lu cc gi tr thnh vin thay i trong vng i ca hot ng (m 667 c th tng quan vi cc gi tr c khi phc trong UI, nhng cc thnh vin nm gi gi tr UI khng c 668 khi phc theo mc nh).</p> 669 670 <p>V vic trin khai mc nh {@link 671 android.app.Activity#onSaveInstanceState onSaveInstanceState()} gip lu trng thi ca UI, nu 672 bn khng ch phng php lu thm thng tin trng thi, bn nn lun lun gi 673 trin khai siu lp ca {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} 674 trc khi thc hin bt k cng vic no. Tng t, bn cng nn gi trin khai siu lp {@link 675 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} nu bn khng ch n, 676 trin khai mc nh c th khi phc cc trng thi xem.</p> 677 678 <p class="note"><strong>Lu :</strong> V {@link android.app.Activity#onSaveInstanceState 679 onSaveInstanceState()} khng m bo 680 s c gi, bn ch nn s dng n ghi trng thi giao thi ca hot ng (trng thi ca 681 UI)—bn khng nn s dng n lu gi d liu lin tc. Thay vo , bn nn s dng {@link 682 android.app.Activity#onPause onPause()} lu gi d liu lin tc (chng hn nh d liu m nn c lu 683 vo mt c s d liu) khi ngi dng ri b hot ng.</p> 684 685 <p>Mt cch hay kim tra kh nng khi phc trng thi ca ng dng ca bn l ch cn xoay 686 thit b sao cho hng mn hnh thay i. Khi hng mn hnh thay i, h thng 687 hy v to li hot ng p dng cc ti nguyn thay th m c th c sn 688 cho cu hnh mn hnh mi. Ch vi l do ny m mt iu rt quan trng l hot ng ca bn 689 hon ton khi phc trng thi ca mnh khi n c to li, v ngi dng thng xoay mn hnh trong khi 690 s dng ng dng.</p> 691 692 693 <h3 id="ConfigurationChanges">X l thay i v cu hnh</h3> 694 695 <p>Mt s cu hnh thit b c th thay i trong thi gian chy (chng hn nh hng mn hnh, s sn c 696 ca bn phm, v ngn ng). Khi s thay i din ra, Android to li hot ng ang chy 697 (h thng gi {@link android.app.Activity#onDestroy}, ri ngay lp tc gi {@link 698 android.app.Activity#onCreate onCreate()}). Hnh vi ny 699 c thit k gip ng dng ca bn iu chnh theo nhng cu hnh mi bng cch t ng ti li ng dng 700 ca bn bng cc ti nguyn thay th m bn cung cp (chng hn nh b tr khc cho 701 cc hng v kch c mn hnh khc).</p> 702 703 <p>Nu bn thit k hot ng ca mnh mt cch ph hp x l khi ng li do thay i hng mn hnh v 704 khi phc trng thi hot ng nh nu trn, ng dng ca bn s linh hot hn trc 705 nhng s kin bt ng khc trong vng i ca hot ng.</p> 706 707 <p>Cch tt nht x l khi ng li l 708 lu v khi phc trng thi hot ng ca bn bng cch s dng {@link 709 android.app.Activity#onSaveInstanceState onSaveInstanceState()} v {@link 710 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} (hoc {@link 711 android.app.Activity#onCreate onCreate()}), nh cp trong phn trc.</p> 712 713 <p> bit thm thng tin v nhng thay i cu hnh xy ra ti thi im chy v cch bn c th x l 714 chng, hy c hng dn <a href="{@docRoot}guide/topics/resources/runtime-changes.html">X l 715 Thay i trong Thi gian chy</a>.</p> 716 717 718 719 <h3 id="CoordinatingActivities">iu phi hot ng</h3> 720 721 <p>Khi mt hot ng bt u mt hot ng khc, c hai u tri qua nhng chuyn tip vng i. Hot ng th nht 722 tm dng v dng (tuy nhin, n s khng dng nu vn hin th c di nn), trong khi hot ng kia 723 c to. Trong trng hp nhng hot ng ny chia s d liu c lu vo a hoc ni khc, iu quan trng l 724 phi hiu rng hot ng th nht khng b dng hon ton trc khi hot ng th hai c to. 725 Thay vo , tin trnh bt u hot ng th hai chng lp vi tin trnh dng hot ng 726 th nht.</p> 727 728 <p>Th t gi li vng i c nh ngha r, c th l khi hai hot ng trong cng tin trnh 729 v hot ng ny bt u hot ng kia. Sau y l th t thao tc din ra khi Hot ng 730 A bt u Hot ng B: </p> 731 732 <ol> 733 <li>Phng php {@link android.app.Activity#onPause onPause()} ca Hot ng A thc thi.</li> 734 735 <li>{@link android.app.Activity#onCreate onCreate()} ca Hot ng B, {@link 736 android.app.Activity#onStart onStart()}, v cc phng php {@link android.app.Activity#onResume onResume()} 737 thc thi theo trnh t. (Hot ng B lc ny c tiu im ca ngi dng.)</li> 738 739 <li>Sau , nu Hot ng A khng cn hin th trn mn hnh, phng php {@link 740 android.app.Activity#onStop onStop()} ca n s thc thi.</li> 741 </ol> 742 743 <p>Trnh t gi li vng i c th d on ny cho php bn qun l chuyn tip 744 thng tin t hot ng ny sang hot ng khc. V d, nu bn phi ghi vo mt c s d liu khi 745 hot ng th nht dng sao cho hot ng theo sau c th c n, khi bn nn ghi vo 746 c s d liu trong khi {@link android.app.Activity#onPause onPause()} thay v trong khi {@link 747 android.app.Activity#onStop onStop()}.</p> 748 749 <!-- 750 <h2>Beginner's Path</h2> 751 752 <p>For more information about how Android maintains a history of activities and 753 enables user multitasking, continue with the <b><a 754 href="{@docRoot}guide/components/tasks-and-back-stack.html">Tasks and Back 755 Stack</a></b> document.</p> 756 --> 757