读书人

onPause()步骤的特殊性

发布时间: 2012-08-31 12:55:03 作者: rapoo

onPause()方法的特殊性
onPause(), onStop(), onDestroy() are "killable after" lifecycle methods. This indicates whether or not the system can kill the process hosting the activity at any time after the method returns, without executing another line of the activity's code. Because onPause() is the first of the three, 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.

onCreate(), onRestart(), onStart(), onResume() are "unkillable after" methods.They protect the process hosting the activity from being killed from the moment they are called. Thus, an activity is killable from the time onPause() returns to the time onResume() is called. It will not again be killable until onPause() is again called and returns.

简单来说,在执行了onPause()方法之后,直到执行onResume()之前,该activity所在的进程都是有可能“突然死亡”的,onStop()和onDestroy()方法并不能保证一定会被执行。所以onPause()方法是持久化相关数据的最后的可靠时机。因此onPause()是特别重要的生命周期方法

读书人网 >移动开发

热点推荐