今天抽空学习了下安卓官网,对安卓的Activity的生命周期有了更深的理解。
http://developer.android.com/training/basics/activity-lifecycle/pausing.html
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:
- Stop animations or other ongoing actions that could consume CPU.
- Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
- 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.
。。。。
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()方法时,Activity处于半可见状态,多数情况下意味着用户要离开这个Activity,很快进入停止状态。通常我们在onPause回调里面执行
1、停止动画
2、提交未保存的东西,比如草稿邮件
3、释放系统资源,如广播接收器、GPS等等,或者可能影响电池生命的资源等等。
。。。。
然而,应该避免在onPause回调中进行消耗CPU的操作,比如写数据库,因为这样会使进入下一个Activity的转换动画造成延迟,最好把这些操作房子onStop方法中。