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"></a> 19 </li> 20 </ol> 21 </div> 22 </div> 23 24 <p> 25 Android 26 27 {@link android.app.Activity} {@link android.app.Activity#onDestroy()} {@link 28 android.app.Activity#onCreate(Bundle) onCreate()} 29 30 </p> 31 32 <p>Activity <a href="{@docRoot}guide/components/activities.html#Lifecycle">Activity </a> Activity Android 33 Activity 34 {@link android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()} 35 36 37 {@link android.app.Activity#onCreate(Bundle) onCreate()} {@link 38 android.app.Activity#onRestoreInstanceState(Bundle) onRestoreInstanceState()} Activity </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 70 android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()} 71 {@link android.os.Bundle} Activity 72 73 Activity 74 {@link 75 android.app.Fragment} Activity 76 </p> 77 78 <p> 79 Android Activity Activity 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> 121 {@link android.app.Activity} {@link 122 android.graphics.drawable.Drawable}{@link android.widget.Adapter}{@link android.view.View} 123 {@link android.content.Context} 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()} 169 {@link android.app.Activity#onCreate(Bundle) onCreate()} 170 {@link android.app.Activity#onDestroy() onDestroy()} 171 172 </p> 173 174 175 176 177 178 <h2 id="HandlingTheChange"></h2> 179 180 <p> Activity Activity<em></em> 181 182 183 </p> 184 185 <p class="note"><strong></strong> 186 187 Activity 188 </p> 189 190 <p> Activity <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> 191 <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code 192 android:configChanges}</a> 193 <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code 194 android:configChanges}</a> {@code "orientation"} 195 196 {@code "keyboardHidden"} 197 {@code |} </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} {@code MyActivity} 209 {@link 210 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()} 211 {@link android.content.res.Configuration} 212 213 {@link android.content.res.Configuration} 214 Activity 215 {@link android.content.res.Resources} 216 Activity 217 UI </p> 218 219 <p class="caution"><strong></strong> 220 Android 221 3.2API 13<strong></strong> 222 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> 223 {@code 224 "orientation"} {@code "screenSize"} {@code 225 android:configChanges="orientation|screenSize"} API 226 12 Activity 227 Android 3.2 Activity</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} 252 {@link android.widget.ImageView#setImageResource(int) 253 setImageResource()} 254 {@link android.widget.ImageView}<a href="providing-resources.html#AlternateResources"></a></p> 255 256 <p>{@link 257 android.content.res.Configuration} 258 {@link android.content.res.Configuration} 259 {@link 260 android.content.res.Configuration} </p> 261 262 <p class="note"><strong></strong> Activity 263 Activity 264 {@link 265 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()} 266 </p> 267 268 <p> 269 {@link 270 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}<em></em> 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