Acticity横竖屏变化时的生命周期

Acticity横竖屏变化时的生命周期

一、代码部分

1.ScreenAcitivity

package com.example.helloworld1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.example.helloworld.R;

public class ScreenActivity2 extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen2);
        System.out.println("onCreate");

    }
   public void onStart() {

        super.onStart();
       System.out.println("onStart");
    }
    public void onRestart() {

        super.onRestart();
        System.out.println("Restart");
    }

    public void onResume() {
        super.onResume();
        System.out.println("onResume");
    }
    public void onPause() {

        super.onPause();
        System.out.println("onPause");
    }
    public void onStop() {

        super.onStop();
        System.out.println("onStop");
    }
    public void onDestroy() {

        super.onDestroy();
        System.out.println("onDestroy");
    }
}

2.AndroidManifest.xml的activity的MainActivity设置

  android:configChanges="keyboardHidden|screenSize|orientation"
            android:exported="true">

3.ScreenActivity设置横屏

<activity
            android:name="com.example.helloworld1.ScreenActivity2"
            android:configChanges="keyboardHidden|screenSize|orientation"
            android:exported="true"
            android:screenOrientation="landscape" />
        <activity

二、演示

1.点击主页面横竖屏切换
Acticity横竖屏变化时的生命周期

2.页面为横屏
Acticity横竖屏变化时的生命周期
3.电脑输出显示
Acticity横竖屏变化时的生命周期

上一篇:Java关键字:final,static,this,super


下一篇:Java继承