Activtiy的生命周期(android)

首先必须看的还是这个官方帮助的生命周期图:

Activtiy的生命周期(android) - huasoft - 快乐的机器猫 小桥加加网易分站
 

  逐项来分析:

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值