Android---Activity 生命周期(二)pause Activity && resume Activity


1》When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity and it will soon enter the Stopped state. 

You should usually use the onPause() callback to:

<翻译>应该使用onPause()方法处理以下工作:


1.1》Stop animations or other ongoing actions that could consume CPU.

<翻译>停止动画和其他可能消耗CPU资源的工作


1.2》Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).

<翻译>提交未保存的更改,但是仅限于当用户离开该Activity时想要永久性的保存更改(例如一封Email的草稿)。


1.3》Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.

<翻译>释放系统资源,例如broadcast receivers,传感器(如GPS),或者是其他当Activity处于paused状态时并且用户不需要它们时,任何可能影响电池寿命的资源。


2》

Generally, you should not use onPause() to store user changes (such as personal information entered into a form) to permanent storage. The only time you should persist user changes to permanent storage within onPause() is when you're certain users expect the changes to be auto-saved (such as when drafting an email). 


However, you should avoid performing CPU-intensive work during onPause(), such as writing to a database, because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during onStop()).
<翻译>应该避免在onPause()方法中执行大计算量的工作(CPU-intensive work ),如写数据库,因为它会增加进入下一个Activity的时间 (你应该将这些操作移入onStop方法中)。


You should keep the amount of operations done in the onPause() method relatively simple in order to allow for a speedy transition to the user's next destination if your activity is actually being stopped.


3》onResume()

Be aware that the system calls this method every time your activity comes into the foreground, including when it's created for the first time. 

<翻译>每当Activity进入前台运行状态,系统都会调用onResume()方法,包括第一次创建的时候。


As such, you should implement onResume() to initialize components that you release during onPause() and perform any other initializations that must occur each time the activity enters the Resumed state (such as begin animations and initialize components only used while the activity has user focus).

<翻译>因此,你应该在onResume()方法中初始化在onPause()方法中释放的资源,并且会执行每当Activity进入Resumed 状态时必须要进行的一些初始化工作(比如开启动画,以及初始化那些仅当Activity获取用户焦点时才使用的组件)。


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值