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.点击主页面横竖屏切换
2.页面为横屏
3.电脑输出显示