天天看點

Android重寫onConfigurationChanged規避橫豎屏切換時候重新進入onCreate生命周期



Android重寫onConfigurationChanged規避橫豎屏切換時候重新進入onCreate生命周期

Android系統内在的機制,當activity在橫豎屏切換時候,會強制重新開始進入activity的生命周期onCreate,在有些特殊的場景,開發者不希望重新進入onCreate生命周期,那麼,可以通過重寫Android的onConfigurationChanged規避。

具體做法:

(1)添權重限:

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses           

(2)因為需要在activity裡面檢測橫豎螢幕的切換變化,故而在Androidmanifest.xml裡面配置目标activity的屬性:

android:configChanges="orientation|screenSize"           

至此,完整的Androidmanifest.xml檔案:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="zhangphil.config"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="23" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" >
    </uses-permission>

</manifest>           

(3)接下來就是重寫activity裡面的onConfigurationChanged:

@Override
	public void onConfigurationChanged(Configuration newConfig) {
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// 目前為橫屏
			Log.d(tag, "ORIENTATION_LANDSCAPE=" + Configuration.ORIENTATION_LANDSCAPE);// 2
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			// 目前為豎屏
			Log.d(tag, "ORIENTATION_PORTRAIT=" + Configuration.ORIENTATION_PORTRAIT);// 1
		}

		super.onConfigurationChanged(newConfig);
	}           

完整的測試MainActivity.java代碼檔案:

package zhangphil.config;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity {

	private final String tag = "zhangphil";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		Toast.makeText(this, "onCreate @ " + System.currentTimeMillis(), Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// 目前為橫屏
			Log.d(tag, "ORIENTATION_LANDSCAPE=" + Configuration.ORIENTATION_LANDSCAPE);// 2
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			// 目前為豎屏
			Log.d(tag, "ORIENTATION_PORTRAIT=" + Configuration.ORIENTATION_PORTRAIT);// 1
		}

		super.onConfigurationChanged(newConfig);
	}
}
           

繼續閱讀