一个Android应用程序的生命周期用以下图解释再清楚不过了.
创建一个demo,代码如下:
public class MainActivity extends Activity { private static final String TAG = "Android_life"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG,"onCtreate()"); setContentView(R.layout.activity_main); } protected void onStart() { super.onStart(); Log.i(TAG,"onStart()"); } protected void onResume() { super.onResume(); Log.i(TAG,"onResume()"); } protected void onPause() { super.onPause(); Log.i(TAG,"onPause()"); } protected void onDestroy() { super.onDestroy(); Log.i(TAG,"onDestroy()"); }
运行程序的效果如下:
1)首次运行该程序可以发现日志信息如下:【注意时间】03-10 20:38:11.531: I/Android_life(4635): onCtreate()
03-10 20:38:11.641: I/Android_life(4635): onStart()
03-10 20:38:11.641: I/Android_life(4635): onResume()
2)关闭手机屏幕
03-10 20:38:11.531: I/Android_life(4635): onCtreate()
03-10 20:38:11.641: I/Android_life(4635): onStart()
03-10 20:38:11.641: I/Android_life(4635): onResume()
03-10 20:38:29.451: I/Android_life(4635): onPause()
3)激活手机屏幕
03-10 20:38:11.531: I/Android_life(4635): onCtreate()
03-10 20:38:11.641: I/Android_life(4635): onStart()
03-10 20:38:11.641: I/Android_life(4635): onResume()
03-10 20:38:29.451: I/Android_life(4635): onPause()
03-10 20:53:51.631: I/Android_life(4635): onStart()
03-10 20:53:51.731: I/Android_life(4635): onResume()
4)退出该应用程序,可以看到日志
03-10 20:57:51.161: I/Android_life(5779): onDestroy()