Android Activity生命周期理解

    好歹今天把Android Activity生命周期理解搞明白了,分享给各读者。

    本文是结合android doc文档进行翻译,及参阅各同伴博客等统一完成的,不正之处,请雅正。

 

    首先,看一下android api中所提供的Activity生命周期图:

    Activity生命周期图

 

    从api中翻译各方法说明:

    1、onCreate()  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 byonStart().

     当Activity第一次创建的时候被调用。一些静态变量,控制,数据绑定到list等功能都建议在些进行。一般紧接着 onStart方法


    2、onRestart()  Called after your activity has been stopped, prior to it being started again.   Always followed byonStart()

    当你的Activity被停止,又重新被启动。 一般紧接着 onStart方法


    3、onStart()    Called when the activity is becoming visible to the user.   Followed byonResume()

     当activity即将变成可视状态(即将展示),并展现给开发者时调用。一般紧接着 onResume方法。


    4、onResume()  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 byonPause().

     当activity开始和用户交互,即展现在用户面前(展示)。此时你的activity在活动栈中顶端,用户可以使用它。


    5、onPause()    Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.   Followed by eitheronResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.

     当系统要去重启开启一个先前的activity。很大部门在这个方法里将未保存信息存储成持久状态,暂停会消耗CPU等的东西。 这个方法的实现必须快速进行,因为下一个活动在些方法返回前并不会展示。 如果是返回到之前的活动,则跳到resume方法,或直接停止而不展示给用户。


    6、onStop()      Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.

Followed by either onRestart() if this activity is coming back to interact with the user, oronDestroy() if this activity is going away.

     活动处于对用户不可见状态,因为另外一个活动已经展示并覆盖了这个。 在新活动开启,或已进行活动被当前调用时才会发生。或活动即将被销毁。


    7、onDestroy()  The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone calledfinish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with theisFinishing() method.  

     此为你的活动被销毁前得到的最后一个方法调用。发生情况:结束方法被调用,或系统临时回收内存时结束它。

 

总的来说,流程基本分为以下四种:

    1、启动时    onCreate() --> onStart() --> onResume()               创建初始化 --> 即将展示 --> 展示  

    2、结束时    onPause() --> onStop() --> onDestroy()                  暂停 --> 停止展示 --> 销毁

    3、Home时 onPause() --> onStop()                                              暂停 --> 停止展示 --> 显示首页()

    4、   -> 重新启动 onRestart() --> onStart() --> onResume()     重新启动 --> 即将展示 --> 展示

 

    按Home返回时:

    android内核机制支持这种程序切换引起的数据保存问题,onPause() 接口是使用者准备离开activity的地方,调用时ContentProvider保存修改的数据,所以不做任何操作文本会自动保存到CP中。 下次重新启动时可直接显示。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值