onSaveInstanceState与onRestoreInstanceState

本文介绍了Android中Activity如何通过onSaveInstanceState和onRestoreInstanceState方法保存与恢复状态,确保在系统资源回收后能够还原用户界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

protected void onSaveInstanceState (Bundle outState)
Since:  API Level 1

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle)(the Bundle populated by this method will be passed to both).

This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.

If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().

Parameters
outState Bundle in which to place your saved state.

        在activity被杀掉之前调用保存每个实例的状态,以保证该状态可以在onCreate(Bundle)或者onRestoreInstanceState(Bundle) (传入的Bundle参数是由onSaveInstanceState封装好的)中恢复。这个方法在一个activity被杀死前调用,当该activity在将来某个时刻回来时可以恢复其先前状态。例如,如果activity B启用后位于activity A的前端,在某个时刻activity A因为系统回收资源的问题要被杀掉,A通过onSaveInstanceState将有机会保存其用户界面状态,使得将来用户返回到activity A时能通过onCreate(Bundle)或者onRestoreInstanceState(Bundle)恢复界面的状态。

        不要将这个方法和activity生命周期回调如onPause()或onStop()搞混淆了,onPause()在activtiy被放置到背景或者自行销毁时总会被调用,onStop()在activity被销毁时被调用。一个会调用onPause()和onStop(),但不触发onSaveInstanceState的例子是当用户从activity B返回到activity A时:没有必要调用B的onSaveInstanceState(Bundle),此时的B实例永远不会被恢复,因此系统会避免调用它。一个调用onPause()但不调用onSaveInstanceState的例子是当activity B启动并处在activity A的前端:如果在B的整个生命周期里A的用户界面状态都没有被破坏的话,系统是不会调用activity A的onSaveInstanceState(Bundle)的。

        默认的实现负责了大部分UI实例状态(的保存),采用的方式是调用UI层上每个拥有id的view的onSaveInstanceState() ,并且保存当前获得焦点的view的id(所有保存的状态信息都会在默认的onRestoreInstanceState(Bundle)实现中恢复)。如果你覆写这个方法来保存额外的没有被各个view保存的信息,你可能想要在默认实现过程中调用或者自己保存每个视图的所有状态。如果被调用,这个方法会在onStop()前被触发,但系统并不保证是否在onPause()之前或者之后触发。

protected void onRestoreInstanceState (Bundle savedInstanceState)
Since:  API Level 1

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. 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. The default implementation of this method performs a restore of any view state that had previously been frozen byonSaveInstanceState(Bundle).

This method is called between onStart() and onPostCreate(Bundle).

Parameters
savedInstanceState the data most recently supplied in onSaveInstanceState(Bundle).

        在Activity保存了之前的状态后重新被初始化时这个方法被调用,同时是在onCreate方法之后调用的。恢复状态这一功能的大多数实现可以仅仅调用onCreate方法实现,但是有时在onRestoreInstanceState方法实现这一功能更加的方便,比如当你希望Activity所有的初始化操作都已经完成的情况下,或者是让你的子类决定是否要使用你的默认恢复状态的实现时。这个方法(onRestoreInstanceState)的默认实现会恢复所有的之前调用了onSaveInstanceState方法的View的状态。

        这个方法在onStart()和onPostCreate之间被调用。


在Android中,`EditText` 的内容需要在Activity销毁(如用户旋转设备、按Home键等)后保存,并在下次启动时恢复,这通常通过重写 ` onSaveInstanceState()` 和 ` onRestoreInstanceState()` 来实现。这两个方法分别用于保存和恢复组件的状态。 首先,在`onSaveInstanceState(Bundle outState)` 中,我们需要将 `EditText` 的文本添加到`Bundle`中: ```java @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // 获取EditText实例并保存其文本 EditText editText = findViewById(R.id.edit_text_id); String text = editText.getText().toString(); // 将文本存入Bundle outState.putString("edit_text_data", text); } ``` 然后,在`onCreate(Bundle savedInstanceState)` 或者 `onRestoreInstanceState(Bundle savedInstanceState)` 中,我们在Activity复原时从`Bundle`中恢复文本: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null) { // 如果存在先前的状态数据 String savedText = savedInstanceState.getString("edit_text_data"); if (savedText != null) { // 从 Bundle 中获取并设置回 EditText EditText editText = findViewById(R.id.edit_text_id); editText.setText(savedText); } } } // 当 Activity 被恢复时,也可以在这里调用 @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // 恢复 EditText 文本同上 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值