在Activity从创建到销毁的过程中需要不同的阶段调用7个不同的生命周期方法:
onCreate(Bundle savedInstanceState);
onStart();
onResume();
onPause();
onStop();
onRestart();
onDestory();
Activity从创建到销毁的四个主要阶段为:
1.开始Activity:这个阶段调用三个生命周期方法:onCreate()->onStart()->onResume();
2.Activity是去焦点:如果在Activity获得焦点的情况下进入其他的Activity或者应用程序,当前的Activity就会失去焦点。指定的生命周期方法为:onPause()->onStop();
3.Activity重新获得焦点:在Activity重新获得焦点时执行:onRestart()->onStart()->onResume();
4.Activity销毁:在Activity关闭时系统将会依次执行:onPause()->onStop()->onDestory();
在执行的过程中可以改变系统的执行轨迹的生命周期方法是:onPause()和OnStop()。
如果在执行onPause()方法的过程中Activity重新获得焦点,然后又失去焦点,系统将不会执行onStop()而是按照onPause()->onResume()->onPause();
如果在执行onStop()方法的过程中Activity重新获得了焦点,然后又失去了,系统将不会执行onDestory(),而是按照:onStop->onRestart()->onStart()->onResume()->onPause()->onStop();
生命周期流程图如下:
在整个Activity的生命周期中包含两层循环,第一层循环是:onPause()->onResume(),第二层循环是:onPause()->onStop->onRestart()->onStart()->onResume().可以将这两层循环定义为Activity生命周期的子周期,第一层成为焦点生命周期,第二层可以称之为可视化生命周期。也就是说,第一层循环在Activity焦点的获得与市区的过程中循环,这一个过程中Activity始终是可见的。第二层循环是在Activity可见于不可见的过程中循环,在这个过程中伴随着Activity焦点的获得与失去。也就是说,Activity首先会被显示,然后获得焦点,接着失去焦点,最后由于弹出其他的Activity使当前的Activity变成了不可见。
总的来说Activity有以下三个生命周期:
整体生命周期:onCreate()->........->onDestory()
焦点生命周期:onPause()->.........->onResume()
可视生命周期:onStart()->...........->onStop()
测试代码:
MainActivity:
package com.test.cycletest;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("onCreate", "onCreate method is Executed");
}
public void press(View view)
{
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("测试生命周期");
builder.setMessage("点击消失");
final Dialog test_dialog=builder.create();
builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
test_dialog.dismiss();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
test_dialog.dismiss();
}
});
test_dialog.show();
}
public void next(View view)
{
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
@Override
protected void onStart() {
super.onStart();
Log.i("onStart", "onStart method is Executed");
}
@Override
protected void onResume() {
super.onResume();
Log.i("onResume", "onResume method is Executed");
}
@Override
protected void onPause() {
super.onPause();
Log.i("onPause", "onPause method is Executed");
}
@Override
protected void onStop() {
super.onStop();
Log.i("onStop", "onStop method is Executed");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("onRestart", "onRestart method is Executed");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("onDestroy", "onDestroy method is Executed");
}
}
SecondActivity:
package com.test.cycletest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
}
public void previce(View view)
{
Intent intent=new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
}
部署程序运行,然后点击“点击切换到下一个界面“按钮,在按住回退键返回,最终退出应用,整个过程中的LogCat打印信息如下:
到此测试完毕