使用onCreate与onRestoreInstanceState

Sea*_*ill 25 android instance oncreate

技术上有什么理由我应该使用onRestoreInstanceState吗?我可以onCreate通过检查savedInstanceState捆绑包是否为空来进行所有恢复吗?使用onRestoreInstanceState过度做什么的主要好处是什么onCreate

Far*_*sbi 31

onRestoreInstanceState

当从以前保存的状态重新初始化活动时,在onStart()之后调用此方法,此处在savedInstanceState中给出.大多数实现只是使用onCreate(Bundle)来恢复它们的状态,但是在完成所有初始化之后有时可以方便地执行此操作,或者允许子类决定是否使用默认实现.

onRestoreInstanceState保证你Bundle在后面调用的活动的生命周期中也会收到一个非null 对象onStart 但是onCreate:你应该总是检查Bundle对象是否为null以确定配置更改,因为你知道它之前被调用onStart 所以这完全由你自己决定根据您的需求.

  • 我知道,我的意思是:大多数时候我们需要在 onRestoreInstanceState 之前的包数据。我认为在 onCreate 中获取捆绑数据更有用且更具可读性。 (2认同)