您的位置:首页 > 移动开发 > Android开发

Android的关键的持久数据应该在onPause()方法中保存

2011-07-03 17:54 519 查看
最近在看Android开发文档,摘录一些关键的段落,方便查找记忆。
Because
onPause()
is the first of the three(onPause(),onStop(),onDestroy()), once the activity is created,
onPause()
is the last method that's guaranteed to be called before the process can be killed—if the system must recover memory in an emergency, then
onStop()
and
onDestroy()
might not be called. Therefore, you should use
onPause()
to write crucial persistent data (such as user edits) to storage. However, you should be selective about what information must be retained during
onPause()
, because any blocking procedures in this method block the transition to the next activity and slow the user experience.
Note: Because
onSaveInstanceState()
is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use
onPause()
to store persistent data (such as data that should be saved to a database) when the user leaves the activity.

关键的持久数据应该在onPause()方法中保存,因为onPause()方法是进程被杀死之前保证会执行的最后一个方法。当然保存的数据必须慎重,因为如果方法阻塞,会影响系统响应时间,影响用户体验。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: