Android系统横竖屏切换时候Activity的生命周期

网上看到有文章说横屏切换成竖屏的时候会打印两次相同的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

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

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

        android:theme="@style/AppTheme">
        ".MainActivity">
            
                "android.intent.action.MAIN" />

                "android.intent.category.LAUNCHER" />
            
        
    

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

".MainActivity"
            android:configChanges="orientation|screenSize">
            
                "android.intent.action.MAIN" />

                "android.intent.category.LAUNCHER" />
            
        

ps: 若编译选项中的minSdkVersion和targetSdkVersion有一项大于13时,需同时添加orientation|screenSize属性才能防止旋转屏幕时Activity重启

    创建Activity:
MainActivity: onCreate:
MainActivity: onStart:
MainActivity: onResume:
    旋转至横屏:
MainActivity: onConfigurationChanged:
    旋转至竖屏:
MainActivity: onConfigurationChanged:

小结:可以看到,添加了android:configChanges=”orientation|screenSize”属性的Activity在旋转屏幕时不会重启。

你可能感兴趣的:(Android学习)