1 page.title=X l Thay i Thi gian chy 2 page.tags=hot ng,vng i 3 @jd:body 4 5 <div id="qv-wrapper"> 6 <div id="qv"> 7 8 <h2>Trong ti liu ny</h2> 9 <ol> 10 <li><a href="#RetainingAnObject">Gi li mt i tng trong khi Thay i Cu hnh</a></li> 11 <li><a href="#HandlingTheChange">T mnh X l Thay i Cu hnh</a> 12 </ol> 13 14 <h2>Xem thm</h2> 15 <ol> 16 <li><a href="providing-resources.html">Cung cp Ti nguyn</a></li> 17 <li><a href="accessing-resources.html">Truy cp Ti nguyn</a></li> 18 <li><a href="http://android-developers.blogspot.com/2009/02/faster-screen-orientation-change.html">Thay i 19 Hng Mn hnh Nhanh hn</a></li> 20 </ol> 21 </div> 22 </div> 23 24 <p>Mt s cu hnh thit b c th thay i trong thi gian chy 25 (chng hn nh hng mn hnh, s sn c ca bn phm, v ngn ng). Khi s thay i nh vy din ra, 26 Android s khi ng li vic chy 27 {@link android.app.Activity} ({@link android.app.Activity#onDestroy()} s c gi, sau l {@link 28 android.app.Activity#onCreate(Bundle) onCreate()}). Hnh vi khi ng li c thit k gip 29 ng dng iu chnh ph hp vi cu hnh mi bng cch t ng ti li ng dng ca bn bng 30 cc ti nguyn thay th khp vi cu hnh thit b mi.</p> 31 32 <p> x l khi ng li cho ng, iu quan trng l hot ng ca bn khi phc li trng thi trc 33 ca n thng qua vng i <a href="{@docRoot}guide/components/activities.html#Lifecycle">Hot ng 34 thng thng</a>, trong Android s gi 35 {@link android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()} trc khi n hy 36 hot ng ca bn sao cho bn c th lu d liu v trng thi ca ng dng. Khi , bn c th khi phc trng thi 37 trong khi {@link android.app.Activity#onCreate(Bundle) onCreate()} hoc {@link 38 android.app.Activity#onRestoreInstanceState(Bundle) onRestoreInstanceState()}.</p> 39 40 <p> kim tra xem ng dng ca bn c t khi ng li m gi nguyn trng thi ng dng hay khng, 41 bn cn gi ra cc thay i cu hnh (chng hn nh thay i hng mn hnh) trong khi thc hin cc 42 tc v khc nhau trong ng dng ca bn. ng dng ca bn s c th khi ng li vo bt c lc no m khng b mt 43 d liu ca ngi dng hay trng thi x l cc s kin nh thay i cu hnh hoc khi ngi dng nhn c 44 mt cuc gi n ri quay li ng dng ca bn mun hn nhiu sau khi tin trnh 45 ng dng ca bn c th b hy. tm hiu v cch bn c th khi phc trng thi hot ng ca mnh, hy c v <a href="{@docRoot}guide/components/activities.html#Lifecycle">Vng i ca hot ng</a>.</p> 46 47 <p>Tuy nhin, bn c th gp phi mt tnh hung trong vic khi ng li ng dng ca bn v 48 khi phc phn ln d liu c th tn km v to nn tri nghim ngi dng km. Trong 49 tnh hung nh vy, bn c hai ty chn:</p> 50 51 <ol type="a"> 52 <li><a href="#RetainingAnObject">Gi li mt i tng trong khi thay i cu hnh</a> 53 <p>Cho php hot ng ca bn khi ng li khi cu hnh thay i, nhng mang theo mt 54 i tng c trng thi ti thc th mi ca hot ng ca bn.</p> 55 56 </li> 57 <li><a href="#HandlingTheChange">T mnh x l thay i cu hnh</a> 58 <p>Ngn khng cho h thng khi ng li hot ng ca bn trong nhng thay i 59 cu hnh nht nh, nhng nhn mt lnh gi li khi cu hnh thay i, sao cho bn c th cp nht th cng 60 hot ng ca mnh nu cn.</p> 61 </li> 62 </ol> 63 64 65 <h2 id="RetainingAnObject">Gi li mt i tng trong khi Thay i Cu hnh</h2> 66 67 <p>Nu vic khi ng li hot ng ca bn yu cu bn phi khi phc nhiu tp hp d liu ln, hy thit lp li kt ni 68 mng, hoc thc hin cc thao tc tng cng khc, khi khi ng li hon ton do thay i cu hnh 69 c th gy ra tri nghim ngi dng chm chp. ng thi, c th bn s khng th hon ton khi phc c 70 trng thi hot ng ca mnh vi {@link android.os.Bundle} m h thng lu cho bn bng phng php gi li {@link 71 android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()}—n khng 72 c thit k mang cc i tng ln (chng hn nh bitmap) v d liu trong n phi c ni tip ha ri 73 b ni tip ha, iu ny c th tiu tn nhiu b nh v khin vic thay i cu hnh din ra chm. Trong mt 74 tnh hung nh vy, bn c th g b gnh nng khi to li hot ng ca mnh bng cch gi li {@link 75 android.app.Fragment} khi hot ng ca bn c khi ng li do thay i cu hnh. Phn on ny 76 c th cha cc tham chiu ti i tng c trng thi m bn mun gi li.</p> 77 78 <p>Khi h thng Android tt hot ng ca bn do mt thay i cu hnh, cc phn on 79 ca hot ng m bn nh du gi li s khng b hy. Bn c th thm cc phn on ny vo 80 hot ng ca mnh gi li cc i tng c trng thi.</p> 81 82 <p> gi li cc i tng c trng thi trong mt phn on trong khi thay i cu hnh thi gian chy:</p> 83 84 <ol> 85 <li>M rng lp {@link android.app.Fragment} v khai bo cc tham chiu ti i tng 86 c trng thi ca bn.</li> 87 <li>Gi {@link android.app.Fragment#setRetainInstance(boolean)} khi phn on c to. 88 </li> 89 <li>Thm phn on vo hot ng ca bn.</li> 90 <li>S dng {@link android.app.FragmentManager} truy xut phn on khi hot ng 91 c khi ng li.</li> 92 </ol> 93 94 <p>V d, nh ngha phn on ca bn nh sau:</p> 95 96 <pre> 97 public class RetainedFragment extends Fragment { 98 99 // data object we want to retain 100 private MyDataObject data; 101 102 // this method is only called once for this fragment 103 @Override 104 public void onCreate(Bundle savedInstanceState) { 105 super.onCreate(savedInstanceState); 106 // retain this fragment 107 setRetainInstance(true); 108 } 109 110 public void setData(MyDataObject data) { 111 this.data = data; 112 } 113 114 public MyDataObject getData() { 115 return data; 116 } 117 } 118 </pre> 119 120 <p class="caution"><strong>Ch :</strong> Trong khi bn c th lu tr bt k i tng no, bn 121 khng nn chuyn mt i tng c gn vi {@link android.app.Activity}, chng hn nh {@link 122 android.graphics.drawable.Drawable}, {@link android.widget.Adapter}, {@link android.view.View} 123 hay bt k i tng no khc i km vi mt {@link android.content.Context}. Nu bn lm vy, n s 124 r r tt c dng xem v ti nguyn ca thc th hot ng gc. (R r ti nguyn 125 c ngha l ng dng ca bn duy tr vic lu gi ti nguyn v chng khng th c thu dn b nh rc, v th 126 rt nhiu b nh c th b mt.)</p> 127 128 <p>Khi , hy s dng {@link android.app.FragmentManager} thm phn on vo hot ng. 129 Bn c th thu c i tng d liu t phn on khi hot ng bt u li trong khi 130 thay i cu hnh thi gian chy. V d, nh ngha hot ng ca bn nh sau:</p> 131 132 <pre> 133 public class MyActivity extends Activity { 134 135 private RetainedFragment dataFragment; 136 137 @Override 138 public void onCreate(Bundle savedInstanceState) { 139 super.onCreate(savedInstanceState); 140 setContentView(R.layout.main); 141 142 // find the retained fragment on activity restarts 143 FragmentManager fm = getFragmentManager(); 144 dataFragment = (DataFragment) fm.findFragmentByTag(data); 145 146 // create the fragment and data the first time 147 if (dataFragment == null) { 148 // add the fragment 149 dataFragment = new DataFragment(); 150 fm.beginTransaction().add(dataFragment, data).commit(); 151 // load the data from the web 152 dataFragment.setData(loadMyData()); 153 } 154 155 // the data is available in dataFragment.getData() 156 ... 157 } 158 159 @Override 160 public void onDestroy() { 161 super.onDestroy(); 162 // store the data in the fragment 163 dataFragment.setData(collectMyLoadedData()); 164 } 165 } 166 </pre> 167 168 <p>Trong v d ny, {@link android.app.Activity#onCreate(Bundle) onCreate()} thm mt phn on 169 hoc khi phc mt tham chiu n n. {@link android.app.Activity#onCreate(Bundle) onCreate()} cng 170 lu tr i tng c trng thi bn trong thc th phn on . 171 {@link android.app.Activity#onDestroy() onDestroy()} cp nht i tng c trng thi bn trong 172 thc th phn on c gi li.</p> 173 174 175 176 177 178 <h2 id="HandlingTheChange">T mnh X l Thay i Cu hnh</h2> 179 180 <p>Nu ng dng ca bn khng cn cp nht cc ti nguyn trong mt thay i 181 cu hnh c th <em>v</em> bn c gii hn v hiu nng yu cu bn phi 182 trnh khi ng li hot ng, khi bn c th khai bo rng hot ng ca bn t mnh x l thay i cu hnh 183 , lm vy s trnh cho h thng khi ng li hot ng ca bn.</p> 184 185 <p class="note"><strong>Lu :</strong> Vic t mnh x l thay i cu hnh c th khin vic 186 s dng cc ti nguyn thay th kh khn hn nhiu, v h thng khng t ng p dng chng 187 cho bn. K thut ny nn c coi l gii php cui cng khi bn phi trnh khi ng li do mt 188 thay i cu hnh v khng c khuyn co i vi hu ht ng dng.</p> 189 190 <p> khai bo rng hot ng ca bn x l mt thay i cu hnh, hy chnh sa phn t <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> ph hp trong 191 tp bn k khai ca bn bao gm thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code 192 android:configChanges}</a> vi mt gi tr c chc nng biu din cu hnh m bn mun 193 x l. Cc gi tr c th c lit k trong ti liu dnh cho thuc tnh <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code 194 android:configChanges}</a> (cc gi tr thng c s dng nht l {@code "orientation"} 195 ngn khi ng li khi hng mn hnh thay i v {@code "keyboardHidden"} ngn 196 khi ng li khi tnh sn c ca bn phm thay i). Bn c th khai bo nhiu gi tr cu hnh trong 197 thuc tnh bng cch tch chng bng mt k t {@code |} ng dn ni.</p> 198 199 <p>V d, on m bn k khai sau khai bo mt hot ng c chc nng x l c 200 thay i v hng mn hnh v thay i v tnh sn c ca bn phm:</p> 201 202 <pre> 203 <activity android:name=".MyActivity" 204 android:configChanges="orientation|keyboardHidden" 205 android:label="@string/app_name"> 206 </pre> 207 208 <p>Lc ny, khi mt trong nhng cu hnh ny thay i, {@code MyActivity} khng khi ng li. 209 Thay vo , {@code MyActivity} nhn c mt lnh gi ti {@link 210 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}. Phng php ny 211 c chuyn bi mt i tng {@link android.content.res.Configuration} m quy nh 212 cu hnh thit b mi. Bng cch c cc trng trong {@link android.content.res.Configuration}, 213 bn c th xc nh cu hnh mi v thc hin nhng thay i ph hp bng cch cp nht 214 ti nguyn c s dng trong giao din ca bn. Ti 215 thi im phng php ny c gi, i tng {@link android.content.res.Resources} ca hot ng ca bn c cp nht 216 tr v cc ti nguyn da trn cu hnh mi, sao cho bn c th d dng 217 t li cc phn t trong UI ca mnh m khng h thng khi ng li hot ng ca bn.</p> 218 219 <p class="caution"><strong>Ch :</strong> Bt u vi Android 3.2 (API mc 13), <strong> 220 "kch c mn hnh" cng thay i</strong> khi thit b chuyn gia hng dc v kh ngang 221 . V th, nu bn mun ngn cn vic khi ng li vo thi gian chy do thay i hng khi pht trin 222 cho API mc 13 hoc cao hn (nh c khai bo bi cc thuc tnh <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> v <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a> 223 ), bn phi bao gm gi tr {@code "screenSize"} bn cnh gi tr {@code 224 "orientation"}. C th, bn phi khai bo {@code 225 android:configChanges="orientation|screenSize"}. Tuy nhin, nu ng dng ca bn nhm ti API mc 226 12 hoc thp hn, khi hot ng ca bn lun t mnh x l thay i cu hnh ny (thay i 227 cu hnh ny khng khi ng li hot ng ca bn, ngay c khi ang chy trn mt thit b phin bn Android 3.2 hoc cao hn).</p> 228 229 <p>V d, vic trin khai {@link 230 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()} sau 231 s kim tra hng thit b hin ti:</p> 232 233 <pre> 234 @Override 235 public void onConfigurationChanged(Configuration newConfig) { 236 super.onConfigurationChanged(newConfig); 237 238 // Checks the orientation of the screen 239 if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 240 Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 241 } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 242 Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 243 } 244 } 245 </pre> 246 247 <p>i tng {@link android.content.res.Configuration} biu din tt c cu hnh 248 hin ti, khng ch nhng cu hnh thay i. Trong phn ln thi gian, bn s khng quan tm chnh xc xem 249 cu hnh thay i nh th no v c th n gin gn li tt c ti nguyn ca mnh vi chc nng cung cp ni dung thay th 250 cho cu hnh m bn ang x l. V d, do i tng {@link 251 android.content.res.Resources} nay c cp nht, bn c th t li 252 bt k{@link android.widget.ImageView} no vi {@link android.widget.ImageView#setImageResource(int) 253 setImageResource()} 254 v ti nguyn ph hp cho cu hnh mi c s dng (nh c m t trong phn <a href="providing-resources.html#AlternateResources">Cung cp Ti nguyn</a>).</p> 255 256 <p>Lu rng gi tr t cc trng {@link 257 android.content.res.Configuration} l nhng s nguyn khp vi hng s c th 258 t lp {@link android.content.res.Configuration}. i vi ti liu v nhng hng s 259 cn s dng vi mi trng, hy tham kho trng ph hp trong tham chiu {@link 260 android.content.res.Configuration}.</p> 261 262 <p class="note"><strong>Hy ghi nh:</strong> Khi bn khai bo hot ng ca mnh x l mt 263 thay i cu hnh, bn c trch nhim t li bt k phn t no m bn cung cp ni dung thay th cho. Nu bn 264 khai bo hot ng ca mnh x l thay i hng v c nhng hnh nh nn thay i 265 gia kh ngang v hng dc, bn phi gn li tng ti nguyn cho tng phn t trong khi {@link 266 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}.</p> 267 268 <p>Nu bn khng cn cp nht ng dng ca mnh da trn nhng thay i 269 cu hnh ny, thay vo bn c th <em>khng</em> trin khai {@link 270 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}. Trong 271 trng hp , tt c ti nguyn c s dng trc khi thay i cu hnh s vn c s dng 272 v bn ch mi trnh c vic khi ng li hot ng ca mnh. Tuy nhin, ng dng ca bn cn lun c kh nng 273 tt v khi ng li vi trng thi trc ca n c gi nguyn, v th bn khng nn coi 274 k thut ny nh mt cch thot khi vic gi li trng thi ca mnh trong vng i ca hot ng bnh thng. Khng ch bi 275 c nhng thay i cu hnh khc m bn khng th ngn khng cho khi ng li ng dng ca mnh, m 276 c bi v bn nn x l nhng s kin nh l khi ngi dng ri khi ng dng ca bn v n b 277 hy trc khi ngi dng quay li.</p> 278 279 <p> bit thm v nhng thay i cu hnh no m bn c th x l trong hot ng ca mnh, hy xem ti liu <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code 280 android:configChanges}</a> v lp {@link android.content.res.Configuration} 281 .</p> 282