activity生命周期
生命周期测试
有以下两个activity
:FirstActivity
,SecondActivit
,用来测试生命周期函数执行情况;
FirstActivity
点击按钮跳转到SecondActivity
,SecondActivity
显示模式为对话框模式
FirstActivity
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("xiu", "执行生命周期函数:=== FirstActivity onCreate() ===");
setContentView(R.layout.first_activity_layout);
// finish();
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 显示启动
// Intent in = new Intent(FirstActivity.this, SecondActivity.class);
// startActivity(in);
// 隐式启动
Intent in = new Intent("com.xiu.core.action.SecondActivity");
startActivity(in);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.e("xiu", "执行生命周期函数:=== FirstActivity onStart() ===");
}
@Override
protected void onResume() {
super.onResume();
Log.e("xiu", "执行生命周期函数:=== FirstActivity onResume() ===");
}
@Override
protected void onPause() {
super.onPause();
Log.e("xiu", "执行生命周期函数:=== FirstActivity onPause() ===");
}
@Override
protected void onStop() {
super.onStop();
Log.e("xiu", "执行生命周期函数:=== FirstActivity onStop() ===");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e("xiu", "执行生命周期函数:=== FirstActivity onRestart() ===");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e("xiu", "执行生命周期函数:=== FirstActivity onDestroy() ===");
}
}
SecondActivity
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("xiu", "执行生命周期函数:=== SecondActivity: onCreate() ===");
setContentView(R.layout.second_activity_layout);
}
@Override
protected void onStart() {
super.onStart();
Log.e("xiu", "执行生命周期函数:=== SecondActivity: onStart() ===");
}
@Override
protected void onResume() {
super.onResume();
Log.e("xiu", "执行生命周期函数:=== SecondActivity: onResume() ===");
}
@Override
protected void onPause() {
super.onPause();
Log.e("xiu", "执行生命周期函数:=== SecondActivity: onPause() ===");
}
@Override
protected void onStop() {
super.onStop();
Log.e("xiu", "执行生命周期函数:=== SecondActivity: onStop() ===");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e("xiu", "执行生命周期函数:=== SecondActivity: onRestart() ===");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e("xiu", "执行生命周期函数:=== SecondActivity: onDestroy() ===");
}
}
设置SecondActivity显示模式为对话框模式
生命周期演示
- 启动
FirstActivity
- 点击home返回桌面
- 从桌面切回应用
- 屏幕翻转
在
FirstActivity
的onCreate
方法执行finish()
onDestroy
执行时机:finish()
/ 系统内存不足强制退出
FirstActivity
跳转到SecondActivity
SecondActivity
点击返回
1
1
1
1
1
1
1
1
1
1