android-屏幕旋轉

當手機屏幕方向變化時,Activity會重新加載,從oncreate開始,這樣很明顯效率變低。


再清單文件裏,添加

<activity
            android:configChanges="orientation|screenSize"

 </activity>,屏幕方向在改變時,Activity 不再重新加載,而是回調onConfigurationChanged() 這個方法。

@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		Log.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.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章