安卓屏幕横竖屏切换以及生命周期演示

关于安卓屏幕横竖屏切换及其生命周期的演示:

(1)什么都不设置的一个生命周期:(生命周期会回调,走两次)

    启动---> 切换横屏---> 点击返回结束

   

(2)只设置了如下方法:(生命周期也会回调走两次)

     

    启动---> 切换横屏---> 点击返回结束

   

(3)在AndroidMainfest.xml里设置如下两个方法的一个生命周期(这个时候切换的时候会走onconfigChange()方法,不回调了,只走一次完整生命周期)

         启动---> 切换横屏---> 点击返回结束

       

(4)相关代码:

布局xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="切换横竖屏"
            android:onClick="changeScreen"/>

    <TextView
            android:id="@+id/text_screen"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

Activity代码:(此处用koltin代码实现)

class MainActivity : AppCompatActivity() {
    /**
     * 屏幕横竖屏切换,和生命周期管理
     */

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //获取当前的时间
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val time = format.format(Date())
        text_screen.text="onCreate"+time

        //给TextView设置点击事件清除文字
        text_screen.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View) {
                text_screen.text = "重新来!"
            }
        })
        Log.e("TAG", "onCreate")
    }

    //改变横竖屏切换的方法
    fun changeScreen(view: View) {
        /**
         * int ORIENTATION_PORTRAIT = 1;  竖屏
         * int ORIENTATION_LANDSCAPE = 2; 横屏
         */
        //获取屏幕的方向  ,数值1表示竖屏,数值2表示横屏
        val screenNum = resources.configuration.orientation
        //(如果竖屏)设置屏幕为横屏
        if (screenNum == 1) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            //设置为置屏幕为竖屏
        } else {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }

    //屏幕方向发生改变的回调方法
    override fun onConfigurationChanged(newConfig: Configuration) {

        if (newConfig.orientation === Configuration.ORIENTATION_LANDSCAPE) {
            text_screen.append("
 当前屏幕为横屏")
        } else {
            text_screen.append("
 当前屏幕为竖屏")
        }
        super.onConfigurationChanged(newConfig)
        Log.e("TAG", "onConfigurationChanged")
        //  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  //设置横屏
    }

    override fun onStart() {
        super.onStart()
        text_screen.append("
 onStart")
        Log.e("TAG", "onStart")
    }

    override fun onRestart() {
        super.onRestart()
        text_screen.append("
 onRestart")
        Log.e("TAG", "onRestart")
    }

    override fun onResume() {
        super.onResume()
        text_screen.append("
 onResume")
        Log.e("TAG", "onResume")
    }

    override fun onPause() {
        super.onPause()
        text_screen.append("
 onPause")
        Log.e("TAG", "onPause")
    }

    override fun onStop() {
        super.onStop()
        text_screen.append("
 onStop")
        Log.e("TAG", "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        text_screen.append("
 onDestroy")
        Log.e("TAG", "onDestroy")
    }

    //AdMainfest.xml里加这个两个和不加的区别:
    //android:configChanges="orientation|keyboardHidden|screenSize"
    //android:screenOrientation="portrait"
    //或者只设置:
    //android:configChanges="orientation"
}

AndroidMainfest.xml代码:

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

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

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

</manifest>

(5)注意:

   A:configChanges属性

configChanges属性可以设置多个值,中间使用竖线分割; 
1. orientation 屏幕在纵向和横向间旋转 
2.keyboardHidden 键盘显示或隐藏 
3.screenSize 屏幕大小改变了 
4.fontScale 用户变更了首选的字体大小 
5.locale 用户选择了不同的语言设定 
6.keyboard 键盘类型变更,例如手机从12键盘切换到全键盘 
7.touchscreen或navigation 键盘或导航方式变化,一般不会发生这样的事件

其中前面三个是常用的,后面的属性很少用!
如果要Activity中的生命周期不回调,就要设置:android:configChanges="orientation|keyboardHidden|screenSize"

 注意一定要设置这三个值,否是生命周期会被回掉

B:screenOrientation属性

screenOrientation有如下选项: 
1.unspecified 默认值,由系统判断状态自动切换 
2.landscape 横屏 
3. portrait 竖屏 
4.user 用户当前设置的orientation值 
5. behind 下一个要显示的Activity的orientation值 
6. sensor 使用传感器 传感器的方向 
7. nosensor 不使用传感器 基本等同于unspecified

screenOrientation只能选一个值!一般设置都是横屏或竖屏,其他也很少用

C:onConfigurationChanged方法

//屏幕方向发生改变的回调方法
    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            text_screen.append("
 当前屏幕为横屏");
        } else {
            text_screen.append("
 当前屏幕为竖屏");
        }
        super.onConfigurationChanged(newConfig);
        Log.e("TAG", "onConfigurationChanged");
      //  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  //设置横屏
    }

前提是AndroidMainfest.xml中已经定义了:android:configChanges="orientation"

原文地址:https://www.cnblogs.com/wx-9/p/10507259.html