Android面试(12): Fragment 的状态保存与恢复

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yz_cfm/article/details/85718050

因为进入停止状态的 fragment 有可能在系统内存不足的时候被回收,所以为了防止数据的丢失我们也要在 fragment 中进行数据的保存和恢复,具体如下:
保存: 和 Activity 一样,在 fragment 的 onSaveInstanceState(Bundle) 方法中通过 Bundle 类型参数保存数据。
恢复: 在 fragment 生命周期中的 onCreate(Bundle)、onCreateView(LayoutInflater , ViewGroup , Bundle)、onActivityCreated(Bundle) 方法通过 Bundle 类型的 saveInstanceState 参数来恢复我们保存的数据。

猜你喜欢

转载自blog.csdn.net/yz_cfm/article/details/85718050