您的位置:首页 > 其它

onCreate的savedInstanceState和onRestoreInstanceState 方法的区别。

2016-01-06 11:16 337 查看
总结了下:

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 说成是模板方法,也是漫漫长路。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息