Home | History | Annotate | Download | only in components
      1 page.title=Mn hnh Tng quan
      2 page.tags="recents","overview"
      3 
      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="#adding">Thm Tc v vo Mn hnh Tng quan</a>
     12       <ol>
     13         <li><a href="#flag-new-doc">S dng c  nh  thm mt tc v</a></li>
     14         <li><a href="#attr-doclaunch">S dng thuc tnh Hot ng  thm mt tc v</a></li>
     15       </ol>
     16     </li>
     17     <li><a href="#removing">Loi b Tc v</a>
     18       <ol>
     19         <li><a href="#apptask-remove">S dng lp AppTask  loi b tc v</a></li>
     20         <li><a href="#retain-finished">Gi li tc v  hon thnh</a></li>
     21       </ol>
     22     </li>
     23   </ol>
     24 
     25   <h2>Lp kha</h2>
     26   <ol>
     27     <li>{@link android.app.ActivityManager.AppTask}</li>
     28     <li>{@link android.content.Intent}</li>
     29   </ol>
     30 
     31   <h2>M mu</h2>
     32   <ol>
     33     <li><a href="{@docRoot}samples/DocumentCentricApps/index.html">ng dng tp trung vo ti liu</a></li>
     34   </ol>
     35 
     36 </div>
     37 </div>
     38 
     39 <p>Mn hnh tng quan (cn c gi l mn hnh gn y, danh sch tc v gn y, hay ng dng gn y)
     40 l mt UI cp h thng lit k cc <a href="{@docRoot}guide/components/activities.html">
     41 hot ng</a> v <a href="{@docRoot}guide/components/tasks-and-back-stack.html">tc v</a> mi c truy cp gn y. Ngi dng
     42 c th iu hng qua danh sch ny v chn mt tc v  tip tc, hoc ngi dng c th loi b mt tc v khi
     43 danh sch bng cch trt nhanh n i. Vi vic pht hnh Android 5.0 (API mc 21), nhiu thc th ca
     44 hot ng tng t cha cc ti liu khc nhau c th xut hin di dng cc tc v trong mn hnh tng quan. V d,
     45 Google Drive c th c mt tc v cho tng ti liu trong mt vi ti liu Google. Mi ti liu xut hin thnh mt
     46 tc v trong mn hnh tng quan.</p>
     47 
     48 <img src="{@docRoot}images/components/recents.png" alt="" width="284" />
     49 <p class="img-caption"><strong>Hnh 1.</strong> Mn hnh tng quan hin th ba ti liu Google Drive
     50 , mi ti liu c biu din nh mt tc v ring.</p>
     51 
     52 <p>Thng th bn s cho php h thng nh ngha cch tc v v
     53 hot ng ca mnh c biu din nh th no trong mn hnh tng quan, v bn khng cn sa i hnh vi ny.
     54 Tuy nhin, ng dng ca bn c th xc nh cch thc v thi gian cc hot ng xut hin trong mn hnh tng quan. Lp
     55 {@link android.app.ActivityManager.AppTask} cho php bn qun l tc v, v c hot ng ca
     56 lp {@link android.content.Intent} cho php bn quy nh khi no th mt hot ng c thm hoc loi b khi
     57 mn hnh tng quan. ng thi, thuc tnh <code><a href="{@docRoot}guide/topics/manifest/activity-element.html">
     58 &lt;activity&gt;</a></code> cho php bn t hnh vi trong bn k khai.</p>
     59 
     60 <h2 id="adding">Thm Tc v vo Mn hnh Tng quan</h2>
     61 
     62 <p>S dng c ca lp {@link android.content.Intent}  thm mt tc v cho php kim sot nhiu hn
     63 i vi thi im v cch thc mt ti liu c m hoc m li trong mn hnh tng quan. Khi s dng cc thuc tnh
     64 <code><a href="{@docRoot}guide/topics/manifest/activity-element.html">&lt;activity&gt;</a></code>
     65 , bn c th chn gia lun m ti liu trong mt tc v mi hoc s dng li mt
     66 tc v hin c cho ti liu.</p>
     67 
     68 <h3 id="flag-new-doc">S dng c  nh  thm mt tc v</h3>
     69 
     70 <p>Khi to mt ti liu mi cho hot ng ca bn, bn gi phng php
     71 {@link android.app.ActivityManager.AppTask#startActivity(android.content.Context, android.content.Intent, android.os.Bundle) startActivity()}
     72 ca lp {@link android.app.ActivityManager.AppTask}, chuyn cho n  nh c
     73 chc nng khi chy hot ng.  chn mt ngt l-gic sao cho h thng coi hot ng ca bn nh mt tc v
     74 mi trong mn hnh tng quan, hy chuyn c {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT}
     75 trong phng php {@link android.content.Intent#addFlags(int) addFlags()} ca {@link android.content.Intent}
     76 c chc nng khi chy hot ng.</p>
     77 
     78 <p class="note"><strong>Lu :</strong> C {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT}
     79 thay th c {@link android.content.Intent#FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET},
     80 c rt bt k t phin bn Android 5.0 (API mc 21).</p>
     81 
     82 <p>Nu bn t c {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK} khi to
     83 ti liu mi, h thng s lun to mt tc v mi ly hot ng mc tiu  lm gc.
     84 Thit t ny cho php m cng ti liu trong nhiu hn mt tc v. on m sau th hin
     85 cch m hot ng chnh thc hin iu ny:</p>
     86 
     87 <p class="code-caption"><a href="{@docRoot}samples/DocumentCentricApps/index.html">
     88 DocumentCentricActivity.java</a></p>
     89 <pre>
     90 public void createNewDocument(View view) {
     91       final Intent newDocumentIntent = newDocumentIntent();
     92       if (useMultipleTasks) {
     93           newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
     94       }
     95       startActivity(newDocumentIntent);
     96   }
     97 
     98   private Intent newDocumentIntent() {
     99       boolean useMultipleTasks = mCheckbox.isChecked();
    100       final Intent newDocumentIntent = new Intent(this, NewDocumentActivity.class);
    101       newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
    102       newDocumentIntent.putExtra(KEY_EXTRA_NEW_DOCUMENT_COUNTER, incrementAndGet());
    103       return newDocumentIntent;
    104   }
    105 
    106   private static int incrementAndGet() {
    107       Log.d(TAG, "incrementAndGet(): " + mDocumentCounter);
    108       return mDocumentCounter++;
    109   }
    110 }
    111 </pre>
    112 
    113 <p class="note"><strong>Lu :</strong> Cc hot ng c khi chy bng c {@code FLAG_ACTIVITY_NEW_DOCUMENT}
    114 phi c gi tr thuc tnh {@code android:launchMode="standard"} (mc nh) c t trong
    115 bn k khai.</p>
    116 
    117 <p>Khi hot ng chnh khi chy mt hot ng mi, h thng s tm kim thng qua cc tc v hin ti 
    118 xem tc v no c  nh khp vi tn thnh phn  nh v d liu  nh cho hot ng . Nu tc v
    119 khng c tm thy, hoc  nh cha c {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK}
    120 th mt tc v mi s c to ly hot ng lm gc. Nu tm thy, n s mang tc v 
    121 ti pha trc v chuyn  nh mi ti {@link android.app.Activity#onNewIntent onNewIntent()}.
    122 Hot ng mi s nhn  nh v to mt ti liu mi trong mn hnh tng quan, nh trong v d
    123 sau:</p>
    124 
    125 <p class="code-caption"><a href="{@docRoot}samples/DocumentCentricApps/index.html">
    126 NewDocumentActivity.java</a></p>
    127 <pre>
    128 &#64;Override
    129 protected void onCreate(Bundle savedInstanceState) {
    130     super.onCreate(savedInstanceState);
    131     setContentView(R.layout.activity_new_document);
    132     mDocumentCount = getIntent()
    133             .getIntExtra(DocumentCentricActivity.KEY_EXTRA_NEW_DOCUMENT_COUNTER, 0);
    134     mDocumentCounterTextView = (TextView) findViewById(
    135             R.id.hello_new_document_text_view);
    136     setDocumentCounterText(R.string.hello_new_document_counter);
    137 }
    138 
    139 &#64;Override
    140 protected void onNewIntent(Intent intent) {
    141     super.onNewIntent(intent);
    142     /* If FLAG_ACTIVITY_MULTIPLE_TASK has not been used, this activity
    143     is reused to create a new document.
    144      */
    145     setDocumentCounterText(R.string.reusing_document_counter);
    146 }
    147 </pre>
    148 
    149 
    150 <h3 id="#attr-doclaunch">S dng thuc tnh hot ng  thm mt tc v</h3>
    151 
    152 <p>Mt hot ng cng c th quy nh trong bn k khai ca n rng n lun khi chy vo mt tc v mi bng cch s dng
    153 thuc tnh <code><a href="{@docRoot}guide/topics/manifest/activity-element.html">&lt;activity&gt;</a></code>
    154 , <a href="{@docRoot}guide/topics/manifest/activity-element.html#dlmode">
    155 {@code android:documentLaunchMode}</a>. Thuc tnh ny c bn gi tr to ra hiu ng
    156 sau khi ngi dng m mt ti liu bng ng dng:</p>
    157 
    158 <dl>
    159   <dt>"{@code intoExisting}"</dt>
    160   <dd>Hot ng s dng li mt tc v hin c cho ti liu. iu ny ging nh khi thit t c
    161   {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT} m <em>khng</em> thit t c
    162   {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK}, nh c m t trong phn
    163   <a href="#flag-new-doc">S dng c  nh  thm mt tc v</a> bn trn.</dd>
    164 
    165   <dt>"{@code always}"</dt>
    166   <dd>Hot ng to mt tc v mi cho ti liu, ngay c khi ti liu  c m. S dng
    167   gi tr ny ging nh thit t c c {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT}
    168   v {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK}.</dd>
    169 
    170   <dt>"{@code none}"</dt>
    171   <dd>Hot ng khng to mt tc v mi cho ti liu. Mn hnh tng quan x l hot ng
    172   nh theo mc nh: n hin th mt tc v n l cho ng dng, tc v ny
    173   tip tc t bt k hot ng no m ngi dng  gi ra cui cng.</dd>
    174 
    175   <dt>"{@code never}"</dt>
    176   <dd>Hot ng khng to mt tc v mi cho ti liu. Vic thit t gi tr ny s khng ch
    177   hnh vi ca {@link android.content.Intent#FLAG_ACTIVITY_NEW_DOCUMENT}
    178   v c {@link android.content.Intent#FLAG_ACTIVITY_MULTIPLE_TASK}, nu mt trong hai c t
    179   trong  nh, v mn hnh tng quan s hin th mt tc v n l cho ng dng, tc v ny tip tc t
    180   bt k hot ng no m ngi dng  gi ra cui cng.</dd>
    181 </dl>
    182 
    183 <p class="note"><strong>Lu :</strong> i vi nhng gi tr ngoi {@code none} v {@code never}
    184 hot ng phi c nh ngha bng {@code launchMode="standard"}. Nu thuc tnh ny khng c quy nh th
    185 {@code documentLaunchMode="none"} s c s dng.</p>
    186 
    187 <h2 id="removing">Loi b Tc v</h2>
    188 
    189 <p>Theo mc nh, mt tc v ti liu s t ng c loi b khi mn hnh tng quan khi hot ng ca n
    190 hon thnh. Bn c th khng ch hnh vi ny bng lp {@link android.app.ActivityManager.AppTask},
    191 bng mt c {@link android.content.Intent}, hoc bng mt thuc tnh<code><a href="{@docRoot}guide/topics/manifest/activity-element.html">
    192 &lt;activity&gt;</a></code>.</p>
    193 
    194 <p>Bn c th lun loi tr hon ton mt tc v khi mn hnh tng quan bng cch thit t thuc tnh
    195 <code><a href="{@docRoot}guide/topics/manifest/activity-element.html">&lt;activity&gt;</a></code>
    196 , <a href="{@docRoot}guide/topics/manifest/activity-element.html#exclude">
    197 {@code android:excludeFromRecents}</a> thnh {@code true}.</p>
    198 
    199 <p>Bn c th thit t s lng tc v ti a m ng dng ca bn c th bao gm trong mn hnh tng quan bng cch t thuc tnh
    200  <code><a href="{@docRoot}guide/topics/manifest/activity-element.html">&lt;activity&gt;</a></code>
    201  <a href="{@docRoot}guide/topics/manifest/activity-element.html#maxrecents">{@code android:maxRecents}
    202 </a> thnh mt gi tr s nguyn. Mc nh l 16. Khi t c s tc v ti a,
    203 tc v t s dng gn y nht s b loi b khi mn hnh tng quan. Gi tr ti a {@code android:maxRecents}
    204 bng 50 (25 trn cc thit b c b nh thp); gi tr thp hn 1 khng hp l.</p>
    205 
    206 <h3 id="#apptask-remove">S dng lp AppTask  loi b tc v</h3>
    207 
    208 <p>Trong hot ng m to mt tc v mi trong mn hnh tng quan, bn c th
    209 quy nh khi no th loi b tc v v hon thnh tt c cc hot ng gn lin vi n bng cch gi
    210 phng php {@link android.app.ActivityManager.AppTask#finishAndRemoveTask() finishAndRemoveTask()}.</p>
    211 
    212 <p class="code-caption"><a href="{@docRoot}samples/DocumentCentricApps/index.html">
    213 NewDocumentActivity.java</a></p>
    214 <pre>
    215 public void onRemoveFromRecents(View view) {
    216     // The document is no longer needed; remove its task.
    217     finishAndRemoveTask();
    218 }
    219 </pre>
    220 
    221 <p class="note"><strong>Lu :</strong> S dng phng php
    222 {@link android.app.ActivityManager.AppTask#finishAndRemoveTask() finishAndRemoveTask()}
    223 s khng ch vic s dng tag {@link android.content.Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS},
    224 nh c trnh by  bn di.</p>
    225 
    226 <h3 id="#retain-finished">Gi li tc v  hon thnh</h3>
    227 
    228 <p>Nu bn mun gi li mt tc v trong mn hnh tng quan, ngay c khi hot ng ca n  hon thnh, hy chuyn
    229 c {@link android.content.Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS} trong phng php
    230 {@link android.content.Intent#addFlags(int) addFlags()} ca  nh m khi chy hot ng.</p>
    231 
    232 <p class="code-caption"><a href="{@docRoot}samples/DocumentCentricApps/index.html">
    233 DocumentCentricActivity.java</a></p>
    234 <pre>
    235 private Intent newDocumentIntent() {
    236     final Intent newDocumentIntent = new Intent(this, NewDocumentActivity.class);
    237     newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
    238       android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS);
    239     newDocumentIntent.putExtra(KEY_EXTRA_NEW_DOCUMENT_COUNTER, incrementAndGet());
    240     return newDocumentIntent;
    241 }
    242 </pre>
    243 
    244 <p> t c cng kt qu nh vy, hy t thuc tnh
    245 <code><a href="{@docRoot}guide/topics/manifest/activity-element.html">&lt;activity&gt;</a></code>
    246 <a href="{@docRoot}guide/topics/manifest/activity-element.html#autoremrecents">
    247 {@code android:autoRemoveFromRecents}</a> thnh {@code false}. Gi tr mc nh bng {@code true}
    248 i vi cc hot ng ti liu, v {@code false} i vi cc hot ng thng thng. Vic s dng thuc tnh ny s khng ch
    249 c {@link android.content.Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS}, nh  trnh by trc .</p>
    250 
    251 
    252 
    253 
    254 
    255 
    256 
    257