首先必须看的还是这个官方帮助的生命周期图:
![Activtiy的生命周期(android) - huasoft - 快乐的机器猫 小桥加加网易分站 Activtiy的生命周期(android) - huasoft - 快乐的机器猫 小桥加加网易分站](http://img0.ph.126.net/eqHDUxG6p7ZcB2IFFwvXsQ==/2536933965110721600.jpg)
逐项来分析:
1) onCreate() 当一个Activtiy被创建时调用onCreate(),永远在后面跟着onStart()
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
Always followed by onStart()
2) onStart() 当activity 可见的时候
Called when the activity is becoming visible to the user.
Followed by
onResume()
if the activity comes to the foreground, or
onStop()
if it becomes hidden.
3)onResume() 当activity获得用户焦点的时候调用
Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
Always followed by
onPause()
.
4) onPause() 在这里面经常要做的是保存现场用户输入的数据之类。 比如用户正填表格的时候来电话了,表格被pause。
Called when the system is about to start resuming a previous activity
5) onStop() 当activity对用户不再可见。 这一条和onStart()是相对应的
Called when the activity is no longer visible to the user
6)onDestroy() 2种可能:一是程序被finish了,二是因为系统资源不足而被临时销毁。 (可以用isFinishing()来分辨这两种情况)
The final call you receive before your activity is destroyed
一个简单例子:
创建一个新工程,添加一个主Activtiy, 运行,得到
06-01 05:29:20.976: I/stephen new02(355): onCreate()
06-01 05:29:20.976: I/stephen new02(355): onStart()
06-01 05:29:20.986: I/stephen new02(355): onResume()
然后按返回键,得到:
06-01 05:29:28.756: I/stephen new02(355): onPause()
06-01 05:29:29.306: I/stephen new02(355): onStop()
06-01 05:29:29.306: I/stephen new02(355): onDestroy()