android-屏幕旋转

来源:互联网 发布:淘宝上手机 编辑:程序博客网 时间:2024/06/08 11:55

当手机屏幕方向变化时,Activity会重新加载,从oncreate开始,这样很明显效率变低。


再清单文件里,添加

<activity
            android:configChanges="orientation|screenSize"

 </activity>,屏幕方向在改变时,Activity 不再重新加载,而是回调onConfigurationChanged() 这个方法。

@Overridepublic void onConfigurationChanged(Configuration newConfig) {// TODO Auto-generated method stubLog.e("--tag--", "onConfigurationChanged");if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {Log.e("--tag--", "ORIENTATION_PORTRAIT");setContentView(R.layout.activity_main);} else {Log.e("--tag--", "ORIENTATION_LANDSCAPE");setContentView(R.layout.shuiping);}super.onConfigurationChanged(newConfig);}

当旋转时,如下


onContentChanged()也是一个回调,只要界面有变化,就会调用它。


------------------------------------------关于configuration的英文文档---------------------------


If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause()onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.

To declare that your Activity handles a configuration change, edit the appropriate <activity> element in your manifest file to include theandroid:configChanges attribute with a string value that represents the configuration that you want to handle. Possible values are listed in the documentation for the android:configChanges attribute (the most commonly used values are orientation to handle when the screen orientation changes and keyboardHidden to handle 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 snippet 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 is not restarted. Instead, the Activity 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.


0 0
原创粉丝点击