1 page.title=Tc v v Ngn xp 2 parent.title=Hot ng 3 parent.link=activities.html 4 @jd:body 5 6 <div id="qv-wrapper"> 7 <div id="qv"> 8 9 <h2>Trong ti liu ny</h2> 10 <ol> 11 <li><a href="#ActivityState">Lu Trng thi ca Hot ng</a></li></li> 12 <li><a href="#ManagingTasks">Qun l Tc v</a> 13 <ol> 14 <li><a href="#TaskLaunchModes">nh ngha cc ch khi chy</a></li> 15 <li><a href="#Affinities">X l quan h</a></li> 16 <li><a href="#Clearing">Xa ngn xp</a></li> 17 <li><a href="#Starting">Bt u mt tc v</a></li> 18 </ol> 19 </li> 20 </ol> 21 22 <h2>Bi vit</h2> 23 <ol> 24 <li><a href="http://android-developers.blogspot.com/2010/04/multitasking-android-way.html"> 25 a nhim theo cch ca Android</a></li> 26 </ol> 27 28 <h2>Xem thm</h2> 29 <ol> 30 <li><a href="{@docRoot}design/patterns/navigation.html">Thit k Android: 31 iu hng</a></li> 32 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html">Phn t bn k khai {@code <activity>} 33 </a></li> 34 <li><a href="{@docRoot}guide/components/recents.html">Mn hnh Tng quan</a></li> 35 </ol> 36 </div> 37 </div> 38 39 40 <p>Mt ng dng thng cha nhiu <a href="{@docRoot}guide/components/activities.html">hot ng</a>. Mi hot ng 41 nn c thit k xung quanh mt kiu hnh ng c th m ngi dng c th thc hin v bt u cc hot ng 42 khc. V d, mt ng dng e-mail c th c mt hot ng hin th mt danh sch cc th mi. 43 Khi ngi dng chn mt th, mt hot ng mi s m ra xem th .</p> 44 45 <p>Hot ng thm ch c th bt u cc hot ng tn ti trong cc ng dng khc trn thit b. V 46 d, nu ng dng ca bn mun gi mt th e-mail, bn c th nh ngha mt nh thc hin mt hnh ng 47 "gi" v bao gm mt s d liu, chng hn nh a ch e-mail v ni dung. Mt hot ng t mt ng dng 48 khc t khai bo l c kh nng x l kiu nh ny s m ra. Trong trng hp ny, 49 nh s gi mt e-mail, sao cho hot ng "son" ca mt ng dng e-mail s bt u (nu nhiu hot ng 50 h tr cng nh, khi h thng cho php ngi dng chn hot ng s s dng). Khi e-mail c 51 gi, hot ng ca bn tip tc v dng nh hot ng e-mail l mt phn ng dng ca bn. Mc d 52 cc hot ng c th n t nhng ng dng khc nhau, Android duy tr tri nghim ngi dng 53 mt m ny bng cch gi c hai hot ng trong cng <em>tc v</em>.</p> 54 55 <p>Tc v l mt tp hp gm nhiu hot ng m ngi dng tng tc vi 56 khi thc hin mt cng vic nht nh. Cc hot ng c sp xp trong mt chng (<em>ngn xp</em>), theo 57 th t m mi hot ng.</p> 58 59 <!-- SAVE FOR WHEN THE FRAGMENT DOC IS ADDED 60 <div class="sidebox-wrapper"> 61 <div class="sidebox"> 62 <h3>Adding fragments to a task's back stack</h3> 63 64 <p>Your activity can also include {@link android.app.Fragment}s to the back stack. For example, 65 suppose you have a two-pane layout using fragments, one of which is a list view (fragment A) and the 66 other being a layout to display an item from the list (fragment B). When the user selects an item 67 from the list, fragment B is replaced by a new fragment (fragment C). In this case, it might be 68 desireable for the user to navigate back to reveal fragment B, using the <em>Back</em> button.</p> 69 <p>In order to add fragment B to the back stack so that this is possible, you must call {@link 70 android.app.FragmentTransaction#addToBackStack addToBackStack()} before you {@link 71 android.app.FragmentTransaction#commit()} the transaction that replaces fragment B with fragment 72 C.</p> 73 <p>For more information about using fragments and adding them to the back stack, see the {@link 74 android.app.Fragment} class documentation.</p> 75 76 </div> 77 </div> 78 --> 79 80 <p>Mn hnh Trang ch ca thit b l ni bt u i vi hu ht tc v. Khi ngi dng chm vo mt biu tng trong trnh khi chy 81 ng dng 82 (hoc li tt trn mn hnh Trang ch), tc v ca ng dng s tin ra tin cnh. Nu khng 83 c tc v no cho ng dng (ng dng cha c s dng gn y), khi mt tc v mi 84 s c to v hot ng "chnh" cho ng dng s m ra thnh hot ng gc trong chng.</p> 85 86 <p>Khi hot ng hin ti bt u mt hot ng khc, hot ng mi s b y ln trn cng ca chng v 87 c tiu im. Hot ng trc vn nm trong chng, nhng b dng li. Khi mt hot ng 88 dng, h thng gi li trng thi hin ti ca giao din ngi dng ca hot ng . Khi ngi dng nhn nt 89 <em>Quay li</em> 90 , hot ng hin ti c bt khi trn cng ca chng (hot ng b hy) v 91 hot ng trc tip tc (trng thi trc ca UI ca n c khi phc). Cc hot ng trong chng 92 khng bao gi c sp xp li, m ch b y v bt khi chng—b y ln trn chng khi c bt u bi 93 hot ng hin ti v b bt khi chng khi ngi dng ri n bng cch s dng nt <em>Quay li</em>. Nh vy, 94 ngn xp 95 vn hnh nh mt cu trc i tng "vo cui, ra u". Hnh 1 minh ha 96 hnh vi ny cng mt dng thi gian th hin tin gia cc hot ng dc theo ngn xp 97 hin ti tng thi im.</p> 98 99 <img src="{@docRoot}images/fundamentals/diagram_backstack.png" alt="" /> 100 <p class="img-caption"><strong>Hnh 1.</strong> Biu din cch mi hot ng mi trong mt 101 tc v thm mt mc vo ngn xp. Khi ngi dng nhn nt <em>Quay li</em>, hot ng 102 hin ti 103 b hy v hot ng trc tip tc.</p> 104 105 106 <p>Nu ngi dng tip tc nhn <em>Quay li</em>, khi mi hot ng trong chng b bt khi 107 hin ra 108 hot ng trc, ti khi ngi dng quay li mn hnh Trang ch (hoc tr v hot ng ang chy khi 109 tc v bt u). Khi tt c hot ng b loi b khi chng, tc v s khng cn tn ti.</p> 110 111 <div class="figure" style="width:287px"> 112 <img src="{@docRoot}images/fundamentals/diagram_multitasking.png" alt="" /> <p 113 class="img-caption"><strong>Hnh 2.</strong> Hai tc v: Tc v B nhn tng tc ngi dng 114 trong tin cnh, trong khi Tc v A nm di nn, ch c tip tc.</p> 115 </div> 116 <div class="figure" style="width:215px"> 117 <img src="{@docRoot}images/fundamentals/diagram_multiple_instances.png" alt="" /> <p 118 class="img-caption"><strong>Hnh 3.</strong> Mt hot ng n l c khi to nhiu ln.</p> 119 </div> 120 121 <p>Tc v l mt n v dnh kt, c th di chuyn ti "nn" khi ngi dng bt u mt tc v mi hoc i n 122 mn hnh Trang ch, thng qua nt <em>Home</em>. Khi trong nn, tt c hot ng trong 123 tc v b 124 dng, nhng ngn xp cho tc v vn khng b nh hng—tc v ch n thun mt tiu im trong khi 125 mt tc v khc thay th, nh minh ha trong hnh 2. Khi , mt tc v c th quay li "tin cnh" ngi dng 126 c th chn ni h ri i. V d, gi s rng tc v hin ti (Tc v A) c ba 127 hot ng trong chng ca mnh—hai trong s nm di hot ng hin ti. Ngi dng nhn nt <em>Trang ch</em> 128 , sau 129 bt u mt ng dng mi t trnh khi chy ng dng. Khi mn hnh Trang ch xut hin, Tc v A i vo 130 nn. Khi ng dng mi bt u, h thng s bt u mt tc v cho ng dng 131 (Tc v B) bng chng cc hot ng ca chnh mnh. Sau khi tng tc vi 132 ng dng , ngi dng quay li Trang ch ln na v chn ng dng 133 bt u Tc v A lc u. Lc ny, Tc v A i n 134 tin cnh—c ba hot ng trong chng ca n u gi nguyn v hot ng trn cng 135 ca chng c tip tc. Ti 136 im ny, ngi dng cng c th chuyn tr li Tc v B bng cch n Trang ch v chn biu tng ng dng 137 bt u tc v (hoc bng cch chn tc v ca ng dng t 138 <a href="{@docRoot}guide/components/recents.html">mn hnh tng quan</a>). 139 y l mt v d v a nhim trn Android.</p> 140 141 <p class="note"><strong>Lu :</strong> Nhiu tc v c th c lu gi cng lc trong nn. 142 Tuy nhin, nu ngi dng ang chy nhiu tc v nn ti cng thi im, h thng c th bt u 143 hy cc hot ng nn khi phc b nh, khin trng thi ca hot ng b mt. 144 Xem phn sau y v <a href="#ActivityState">Trng thi ca hot ng</a>.</p> 145 146 <p>V cc hot ng trong ngn xp khng bao gi c sp xp li, nu ng dng ca bn cho php 147 ngi dng bt u mt hot ng c th t nhiu hn mt hot ng, mt thc th mi ca 148 hot ng s c to v y ln chng (thay v mang bt k thc th no trc ca 149 hot ng ln trn cng). Nh vy, mt hot ng trong ng dng ca bn c th c to phin bn nhiu 150 ln (thm ch t cc tc v khc nhau), nh minh ha trong hnh 3. Nh vy, nu ngi dng iu hng ngc li 151 bng cch s dng nt <em>Quay li</em>, mi thc th ca hot ng c hin th theo th t c 152 m (mi hot ng 153 c trng thi UI ca chnh chng). Tuy nhin, bn c th sa i hnh vi ny nu khng mun mt hot ng c 154 khi to nhiu hn mt ln. Cch lm nh vy c bn trong phn sau v <a href="#ManagingTasks">Qun l Tc v</a>.</p> 155 156 157 <p> tm tt hnh vi mc nh i vi cc hot ng v tc v:</p> 158 159 <ul> 160 <li>Khi Hot ng A bt u Hot ng B, Hot ng A b dng, nhng h thng gi li trng thi ca n 161 (chng hn nh v tr cun v vn bn c nhp vo cc mu). 162 Nu ngi dng nhn nt <em>Quay li</em> khi ang trong Hot ng B, Hot ng A s tip tc vi trng thi 163 c khi phc.</li> 164 <li>Khi ngi dng ri khi mt tc v bng cch nhn nt <em>Trang ch</em>, hot ng hin ti b 165 dng v 166 tc v ca n s a xung di nn. H thng s gi li trng thi ca mi hot ng trong tc v. Nu 167 sau ngi dng tip tc tc v bng cch chn biu tng trnh khi chy bt u tc v, tc v s vo 168 tin cnh v tip tc hot ng trn cng ca chng.</li> 169 <li>Nu ngi dng nhn nt <em>Quay li</em>, hot ng hin ti b bt khi chng 170 v 171 b hy. Hot ng trc trong chng s c tip tc. Khi mt hot ng b hy, h thng 172 <em>khng</em> gi li trng thi ca hot ng .</li> 173 <li>Hot ng c th c khi to nhiu ln, thm ch t cc tc v khc.</li> 174 </ul> 175 176 177 <div class="note design"> 178 <p><strong>Thit k iu hng</strong></p> 179 <p> bit thm v cch iu hng ng dng hot ng trn Android, hy c hng dn <a href="{@docRoot}design/patterns/navigation.html">iu hng</a> ca Thit k Android.</p> 180 </div> 181 182 183 <h2 id="ActivityState">Lu Trng thi ca Hot ng</h2> 184 185 <p>Nh c cp trn, hnh vi mc nh ca h thng gi nguyn trng thi ca mt hot ng khi n b 186 dng. Bng cch ny, khi ngi dng iu hng tr li mt hot ng trc , giao din ngi dng ca n s xut hin 187 nh lc b ri i. Tuy nhin, bn c th—v <strong>nn</strong>—ch ng gi li 188 trng thi ca cc hot ng ca mnh bng cch s dng cc phng php gi li, trong trng hp hot ng b hy v phi 189 c to li.</p> 190 191 <p>Khi h thng dng mt trong cc hot ng ca bn (chng hn nh khi mt hot ng mi bt u hoc tc v 192 di chuyn v nn), h thng c th hon ton hy hot ng nu n cn khi phc 193 b nh h thng. Khi iu ny xy ra, thng tin v trng thi ca hot ng s b mt. Nu iu ny xy ra, 194 h thng vn 195 bit rng hot ng c mt v tr trong ngn xp, nhng khi hot ng c a ti v tr trn cng 196 ca chng, h thng phi to li n (thay v tip tc). trnh 197 lm mt cng vic ca ngi dng, bn nn ch ng gi li n bng cch trin khai phng php gi li 198 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} trong 199 hot ng ca mnh.</p> 200 201 <p> bit thm thng tin v cch lu trng thi hot ng ca mnh, hy xem ti liu <a href="{@docRoot}guide/components/activities.html#SavingActivityState">Hot ng</a> 202 .</p> 203 204 205 206 <h2 id="ManagingTasks">Qun l Tc v</h2> 207 208 <p>Cch Android qun l tc v v ngn xp, nh c m t bn trn—bng cch t tt c 209 hot ng c bt u ni tip nhau vo cng tc v v trong mt chng "vo cui, ra u"—rt 210 hiu qu i vi hu ht ng dng v bn khng phi lo lng v cch thc cc hot ng ca mnh c lin kt vi 211 cc tc v hay cch chng tn ti trong ngn xp. Tuy nhin, bn c th quyt nh rng mnh mun gin on 212 hnh vi thng thng. C th bn mun mt hot ng trong ng dng ca mnh bt u mt tc v mi khi n c 213 bt u (thay v c t trong tc v hin ti); hoc, khi bn bt u mt hot ng, bn mun 214 mang ln trc mt thc th hin ti ca n (thay v to mt 215 thc th mi trn cng ca ngn xp); hoc, bn mun ngn xp ca mnh c xa sch tt c cc 216 hot ng, ngoi tr hot ng gc khi ngi dng ri khi tc v.</p> 217 218 <p>Bn c th lm nhng iu ny v nhiu iu khc vi cc thuc tnh trong phn t bn k khai 219 <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> 220 v c trong nh m bn chuyn cho 221 {@link android.app.Activity#startActivity startActivity()}.</p> 222 223 <p>V mt ny, cc thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html"> 224 {@code <activity>}</a> chnh m bn c th s dng l:</p> 225 226 <ul class="nolist"> 227 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#aff"> 228 {@code taskAffinity}</a></li> 229 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode"> 230 {@code launchMode}</a></li> 231 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#reparent"> 232 {@code allowTaskReparenting}</a></li> 233 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#clear"> 234 {@code clearTaskOnLaunch}</a></li> 235 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#always"> 236 {@code alwaysRetainTaskState}</a></li> 237 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#finish"> 238 {@code finishOnTaskLaunch}</a></li> 239 </ul> 240 241 <p>V cc c nh chnh m bn c th s dng l:</p> 242 243 <ul class="nolist"> 244 <li>{@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK}</li> 245 <li>{@link android.content.Intent#FLAG_ACTIVITY_CLEAR_TOP}</li> 246 <li>{@link android.content.Intent#FLAG_ACTIVITY_SINGLE_TOP}</li> 247 </ul> 248 249 <p>Trong nhng phn sau, bn s thy cch bn c th s dng nhng thuc tnh ca bn k khai 250 v c nh ny nh ngha cch cc hot ng c lin kt vi tc v v hnh vi ca chng nh th no trong ngn xp.</p> 251 252 <p>Bn cnh , c phn bn ring v vic cn nhc cch cc tc v v hot ng c th 253 c biu din v qun l trong mn hnh tng quan. Xem phn <a href="{@docRoot}guide/components/recents.html">Mn hnh Tng quan</a> 254 bit thm thng tin. Thng thng, bn nn cho php h thng nh ngha tc v v cc hot ng 255 ca bn c biu din nh th no trong mn hnh tng quan, v bn khng cn sa i hnh vi ny.</p> 256 257 <p class="caution"><strong>Ch :</strong> Hu ht cc ng dng khng nn gin on hnh vi 258 mc nh cho cc hot ng v tc v. Nu bn xc nh rng hot ng ca bn cn phi sa i 259 hnh vi mc nh, hy thn trng v m bo kim tra kh nng s dng c ca hot ng trong khi 260 khi chy v khi iu hng tr li n t cc hot ng v tc v khc bng nt <em>Quay li</em>. 261 m bo kim tra hnh vi iu hng m c th xung t vi hnh vi c k vng ca ngi dng.</p> 262 263 264 <h3 id="TaskLaunchModes">nh ngha cc ch khi chy</h3> 265 266 <p>Cc ch khi chy cho php bn nh ngha mt thc th mi ca mt hot ng c lin kt vi tc v hin ti 267 nh th no. Bn c th nh ngha cc ch khi chy khc nhau theo hai cch:</p> 268 <ul class="nolist"> 269 <li><a href="#ManifestForTasks">S dng tp bn k khai</a> 270 <p>Khi bn khai bo mt hot ng trong tp bn k khai ca mnh, bn c th quy nh hot ng 271 s lin kt vi cc tc v nh th no khi n bt u.</li> 272 <li><a href="#IntentFlagsForTasks">S dng c nh</a> 273 <p>Khi bn gi {@link android.app.Activity#startActivity startActivity()}, 274 bn c th thm mt c vo {@link android.content.Intent} m khai bo cch (hoc 275 liu c hay khng) hot ng mi s lin kt vi tc v hin ti.</p></li> 276 </ul> 277 278 <p>Nh vy, nu Hot ng A bt u Hot ng B, Hot ng B c th nh ngha trong bn k khai ca mnh cch n 279 s lin kt vi tc v hin ti (nu c) v Hot ng A cng c th yu cu cch m Hot ng 280 B s lin kt vi tc v hin ti. Nu c hai hot ng u nh ngha cch Hot ng B 281 nn lin kt vi mt tc v th yu cu ca Hot ng A (nh nh ngha trong nh) c u tin 282 so vi yu cu ca Hot ng B (nh c nh ngha trong bn k khai ca n).</p> 283 284 <p class="note"><strong>Lu :</strong> Mt s ch khi chy sn c cho tp bn k khai 285 khng sn c di dng c cho mt nh v, tng t, mt s ch khi chy sn c di dng c 286 cho mt nh khng th c nh ngha trong bn k khai.</p> 287 288 289 <h4 id="ManifestForTasks">S dng tp bn k khai</h4> 290 291 <p>Khi khai bo mt hot ng trong tp bn k khai ca bn, bn c th quy nh cch m hot ng 292 s lin kt vi mt tc v bng cch s dng thuc tnh ca phn t <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> 293 , <a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code 294 launchMode}</a>.</p> 295 296 <p>Thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code 297 launchMode}</a> quy nh mt ch lnh v cch hot ng s c khi chy vo mt 298 tc v. C bn ch khi chy khc nhau m bn c th gn cho thuc tnh 299 <code><a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">launchMode</a></code> 300 :</p> 301 302 <dl> 303 <dt>{@code "standard"} (ch mc nh)</dt> 304 <dd>Mc nh. H thng to mt thc th mi ca hot ng trong tc v l ni 305 m n c bt u v nh tuyn nh ti . Hot ng c th c khi to nhiu ln, 306 mi thc th c th thuc v cc tc v khc nhau, v mt tc v c th c nhiu thc th.</dd> 307 <dt>{@code "singleTop"}</dt> 308 <dd>Nu mt thc th ca hot ng tn ti trn cng ca tc v hin ti, h thng 309 s nh tuyn nh ti thc th thng qua mt lnh gi ti phng php {@link 310 android.app.Activity#onNewIntent onNewIntent()} ca n, thay v to mt thc th mi ca 311 hot ng. Hot ng c th c to phin bn nhiu ln, mi thc th c th 312 thuc v cc tc v khc nhau, v mt tc v c th c nhiu thc th (nhng ch nu 313 hot ng nm trn cng ca ngn xp <em>khng</em> phi l mt thc th ca hot ng hin c). 314 <p>V d, gi s ngn xp ca mt tc v bao gm hot ng gc A vi cc hot ng B, C, 315 v D trn cng (chng l A-B-C-D; D trn cng). Mt nh n cho loi hot ng D. 316 Nu D c ch khi chy {@code "standard"} mc nh, mt thc th mi ca lp s c khi chy v 317 chng tr thnh A-B-C-D-D. Tuy nhin, nu ch khi chy ca D l {@code "singleTop"}, thc th hin ti 318 ca D s nhn nh thng qua {@link 319 android.app.Activity#onNewIntent onNewIntent()}, bi n nm v tr trn cng ca chng—chng 320 vn l A-B-C-D. Tuy nhin, nu mt nh n cho hot ng loi B, khi mt thc th 321 mi ca B s c thm vo chng ngay c khi ch khi chy ca n l {@code "singleTop"}.</p> 322 <p class="note"><strong>Lu :</strong> Khi mt thc th mi ca hot ng c to, 323 ngi dng c th nhn nt <em>Quay li</em> quay v hot ng trc . Nhng khi mt thc th 324 hin ti ca 325 hot ng x l mt nh mi, ngi dng khng th nhn nt <em>Quay li</em> quay v trng thi 326 ca 327 hot ng trc khi nh mi n trong {@link android.app.Activity#onNewIntent 328 onNewIntent()}.</p> 329 </dd> 330 331 <dt>{@code "singleTask"}</dt> 332 <dd>H thng s to ra mt tc v mi v khi to hot ng gc ca tc v mi. 333 Tuy nhin, nu mt thc th ca hot ng tn ti trong mt tc v ring, h thng s nh tuyn 334 nh ti thc th hin ti thng qua mt lnh gi ti phng php {@link 335 android.app.Activity#onNewIntent onNewIntent()} ca n thay v to mt thc th mi. Ch 336 mt thc th ca hot ng c th tn ti mt thi im. 337 <p class="note"><strong>Lu :</strong> Mc d hot ng bt u mt tc v mi, nt 338 <em>Quay li</em> s vn a ngi dng quay v hot ng trc .</p></dd> 339 <dt>{@code "singleInstance"}.</dt> 340 <dd>Ging nh {@code "singleTask"}, tr khi h thng khng khi chy bt k hot ng no khc vo 341 tc v ang nm gi thc th. Hot ng lun l thnh vin n l v duy nht ca tc v; 342 bt k hot ng no c bt u bi hot ng ny s m ra trong mt tc v ring.</dd> 343 </dl> 344 345 346 <p>Ly mt v d khc, ng dng Trnh duyt ca Android khai bo rng hot ng trnh duyt web s 347 lun m tc v ca chnh mnh—bng cch quy nh ch khi chy {@code singleTask} trong phn t <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a>. 348 iu ny c ngha rng nu ng dng ca bn pht hnh mt 349 nh m Trnh duyt Android, hot ng ca n <em>khng</em> c t trong cng 350 tc v nh ng dng ca bn. Thay vo , hoc l mt tc v mi bt u cho Trnh duyt hoc, nu Trnh duyt 351 c mt tc v ang chy trong nn th tc v s c a ln trc x l 352 nh mi.</p> 353 354 <p>Khng ph thuc vo vic mt hot ng bt u trong mt tc v mi hay trong cng tc v m hot ng 355 bt u, nt <em>Quay li</em> s lun a ngi dng v hot ng trc . Tuy nhin, nu bn 356 bt u mt hot ng m quy nh ch khi chy {@code singleTask}, khi nu mt thc th ca 357 hot ng tn ti trong mt tc v nn, ton b tc v s c a ra tin cnh. Ti thi im ny 358 , lc ny ngn xp bao gm tt c hot ng t tc v c mang ra, v tr trn cng ca 359 chng. Hnh 4 minh ha loi kch bn ny.</p> 360 361 <img src="{@docRoot}images/fundamentals/diagram_backstack_singletask_multiactivity.png" alt="" /> 362 <p class="img-caption"><strong>Hnh 4.</strong> Biu din cch thc mt hot ng vi 363 ch khi chy "singleTask" c thm vo ngn xp. Nu hot ng l mt phn ca mt 364 tc v nn vi ngn xp ca chnh n, khi ton b ngn xp cng tin 365 v pha trc, trn cng tc v hin ti.</p> 366 367 <p> bit thm thng tin v vic s dng cc ch khi chy trong tp bn k khai, hy xem ti liu phn t 368 <code><a href="{@docRoot}guide/topics/manifest/activity-element.html"><activity></a></code> 369 , trong c trnh by thm v thuc tnh {@code launchMode} v cc gi tr 370 c chp nhn.</p> 371 372 <p class="note"><strong>Lu :</strong> Hnh vi m bn quy nh cho hot ng ca mnh bng thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code launchMode}</a> c th 373 b khng ch bi c c nh bt u hot ng ca bn, nh c trnh by trong 374 phn tip theo.</p> 375 376 377 378 <h4 id="#IntentFlagsForTasks">S dng c nh</h4> 379 380 <p>Khi bt u mt hot ng, bn c th sa i lin kt mc nh gia mt hot ng vi tc v ca n 381 bng cch thm c vo trong nh m bn chuyn ti {@link 382 android.app.Activity#startActivity startActivity()}. Nhng c m bn c th s dng sa i 383 hnh vi mc nh l:</p> 384 385 <p> 386 <dt>{@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK}</dt> 387 <dd>Bt u mt hot ng trong mt tc v mi. Nu c mt tc v ang chy cho hot ng m bn ang 388 bt u, tc v s c a ra tin cnh vi trng thi cui cng c khi phc v hot ng 389 nhn nh mi trong {@link android.app.Activity#onNewIntent onNewIntent()}. 390 <p>iu ny s to ra cng hnh vi nh gi tr {@code "singleTask"} <a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code launchMode}</a>, 391 c trnh by phn trc.</p></dd> 392 <dt>{@link android.content.Intent#FLAG_ACTIVITY_SINGLE_TOP}</dt> 393 <dd>Nu hot ng ang c bt u l hot ng hin ti ( trn cng ca ngn xp), khi 394 thc th hin c s nhn mt lnh gi n {@link android.app.Activity#onNewIntent onNewIntent()}, 395 thay v to mt thc th ca hot ng mi. 396 <p>iu ny s to ra cng hnh vi nh gi tr {@code "singleTop"} <a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code launchMode}</a>, 397 c trnh by phn trc.</p></dd> 398 <dt>{@link android.content.Intent#FLAG_ACTIVITY_CLEAR_TOP}</dt> 399 <dd>Nu hot ng ang c bt u ang chy trong tc v hin ti, khi 400 thay v khi chy mt thc th mi ca hot ng , tt c cc hot ng khc bn trn n u 401 b hy v nh ny c chuyn ti thc th c tip tc ca hot ng (lc ny ang trn cng), 402 thng qua {@link android.app.Activity#onNewIntent onNewIntent()}). 403 <p>Khng c gi tr cho thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code launchMode}</a> 404 m sinh ra hnh vi ny.</p> 405 <p>{@code FLAG_ACTIVITY_CLEAR_TOP} c s dng thng xuyn nht cng vi 406 {@code FLAG_ACTIVITY_NEW_TASK}. 407 Khi c s dng cng nhau, nhng c ny l mt cch nh v hot ng hin ti 408 trong mt tc v khc v t n vo v tr ni n c th hi p nh. </p> 409 <p class="note"><strong>Lu :</strong> Nu ch khi chy ca hot ng c ch nh l 410 {@code "standard"}, 411 n cng b loi b khi chng v mt thc th mi s c khi chy thay ch n x l 412 nh n. l bi mt thc th mi lun c to cho nh mi khi ch khi chy 413 l {@code "standard"}. </p> 414 </dd> 415 </dl> 416 417 418 419 420 421 <h3 id="Affinities">X l quan h</h3> 422 423 <p><em>Quan h</em> s cho bit mt hot ng u tin thuc v tc v no hn. Theo mc nh, tt c 424 cc hot ng t cng ng dng c quan h vi nhau. V th, theo mc nh, tt c 425 hot ng trong cng ng dng u tin trong cng tc v hn. Tuy nhin, bn c th sa i 426 quan h mc nh cho mt hot ng. Cc hot ng c nh ngha trong 427 cc ng dng khc nhau c th chia s mt quan h, hoc cc hot ng c nh ngha trong cng ng dng c th 428 c gn cc quan h tc v khc nhau.</p> 429 430 <p>Bn c th sa i quan h cho bt k hot ng cho no bng thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html#aff">{@code taskAffinity}</a> ca 431 phn t <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> 432 .</p> 433 434 <p>Thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html#aff">{@code taskAffinity}</a> 435 ly mt gi tr xu, phi l gi tr duy nht t tn gi mc nh 436 c khai bo trong phn t <a href="{@docRoot}guide/topics/manifest/manifest-element.html"> 437 {@code <manifest>} 438 </a>, do h thng s dng tn nhn bit quan h 439 tc v mc nh cho ng dng.</p> 440 441 <p>Vn quan h c xt trong hai trng hp:</p> 442 <ul> 443 <li>Khi nh khi chy hot ng cha c 444 {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} 445 . 446 447 <p>Theo mc nh, mt hot ng mi c khi chy vo tc v ca hot ng 448 gi {@link android.app.Activity#startActivity startActivity()}. N c y ln cng 449 ngn xp nh trnh gi. Tuy nhin, nu nh c chuyn ti 450 {@link android.app.Activity#startActivity startActivity()} 451 cha c {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} 452 , h thng s tm mt tc v khc cha hot ng mi. Thng th l mt tc v mi. 453 Tuy nhin, khng nht thit phi nh vy. Nu c mt tc v hin ti vi cng quan h nh 454 hot ng mi, hot ng s c khi chy vo tc v . Nu khng, n s bt u mt tc v mi.</p> 455 456 <p>Nu c ny khin mt hot ng bt u mt tc v mi v ngi dng nhn nt <em>Home</em> 457 ri 458 n th phi c mt cch no ngi dng iu hng quay li tc v. Mt s i tng (chng hn nh 459 trnh qun l thng bo) lun bt u cc hot ng trong mt tc v bn ngoi, khng bao gi bt u trong chnh tc v ca mnh, v th 460 chng lun t {@code FLAG_ACTIVITY_NEW_TASK} vo nh m chng chuyn ti 461 {@link android.app.Activity#startActivity startActivity()}. 462 Nu bn c mt hot ng c th c gi ra bi 463 mt i tng bn ngoi m c th s dng c ny, hy lu l ngi dng c mt cch c lp quay li 464 tc v c bt u, chng hn nh bng mt biu tng trnh khi chy (hot ng gc ca tc v 465 c mt b lc nh {@link android.content.Intent#CATEGORY_LAUNCHER}; xem phn <a href="#Starting">Bt u mt tc v</a> bn di).</p> 466 </li> 467 468 <li>Khi mt hot ng c thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html#reparent"> 469 {@code allowTaskReparenting}</a> ca n c t thnh {@code "true"}. 470 <p>Trong trng hp ny, hot ng c th di chuyn t tc v m n bt u ti tc v m n c quan h 471 khi tc v i ra tin cnh.</p> 472 <p>V d, gi s rng mt hot ng vi chc nng bo co tnh hnh thi tit cc thnh ph c chn 473 c nh ngha l mt phn trong mt ng dng du lch. N c cng quan h nh cc hot ng khc trong cng 474 ng dng (quan h ng dng mc nh) v n cho php to li tp m vi thuc tnh ny. 475 Khi mt trong cc hot ng ca bn bt u hot ng trnh bo co thi tit, ban u n thuc v cng 476 tc v nh hot ng ca bn. Tuy nhin, khi tc v ca ng dng du lch i ra tin cnh, hot ng 477 ca trnh bo co thi tit c gn li cho tc v v c hin th bn trong n.</p> 478 </li> 479 </ul> 480 481 <p class="note"><strong>Mo:</strong> Nu mt tp {@code .apk} cha nhiu hn mt "ng dng" 482 t quan im ca ngi dng, bn c th mun s dng thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html#aff">{@code taskAffinity}</a> 483 gn cc quan h khc nhau cho hot ng c lin kt vi tng "ng dng".</p> 484 485 486 487 <h3 id="Clearing">Xa ngn xp</h3> 488 489 <p>Nu ngi dng ri mt tc v trong khong thi gian di, h thng s xa tc v ca tt c hot ng ngoi tr 490 hot ng gc. Khi ngi dng quay tr li tc v, ch hot ng gc c khi phc. 491 H thng s hot ng theo cch ny, v, sau mt khong thi gian di, ngi dng c th t b 492 vic m h ang lm trc v quay li tc v bt u mt vic mi. </p> 493 494 <p>C mt s thuc tnh hot ng m bn c th s dng sa i hnh vi ny: </p> 495 496 <dl> 497 <dt><code><a 498 href="{@docRoot}guide/topics/manifest/activity-element.html#always">alwaysRetainTaskState</a></code> 499 </dt> 500 <dd>Nu thuc tnh ny c t thnh {@code "true"} trong hot ng gc ca mt tc v, 501 hnh vi mc nh c m t s khng xy ra. 502 Tc v gi li tt c hot ng trong chng ca mnh k c sau mt khong thi gian di.</dd> 503 504 <dt><code><a 505 href="{@docRoot}guide/topics/manifest/activity-element.html#clear">clearTaskOnLaunch</a></code></dt> 506 <dd>Nu thuc tnh ny c t thnh {@code "true"} trong hot ng gc ca mt tc v, 507 chng s b xa ti hot ng gc bt c khi no ngi dng ri khi tc v 508 v quay li. Ni cch khc, n ngc vi 509 <a href="{@docRoot}guide/topics/manifest/activity-element.html#always"> 510 {@code alwaysRetainTaskState}</a>. Ngi dng lun quay li tc v 511 trng thi ban u ca n, ngay c sau khi ri khi tc v trong ch mt lc.</dd> 512 513 <dt><code><a 514 href="{@docRoot}guide/topics/manifest/activity-element.html#finish">finishOnTaskLaunch</a></code> 515 </dt> 516 <dd>Thuc tnh ny ging nh <a href="{@docRoot}guide/topics/manifest/activity-element.html#clear">{@code clearTaskOnLaunch}</a>, 517 nhng n hot ng trn mt tc v 518 n l ch khng phi mt tc v ton b. N cng c th khin bt k hot ng no 519 thot mt, bao gm c hot ng gc. Khi n c t thnh {@code "true"}, hot ng 520 vn l mt b phn ca tc v ch cho phin lm vic hin ti. Nu ngi dng ri i 521 ri quay li tc v, n khng cn xut hin na.</dd> 522 </dl> 523 524 525 526 527 <h3 id="Starting">Bt u mt tc v</h3> 528 529 <p>Bn c th thit lp mt hot ng lm im bt u cho mt tc v bng cch a cho n mt b lc nh vi 530 {@code "android.intent.action.MAIN"} l hnh ng c quy nh v 531 {@code "android.intent.category.LAUNCHER"} 532 l th loi c quy nh. V d:</p> 533 534 <pre> 535 <activity ... > 536 <intent-filter ... > 537 <action android:name="android.intent.action.MAIN" /> 538 <category android:name="android.intent.category.LAUNCHER" /> 539 </intent-filter> 540 ... 541 </activity> 542 </pre> 543 544 <p>Mt b lc nh loi ny khin mt biu tng v nhn cho 545 hot ng c hin th trong trnh khi chy ng dng, cho php ngi dng khi chy hot ng v 546 quay li tc v m n to ra vo bt c lc no sau khi n c khi chy. 547 </p> 548 549 <p>Kh nng th hai ny l quan trng: Ngi dng chc chn c th ri mt tc v ri quay li 550 sau bng cch s dng trnh khi chy hot ng ny. V l do ny, hai <a href="#LaunchModes">ch 551 khi chy</a> m nh du cc hot ng l lun khi to mt tc v, {@code "singleTask"} v 552 {@code "singleInstance"}, s ch c s dng khi hot ng c mt 553 {@link android.content.Intent#ACTION_MAIN} 554 v mt b lc {@link android.content.Intent#CATEGORY_LAUNCHER}. V d, hy tng tng chuyn g 555 s xy ra nu thiu b lc: Mt nh khi chy mt hot ng {@code "singleTask"}, khi u mt 556 tc v mi v ngi dng dnh mt khong thi gian lm vic trong tc v . Khi , ngi dng nhn nt <em>Home</em> 557 . Lc ny, tc v c gi ti nn v khng hin th. By gi, ngi dng khng c cch no quay li 558 tc v bi n khng c biu din trong trnh khi chy ng dng.</p> 559 560 <p>i vi nhng trng hp m bn khng mun ngi dng c th quay li mt hot ng, hy t gi tr ca phn t 561 <code><a href="{@docRoot}guide/topics/manifest/activity-element.html"><activity></a></code> 562 , 563 <a href="{@docRoot}guide/topics/manifest/activity-element.html#finish">{@code finishOnTaskLaunch}</a> 564 thnh {@code "true"} (xem <a href="#Clearing">Xa chng</a>).</p> 565 566 <p>Bn c th tham kho thm thng tin v cch cc tc v v hot ng c trnh by v qun l trong 567 mn hnh tng quan sn c ti phn <a href="{@docRoot}guide/components/recents.html"> 568 Mn hnh Tng quan</a>.</p> 569 570 <!-- 571 <h2>Beginner's Path</h2> 572 573 <p>For more information about how to use intents to 574 activate other application components and publish the intents to which your components 575 respond, continue with the <b><a 576 href="{@docRoot}guide/components/intents-filters.html">Intents and Intent 577 Filters</a></b> document.</p> 578 --> 579