1 page.title= 2 page.tags=Activity 3 @jd:body 4 5 <div id="qv-wrapper"> 6 <div id="qv"> 7 8 <h2></h2> 9 <ol> 10 <li><a href="#RetainingAnObject"></a></li> 11 <li><a href="#HandlingTheChange"></a> 12 </ol> 13 14 <h2></h2> 15 <ol> 16 <li><a href="providing-resources.html"></a></li> 17 <li><a href="accessing-resources.html"></a></li> 18 <li><a href="http://android-developers.blogspot.com/2009/02/faster-screen-orientation-change.html"> 19 </a></li> 20 </ol> 21 </div> 22 </div> 23 24 <p> () 25 Android 26 {@link android.app.Activity} ( {@link android.app.Activity#onDestroy()} {@link 27 android.app.Activity#onCreate(Bundle) onCreate()}) 28 29 30 </p> 31 32 <p> <a href="{@docRoot}guide/components/activities.html#Lifecycle">Activity </a> Activity Android 33 {@link android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()} Activity 34 35 36 {@link android.app.Activity#onCreate(Bundle) onCreate()} {@link 37 android.app.Activity#onRestoreInstanceState(Bundle) onRestoreInstanceState()} 38 </p> 39 40 <p> () 41 42 43 44 45 Activity <a href="{@docRoot}guide/components/activities.html#Lifecycle">Activity </a></p> 46 47 <p> 48 49 </p> 50 51 <ol type="a"> 52 <li><a href="#RetainingAnObject"></a> 53 <p> Activity Activity 54 </p> 55 56 </li> 57 <li><a href="#HandlingTheChange"></a> 58 <p> Activity Activity 59 60 </p> 61 </li> 62 </ol> 63 64 65 <h2 id="RetainingAnObject"></h2> 66 67 <p> Activity 68 69 {@link android.os.Bundle} Activity {@link android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()} — () 70 71 72 73 Activity {@link 74 android.app.Fragment} Activity 75 76 </p> 77 78 <p> Android Activity Activity 79 Activity 80 </p> 81 82 <p></p> 83 84 <ol> 85 <li> {@link android.app.Fragment} 86 </li> 87 <li> {@link android.app.Fragment#setRetainInstance(boolean)} 88 </li> 89 <li> Activity</li> 90 <li> Activity {@link android.app.FragmentManager} 91 </li> 92 </ol> 93 94 <p></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></strong> {@link android.app.Activity} {@link 121 android.graphics.drawable.Drawable}{@link android.widget.Adapter}{@link android.view.View} {@link android.content.Context} 122 123 Activity 124 () 125 126 </p> 127 128 <p> {@link android.app.FragmentManager} Activity Activity 129 130 Activity </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>{@link android.app.Activity#onCreate(Bundle) onCreate()} Activity 169 {@link android.app.Activity#onCreate(Bundle) onCreate()} 170 171 {@link android.app.Activity#onDestroy() onDestroy()} 172 </p> 173 174 175 176 177 178 <h2 id="HandlingTheChange"></h2> 179 180 <p><em></em> Activity Activity Activity 181 182 183 </p> 184 185 <p class="note"><strong></strong> 186 187 188 </p> 189 190 <p> Activity <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code 191 android:configChanges}</a> 192 193 <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code 194 android:configChanges}</a> ( {@code "orientation"} {@code "keyboardHidden"} ) 195 196 {@code |} 197 </p> 198 199 <p> Activity 200 </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> {@code MyActivity} 209 {@code MyActivity} {@link android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()} 210 {@link android.content.res.Configuration} 211 212 {@link android.content.res.Configuration} 213 214 Activity {@link android.content.res.Resources} UI Activity 215 216 217 </p> 218 219 <p class="caution"><strong></strong> Android 3.2 (API 13) <strong></strong> 220 221 API 13 ( <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a> ){@code 222 "orientation"} {@code "screenSize"} 223 224 {@code 225 android:configChanges="orientation|screenSize"} API 12 Activity ( Android 3.2 Activity) 226 227 </p> 228 229 <p> {@link 230 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()} 231 </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>{@link android.content.res.Configuration} 248 249 250 {@link 251 android.content.res.Resources} {@link android.widget.ImageView#setImageResource(int) 252 setImageResource()} {@link android.widget.ImageView} (<a href="providing-resources.html#AlternateResources"></a>) 253 254 </p> 255 256 <p>{@link 257 android.content.res.Configuration} {@link android.content.res.Configuration} 258 {@link 259 android.content.res.Configuration} 260 </p> 261 262 <p class="note"><strong></strong> Activity 263 Activity {@link 264 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()} 265 266 </p> 267 268 <p><em></em> {@link 269 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()} 270 Activity 271 272 Activity 273 274 275 276 277 </p> 278 279 <p> Activity <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code 280 android:configChanges}</a> {@link android.content.res.Configuration} 281 </p> 282