Activity状态图、生命周期图(超详细),onSaveInstanceState只保存、恢复基本ui数据,持久数据不在这里保存。

1.Activity状态图

2.Activity生命周期简图

  • 启动Activity: onCreate()—>onStart()—>onResume(),Activity进入running状态。
  • 被其它Activity部分遮盖: onPause(),仍有部分可见,还在内存中。可被回收。同时在onPause里持久化数据。不是在onSaveInstanceState中。
  • 从onPause回到前台:onResume(),然后再次进入running态。
  • 被其它Activity遮盖(HOME,锁屏,启动其它Activity等): onPause()->onStop()。此时仍可能在内存中,系统内存不足时,可被回收。
  • 在Stopped时,用户再次点击进入Activity:如果已被回收,则onCreate()->onStart()->onResume(),没被回收onRestart()->onStart()->onResume()

3.超详细的Activity生命周期图

4.Activity保存、恢复数据

  • 系统一般在 onStop()onPause()前调用onSaveInstanceState(),而不是onDestory()前一步,因为用户可能就是退出。
  • 不要忘记调用super.onSaveInstanceState();
  • onSaveInstanceState只保存一些ui信息,并且不保证一定被调用,持久化的数据一定不要保存在这里。而应在onPause中。
    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.

     

 

posted @ 2016-09-15 12:45  f9q  阅读(1867)  评论(0编辑  收藏  举报