002_SaveInstanceState 翻译学习

本文介绍了Android中Activity状态保存与恢复的机制,包括onCreate、onSaveInstanceState与onRestoreInstanceState三个关键方法的作用及调用时机。

SaveInstanceState

官方:http://developer.android.com/training/basics/activity-lifecycle/recreating.html

相关博客:http://blog.youkuaiyun.com/shulianghan/article/details/38297083

简介:

SaveInstanceState用于保存Activity的状态,当Activity销毁或回复前后用于保存或恢复数据。

主要在三个方法中出现:onCreate(Bundle savedInstanceState)、onSaveInstanceState()、onRestoreInstanceState(BundlesavedInstanceState).

方法介绍:

(1) onCreate(Bundle savedInstanceState) 方法

Activity 创建时回调 : 该方法会自动传入一个 Bundle 对象, 该 Bundle 对象就是上次被系统销毁时在 onSaveInstanceState或者 onRestoreInstanceState中保存的数据;

-- 注意 : 只有是系统自动回收的时候才会保存 Bundle 对象数据;

-- Bundle 对象来源 : onCreate()方法中的 Bundle 对象参数, 是在 onSaveInstance() 或者 onRestoreInstanceState() 方法中保存的 Bundle 对象;

(2) onSaveInstanceState(Bundle outState) 方法

outState 参数作用 

-- 数据保存 : Activity 声明周期结束的时候, 需要保存 Activity 状态的时候, 会将要保存的数据使用键值对的形式 保存在 Bundle 对象中;

-- 恢复数据 : 在 Activity 的 onCreate()方法 创建 Activity 的时候会传入一个 Bundle 对象, 这个 Bundle 对象就是这个 outState 参数;

调用时机 : Activity 容易被销毁的时候调用, 注意是容易被销毁, 也可能没有销毁就调用了;

-- 按下Home : Activity 进入了后台, 此时会调用该方法;

-- 按下电源键 : 屏幕关闭, Activity 进入后台;

-- 启动其它Activity : Activity 被压入了任务栈的栈底;

-- 横竖屏切换 : 会销毁当前 Activity 并重新创建;

onSaveInstanceState方法调用注意事项 : 

-- 用户主动销毁不会调用 :当用户点击回退键 或者 调用了 finish() 方法, 不会调用该方法;

-- 调用时机不固定 :该方法一定是在 onStop() 方法之前调用, 但是不确定是在 onPause() 方法之前 还是 之后调用;

-- 布局中组件状态存储 :每个组件都实现了 onSaveInstance() 方法, 在调用函数的时候, 会自动保存组件的状态, 注意, 只有有 id 的组件才会保存;

-- 关于默认的 super.onSaveInstanceState(outState) :该默认的方法是实现组件状态保存的;几乎所有的Android控件都实现了此方法。

(3) onRestoreInstanceState(Bundle savedInstanceState) 方法

方法回调时机 : 在 Activity 被系统销毁之后 恢复 Activity 被调用, 只有销毁了之后重建的时候才调用, 如果内存充足, 系统没有销毁这个 Activity, 就不需要调用;

-- Bundle 对象传递 : 该方法保存的 Bundle 对象在 Activity 恢复的时候也会通过参数传递到 onCreate() 方法中;

-- 位于生命周期位置吧 : 该方法在 onResume() 方法之前保存信息; 

 

你提到的这个问题非常典型:**多个自定义 View 都需要实现 `onSaveInstanceState()` 和 `onRestoreInstanceState()` 方法,代码重复度高,维护困难。** --- ### ✅ 解决方案:使用 **自定义 BaseSavedState View 基类 + Kotlin 扩展委托属性** 你可以通过以下方式 **提取通用逻辑**: --- ## ✅ 方法一:封装为基类(BaseCustomView) ```kotlin abstract class BaseCustomView(context: Context, attrs: AttributeSet) : View(context, attrs) { companion object { private const val SUPER_STATE_KEY = "superState" private const val CUSTOM_STATE_KEY = "customState" } // 保存自定义状态的接口 interface CustomState { fun writeToBundle(bundle: Bundle) fun restoreFromBundle(bundle: Bundle) } // 自定义状态对象 private var customState: CustomState? = null override fun onSaveInstanceState(): Parcelable { val bundle = Bundle() bundle.putParcelable(SUPER_STATE_KEY, super.onSaveInstanceState()) customState?.let { state -> val customBundle = Bundle() state.writeToBundle(customBundle) bundle.putBundle(CUSTOM_STATE_KEY, customBundle) } return bundle } override fun onRestoreInstanceState(state: Parcelable?) { if (state is Bundle) { val superState = state.getParcelable<Parcelable>(SUPER_STATE_KEY) val customBundle = state.getBundle(CUSTOM_STATE_KEY) customState?.let { it.restoreFromBundle(customBundle ?: Bundle()) } super.onRestoreInstanceState(superState) } else { super.onRestoreInstanceState(state) } } // 子类需要实现该方法来提供自定义状态 protected fun setCustomStateProvider(provider: () -> CustomState?) { this.customState = provider() } } ``` ### 使用示例: ```kotlin class MyCustomView(context: Context, attrs: AttributeSet) : BaseCustomView(context, attrs) { private var isLoading = false init { setCustomStateProvider { object : CustomState { override fun writeToBundle(bundle: Bundle) { bundle.putBoolean("isLoading", isLoading) } override fun restoreFromBundle(bundle: Bundle) { isLoading = bundle.getBoolean("isLoading") } } } } } ``` --- ## ✅ 方法二:使用 Kotlin 委托属性(Delegate) 如果你不想用继承,可以使用 Kotlin 的 **委托属性** 来简化: ```kotlin class SavedStateDelegate(private val view: View) { companion object { private const val SUPER_STATE_KEY = "superState" private const val IS_LOADING_PARAM = "is_loading" } var isLoading = false fun saveInstanceState(): Parcelable { val bundle = Bundle() bundle.putParcelable(SUPER_STATE_KEY, view.super.onSaveInstanceState()) bundle.putBoolean(IS_LOADING_PARAM, isLoading) return bundle } fun restoreInstanceState(state: Parcelable?) { if (state is Bundle) { val superState = state.getParcelable<Parcelable>(SUPER_STATE_KEY) isLoading = state.getBoolean(IS_LOADING_PARAM) view.super.onRestoreInstanceState(superState) } else { view.super.onRestoreInstanceState(state) } } } ``` 然后在你的 View 中使用: ```kotlin class MyCustomView(context: Context, attrs: AttributeSet) : View(context, attrs) { private var isLoading = false private val savedStateDelegate = SavedStateDelegate(this) override fun onSaveInstanceState(): Parcelable { return savedStateDelegate.saveInstanceState() } override fun onRestoreInstanceState(state: Parcelable?) { savedStateDelegate.restoreInstanceState(state) } } ``` --- ## ✅ 方法三:使用 `ViewGroup` 或 `View` 扩展函数(适用于 Kotlin) 你可以封装一个 Kotlin 扩展函数来简化状态保存逻辑,但受限于 Android 的机制,这种方式不如前两种直观。 --- ### ✅ 总结 | 方法 | 是否推荐 | 说明 | |------|----------|------| | 基类封装 `BaseCustomView` | ✅ 强烈推荐 | 可复用、结构清晰,适合多个 View | | Kotlin 委托属性 | ✅ 推荐 | 更灵活,适合已有继承结构的 View | | 扩展函数/工具类 | ⚠️ 一般 | 不太适合直接处理 `onSave/RestoreInstanceState` | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值