关于横竖屏切换问题,android:configChanges

1、测试手机:小米5C android7.0
2、测试环境:

    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.lzy.myapplication"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

3、过程
(1)创建activity,并在所有的生命周期添加打印。
(2)首次进入

横屏启动
2018-11-30 12:56:11.397 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 12:56:11.431 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 12:56:11.437 com.example.lzy.myapplication D/MainActivity: onResume

(3)竖屏转横屏,activity重建一次

横屏启动
2018-11-30 12:56:11.397 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 12:56:11.431 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 12:56:11.437 com.example.lzy.myapplication D/MainActivity: onResume
切换竖屏
2018-11-30 12:57:05.632 com.example.lzy.myapplication D/MainActivity: onPause
2018-11-30 12:57:05.634 com.example.lzy.myapplication D/MainActivity: onSaveInstanceState
2018-11-30 12:57:05.641 com.example.lzy.myapplication D/MainActivity: onStop
2018-11-30 12:57:05.642 com.example.lzy.myapplication D/MainActivity: onDestroy
2018-11-30 12:57:05.684 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 12:57:05.712 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 12:57:05.712 com.example.lzy.myapplication D/MainActivity: onRestoreInstanceState
2018-11-30 12:57:05.719 com.example.lzy.myapplication D/MainActivity: onResume

(4)横屏再转竖屏,activity重建一次

横屏启动
2018-11-30 12:56:11.397 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 12:56:11.431 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 12:56:11.437 com.example.lzy.myapplication D/MainActivity: onResume
切换竖屏
2018-11-30 12:57:05.632 com.example.lzy.myapplication D/MainActivity: onPause
2018-11-30 12:57:05.634 com.example.lzy.myapplication D/MainActivity: onSaveInstanceState
2018-11-30 12:57:05.641 com.example.lzy.myapplication D/MainActivity: onStop
2018-11-30 12:57:05.642 com.example.lzy.myapplication D/MainActivity: onDestroy
2018-11-30 12:57:05.684 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 12:57:05.712 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 12:57:05.712 com.example.lzy.myapplication D/MainActivity: onRestoreInstanceState
2018-11-30 12:57:05.719 com.example.lzy.myapplication D/MainActivity: onResume
切换横屏
2018-11-30 12:58:07.475 com.example.lzy.myapplication D/MainActivity: onPause
2018-11-30 12:58:07.477 com.example.lzy.myapplication D/MainActivity: onSaveInstanceState
2018-11-30 12:58:07.489 com.example.lzy.myapplication D/MainActivity: onStop
2018-11-30 12:58:07.490 com.example.lzy.myapplication D/MainActivity: onDestroy
2018-11-30 12:58:07.536 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 12:58:07.567 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 12:58:07.567 com.example.lzy.myapplication D/MainActivity: onRestoreInstanceState
2018-11-30 12:58:07.572 com.example.lzy.myapplication D/MainActivity: onResume

(5)持续的横屏转竖屏,竖屏转横屏,生命周期同上,都是activity重建一次。
(6)在AndroidManifest.xml 的avtivity中配置android:configChanges="orientation"属性,竖屏转横屏,结果同上,没有变化,activity重建一次

横屏启动
2018-11-30 13:03:43.730 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 13:03:43.899 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 13:03:43.907 com.example.lzy.myapplication D/MainActivity: onResume
切换竖屏
2018-11-30 13:03:48.619 com.example.lzy.myapplication D/MainActivity: onPause
2018-11-30 13:03:48.625 com.example.lzy.myapplication D/MainActivity: onSaveInstanceState
2018-11-30 13:03:48.633 com.example.lzy.myapplication D/MainActivity: onStop
2018-11-30 13:03:48.635 com.example.lzy.myapplication D/MainActivity: onDestroy
2018-11-30 13:03:48.689 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 13:03:48.718 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 13:03:48.718 com.example.lzy.myapplication D/MainActivity: onRestoreInstanceState
2018-11-30 13:03:48.725 com.example.lzy.myapplication D/MainActivity: onResume

(7)在AndroidManifest.xml 的avtivity中配置 android:configChanges="orientation|keyboardHidden"属性,依旧没有变化,重建一次。

横屏启动
2018-11-30 13:05:52.480 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 13:05:52.642 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 13:05:52.650 com.example.lzy.myapplication D/MainActivity: onResume
切换竖屏
2018-11-30 13:05:57.275 com.example.lzy.myapplication D/MainActivity: onPause
2018-11-30 13:05:57.280 com.example.lzy.myapplication D/MainActivity: onSaveInstanceState
2018-11-30 13:05:57.289 com.example.lzy.myapplication D/MainActivity: onStop
2018-11-30 13:05:57.290 com.example.lzy.myapplication D/MainActivity: onDestroy
2018-11-30 13:05:57.364 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 13:05:57.388 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 13:05:57.389 com.example.lzy.myapplication D/MainActivity: onRestoreInstanceState
2018-11-30 13:05:57.393 com.example.lzy.myapplication D/MainActivity: onResume

(8)在AndroidManifest.xml 的avtivity中配置 android:configChanges="orientation|keyboardHidden"属性,依旧没有变化,同上
(9)在AndroidManifest.xml 的avtivity中配置android:configChanges="orientation|screenSize",横屏切换竖屏,调用onConfigurationChanged。但不知道为什么第一次切换的时候调用两次,之后再切换值调用一次,希望有知道的老铁能帮忙解答一下。

横屏启动
2018-11-30 13:31:01.487 com.example.lzy.myapplication D/MainActivity: onCreate
2018-11-30 13:31:01.647 com.example.lzy.myapplication D/MainActivity: onStart
2018-11-30 13:31:01.656 com.example.lzy.myapplication D/MainActivity: onResume
切换竖屏
2018-11-30 13:31:10.432 com.example.lzy.myapplication D/MainActivity: onConfigurationChanged
2018-11-30 13:31:10.497 com.example.lzy.myapplication D/MainActivity: onConfigurationChanged

注:自从Android 3.2(API 13),在设置Activity的android:configChanges="orientation|keyboardHidden"后。还是一样会又一次调用各个生命周期的。由于screensize也開始跟着设备的横竖切换而改变。所以在AndroidManifest.xml里设置的MiniSdkVersion和 TargetSdkVersion属性大于等于13的情况下,假设你想阻止程序在执行时又一次载入Activity,除了设置"orientation"。 你还必须设置" screenSize"。(摘自网络,并未实际测试)

你可能感兴趣的:(关于横竖屏切换问题,android:configChanges)