为了避免切屏时Activity重启,我们平时做法是在manifest的activity节点设置
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name">
To declare that your activity handles a configuration change, edit the appropriate <activity>
element in your manifest file to include the android:configChanges
attribute with a value that represents the configuration you want to handle. Possible values are listed in the documentation for the android:configChanges
attribute (the most commonly used values are "orientation"
to prevent restarts when the screen orientation changes and"keyboardHidden"
to prevent restarts when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe |
character.
For example, the following manifest code declares an activity that handles both the screen orientation change and keyboard availability change:
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name">
Now, when one of these configurations change, MyActivity
does not restart. Instead, the MyActivity
receives a call to onConfigurationChanged()
. This method is passed a Configuration
object that specifies the new device configuration. By reading fields in the Configuration
, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources
object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion
andtargetSdkVersion
attributes), you must include the "screenSize"
value in addition to the "orientation"
value. That is, you must decalare android:configChanges="orientation|screenSize"
. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
For example, the following onConfigurationChanged()
implementation checks the current device orientation:
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Checks the orientation of the screen if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); } }
The Configuration
object represents all of the current configurations, not just the ones that have changed. Most of the time, you won't care exactly how the configuration has changed and can simply re-assign all your resources that provide alternatives to the configuration that you're handling. For example, because theResources
object is now updated, you can reset any ImageView
s with setImageResource()
and the appropriate resource for the new configuration is used (as described in Providing Resources).
Notice that the values from the Configuration
fields are integers that are matched to specific constants from the Configuration
class. For documentation about which constants to use with each field, refer to the appropriate field in the Configuration
reference.
Remember: When you declare your activity to handle a configuration change, you are responsible for resetting any elements for which you provide alternatives. If you declare your activity to handle the orientation change and have images that should change between landscape and portrait, you must re-assign each resource to each element during onConfigurationChanged()
.