横竖屏切换时Activity生命周期变化解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tuike/article/details/79401361

本文已同步发表到我的微信公众号,扫一扫文章底部的二维码或在微信搜索 “程序员驿站”即可关注,每天都会更新优质技术文章。

作为参考,我们先看下Fragment和Activity的生命周期流程图

Activity各种场景生命周期介绍

下面是Activity的源码:

package com.sharejoys.interviewdemo;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class OneActivity extends AppCompatActivity{
    private static final String TAG = OneActivity.class.getSimpleName();

    @OnClick({R.id.btn1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                startActivity(new Intent(OneActivity.this, MainActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        Log.d(TAG, "onCreate");

        getSupportActionBar().setTitle("One");
        ButterKnife.bind(this);
        if (savedInstanceState != null) {
            Log.d(TAG, savedInstanceState.getString("key1"));
            Log.d(TAG, savedInstanceState.getString("key2"));
        }
    }


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


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d(TAG, "onSaveInstanceState");

        outState.putString("key1", "value1");
        outState.putString("key2", "value2");
    }

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

        if (savedInstanceState != null) {
            Log.d(TAG, savedInstanceState.getString("key1"));
            Log.d(TAG, savedInstanceState.getString("key2"));
        }
    }

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

    @Override
    protected void onResume() {
        Log.d(TAG, "onResume");

        Configuration mConfiguration = this.getResources().getConfiguration(); //获取设置的配置信息
        int ori = mConfiguration.orientation; //获取屏幕方向
        if (ori == Configuration.ORIENTATION_LANDSCAPE) {
            //横屏
            Log.d(TAG, "当前为横屏");
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//强制为竖屏
        } else if (ori == Configuration.ORIENTATION_PORTRAIT) {
            //竖屏
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
            Log.d(TAG, "当前为竖屏");
        }
        super.onResume();
    }

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

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

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

}

 下面是清单文件的配置:

    <activity
            android:name=".OneActivity"
           />

点击运行,正常进入,打印如下信息:

02-28 15:43:16.216 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onCreate
02-28 15:43:16.218 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStart
02-28 15:43:16.220 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onResume
02-28 15:43:16.220 10244-10244/com.sharejoys.interviewdemo D/OneActivity: 当前为竖屏

锁屏打印信息如下:

02-28 15:47:12.897 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onPause
02-28 15:47:12.966 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onSaveInstanceState
02-28 15:47:12.968 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStop

解锁打印信息如下:

02-28 15:49:39.112 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStart
02-28 15:49:39.113 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onResume
02-28 15:49:39.113 10244-10244/com.sharejoys.interviewdemo D/OneActivity: 当前为竖屏

跳转新界面打印信息如下:

02-28 15:50:16.240 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onPause
02-28 15:50:16.629 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onSaveInstanceState
02-28 15:50:16.630 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStop

从新界面返回打印信息如下:

02-28 15:51:05.060 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStart
02-28 15:51:05.060 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onResume
02-28 15:51:05.061 10244-10244/com.sharejoys.interviewdemo D/OneActivity: 当前为竖屏

关闭当前界面打印信息如下:

02-28 15:51:38.904 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onPause
02-28 15:51:39.309 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStop
02-28 15:51:39.310 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onDestroy

横竖屏生命周期变化

Activity清单文件申明没有配置configChanges属性。

进入界面

02-28 15:56:19.791 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onCreate
02-28 15:56:19.793 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStart
02-28 15:56:19.795 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onResume
02-28 15:56:19.795 10244-10244/com.sharejoys.interviewdemo D/OneActivity: 当前为竖屏

手机切换到横屏

02-28 15:57:09.175 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onPause
02-28 15:57:09.176 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onSaveInstanceState
02-28 15:57:09.177 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStop
02-28 15:57:09.178 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onDestroy
02-28 15:57:09.244 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onCreate
02-28 15:57:09.245 10244-10244/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 15:57:09.245 10244-10244/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 15:57:09.246 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStart
02-28 15:57:09.246 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onRestoreInstanceState
02-28 15:57:09.246 10244-10244/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 15:57:09.246 10244-10244/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 15:57:09.248 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onResume
02-28 15:57:09.249 10244-10244/com.sharejoys.interviewdemo D/OneActivity: 当前为横屏

备注:其中value1和value2是在onSaveInstanceState存储的信息

手机切换回竖屏

02-28 15:58:58.213 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onPause
02-28 15:58:58.213 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onSaveInstanceState
02-28 15:58:58.216 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStop
02-28 15:58:58.217 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onDestroy
02-28 15:58:58.266 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onCreate
02-28 15:58:58.266 10244-10244/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 15:58:58.266 10244-10244/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 15:58:58.278 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onStart
02-28 15:58:58.279 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onRestoreInstanceState
02-28 15:58:58.279 10244-10244/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 15:58:58.279 10244-10244/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 15:58:58.282 10244-10244/com.sharejoys.interviewdemo D/OneActivity: onResume
02-28 15:58:58.282 10244-10244/com.sharejoys.interviewdemo D/OneActivity: 当前为竖屏

从上可知,当清单中没有配置configChanges属性时,从竖屏切换到横屏或者从横屏切换到竖屏,各生命周期都会走一次。

Activity清单文件申明配置configChanges属性,且值设置为orientation。

        <activity
            android:name=".OneActivity"
            android:configChanges="orientation"/>

手机竖屏切换到横屏:

02-28 16:03:21.855 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onPause
02-28 16:03:21.856 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onSaveInstanceState
02-28 16:03:21.857 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onStop
02-28 16:03:21.858 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onDestroy
02-28 16:03:21.911 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onCreate
02-28 16:03:21.912 11214-11214/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 16:03:21.912 11214-11214/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 16:03:21.914 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onStart
02-28 16:03:21.914 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onRestoreInstanceState
02-28 16:03:21.914 11214-11214/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 16:03:21.914 11214-11214/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 16:03:21.916 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onResume
02-28 16:03:21.916 11214-11214/com.sharejoys.interviewdemo D/OneActivity: 当前为横屏

手机横屏切换到竖屏:

02-28 16:04:10.753 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onPause
02-28 16:04:10.754 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onSaveInstanceState
02-28 16:04:10.756 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onStop
02-28 16:04:10.756 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onDestroy
02-28 16:04:10.798 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onCreate
02-28 16:04:10.798 11214-11214/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 16:04:10.799 11214-11214/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 16:04:10.808 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onStart
02-28 16:04:10.808 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onRestoreInstanceState
02-28 16:04:10.809 11214-11214/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 16:04:10.809 11214-11214/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 16:04:10.811 11214-11214/com.sharejoys.interviewdemo D/OneActivity: onResume
02-28 16:04:10.811 11214-11214/com.sharejoys.interviewdemo D/OneActivity: 当前为竖屏

从上可知,当我们将configChanges的属性设置为orientation,各生命周期也会周一次

Activity清单文件申明配置configChanges属性,且值设置为orientation|keyboardHidden。

        <activity
            android:name=".OneActivity"
            android:configChanges="orientation|keyboardHidden"/>

手机竖屏切换到横屏

02-28 16:05:58.115 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onPause
02-28 16:05:58.115 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onSaveInstanceState
02-28 16:05:58.126 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onStop
02-28 16:05:58.127 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onDestroy
02-28 16:05:58.165 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onCreate
02-28 16:05:58.165 11793-11793/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 16:05:58.165 11793-11793/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 16:05:58.169 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onStart
02-28 16:05:58.170 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onRestoreInstanceState
02-28 16:05:58.170 11793-11793/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 16:05:58.170 11793-11793/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 16:05:58.172 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onResume
02-28 16:05:58.172 11793-11793/com.sharejoys.interviewdemo D/OneActivity: 当前为横屏

手机横屏切换到竖屏:

02-28 16:06:51.952 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onPause
02-28 16:06:51.953 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onSaveInstanceState
02-28 16:06:51.954 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onStop
02-28 16:06:51.955 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onDestroy
02-28 16:06:52.009 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onCreate
02-28 16:06:52.010 11793-11793/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 16:06:52.010 11793-11793/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 16:06:52.013 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onStart
02-28 16:06:52.013 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onRestoreInstanceState
02-28 16:06:52.013 11793-11793/com.sharejoys.interviewdemo D/OneActivity: value1
02-28 16:06:52.013 11793-11793/com.sharejoys.interviewdemo D/OneActivity: value2
02-28 16:06:52.015 11793-11793/com.sharejoys.interviewdemo D/OneActivity: onResume
02-28 16:06:52.015 11793-11793/com.sharejoys.interviewdemo D/OneActivity: 当前为竖屏

从上可知,从上可知,当我们将configChanges的属性设置为orientation|keyboardHidde,各生命周期同样也会走一次。

将configChanges的属性设置为orientation|screenSize时

        <activity
            android:name=".OneActivity"
            android:configChanges="orientation|screenSize"/>

手机竖屏切换到横屏:

02-28 16:10:41.822 12199-12199/com.sharejoys.interviewdemo D/OneActivity: onConfigurationChanged

手机横屏切换到竖屏:

02-28 16:10:59.392 12199-12199/com.sharejoys.interviewdemo D/OneActivity: onConfigurationChanged

从上可知,当我们将configChanges的属性设置为orientation|screenSize,只会走onConfigurationChange。

总结:

注:以上结果是在android 27(>android 13)上测试得到的结果。

大于Android 3.2(API Level 13)

  • 不设置Activity的android:configChanges,或设置Activity的android:configChanges="orientation"时,或是设置android:configChanges="orientation|keyboardHidden,切屏会重新调用各个生命周期,且都只执行一次。
  • 设置Activity的android:configChanges="orientation|screenSize"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法。

小于等于Android 3.2(API Level 13)

  • 不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次。
  • 设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次。
  • 设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法。

关注我的技术公众号"程序员驿站",每天都有优质技术文章推送,微信扫一扫下方二维码即可关注:


展开阅读全文

没有更多推荐了,返回首页