一.activity的生命周期
一个activity的生命周期流程图
activity生命周期有四个状态:运行状态(running),暂停状态(paused),停止状态(stoped),断开状态(killed),七个方法:onCreate,onStart,onResume,onPause,onStop,onDestory,onRestart。
二.测试Activity生命周期
新建项目
MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("onCreate");
}
@Override
protected void onStart() {
super.onStart();
System.out.println("onStart");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("onResume");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("onPause");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestory");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
System.out.println("onConfigurationChanged");
}
@Override
protected void onRestart() {
super.onRestart();
System.out.println("onRestart");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
System.out.println("onSaveInstanceState");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
System.out.println("onRestoreInstanceState");
}
}
运行
切换横屏