Home | History | Annotate | Download | only in components
      1 page.title=Phn on
      2 parent.title=Hot ng
      3 parent.link=activities.html
      4 @jd:body
      5 
      6 <div id="qv-wrapper">
      7 <div id="qv">
      8   <h2>Trong ti liu ny</h2>
      9   <ol>
     10     <li><a href="#Design">Trit l Thit k</a></li>
     11     <li><a href="#Creating">To mt Phn on</a>
     12       <ol>
     13         <li><a href="#UI">Thm mt giao din ngi dng</a></li>
     14         <li><a href="#Adding">Thm mt phn on vo mt hot ng</a></li>
     15       </ol>
     16     </li>
     17     <li><a href="#Managing">Qun l Phn on</a></li>
     18     <li><a href="#Transactions">Thc hin Giao tc Phn on</a></li>
     19     <li><a href="#CommunicatingWithActivity">Giao tip vi Hot ng</a>
     20       <ol>
     21         <li><a href="#EventCallbacks">To gi li s kin cho hot ng</a></li>
     22         <li><a href="#ActionBar">Thm mc vo Thanh Hnh ng</a></li>
     23       </ol>
     24     </li>
     25     <li><a href="#Lifecycle">X l Vng i ca Phn on</a>
     26       <ol>
     27         <li><a href="#CoordinatingWithActivity">Phi hp vi vng i ca hot ng</a></li>
     28       </ol>
     29     </li>
     30     <li><a href="#Example">V d</a></li>
     31   </ol>
     32 
     33   <h2>Lp kha</h2>
     34   <ol>
     35     <li>{@link android.app.Fragment}</li>
     36     <li>{@link android.app.FragmentManager}</li>
     37     <li>{@link android.app.FragmentTransaction}</li>
     38   </ol>
     39   
     40   <h2>Xem thm</h2>
     41   <ol>
     42     <li><a href="{@docRoot}training/basics/fragments/index.html">Xy dng mt UI ng bng cc Phn on</a></li>
     43     <li><a href="{@docRoot}guide/practices/tablets-and-handsets.html">H tr My tnh bng
     44 v Thit b cm tay</a></li>
     45   </ol>
     46 </div>
     47 </div>
     48 
     49 <p>{@link android.app.Fragment} biu din mt hnh vi hay mt phn giao din ngi dng trong mt
     50 {@link android.app.Activity}. Bn c th kt hp nhiu phn on trong mt hot ng duy nht  xy dng mt
     51 UI nhiu bng v s dng li phn on trong nhiu hot ng. Bn c th coi phn on nh l mt
     52 phn m-un ca mt hot ng, c vng i ca chnh n, nhn cc s kin u vo ca chnh n, v
     53 bn c th thm hoc g b trong khi hot ng ang chy (kiu nh mt "hot ng con" m
     54 bn c th s dng li trong cc hot ng khc nhau).</p>
     55 
     56 <p>Phn on phi lun c nhng trong mt hot ng v vng i ca phn on b nh hng trc tip bi
     57 vng i ca hot ng ch. V d, khi hot ng b tm dng, tt c
     58 phn on trong n cng vy, v khi hot ng b hy, tt c phn on cng vy. Tuy nhin, trong khi mt
     59 hot ng ang chy (n  trong trng thi vng i <em>c tip tc</em><a href="{@docRoot}guide/components/activities.html#Lifecycle"></a>), bn c th
     60 thao tc tng phn on c lp, chng hn nh thm hay xa chng. Khi bn thc hin mt
     61 giao tc phn on, bn cng c th thm n vo mt ngn xp c qun l bi
     62 hot ng &mdash;tng mc nhp vo ngn xp trong hot ng l mt bn ghi giao tc phn on
     63  xy ra. Ngn xp cho php ngi dng o ngc mt giao tc phn on (iu hng ngc li),
     64 bng cch nhn nt <em>Quay li</em>.</p>
     65 
     66 <p>Khi bn thm mt phn on nh mt phn trong b tr hot ng ca mnh, n s  trong mt {@link
     67 android.view.ViewGroup} bn trong phn cp dng xem ca hot ng  v phn on ny s nh ngha b tr
     68 dng xem ca chnh n.
     69 Bn c th chn mt phn on vo b tr hot ng ca mnh bng cch khai bo phn on trong tp
     70 b tr ca hot ng, di dng mt phn t {@code &lt;fragment&gt;}, hoc t m ng dng ca bn bng cch thm n vo mt
     71 {@link android.view.ViewGroup} hin hu. Tuy nhin, khng bt buc phi c mt phn on l mt b phn ca b tr hot ng
     72 ; bn cng c th s dng mt phn on m khng cn UI ca chnh n nh mt trnh thc hin v hnh cho hot ng
     73 .</p>
     74 
     75 <p>Ti liu ny m t cch xy dng ng dng ca bn  s dng phn on, bao gm
     76 cch cc phn on c th duy tr trng thi ca chng khi c thm vo ngn xp ca hot ng, chia s
     77 cc s kin vi hot ng v cc phn on khc trong hot ng, ng gp vo thanh hnh ng ca hot ng
     78 v nhiu thng tin khc.</p>
     79 
     80 
     81 <h2 id="Design">Trit l Thit k</h2>
     82 
     83 <p>Android gii thiu phn on trong phin bn Android 3.0 (API mc 11), ch yu nhm h tr
     84 cc thit k UI ng v linh hot hn trn mn hnh ln, chng hn nh my tnh bng. V
     85 mn hnh ca my tnh bng ln hn nhiu mn hnh ca thit b cm tay, c nhiu khong trng hn  kt hp v
     86 trao i cc thnh phn UI. Phn on cho php nhng thit k nh vy m khng cn bn phi qun l nhng thay i
     87 phc tp v phn cp dng xem. Bng cch chia b tr ca mt hot ng thnh cc phn on, bn c th
     88 sa i din mo ca hot ng vo thi gian chy v gi nhng thay i  trong mt ngn xp
     89 c qun l bi hot ng.</p>
     90 
     91 <p>V d, mt ng dng tin tc c th s dng mt phn on  hin th mt danh sch bi vit 
     92 bn tri v mt phn on khc  hin th mt bi vit  bn phi&mdash;c hai phn on u xut hin trong mt
     93 hot ng, bn cnh nhau, v tng phn on c tp phng php gi li vng i ring v x l
     94 cc s kin nhp liu ngi dng ring ca mnh. V th, thay v s dng mt hot ng  chn mt bi vit v mt
     95 hot ng khc  c bi vit, ngi dng c th chn mt bi vit v c n trong cng
     96 hot ng, nh c minh ha trong b tr my tnh bng trong hnh 1.</p>
     97 
     98 <p>Bn nn thit k tng phn on nh mt thnh phn hot ng dng m-un v c th s dng li.  l bi
     99 mi phn on s nh ngha b tr v hnh vi ca chnh n vi cc phng php gi li vng i ca chnh n, bn c th
    100 bao gm mt phn on trong nhiu hot ng, v th bn nn thit k  ti s dng v trnh trc tip
    101 thao tc mt phn on t mt phn on khc. iu ny c bit quan trng v mt phn on
    102 m-un cho php bn thay i kt hp phn on ca mnh cho cc kch c mn hnh khc nhau. Khi thit k
    103 ng dng ca bn  h tr c my tnh bng v thit b cm tay, bn c th s dng li phn on ca mnh trong cc cu hnh
    104 b tr khc nhau nhm ti u ha tri nghim ngi dng da trn khng gian mn hnh c sn. V
    105 d, trn mt thit b cm tay, c th cn phi tch ring cc phn on  cung cp mt UI n bng khi m
    106 khng th lm va kht nhiu hn mt phn on trong cng hot ng.</p>
    107 
    108 <img src="{@docRoot}images/fundamentals/fragments.png" alt="" />
    109 <p class="img-caption"><strong>Hnh 1.</strong> V d v cch hai m-un UI c nh ngha
    110 bi cc phn on c th c kt hp thnh mt hot ng i vi thit k my tnh bng, nhng c tch ring i vi
    111 thit k thit b cm tay.</p>
    112 
    113 <p>V d&mdash; tip tc vi v d v ng dng tin tc&mdash;ng dng c th nhng
    114 hai phn on trong <em>Hot ng A</em>, khi ang chy trn mt thit b c kch c my tnh bng. Tuy nhin, trn mt
    115 mn hnh kch c thit b cm tay, khng c  khong trng cho c hai phn on, v th <em>Hot ng A</em> ch
    116 bao gm phn on cho danh sch bi vit, v khi ngi dng chn mt bi vit, n s khi ng
    117 <em>Hot ng B</em>, hot ng ny cha phn on th hai l c bi vit. V th, ng dng
    118 h tr c my tnh bng v thit b cm tay bng cch s dng li cc phn on theo cc cch kt hp khc nhau nh c minh ha trong
    119 hnh 1.</p>
    120 
    121 <p> bit thm thng tin v vic thit k ng dng ca bn bng cc cch kt hp phn on khc nhau cho
    122 cu hnh mn hnh khc nhau, hy xem hng dn <a href="{@docRoot}guide/practices/tablets-and-handsets.html">H tr My tnh bng v Thit b cm tay</a>.</p>
    123 
    124 
    125 
    126 <h2 id="Creating">To mt Phn on</h2>
    127 
    128 <div class="figure" style="width:327px">
    129 <img src="{@docRoot}images/fragment_lifecycle.png" alt="" />
    130 <p class="img-caption"><strong>Hnh 2.</strong> Vng i ca mt phn on (trong khi hot ng
    131 ca n ang chy).</p>
    132 </div>
    133 
    134 <p> to mt phn on, bn phi to mt lp con ca {@link android.app.Fragment} (hoc
    135 mt lp con hin ti ca n). Lp {@link android.app.Fragment} c m trng rt ging
    136 mt {@link android.app.Activity}. N cha cc phng php gi li tng t nh hot ng, chng
    137 hn nh {@link android.app.Fragment#onCreate onCreate()}, {@link android.app.Fragment#onStart onStart()},
    138 {@link android.app.Fragment#onPause onPause()}, v {@link android.app.Fragment#onStop onStop()}. Trn
    139 thc t, nu bn ang chuyn i mt ng dng Android hin ti  s dng cc phn on, bn c th ch cn di chuyn
    140 m khi cc phng php gi li ca hot ng ca bn vo cc phng php gi li tng ng ca phn on
    141 ca bn.</p>
    142 
    143 <p>Thng th t nht bn nn trin khai cc phng php vng i sau:</p>
    144 
    145 <dl>
    146   <dt>{@link android.app.Fragment#onCreate onCreate()}</dt>
    147   <dd>H thng s gi phng php ny khi to phn on. Trong trin khai ca mnh, bn nn
    148 khi to cc thnh phn thit yu ca phn on m bn mun gi li khi phn on
    149 b tm dng hoc dng hn, sau  tip tc.</dd>
    150   <dt>{@link android.app.Fragment#onCreateView onCreateView()}</dt>
    151   <dd>H thng s gi phng php ny khi n lc phn on v giao din ngi dng ca n 
    152 ln u tin.  v mt UI cho phn on ca mnh, bn phi tr v mt {@link android.view.View} t phng php
    153 ny, y l gc ca b tr phn on ca bn. Bn c th tr v gi tr rng nu phn on khng
    154 cung cp UI.</dd>
    155   <dt>{@link android.app.Activity#onPause onPause()}</dt>
    156   <dd>H thng gi phng php ny l du hiu u tin v vic ngi dng ang ri khi
    157 phn on (mc d khng phi lc no cng c ngha rng phn on ang b hy). Trng hp ny thng l khi bn
    158 nh thc hin bt k thay i no vn cn c hiu lc ngoi phin ca ngi dng hin thi (v
    159 ngi dng c th khng quay li).</dd>
    160 </dl>
    161 
    162 <p>Phn ln ng dng nn trin khai t nht ba phng php sau i vi mi phn on, nhng c mt vi
    163 phng php gi li khc m bn cng nn s dng  x l cc giai on khc nhau trong
    164 vng i ca phn on. Tt c phng php gi li vng i c  cp chi tit hn trong phn
    165 v <a href="#Lifecycle">X l Vng i ca Phn on</a>.</p>
    166 
    167 
    168 <p>Cng c mt vi lp con m bn c th mun m rng thay v lp c bn {@link
    169 android.app.Fragment}:</p>
    170 
    171 <dl>
    172   <dt>{@link android.app.DialogFragment}</dt>
    173   <dd>Hin th mt hp thoi tri ni. S dng lp ny  to mt hp thoi l mt phng n hay cho vic s dng cc phng php trnh tr gip
    174 hp thoi trong lp {@link android.app.Activity}, v bn c th
    175 kt hp mt hp thoi phn on vo ngn xp ca cc phn on c qun l bi hot ng,
    176 cho php ngi dng tr v mt phn on b b.</dd>
    177 
    178   <dt>{@link android.app.ListFragment}</dt>
    179   <dd>Hin th mt danh sch cc mc c qun l bi mt trnh iu hp (chng hn nh mt {@link
    180 android.widget.SimpleCursorAdapter}), tng t nh {@link android.app.ListActivity}. N cung cp
    181 mt vi phng php  qun l mt dng xem danh sch, chng hn nh phng php gi li {@link
    182 android.app.ListFragment#onListItemClick(ListView,View,int,long) onListItemClick()} 
    183 x l cc s kin nhp.</dd>
    184 
    185   <dt>{@link android.preference.PreferenceFragment}</dt>
    186   <dd>Hin th mt phn cp cc i tng {@link android.preference.Preference} di dng mt danh sch, tng t nh
    187 {@link android.preference.PreferenceActivity}. iu ny hu ch khi to mt hot ng "thit t"
    188 cho ng dng ca bn.</dd>
    189 </dl>
    190 
    191 
    192 <h3 id="UI">Thm mt giao din ngi dng</h3>
    193 
    194 <p>Phn on thng c s dng nh mt phn giao din ngi dng ca hot ng v ng gp b tr ca
    195 chnh n cho hot ng.</p>
    196 
    197 <p> cung cp mt b tr cho mt phn on, bn phi trin khai phng php gi li {@link
    198 android.app.Fragment#onCreateView onCreateView()}, phng php ny c h thng Android gi
    199 khi n lc phn on v b tr ca n. Vic bn trin khai phng php ny phi tr v mt
    200 {@link android.view.View} l phn gc cho b tr phn on ca bn.</p>
    201 
    202 <p class="note"><strong>Lu :</strong> Nu phn on ca bn l mt lp con ca {@link
    203 android.app.ListFragment}, trin khai mc nh s tr v mt {@link android.widget.ListView} t
    204 {@link android.app.Fragment#onCreateView onCreateView()}, v th bn khng cn trin khai n.</p>
    205 
    206 <p> tr v mt b tr t {@link
    207 android.app.Fragment#onCreateView onCreateView()}, bn c th bung n t mt <a href="{@docRoot}guide/topics/resources/layout-resource.html">ti nguyn b tr</a> c nh ngha trong XML. 
    208 gip bn lm vy, {@link android.app.Fragment#onCreateView onCreateView()} cung cp mt i tng
    209 {@link android.view.LayoutInflater}.</p>
    210 
    211 <p>V d, sau y l mt lp con ca {@link android.app.Fragment} vi chc nng np mt b tr t tp
    212 {@code example_fragment.xml}:</p>
    213 
    214 <pre>
    215 public static class ExampleFragment extends Fragment {
    216     &#64;Override
    217     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    218                              Bundle savedInstanceState) {
    219         // Inflate the layout for this fragment
    220         return inflater.inflate(R.layout.example_fragment, container, false);
    221     }
    222 }
    223 </pre>
    224 
    225 <div class="sidebox-wrapper">
    226 <div class="sidebox">
    227   <h3>To mt b tr</h3>
    228   <p>Trong v d trn, {@code R.layout.example_fragment} l mt tham chiu ti ti nguyn b tr
    229 c tn {@code example_fragment.xml} c lu trong ti nguyn ng dng.  bit thng tin v cch
    230 to mt b tr trong XML, hy xem ti liu <a href="{@docRoot}guide/topics/ui/index.html">Giao din Ngi dng</a>
    231 .</p>
    232 </div>
    233 </div>
    234 
    235 <p>Tham s {@code container} c chuyn ti {@link android.app.Fragment#onCreateView
    236 onCreateView()} l {@link android.view.ViewGroup} m (tc b tr ca hot ng), trong 
    237 b tr phn on ca bn
    238 s c chn vo. Tham s {@code savedInstanceState} l mt {@link android.os.Bundle} c chc nng
    239 cung cp d liu v thc th trc  ca phn on, nu phn on ang c tip tc
    240 (vic khi phc trng thi c bn k hn trong phn v <a href="#Lifecycle">X l
    241 Vng i ca Phn on</a>).</p>
    242 
    243 <p>Phng php {@link android.view.LayoutInflater#inflate(int,ViewGroup,boolean) inflate()} c
    244 ba tham i:</p>
    245 <ul>
    246   <li>ID ti nguyn ca b tr m bn mun bung.</li>
    247   <li>{@link android.view.ViewGroup} l m ca b tr c bung. Vic chuyn {@code
    248 container} c vai tr quan trng  h thng p dng cc tham s b tr cho dng xem gc ca b tr
    249 c bung, c quy nh bi dng xem m l ni m n din ra trong .</li>
    250   <li>Mt boolean cho bit b tr c bung c nn c gn vi {@link
    251 android.view.ViewGroup} (tham s th hai) trong khi bung hay khng. (Trong trng hp ny, iu ny l
    252 sai v h thng  ang chn b tr c bung vo {@code
    253 container}&mdash;vic chuyn ng s to ra mt nhm dng xem tha trong b tr cui cng.)</li>
    254 </ul>
    255 
    256 <p>Gi bn  thy cch to mt phn on nhm cung cp mt b tr. Tip theo, bn cn thm
    257 phn on vo hot ng ca mnh.</p>
    258 
    259 
    260 
    261 <h3 id="Adding">Thm mt phn on vo mt hot ng</h3>
    262 
    263 <p>Thng th mt phn on ng gp mt phn UI vo hot ng ch, n c nhng nh mt phn
    264 trong phn cp dng xem tng th ca hot ng. C hai cch m bn c th thm mt phn on vo b tr
    265 ca hot ng:</p>
    266 
    267 <ul>
    268   <li><b>Khai bo phn on bn trong tp b tr ca hot ng.</b>
    269 <p>Trong trng hp ny, bn c th
    270 ch nh cc tnh cht b tr cho phn on nh th n l mt dng xem. V d, sau y l tp b tr
    271 cho mt hot ng c hai phn on:</p>
    272 <pre>
    273 &lt;?xml version="1.0" encoding="utf-8"?&gt;
    274 &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    275     android:orientation="horizontal"
    276     android:layout_width="match_parent"
    277     android:layout_height="match_parent"&gt;
    278     &lt;fragment android:name="com.example.news.ArticleListFragment"
    279             android:id="@+id/list"
    280             android:layout_weight="1"
    281             android:layout_width="0dp"
    282             android:layout_height="match_parent" /&gt;
    283     &lt;fragment android:name="com.example.news.ArticleReaderFragment"
    284             android:id="@+id/viewer"
    285             android:layout_weight="2"
    286             android:layout_width="0dp"
    287             android:layout_height="match_parent" /&gt;
    288 &lt;/LinearLayout&gt;
    289 </pre>
    290   <p>Thuc tnh {@code android:name} trong {@code &lt;fragment&gt;} s ch nh lp {@link
    291 android.app.Fragment}  khi to trong b tr.</p>
    292 
    293 <p>Khi h thng to b tr hot ng ny, n s khi to tng phn on c ch nh trong b tr
    294 v gi ra phng php {@link android.app.Fragment#onCreateView onCreateView()} cho tng phn on,
    295  truy xut b tr ca tng phn on. H thng s chn {@link android.view.View} c tr v bi phn on
    296 trc tip th ch phn t {@code &lt;fragment&gt;}.</p>
    297 
    298 <div class="note">
    299   <p><strong>Lu :</strong> Mi phn on yu cu mt m nh danh duy nht
    300 m h thng c th s dng  khi phc phn on nu hot ng b khi ng li (v bn c th s dng 
    301 nm bt phn on s thc hin giao tc, chng hn nh g b n). C ba cch  cung cp ID cho mt
    302 phn on:</p>
    303   <ul>
    304     <li>Cung cp thuc tnh {@code android:id} vi mt ID duy nht.</li>
    305     <li>Cung cp thuc tnh {@code android:tag} vi mt xu duy nht.</li>
    306     <li>Nu bn khng cung cp c thuc tnh no, h thng s s dng ID ca dng xem 
    307 ca b cha.</li>
    308   </ul>
    309 </div>
    310   </li>
    311 
    312   <li><b>Hoc, bng cch lp trnh, thm phn on vo mt {@link android.view.ViewGroup} hin hu.</b>
    313 <p>Vo bt c lc no trong khi hot ng ca bn ang chy, bn c th thm phn on vo b tr hot ng ca mnh. Bn
    314 ch cn ch nh mt {@link
    315 android.view.ViewGroup} l ni m bn s t phn on vo.</p>
    316   <p> thc hin giao tc phn on trong hot ng ca mnh (chng hn nh thm, g b, hay thay th mt
    317 phn on), bn phi s dng cc API t {@link android.app.FragmentTransaction}. Bn c th nhn mt thc th
    318 ca {@link android.app.FragmentTransaction} t {@link android.app.Activity} ca mnh nh sau:</p>
    319 
    320 <pre>
    321 FragmentManager fragmentManager = {@link android.app.Activity#getFragmentManager()}
    322 FragmentTransaction fragmentTransaction = fragmentManager.{@link android.app.FragmentManager#beginTransaction()};
    323 </pre>
    324 
    325 <p>Sau , bn c th thm mt phn on bng cch s dng phng php {@link
    326 android.app.FragmentTransaction#add(int,Fragment) add()}, ch nh phn on s thm v
    327 dng xem m bn s chn n vo. V d:</p>
    328 
    329 <pre>
    330 ExampleFragment fragment = new ExampleFragment();
    331 fragmentTransaction.add(R.id.fragment_container, fragment);
    332 fragmentTransaction.commit();
    333 </pre>
    334 
    335   <p>Tham i u tin c chuyn cho {@link android.app.FragmentTransaction#add(int,Fragment) add()}
    336 l {@link android.view.ViewGroup}, l ni m phn on s c t vo, c ch nh bi
    337 ID ti nguyn, v tham i th hai l phn on cn thm.</p>
    338   <p>Sau khi bn  thc hin cc thay i ca mnh bng
    339 {@link android.app.FragmentTransaction}, bn phi
    340 gi {@link android.app.FragmentTransaction#commit}  cc thay i c hiu lc.</p>
    341   </li>
    342 </ul>
    343 
    344 
    345 <h4 id="AddingWithoutUI">Thm mt phn on khng c UI</h4>
    346 
    347 <p>Cc v d nu trn cho bit cch thm mt phn on vo hot ng ca bn  cung cp mt UI. Tuy nhin,
    348 bn cng c th s dng mt phn on  cung cp mt hnh vi chy ngm cho hot ng m khng cn a
    349 UI b sung.</p>
    350 
    351 <p> thm mt phn on khng c UI, hy thm phn on t hot ng ang bng cch s dng {@link
    352 android.app.FragmentTransaction#add(Fragment,String)} (cung cp mt "tag" xu duy nht cho phn on
    353 , thay v mt ID dng xem). Lm vy s thm phn on, nhng v khng lin kt vi mt dng xem
    354 trong b tr hot ng, n s khng nhn c lnh gi ti {@link
    355 android.app.Fragment#onCreateView onCreateView()}. V th, bn khng cn trin khai phng php .</p>
    356 
    357 <p>Vic cung cp tag xu cho phn on khng ch p dng cho cc phn on khng c UI&mdash;bn cng c th
    358 cung cp tag xu cho phn on c UI&mdash;nhng nu phn on khng c
    359 UI, khi , tag xu l cch duy nht  nhn bit n. Nu sau ny bn mun nhn phn on t
    360 hot ng, bn cn s dng {@link android.app.FragmentManager#findFragmentByTag
    361 findFragmentByTag()}.</p>
    362 
    363 <p> bit v d v hot ng s dng phn on nh mt trnh thc hin nn, khng c UI, hy xem mu {@code
    364 FragmentRetainInstance.java}, mu ny c trong cc mu SDK (c sn thng qua
    365 Trnh qun l SDK Android) v nm trn h thng ca bn nh l 
    366 <code>&lt;sdk_root&gt;/APIDemos/app/src/main/java/com/example/android/apis/app/FragmentRetainInstance.java</code>.</p>
    367 
    368 
    369 
    370 <h2 id="Managing">Qun l Phn on</h2>
    371 
    372 <p> qun l cc phn on trong hot ng ca mnh, bn cn s dng {@link android.app.FragmentManager}. 
    373 c n, hy gi {@link android.app.Activity#getFragmentManager()} t hot ng ca bn.</p>
    374 
    375 <p>Mt s vic bn c th lm vi {@link android.app.FragmentManager} bao gm:</p>
    376 
    377 <ul>
    378   <li>Nhn cc phn on tn ti trong hot ng, bng {@link
    379 android.app.FragmentManager#findFragmentById findFragmentById()} (i vi cc phn on cung cp UI trong
    380 b tr hot ng) hoc {@link android.app.FragmentManager#findFragmentByTag
    381 findFragmentByTag()} (i vi cc phn on c hoc khng cung cp UI).</li> 
    382   <li>Ly phn on ra khi ngn xp, bng {@link
    383 android.app.FragmentManager#popBackStack()} (m phng mt cu lnh <em>Quay li</em> ca ngi dng).</li>
    384   <li>ng k mt i tng theo di cho nhng thay i i vi ngn xp, bng {@link
    385 android.app.FragmentManager#addOnBackStackChangedListener addOnBackStackChangedListener()}.</li>
    386 </ul>
    387 
    388 <p> bit thm thng tin v nhng phng php ny v phng php khc, hy tham kho ti liu lp {@link
    389 android.app.FragmentManager}.</p>
    390 
    391 <p>Nh minh ha trong phn trc, bn cng c th s dng {@link android.app.FragmentManager}
    392  m mt {@link android.app.FragmentTransaction}, n cho php bn thc hin cc giao tc, v d nh
    393 thm hoc g b phn on.</p>
    394 
    395 
    396 <h2 id="Transactions">Thc hin Giao tc Phn on</h2>
    397 
    398 <p>Mt tnh nng tuyt vi khi s dng phn on trong hot ng ca bn  l kh nng thm, g b, thay th,
    399 v thc hin cc hnh ng khc vi chng,  hi p li tng tc ca ngi dng. Mi tp hp thay i m bn
    400 thc thi cho hot ng c gi l mt giao tc v bn c th thc hin mt giao tc bng cch s dng cc API trong {@link
    401 android.app.FragmentTransaction}. Bn cng c th lu tng giao tc vo mt ngn xp c qun l bi
    402 hot ng, cho php ngi dng iu hng ngc li thng qua nhng thay i phn on (tng t nh iu hng
    403 ngc li thng qua hot ng).</p>
    404 
    405 <p>Bn c th thu c mt thc th ca {@link android.app.FragmentTransaction} t {@link
    406 android.app.FragmentManager} nh sau:</p>
    407 
    408 <pre>
    409 FragmentManager fragmentManager = {@link android.app.Activity#getFragmentManager()};
    410 FragmentTransaction fragmentTransaction = fragmentManager.{@link android.app.FragmentManager#beginTransaction()};
    411 </pre>
    412 
    413 <p>Mi giao tc l mt tp hp nhng thay i m bn mun thc hin ti cng thi im. Bn c th thit lp
    414 tt c thay i m mnh mun thc hin i vi mt giao tc cho trc bng cch s dng cc phng php nh {@link
    415 android.app.FragmentTransaction#add add()}, {@link android.app.FragmentTransaction#remove remove()},
    416 v {@link android.app.FragmentTransaction#replace replace()}. Sau ,  p dng giao tc
    417 cho hot ng, bn phi gi {@link android.app.FragmentTransaction#commit()}.</p>
    418 </dl>
    419 
    420 <p>Trc khi bn gi {@link
    421 android.app.FragmentTransaction#commit()}, tuy nhin, bn c th mun gi {@link
    422 android.app.FragmentTransaction#addToBackStack addToBackStack()},  thm giao tc
    423 vo mt ngn xp ca cc giao tc phn on. Ngn xp ny c qun l bi hot ng v cho php
    424 ngi dng tr v trng thi phn on trc , bng cch nhp nt <em>Quay li</em>.</p>
    425 
    426 <p>V d, sau y l cch bn c th thay th phn on ny bng phn on khc, v gi nguyn
    427 trng thi trc  ca ngn xp:</p>
    428 
    429 <pre>
    430 // Create new fragment and transaction
    431 Fragment newFragment = new ExampleFragment();
    432 FragmentTransaction transaction = getFragmentManager().beginTransaction();
    433 
    434 // Replace whatever is in the fragment_container view with this fragment,
    435 // and add the transaction to the back stack
    436 transaction.replace(R.id.fragment_container, newFragment);
    437 transaction.addToBackStack(null);
    438 
    439 // Commit the transaction
    440 transaction.commit();
    441 </pre>
    442 
    443 <p>Trong v d ny, {@code newFragment} thay th mi phn on (nu c) hin ang  trong
    444 b cha b tr c nhn bit bi ID {@code R.id.fragment_container}. Bng cch gi {@link
    445 android.app.FragmentTransaction#addToBackStack addToBackStack()}, giao tc thay th
    446 c lu vo ngn xp, v th ngi dng c th o ngc giao tc v mang
    447 giao tc trc  tr li bng cch nhn nt <em>Quay li</em>.</p>
    448 
    449 <p>Nu bn thm nhiu thay i vo giao tc (chng hn nh mt {@link
    450 android.app.FragmentTransaction#add add()} khc hoc {@link android.app.FragmentTransaction#remove
    451 remove()}) v gi {@link
    452 android.app.FragmentTransaction#addToBackStack addToBackStack()}, khi , tt c thay i c p dng
    453 trc khi bn gi {@link android.app.FragmentTransaction#commit commit()} u c thm vo
    454 ngn xp nh mt giao tc ring l v nt <em>Quay li</em> s o ngc tt c cng nhau.</p>
    455 
    456 <p>Th t m bn thm thay i vo mt {@link android.app.FragmentTransaction} khng quan trng,
    457 ngoi tr:</p>
    458 <ul>
    459   <li>Bn phi gi {@link android.app.FragmentTransaction#commit()} cui cng</li>
    460   <li>Nu bn thm nhiu phn on vo cng b cha, khi  th t m
    461 bn thm chng s xc nh th t chng xut hin trong phn cp dng xem</li>
    462 </ul>
    463 
    464 <p>Nu bn khng gi {@link android.app.FragmentTransaction#addToBackStack(String)
    465 addToBackStack()} khi thc hin mt giao tc  xa mt phn on, khi , phn on  s b
    466 hy khi giao tc c thc hin v ngi dng khng th iu hng tr li n. Trong khi , nu bn
    467 gi {@link android.app.FragmentTransaction#addToBackStack(String) addToBackStack()} khi
    468 g b mt phn on, khi  phn on b <em>dng</em> v s c khi phc nu ngi dng iu hng
    469 tr li.</p>
    470 
    471 <p class="note"><strong>Mo:</strong> Vi mi giao tc phn on, bn c th p dng mt hot nh
    472 chuyn tip bng cch gi {@link android.app.FragmentTransaction#setTransition setTransition()} trc khi
    473 thc thi.</p>
    474 
    475 <p>Vic gi {@link android.app.FragmentTransaction#commit()} khng thc hin giao tc
    476 ngay lp tc. Thay vo , n lp lch biu  chy trn lung UI ca hot ng (lung "chnh") ngay khi
    477 lung c th lm vy. Tuy nhin, nu cn, bn c th gi {@link
    478 android.app.FragmentManager#executePendingTransactions()} t lung UI ca mnh  ngay lp tc thc hin
    479 cc giao tc c gi bi {@link android.app.FragmentTransaction#commit()}. Lm vy
    480 thng khng cn thit tr khi giao tc  l ph thuc cho cc tc v  nhng lung khc.</p>
    481 
    482 <p class="caution"><strong>Ch :</strong> Bn c th thc thi mt giao tc bng cch s dng {@link
    483 android.app.FragmentTransaction#commit commit()} ch trc khi hot ng <a href="{@docRoot}guide/components/activities.html#SavingActivityState">lu
    484 trng thi</a> ca n (khi ngi dng ri khi hot ng). Nu bn nh thc thi sau thi im  s pht sinh mt li
    485 ngoi l. Nguyn nhn l v trng thi sau khi thc thi c th b mt nu hot ng
    486 cn c khi phc. i vi nhng trng hp m bn c th mt thc thi, hy s dng {@link
    487 android.app.FragmentTransaction#commitAllowingStateLoss()}.</p>
    488 
    489 
    490 
    491 
    492 <h2 id="CommunicatingWithActivity">Giao tip vi Hot ng</h2>
    493 
    494 <p>Mc d {@link android.app.Fragment} c trin khai nh mt i tng c lp vi
    495 {@link android.app.Activity} v c th c s dng bn trong nhiu hot ng, mt thc th  cho ca
    496 phn on s c gn kt trc tip vi hot ng cha n.</p>
    497 
    498 <p>C th, phn on c th truy cp thc th {@link android.app.Activity} bng {@link
    499 android.app.Fragment#getActivity()} v d dng thc hin cc tc v nh tm mt dng xem trong b tr
    500 hot ng:</p>
    501 
    502 <pre>
    503 View listView = {@link android.app.Fragment#getActivity()}.{@link android.app.Activity#findViewById findViewById}(R.id.list);
    504 </pre>
    505 
    506 <p>Tng t, hot ng ca bn c th gi ra cc phng php trong phn on bng cch thu c mt tham chiu ti
    507 {@link android.app.Fragment} t {@link android.app.FragmentManager}, bng cch s dng {@link
    508 android.app.FragmentManager#findFragmentById findFragmentById()} hoc {@link
    509 android.app.FragmentManager#findFragmentByTag findFragmentByTag()}. V d:</p>
    510 
    511 <pre>
    512 ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
    513 </pre>
    514 
    515 
    516 <h3 id="EventCallbacks">To gi li s kin cho hot ng</h3>
    517 
    518 <p>Trong mt s trng hp, bn c th cn mt phn on  chia s s kin vi hot ng. Mt cch hay  lm iu ny
    519  l nh ngha mt giao din gi li bn trong phn on v yu cu hot ng ch trin khai
    520 n. Khi hot ng nhn c mt lnh gi li thng qua giao din, n c th chia s thng tin vi
    521 cc phn on khc trong b tr nu cn.</p>
    522 
    523 <p>V d, nu mt ng dng tin tc c hai phn on trong mt hot ng&mdash;mt  hin th danh sch
    524 bi vit (phn on A) v mt  hin th mt bi vit (phn on B)&mdash;khi , phn on A phi thng bo vi
    525 hot ng khi no th mt mc danh sch c chn  n c th yu cu phn on B hin th bi vit . Trong
    526 trng hp ny, giao din {@code OnArticleSelectedListener} s c khai bo bn trong phn on A:</p>
    527 
    528 <pre>
    529 public static class FragmentA extends ListFragment {
    530     ...
    531     // Container Activity must implement this interface
    532     public interface OnArticleSelectedListener {
    533         public void onArticleSelected(Uri articleUri);
    534     }
    535     ...
    536 }
    537 </pre>
    538 
    539 <p>Khi , hot ng lu tr phn on s trin khai giao din {@code OnArticleSelectedListener}
    540 v
    541 khng ch {@code onArticleSelected()}  thng bo vi phn on B v s kin t phn on A.  m bo
    542 rng hot ng ch trin khai giao din ny, phng php gi li {@link
    543 android.app.Fragment#onAttach onAttach()} ca phn on A (m h thng gi khi thm
    544 phn on vo hot ng) s khi to mt thc th ca {@code OnArticleSelectedListener} bng cch
    545 i kiu {@link android.app.Activity} m c chuyn vo {@link android.app.Fragment#onAttach
    546 onAttach()}:</p>
    547 
    548 <pre>
    549 public static class FragmentA extends ListFragment {
    550     OnArticleSelectedListener mListener;
    551     ...
    552     &#64;Override
    553     public void onAttach(Activity activity) {
    554         super.onAttach(activity);
    555         try {
    556             mListener = (OnArticleSelectedListener) activity;
    557         } catch (ClassCastException e) {
    558             throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
    559         }
    560     }
    561     ...
    562 }
    563 </pre>
    564 
    565 <p>Nu hot ng cha trin khai giao din, khi  phn on s a ra li 
    566 {@link java.lang.ClassCastException}.
    567 Nu thnh cng, thnh vin {@code mListener} gi mt tham chiu ti trin khai 
    568 {@code OnArticleSelectedListener}ca hot ng, sao cho phn on A c th chia s s kin vi hot ng bng cch gi cc phng php
    569 c nh ngha bi giao din {@code OnArticleSelectedListener}. V d, nu phn on A l mt
    570 phn m rng ca {@link android.app.ListFragment}, mi ln
    571 ngi dng nhp vo mt mc danh sch, h thng s gi ra {@link android.app.ListFragment#onListItemClick
    572 onListItemClick()} trong phn on, v n li gi {@code onArticleSelected()}  chia s
    573 s kin vi hot ng:</p>
    574 
    575 <pre>
    576 public static class FragmentA extends ListFragment {
    577     OnArticleSelectedListener mListener;
    578     ...
    579     &#64;Override
    580     public void onListItemClick(ListView l, View v, int position, long id) {
    581         // Append the clicked item's row ID with the content provider Uri
    582         Uri noteUri = ContentUris.{@link android.content.ContentUris#withAppendedId withAppendedId}(ArticleColumns.CONTENT_URI, id);
    583         // Send the event and Uri to the host activity
    584         mListener.onArticleSelected(noteUri);
    585     }
    586     ...
    587 }
    588 </pre>
    589 
    590 <p>Tham s {@code id} c chuyn vo {@link
    591 android.app.ListFragment#onListItemClick onListItemClick()} l ID hng ca mc c nhp,
    592 n c s dng bi hot ng (hoc phn on kia)  ti bi vit t {@link
    593 android.content.ContentProvider} ca ng dng.</p>
    594 
    595 <p><!--To see a complete implementation of this kind of callback interface, see the <a
    596 href="{@docRoot}resources/samples/NotePad/index.html">NotePad sample</a>. -->Bn c th xem thm thng tin v
    597 cch s dng mt trnh cung cp ni dung trong ti liu <a href="{@docRoot}guide/topics/providers/content-providers.html">Trnh cung cp Ni dung</a>.</p>
    598 
    599 
    600 
    601 <h3 id="ActionBar">Thm mc vo Thanh Hnh ng</h3>
    602 
    603 <p>Phn on ca bn c th ng gp cc mc menu vo <a href="{@docRoot}guide/topics/ui/menus.html#options-menu">Menu Ty chn</a> ca hot ng (v tip  l c <a href="{@docRoot}guide/topics/ui/actionbar.html">Thanh Hnh ng</a>) bng cch trin khai
    604 {@link android.app.Fragment#onCreateOptionsMenu(Menu,MenuInflater) onCreateOptionsMenu()}. Tuy nhin, 
    605 phng php ny nhn lnh gi, bn phi gi {@link
    606 android.app.Fragment#setHasOptionsMenu(boolean) setHasOptionsMenu()} trong khi {@link
    607 android.app.Fragment#onCreate(Bundle) onCreate()},  cho bit rng phn on
    608 s mun thm mc vo Menu Ty chn (nu khng, phn on s khng nhn c lnh gi ti
    609 {@link android.app.Fragment#onCreateOptionsMenu onCreateOptionsMenu()}).</p>
    610 
    611 <p>Bt k mc no m bn thm vo Menu Ty chn sau  t phn on u c ni vi cc mc menu
    612 hin ti. Phn on cng nhn cc lnh gi li ti {@link
    613 android.app.Fragment#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} khi mt mc menu
    614 c chn.</p>
    615 
    616 <p>Bn cng c th ng k mt dng xem trong b tr phn on ca mnh  cung cp mt menu ng cnh bng cch gi {@link
    617 android.app.Fragment#registerForContextMenu(View) registerForContextMenu()}. Khi ngi dng m
    618 menu ng cnh, phn on nhn mt lnh gi ti {@link
    619 android.app.Fragment#onCreateContextMenu(ContextMenu,View,ContextMenu.ContextMenuInfo)
    620 onCreateContextMenu()}. Khi ngi dng chn mt mc, phn on nhn c mt lnh gi ti {@link
    621 android.app.Fragment#onContextItemSelected(MenuItem) onContextItemSelected()}.</p>
    622 
    623 <p class="note"><strong>Lu :</strong> Mc d phn on ca bn nhn c mt lnh gi khi chn mc
    624 i vi tng mc menu m n thm, trc tin hot ng s nhn phng php gi li tng ng khi ngi dng
    625 chn mt mc menu. Nu vic trin khai gi li khi chn mc ca hot ng khng
    626 x l mc c chn, khi  s kin c chuyn sang phng php gi li ca phn on. iu ny ng i vi
    627 Menu Ty chn v cc menu ng cnh.</p>
    628 
    629 <p> bit thm thng tin v cc menu, xem cc hng dn cho nh pht trin <a href="{@docRoot}guide/topics/ui/menus.html">Menu</a> v <a href="{@docRoot}guide/topics/ui/actionbar.html">Thanh Hnh ng</a>.</p>
    630 
    631 
    632 
    633 
    634 <h2 id="Lifecycle">X l Vng i ca Phn on</h2>
    635 
    636 <div class="figure" style="width:350px">
    637 <img src="{@docRoot}images/activity_fragment_lifecycle.png" alt="" />
    638 <p class="img-caption"><strong>Hnh 3.</strong> nh hng ca vng i hot ng ti vng i
    639 ca phn on.</p>
    640 </div>
    641 
    642 <p>Vic qun l vng i ca mt phn on rt ging vi qun l vng i ca mt hot ng. Ging nh
    643 hot ng, phn on c th tn ti  ba trng thi:</p>
    644 
    645 <dl>
    646   <dt><i>Tip tc</i></dt>
    647     <dd>Phn on hin th trong hot ng ang chy.</dd>
    648 
    649   <dt><i>Tm dng</i></dt>
    650     <dd>Mt hot ng khc  trong tin cnh v c tiu im, nhng hot ng m phn on
    651 ny nm trong vn hin th (hot ng tin cnh m mt phn hoc khng
    652 che ph ton b mn hnh).</dd>
    653 
    654   <dt><i>Dng</i></dt>
    655     <dd>Phn on khng hin th. Hoc l hot ng ch  b dng hoc
    656 phn on  c g b khi hot ng, nhng c thm vo ngn xp. Phn on dng
    657 vn cn hot ng (tt c thng tin v trng thi v thnh vin u c h thng gi li). Tuy nhin, n khng cn
    658 hin th vi ngi dng na v s b tt b nu hot ng b tt b.</dd>
    659 </dl>
    660 
    661 <p>Cng nh mt hot ng, bn c th gi li trng thi ca mt phn on bng cch s dng {@link
    662 android.os.Bundle}, trong trng hp tin trnh ca hot ng b tt b v bn cn khi phc
    663 trng thi ca phn on khi hot ng c to li. Bn c th lu trng thi trong phng php gi li {@link
    664 android.app.Fragment#onSaveInstanceState onSaveInstanceState()} ca phn on v khi phc n trong
    665 hoc {@link android.app.Fragment#onCreate onCreate()}, {@link
    666 android.app.Fragment#onCreateView onCreateView()}, hoc {@link
    667 android.app.Fragment#onActivityCreated onActivityCreated()}.  bit thm thng tin v vic lu
    668 trng thi, xem ti liu <a href="{@docRoot}guide/components/activities.html#SavingActivityState">Hot ng</a>
    669 .</p>
    670 
    671 <p>S khc nhau quan trng nht trong vng i gia mt hot ng v mt phn on  l cch chng
    672 c lu tr trong ngn xp tng ng. Hot ng c t vo mt ngn xp gm nhiu hot ng
    673 , c qun l bi h thng theo mc nh khi b dng (sao cho ngi dng c th iu hng li
    674 n bng nt <em>Quay li</em> nh c  cp trong <a href="{@docRoot}guide/components/tasks-and-back-stack.html">Tc v v Ngn xp</a>).
    675 Tuy nhin, phn on ch c t vo mt ngn xp do hot ng ch qun l khi bn
    676 yu cu r rng rng trng hp  phi c lu bng cch gi {@link
    677 android.app.FragmentTransaction#addToBackStack(String) addToBackStack()} trong mt giao tc
    678 g b phn on.</p>
    679 
    680 <p>Nu khng th vic qun l vng i ca phn on rt ging vi vic qun l vng i
    681 ca hot ng. V th, nhng ni dung p dng cho <a href="{@docRoot}guide/components/activities.html#Lifecycle">qun l vng i ca
    682 hot ng</a> cng p dng cho phn on. Tuy nhin, vic m bn cng cn phi hiu  l cch
    683 vng i ca hot ng nh hng ti vng i ca phn on.</p>
    684 
    685 <p class="caution"><strong>Ch :</strong> Nu bn cn mt i tng {@link android.content.Context} trong
    686 {@link android.app.Fragment}ca mnh, bn c th gi {@link android.app.Fragment#getActivity()}.
    687 Tuy nhin, nh ch c gi {@link android.app.Fragment#getActivity()} khi phn on c gn vi
    688 mt hot ng. Khi phn on cha c gn, hoc b g trong khi kt thc
    689 vng i ca n, {@link android.app.Fragment#getActivity()} s tr v rng.</p>
    690 
    691 
    692 <h3 id="CoordinatingWithActivity">Phi hp vi vng i ca hot ng</h3>
    693 
    694 <p>Vng i ca hot ng m phn on c  trong  s trc tip nh hng ti vng i ca phn on
    695 , sao cho mi lnh gi li vng i cho hot ng  s dn ti mt lnh gi li tng t cho tng
    696 phn on. V d, khi hot ng nhn c {@link android.app.Activity#onPause}, mi
    697 phn on trong hot ng s nhn c {@link android.app.Fragment#onPause}.</p>
    698 
    699 <p>Tuy nhin, cc phn on c thm mt vi lnh gi li vng i nhm x l tng tc duy nht vi
    700 hot ng  thc hin cc hnh ng nh xy dng v hy UI ca phn on. Nhng phng php gi li
    701 b sung ny l:</p>
    702 
    703 <dl>
    704   <dt>{@link android.app.Fragment#onAttach onAttach()}</dt>
    705     <dd>c gi khi phn on  c lin kt vi hot ng {@link
    706 android.app.Activity} c chuyn  y).</dd>
    707   <dt>{@link android.app.Fragment#onCreateView onCreateView()}</dt>
    708     <dd>c gi khi to phn cp dng xem c lin kt vi phn on.</dd>
    709   <dt>{@link android.app.Fragment#onActivityCreated onActivityCreated()}</dt>
    710     <dd>c gi khi phng php {@link android.app.Activity#onCreate
    711 onCreate()} ca hot ng  tr v.</dd>
    712   <dt>{@link android.app.Fragment#onDestroyView onDestroyView()}</dt>
    713     <dd>c gi khi phn cp dng xem c lin kt vi phn on ang c g b.</dd>
    714   <dt>{@link android.app.Fragment#onDetach onDetach()}</dt>
    715     <dd>c gi khi phn on ang c b lin kt khi hot ng.</dd>
    716 </dl>
    717 
    718 <p>Tin trnh vng i ca mt phn on, do b nh hng bi hot ng ch ca n, c minh ha
    719 bi hnh 3. Trong hnh ny, bn c th thy cch thc mi trng thi ni tip nhau ca hot ng s xc nh
    720 cc phng php gi li no m mt phn on c th nhn c. V d, khi hot ng  nhn c lnh gi li {@link
    721 android.app.Activity#onCreate onCreate()} ca n, phn on trong hot ng s nhn c khng qu
    722 lnh gi li {@link android.app.Fragment#onActivityCreated onActivityCreated()}.</p>
    723 
    724 <p>Sau khi hot ng t trng thi tip tc, bn c th t do thm v g b phn on vo
    725 hot ng. V th, ch trong khi hot ng  trng thi tip tc th vng i ca mt phn on
    726 mi c th thay i c lp.</p>
    727 
    728 <p>Tuy nhin, khi hot ng ri khi trng thi tip tc, phn on li b hot ng y qua vng i
    729 ca mnh.</p>
    730 
    731 
    732 
    733 
    734 <h2 id="Example">V d</h2>
    735 
    736 <p> kt hp mi ni dung c  cp trong ti liu ny, sau y l mt v d v hot ng
    737 s dng hai phn on  to mt b tr hai bng. Hot ng bn di bao gm mt phn on 
    738 hin th danh sch cc v kch ca Shakespeare v mt phn on khc  hin th tm tt v v kch khi c chn
    739 t danh sch. N cng minh ha cch cung cp cc cu hnh phn on khc nhau,
    740 da trn cu hnh mn hnh.</p>
    741 
    742 <p class="note"><strong>Lu :</strong> M ngun hon chnh cho hot ng ny c sn trong
    743 <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.html">{@code
    744 FragmentLayout.java}</a>.</p>
    745 
    746 <p>Hot ng chnh p dng mt b tr theo cch thng thng, trong {@link
    747 android.app.Activity#onCreate onCreate()}:</p>
    748 
    749 {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java main}
    750 
    751 <p>B tr c p dng l {@code fragment_layout.xml}:</p>
    752 
    753 {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout}
    754 
    755 <p>Khi s dng b tr ny, h thng s khi to {@code TitlesFragment} (lit k tn
    756 cc v kch) ngay khi hot ng np b tr, trong khi {@link android.widget.FrameLayout}
    757 (ni s xut hin phn on hin th tm tt v v kch) chim khong trng pha bn phi ca
    758 mn hnh, nhng ban u vn trng. Nh bn s thy bn di, mi ti khi ngi dng chn mt mc
    759 t danh sch th mt phn on mi c t vo {@link android.widget.FrameLayout}.</p>
    760 
    761 <p>Tuy nhin, khng phi tt c cu hnh mn hnh u  rng  hin th c danh sch
    762 cc v kch v tm tt bn cnh nhau. V th, b tr trn ch c s dng cho cu hnh
    763 mn hnh kh ngang bng cch lu n di dng {@code res/layout-land/fragment_layout.xml}.</p>
    764 
    765 <p>V th, khi mn hnh hng ng, h thng s p dng b tr sau, n
    766 c lu ti {@code res/layout/fragment_layout.xml}:</p>
    767 
    768 {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout}
    769 
    770 <p>B tr ny ch bao gm {@code TitlesFragment}. iu ny c ngha l, khi thit b 
    771 hng ng, ch danh sch tn v kch c hin th. V th, khi ngi dng nhp vo mt
    772 mc danh sch trong cu hnh ny, ng dng s bt u mt hot ng mi  hin th tm tt,
    773 thay v ti mt phn on th hai.</p>
    774 
    775 <p>Tip theo, bn c th thy cch hon thnh iu ny trong cc lp phn on. u tin l {@code
    776 TitlesFragment}, hin th danh sch tn cc v kch ca Shakespeare. Phn on ny s m rng {@link
    777 android.app.ListFragment} v da vo n  x l hu ht cng vic v dng xem danh sch.</p>
    778 
    779 <p>Khi bn kim tra on m ny, hy   rng c hai hnh vi c th khi ngi dng nhp vo mt
    780 mc danh sch: ph thuc vo b tr no trong hai b tr ang hin hot, n c th hoc to v hin th mt phn on
    781 mi  hin th chi tit trong cng hot ng (thm phn on vo {@link
    782 android.widget.FrameLayout}), hoc bt u mt hot ng mi (ti  phn on c th c hin th).</p>
    783 
    784 {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java titles}
    785 
    786 <p>Phn on th hai, {@code DetailsFragment} s hin th tm tt v kch cho mc c chn t
    787 danh sch trong {@code TitlesFragment}:</p>
    788  
    789 {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java details}
    790 
    791 <p>Nh li  lp {@code TitlesFragment} rng, nu ngi dng nhp vo mt mc danh sch v b tr
    792 hin ti <em>khng</em> c dng xem {@code R.id.details} (l ni m
    793 {@code DetailsFragment} thuc v), khi , ng dng s bt u hot ng {@code DetailsActivity}
    794  hin th ni dung ca mc .</p>
    795 
    796 <p>Sau y l {@code DetailsActivity}, n ch n thun nhng {@code DetailsFragment}  hin th
    797 tm tt v kch c chn khi mn hnh  hng ng:</p>
    798 
    799 {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
    800 details_activity}
    801  
    802 <p>Lu  rng hot ng ny t kt thc nu cu hnh l kh ngang, sao cho hot ng
    803 chnh c th chim ly v hin th {@code DetailsFragment} bn cnh {@code TitlesFragment}.
    804 iu ny c th xy ra nu ngi dng bt u {@code DetailsActivity}  dng hng ng, nhng
    805 sau  xoay thnh kh ngang (lm vy s bt u li hot ng hin ti).</p>
    806 
    807 
    808 <p> bit thm mu s dng phn on (v ton b tp ngun cho v d ny),
    809 hy xem ng dng mu API Demos c sn trong <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/index.html#Fragment">
    810 ApiDemos</a> (c th ti xung t <a href="{@docRoot}resources/samples/get.html">Thnh phn SDK Mu</a>).</p>
    811 
    812 
    813