Android系統橫豎屏切換時候Activity的生命週期

Darkchaox發表於2016-06-09

網上看到有文章說橫屏切換成豎屏的時候會列印兩次相同的Log,百思不得其解啊,於是自己動手驗證一下。

環境:
AndroidStudio 2.1.2
minSdkVersion 15 、targetSdkVersion 23
裝置:Genymotion 4.1.1-API16

分為兩個步驟:

  • 不新增android:configChanges屬性
  • 新增android:configChanges=”orientation|screenSize”

原始碼

MainActivity.class

package com.example.activitydemo;

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

public class MainActivity extends Activity {
    private String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart: ");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState: ");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState: ");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i(TAG, "onConfigurationChanged: ");
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activitydemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"

        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

1.不新增android:configChanges屬性:

    建立Activity:
MainActivity: onCreate:
MainActivity: onStart:
MainActivity: onResume:
    旋轉至橫屏:
MainActivity: onPause:
MainActivity: onSaveInstanceState:
MainActivity: onStop:
MainActivity: onDestroy:
MainActivity: onCreate:
MainActivity: onStart:
MainActivity: onRestoreInstanceState:
MainActivity: onResume:
    旋轉至豎屏:
MainActivity: onPause:
MainActivity: onSaveInstanceState:
MainActivity: onStop:
MainActivity: onDestroy:
MainActivity: onCreate:
MainActivity: onStart:
MainActivity: onRestoreInstanceState:
MainActivity: onResume:
    再旋轉至橫屏:
MainActivity: onPause:
MainActivity: onSaveInstanceState:
MainActivity: onStop:
MainActivity: onDestroy:
MainActivity: onCreate:
MainActivity: onStart:
MainActivity: onRestoreInstanceState:
MainActivity: onResume:

小結:橫豎屏切換Activity的生命週期跟預想的一樣,只重啟了一次。可以理解為先銷燬豎屏的Activity,再建立橫屏的Activity。網上說的列印兩次Log的說法不知道是怎麼來的。

2.新增android:configChanges屬性:orientation|screenSize

<activity android:name=".MainActivity"
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

ps: 若編譯選項中的minSdkVersion和targetSdkVersion有一項大於13時,需同時新增orientation|screenSize屬性才能防止旋轉螢幕時Activity重啟

    建立Activity:
MainActivity: onCreate:
MainActivity: onStart:
MainActivity: onResume:
    旋轉至橫屏:
MainActivity: onConfigurationChanged:
    旋轉至豎屏:
MainActivity: onConfigurationChanged:

小結:可以看到,新增了android:configChanges=”orientation|screenSize”屬性的Activity在旋轉螢幕時不會重啟。

相關文章