onCreate的savedInstanceState和onRestoreInstanceState 方法的区别。

总结了下:


oncreate是在onstart之前执行是activity声明周期,而onRestoreInstanceState 是在onstart之后onresume之前执行。

stackoverflow上看到的一个比较好的解释:

onRestoreInstanceState is redundant because you can easily restore state in onCreate.
Having said that here is what the official doc says for onRestoreInstanceState:

Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation.

So for best practice, lay out your view hierarchy in onCreate and restore the previous state in onRestoreInstanceState. If you do that, anyone who subclasses your Activity can chose to override your onRestoreInstanceState to augment or replace your restore state logic. This is a long way of saying onRestoreInstanceState serves as a template method.

大致意思就是:

  onRestoreInstanceState 说白了很费官方说法就是为了子类继承方便调用。
 
 初始化操作在oncreate, 恢复数据在onRestoreInstanceState (参考上述生命周期,大致意思应该是oncreate应该执行一些初始化相关操作,在启动和显示之间操作一些)。
 只是为了 子类继承的时候业务逻辑分开。但是要把onRestoreInstanceState 说成是模板方法,也是漫漫长路。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值