Activity life cycle

来源:互联网 发布:淘宝黄牛怎么举报 编辑:程序博客网 时间:2024/05/21 10:46

 1. Key life cyle: onCreate(Bundle savedBundleInstance) -> onStart()-> onResume() -> onPause() -> onStop() -> onDestory()
 2. Save status: there are two methods you can use to save state:
  * onSaveInstanceState(Bundle outInstanceState...): this method will be called when the activity is destroyed by system, such as when you rotate screen, or when memory is low and the activity is destroyed to save resource. Normally, you should save temporary state of activity information.
  * onPause(): this method will be called when user try to destroy the activity, such as press "BACK" button. And as you can see, there is no bundle parameter, so with this method, you should save information that you want to persist (save in file)
 3. Get back saved status: there are two method you can use to get back saved status when the activity is resnumed
  * onCreate(Bundle savedInstance): the savedInstance parameter is the parameter saved by onSaveInstanceState(), so you can get back status. Of cause, you can also get back status persisted by onPause(), since the file is always available.
  * onRestoreInstanceState(Bundle savedInstanceState): the savedInstanceState is also saved by onSaveInstanceState(), so you can get back status. Of cause, you can also get back status persisted by onPause(), since the file is always available.
 4. When will onSavedInstanceState() be called:
  * Press Home Button
  * Press Power Button
  * Start a new activity from old activity, the old activity's onSavedInstanceState() will be called
  * Rotate screen
 5. When will onPause() be called: before activity fall into Paused status
 6. When will onRestoreSavedInstance() be called: when the activity is destroyed by system, such as rotate screen. (if you press Home button, and go back to the activity quickly, the system will quite likely to destroy the activity, in this case, the onRestoreSavedInstance() will not be called)

0 0
原创粉丝点击