| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 1 | page.title=Handling Runtime Changes |
| Joe Fernandez | 33baa5a | 2013-11-14 11:41:19 -0800 | [diff] [blame] | 2 | page.tags=activity,lifecycle |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 3 | @jd:body |
| 4 | |
| 5 | <div id="qv-wrapper"> |
| 6 | <div id="qv"> |
| 7 | |
| 8 | <h2>In this document</h2> |
| 9 | <ol> |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 10 | <li><a href="#RetainingAnObject">Retaining an Object During a Configuration Change</a></li> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 11 | <li><a href="#HandlingTheChange">Handling the Configuration Change Yourself</a> |
| 12 | </ol> |
| 13 | |
| 14 | <h2>See also</h2> |
| 15 | <ol> |
| 16 | <li><a href="providing-resources.html">Providing Resources</a></li> |
| 17 | <li><a href="accessing-resources.html">Accessing Resources</a></li> |
| Scott Main | f284d49 | 2012-07-31 09:46:52 -0700 | [diff] [blame] | 18 | <li><a href="http://android-developers.blogspot.com/2009/02/faster-screen-orientation-change.html">Faster |
| 19 | Screen Orientation Change</a></li> |
| Andrew Solovay | dda5e4b | 2016-07-21 16:25:26 -0700 | [diff] [blame] | 20 | <li><a href="{@docRoot}guide/topics/ui/multi-window.html#lifecycle"> |
| 21 | Multi-Window Lifecycle</a></li> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 22 | </ol> |
| 23 | </div> |
| 24 | </div> |
| 25 | |
| 26 | <p>Some device configurations can change during runtime |
| 27 | (such as screen orientation, keyboard availability, and language). When such a change occurs, |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 28 | Android restarts the running |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 29 | {@link android.app.Activity} ({@link android.app.Activity#onDestroy()} is called, followed by {@link |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 30 | android.app.Activity#onCreate(Bundle) onCreate()}). The restart behavior is designed to help your |
| 31 | application adapt to new configurations by automatically reloading your application with |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 32 | alternative resources that match the new device configuration.</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 33 | |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 34 | <p>To properly handle a restart, it is important that your activity restores its previous |
| Scott Main | 9bf45a0 | 2011-02-03 18:46:45 -0800 | [diff] [blame] | 35 | state through the normal <a |
| Scott Main | 50e990c | 2012-06-21 17:14:39 -0700 | [diff] [blame] | 36 | href="{@docRoot}guide/components/activities.html#Lifecycle">Activity |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 37 | lifecycle</a>, in which Android calls |
| 38 | {@link android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()} before it destroys |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 39 | your activity so that you can save data about the application state. You can then restore the state |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 40 | during {@link android.app.Activity#onCreate(Bundle) onCreate()} or {@link |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 41 | android.app.Activity#onRestoreInstanceState(Bundle) onRestoreInstanceState()}.</p> |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 42 | |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 43 | <p>To test that your application restarts itself with the application state intact, you should |
| 44 | invoke configuration changes (such as changing the screen orientation) while performing various |
| 45 | tasks in your application. Your application should be able to restart at any time without loss of |
| 46 | user data or state in order to handle events such as configuration changes or when the user receives |
| 47 | an incoming phone call and then returns to your application much later after your application |
| 48 | process may have been destroyed. To learn how you can restore your activity state, read about the <a |
| Scott Main | 50e990c | 2012-06-21 17:14:39 -0700 | [diff] [blame] | 49 | href="{@docRoot}guide/components/activities.html#Lifecycle">Activity lifecycle</a>.</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 50 | |
| 51 | <p>However, you might encounter a situation in which restarting your application and |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 52 | restoring significant amounts of data can be costly and create a poor user experience. In such a |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 53 | situation, you have two other options:</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 54 | |
| 55 | <ol type="a"> |
| Scott Main | 369c1c1 | 2010-12-07 11:17:00 -0800 | [diff] [blame] | 56 | <li><a href="#RetainingAnObject">Retain an object during a configuration change</a> |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 57 | <p>Allow your activity to restart when a configuration changes, but carry a stateful |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 58 | object to the new instance of your activity.</p> |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 59 | |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 60 | </li> |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 61 | <li><a href="#HandlingTheChange">Handle the configuration change yourself</a> |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 62 | <p>Prevent the system from restarting your activity during certain configuration |
| 63 | changes, but receive a callback when the configurations do change, so that you can manually update |
| 64 | your activity as necessary.</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 65 | </li> |
| 66 | </ol> |
| 67 | |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 68 | |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 69 | <h2 id="RetainingAnObject">Retaining an Object During a Configuration Change</h2> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 70 | |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 71 | <p>If restarting your activity requires that you recover large sets of data, re-establish a network |
| 72 | connection, or perform other intensive operations, then a full restart due to a configuration change |
| 73 | might be a slow user experience. Also, it might not be possible for you to completely restore your |
| 74 | activity state with the {@link android.os.Bundle} that the system saves for you with the {@link |
| 75 | android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()} callback—it is not |
| 76 | designed to carry large objects (such as bitmaps) and the data within it must be serialized then |
| 77 | deserialized, which can consume a lot of memory and make the configuration change slow. In such a |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 78 | situation, you can alleviate the burden of reinitializing your activity by retaining a {@link |
| 79 | android.app.Fragment} when your activity is restarted due to a configuration change. This fragment |
| 80 | can contain references to stateful objects that you want to retain.</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 81 | |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 82 | <p>When the Android system shuts down your activity due to a configuration change, the fragments |
| 83 | of your activity that you have marked to retain are not destroyed. You can add such fragments to |
| 84 | your activity to preserve stateful objects.</p> |
| 85 | |
| 86 | <p>To retain stateful objects in a fragment during a runtime configuration change:</p> |
| 87 | |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 88 | <ol> |
| Mark Lu | c4a0139 | 2016-07-18 10:42:11 -0700 | [diff] [blame] | 89 | <li>Extend the {@link android.app.Fragment} class and declare references to your stateful |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 90 | objects.</li> |
| 91 | <li>Call {@link android.app.Fragment#setRetainInstance(boolean)} when the fragment is created. |
| 92 | </li> |
| 93 | <li>Add the fragment to your activity.</li> |
| Mark Lu | c4a0139 | 2016-07-18 10:42:11 -0700 | [diff] [blame] | 94 | <li>Use {@link android.app.FragmentManager} to retrieve the fragment when the activity is |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 95 | restarted.</li> |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 96 | </ol> |
| 97 | |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 98 | <p>For example, define your fragment as follows:</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 99 | |
| 100 | <pre> |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 101 | public class RetainedFragment extends Fragment { |
| 102 | |
| 103 | // data object we want to retain |
| 104 | private MyDataObject data; |
| 105 | |
| 106 | // this method is only called once for this fragment |
| 107 | @Override |
| 108 | public void onCreate(Bundle savedInstanceState) { |
| 109 | super.onCreate(savedInstanceState); |
| 110 | // retain this fragment |
| 111 | setRetainInstance(true); |
| 112 | } |
| 113 | |
| 114 | public void setData(MyDataObject data) { |
| 115 | this.data = data; |
| 116 | } |
| 117 | |
| 118 | public MyDataObject getData() { |
| 119 | return data; |
| 120 | } |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 121 | } |
| 122 | </pre> |
| 123 | |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 124 | <p class="caution"><strong>Caution:</strong> While you can store any object, you |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 125 | should never pass an object that is tied to the {@link android.app.Activity}, such as a {@link |
| 126 | android.graphics.drawable.Drawable}, an {@link android.widget.Adapter}, a {@link android.view.View} |
| 127 | or any other object that's associated with a {@link android.content.Context}. If you do, it will |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 128 | leak all the views and resources of the original activity instance. (Leaking resources |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 129 | means that your application maintains a hold on them and they cannot be garbage-collected, so |
| 130 | lots of memory can be lost.)</p> |
| 131 | |
| Mark Lu | c4a0139 | 2016-07-18 10:42:11 -0700 | [diff] [blame] | 132 | <p>Then use {@link android.app.FragmentManager} to add the fragment to the activity. |
| 133 | You can obtain the data object from the fragment when the activity starts again during runtime |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 134 | configuration changes. For example, define your activity as follows:</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 135 | |
| 136 | <pre> |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 137 | public class MyActivity extends Activity { |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 138 | |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 139 | private RetainedFragment dataFragment; |
| 140 | |
| 141 | @Override |
| 142 | public void onCreate(Bundle savedInstanceState) { |
| 143 | super.onCreate(savedInstanceState); |
| 144 | setContentView(R.layout.main); |
| 145 | |
| 146 | // find the retained fragment on activity restarts |
| 147 | FragmentManager fm = getFragmentManager(); |
| 148 | dataFragment = (DataFragment) fm.findFragmentByTag(“data”); |
| 149 | |
| 150 | // create the fragment and data the first time |
| 151 | if (dataFragment == null) { |
| 152 | // add the fragment |
| 153 | dataFragment = new DataFragment(); |
| 154 | fm.beginTransaction().add(dataFragment, “data”).commit(); |
| 155 | // load the data from the web |
| 156 | dataFragment.setData(loadMyData()); |
| 157 | } |
| 158 | |
| 159 | // the data is available in dataFragment.getData() |
| 160 | ... |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 161 | } |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 162 | |
| 163 | @Override |
| 164 | public void onDestroy() { |
| 165 | super.onDestroy(); |
| 166 | // store the data in the fragment |
| 167 | dataFragment.setData(collectMyLoadedData()); |
| 168 | } |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 169 | } |
| 170 | </pre> |
| 171 | |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 172 | <p>In this example, {@link android.app.Activity#onCreate(Bundle) onCreate()} adds a fragment |
| 173 | or restores a reference to it. {@link android.app.Activity#onCreate(Bundle) onCreate()} also |
| 174 | stores the stateful object inside the fragment instance. |
| Mark Lu | c4a0139 | 2016-07-18 10:42:11 -0700 | [diff] [blame] | 175 | {@link android.app.Activity#onDestroy() onDestroy()} updates the stateful object inside the |
| Ricardo Cervera | 893ee42 | 2014-01-27 17:47:39 -0800 | [diff] [blame] | 176 | retained fragment instance.</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | <h2 id="HandlingTheChange">Handling the Configuration Change Yourself</h2> |
| 183 | |
| 184 | <p>If your application doesn't need to update resources during a specific configuration |
| 185 | change <em>and</em> you have a performance limitation that requires you to |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 186 | avoid the activity restart, then you can declare that your activity handles the configuration change |
| 187 | itself, which prevents the system from restarting your activity.</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 188 | |
| 189 | <p class="note"><strong>Note:</strong> Handling the configuration change yourself can make it much |
| Scott Main | c6cb8a7 | 2010-04-09 15:52:18 -0700 | [diff] [blame] | 190 | more difficult to use alternative resources, because the system does not automatically apply them |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 191 | for you. This technique should be considered a last resort when you must avoid restarts due to a |
| 192 | configuration change and is not recommended for most applications.</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 193 | |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 194 | <p>To declare that your activity handles a configuration change, edit the appropriate <a |
| Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 195 | href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> element in |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 196 | your manifest file to include the <a |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 197 | href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 198 | android:configChanges}</a> attribute with a value that represents the configuration you want to |
| 199 | handle. Possible values are listed in the documentation for the <a |
| 200 | href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code |
| 201 | android:configChanges}</a> attribute (the most commonly used values are {@code "orientation"} to |
| 202 | prevent restarts when the screen orientation changes and {@code "keyboardHidden"} to prevent |
| 203 | restarts when the keyboard availability changes). You can declare multiple configuration values in |
| 204 | the attribute by separating them with a pipe {@code |} character.</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 205 | |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 206 | <p>For example, the following manifest code declares an activity that handles both the |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 207 | screen orientation change and keyboard availability change:</p> |
| 208 | |
| 209 | <pre> |
| 210 | <activity android:name=".MyActivity" |
| 211 | android:configChanges="orientation|keyboardHidden" |
| 212 | android:label="@string/app_name"> |
| 213 | </pre> |
| 214 | |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 215 | <p>Now, when one of these configurations change, {@code MyActivity} does not restart. |
| 216 | Instead, the {@code MyActivity} receives a call to {@link |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 217 | android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}. This method |
| 218 | is passed a {@link android.content.res.Configuration} object that specifies |
| 219 | the new device configuration. By reading fields in the {@link android.content.res.Configuration}, |
| 220 | you can determine the new configuration and make appropriate changes by updating |
| 221 | the resources used in your interface. At the |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 222 | time this method is called, your activity's {@link android.content.res.Resources} object is updated |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 223 | to return resources based on the new configuration, so you can easily |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 224 | reset elements of your UI without the system restarting your activity.</p> |
| 225 | |
| 226 | <p class="caution"><strong>Caution:</strong> Beginning with Android 3.2 (API level 13), <strong>the |
| 227 | "screen size" also changes</strong> when the device switches between portrait and landscape |
| 228 | orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing |
| 229 | for API level 13 or higher (as declared by the <a |
| 230 | href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> and <a |
| 231 | href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a> |
| 232 | attributes), you must include the {@code "screenSize"} value in addition to the {@code |
| 233 | "orientation"} value. That is, you must decalare {@code |
| 234 | android:configChanges="orientation|screenSize"}. However, if your application targets API level |
| 235 | 12 or lower, then your activity always handles this configuration change itself (this configuration |
| 236 | change does not restart your activity, even when running on an Android 3.2 or higher device).</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 237 | |
| 238 | <p>For example, the following {@link |
| 239 | android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()} implementation |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 240 | checks the current device orientation:</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 241 | |
| 242 | <pre> |
| 243 | @Override |
| 244 | public void onConfigurationChanged(Configuration newConfig) { |
| 245 | super.onConfigurationChanged(newConfig); |
| 246 | |
| 247 | // Checks the orientation of the screen |
| 248 | if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 249 | Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); |
| 250 | } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ |
| 251 | Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); |
| 252 | } |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 253 | } |
| 254 | </pre> |
| 255 | |
| 256 | <p>The {@link android.content.res.Configuration} object represents all of the current |
| 257 | configurations, not just the ones that have changed. Most of the time, you won't care exactly how |
| 258 | the configuration has changed and can simply re-assign all your resources that provide alternatives |
| 259 | to the configuration that you're handling. For example, because the {@link |
| 260 | android.content.res.Resources} object is now updated, you can reset |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 261 | any {@link android.widget.ImageView}s with {@link android.widget.ImageView#setImageResource(int) |
| 262 | setImageResource()} |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 263 | and the appropriate resource for the new configuration is used (as described in <a |
| 264 | href="providing-resources.html#AlternateResources">Providing Resources</a>).</p> |
| 265 | |
| 266 | <p>Notice that the values from the {@link |
| 267 | android.content.res.Configuration} fields are integers that are matched to specific constants |
| 268 | from the {@link android.content.res.Configuration} class. For documentation about which constants |
| 269 | to use with each field, refer to the appropriate field in the {@link |
| 270 | android.content.res.Configuration} reference.</p> |
| 271 | |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 272 | <p class="note"><strong>Remember:</strong> When you declare your activity to handle a configuration |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 273 | change, you are responsible for resetting any elements for which you provide alternatives. If you |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 274 | declare your activity to handle the orientation change and have images that should change |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 275 | between landscape and portrait, you must re-assign each resource to each element during {@link |
| 276 | android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}.</p> |
| 277 | |
| 278 | <p>If you don't need to update your application based on these configuration |
| 279 | changes, you can instead <em>not</em> implement {@link |
| 280 | android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}. In |
| 281 | which case, all of the resources used before the configuration change are still used |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 282 | and you've only avoided the restart of your activity. However, your application should always be |
| 283 | able to shutdown and restart with its previous state intact, so you should not consider this |
| 284 | technique an escape from retaining your state during normal activity lifecycle. Not only because |
| 285 | there are other configuration changes that you cannot prevent from restarting your application, but |
| 286 | also because you should handle events such as when the user leaves your application and it gets |
| 287 | destroyed before the user returns to it.</p> |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 288 | |
| Scott Main | 8da1191 | 2011-08-12 12:22:18 -0700 | [diff] [blame] | 289 | <p>For more about which configuration changes you can handle in your activity, see the <a |
| Scott Main | f940a1f | 2010-02-09 18:48:27 -0800 | [diff] [blame] | 290 | href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code |
| 291 | android:configChanges}</a> documentation and the {@link android.content.res.Configuration} |
| 292 | class.</p> |