Activity作为Android的四大组件之一,在实际开发中扮演着很重要的角色,了解它的声明周期对我们以后的开发会很有帮助,下面我们来看看在不同场景下Activity的生命周期
准备两个Activity,IndexActivity和TwoActivity
public class IndexActivity extends AppCompatActivity {
public static final String TAG = "IndexActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.text_request_one).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(IndexActivity.this, TwoActivity.class));
}
});
Log.i(TAG, "onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i(TAG, "onNewIntent");
}
}
public class TwoActivity extends AppCompatActivity {
public static final String TAG = "TwoActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
}
启动App IndexActivity的生命周期
01-22 15:44:02.131 23541-23541/fe.dotwoven I/IndexActivity: onCreate
01-22 15:44:02.131 23541-23541/fe.dotwoven I/IndexActivity: onStart
01-22 15:44:02.133 23541-23541/fe.dotwoven I/IndexActivity: onResume
启动IndexActivity分别调用了onCreate,onStart, onResume方法。
按Home键回到桌面 Activity生命周期
01-22 15:46:52.583 23541-23541/fe.dotwoven I/IndexActivity: onPause
01-22 15:46:53.110 23541-23541/fe.dotwoven I/IndexActivity: onStop
回到桌面会调用IndexActivity的onPause,onStop方法
从桌面再次进入IndexActivity 生命周期
01-22 15:47:51.388 23541-23541/fe.dotwoven I/IndexActivity: onRestart
01-22 15:47:51.388 23541-23541/fe.dotwoven I/IndexActivity: onStart
01-22 15:47:51.388 23541-23541/fe.dotwoven I/IndexActivity: onResume
再次进入的时候调用了IndexActivity的onRestart,onStart,onResume方法
IndexActivity上按返回键时生命周期
01-22 15:50:03.004 23541-23541/fe.dotwoven I/IndexActivity: onPause
01-22 15:50:03.558 23541-23541/fe.dotwoven I/IndexActivity: onStop
01-22 15:50:03.558 23541-23541/fe.dotwoven I/IndexActivity: onDestroy
分别执行了IndexActivity的onPause,onStop,onDestroy方法
从IndexActivity跳转到TwoActivity生命周期
01-22 15:51:54.183 23541-23541/fe.dotwoven I/IndexActivity: onPause
01-22 15:51:54.270 23541-23541/fe.dotwoven I/TwoActivity: onCreate
01-22 15:51:54.271 23541-23541/fe.dotwoven I/TwoActivity: onStart
01-22 15:51:54.273 23541-23541/fe.dotwoven I/TwoActivity: onResume
01-22 15:51:54.688 23541-23541/fe.dotwoven I/IndexActivity: onStop
首先会执行IndexActivity的onPause方法,接下来执行TwoActivity的onCreate,onStart,onResume方法;最后才会执行IndexActivity的onStop方法,最后这点大家要记住
TwoActivity按返回键返回IndexActivity生命周期
01-22 15:53:49.444 23541-23541/fe.dotwoven I/TwoActivity: onPause
01-22 15:53:49.484 23541-23541/fe.dotwoven I/IndexActivity: onRestart
01-22 15:53:49.485 23541-23541/fe.dotwoven I/IndexActivity: onStart
01-22 15:53:49.494 23541-23541/fe.dotwoven I/IndexActivity: onResume
01-22 15:53:49.870 23541-23541/fe.dotwoven I/TwoActivity: onStop
01-22 15:53:49.870 23541-23541/fe.dotwoven I/TwoActivity: onDestroy
首先会执行TwoActivity的onPause方法,接下来调用IndexActivity的onRestart,onStart,onResume方法,最后再调用TwoActivity的onStop和onDestroy方法。
启动模式不同 Activity的生命周期
如果IndexActivity的启动模式是singleTask或是singleInstance,然后从IndexActivity跳转到TwoActivity,再从TwoActivity跳转到IndexActivity,这个时候生命周期会有什么不同呢?我们来看看
01-22 16:07:53.739 18403-18403/fe.dotwoven I/TwoActivity: onPause
01-22 16:07:53.762 18403-18403/fe.dotwoven I/IndexActivity: onNewIntent
01-22 16:07:53.762 18403-18403/fe.dotwoven I/IndexActivity: onRestart
01-22 16:07:53.765 18403-18403/fe.dotwoven I/IndexActivity: onStart
01-22 16:07:53.767 18403-18403/fe.dotwoven I/IndexActivity: onResume
01-22 16:07:54.103 18403-18403/fe.dotwoven I/TwoActivity: onStop
01-22 16:07:54.103 18403-18403/fe.dotwoven I/TwoActivity: onDestroy
IndexActivity多调用了一个onNewIntent方法。
如果IndexActivity的启动模式是singleTop,然后从IndexActivity启动到IndexActivity,它其实显示的还是IndexActivity,那IndexActivity的生命周期会有什么不同呢?
01-22 16:13:03.392 4830-4830/fe.dotwoven I/IndexActivity: onPause
01-22 16:13:03.392 4830-4830/fe.dotwoven I/IndexActivity: onNewIntent
01-22 16:13:03.393 4830-4830/fe.dotwoven I/IndexActivity: onResume
可以看到它先调用了自己的onPause方法,然后调用了onNewIntent,onResume方法。
总结
到此Activity各个场景的生命周期差不多都分析完了,理解好Activity的生命周期有利于我们的开发工作