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.

     

 

转载于:https://www.cnblogs.com/sjjg/p/5874644.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值