简体   繁体   English

onSaveInstanceState / onRestoreInstanceState和视图状态

[英]onSaveInstanceState/onRestoreInstanceState and view state

According to the documentation of onSaveInstanceState: 根据onSaveInstanceState的文档:

The default implementation takes care of most of the UI per-instance state 默认实现负责大多数UI每实例状态

and onRestoreInstanceState: 和onRestoreInstanceState:

The default implementation of this method performs a restore of any view state that had previously been frozen 此方法的默认实现执行先前已冻结的任何视图状态的恢复

I'm not sure exactly what that means. 我不确定那意味着什么。 Is it meant to mean that when returning after being killed and now restored, that the UI screen shown to the user is automatically restored with all its data? 是否意味着在被杀死后现在恢复后返回时,显示给用户的UI屏幕会自动恢复其所有数据? If so, I am not seeing that. 如果是这样,我没有看到。 All I get is an empty screen unless I do setContentView myself. 我得到的只是一个空屏幕,除非我自己设置setContentView。

AM I misunderstanding the meaning? AM我误解了意思?

Default implementation will work for every widget which ids are defined. 默认实现适用于定义了id的每个小部件。 For example, If you have one EditText and if you will provide its id then system will save its value when Activity will be killed due to orientation and same and it will restore the EditText value when activity will be re-created. 例如,如果您有一个EditText,并且如果您将提供其id,那么当Activity因为方向而被杀死时系统将保存其值,并且它将在重新创建活动时恢复EditText值。

Edit 编辑

If you have one base layout and if you are dynamically adding some views in the view hierarchy then you will have to handle the save state and restore state your self. 如果您有一个基本布局,并且如果要在视图层次结构中动态添加一些视图,则必须处理保存状态并恢复自己的状态。 also when your activity will be re-created then onCreate() method of the activity will be called so in this method first set all the addition views which you are creating and adding dynamically and then you can check the extra parameters with the intent which you are getting in the onCreate() method. 当你的活动将被重新创建时,将调用活动的onCreate()方法,因此在此方法中首先设置您正在创建并动态添加的所有添加视图,然后您可以检查具有您的意图的额外参数正在进入onCreate()方法。 This extra parameters are exactly same as you have adding extra parameters in the onSaveInstanceState method. 这些额外参数与在onSaveInstanceState方法中添加额外参数完全相同。

So implement like below. 所以实现如下。

int x = 10;
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("x", x);
}

And in onCreate method you can get this x parameters like below 在onCreate方法中,您可以获得如下所示的x参数

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.xxx);

    if(savedInstanceState.containsKey("x")) {
        x = savedInstanceState.getInt("x");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不调用onSaveInstanceState()和onRestoreInstanceState(Parcelable状态)? - onSaveInstanceState() and onRestoreInstanceState(Parcelable state) are not called? onSaveInstanceState() 和 onRestoreInstanceState() - onSaveInstanceState () and onRestoreInstanceState () 如何在方向更改时使用onSaveInstanceState和onRestoreInstanceState保存状态 - how to save state with onSaveInstanceState and onRestoreInstanceState while orientation change onSaveInstanceState和onRestoreInstanceState似乎不起作用 - onSaveInstanceState and onRestoreInstanceState seems not to work onSaveInstanceState和onRestoreInstanceState的可见性 - Visibility of onSaveInstanceState and onRestoreInstanceState 大型应用程序中的onSaveInstanceState()/ onRestoreInstanceState() - onSaveInstanceState()/onRestoreInstanceState() in large application 使用onSaveInstanceState和onRestoreInstanceState时出现问题 - Issue with use of onSaveInstanceState and onRestoreInstanceState 如何使用onSaveInstanceState()和onRestoreInstanceState()? - How to use onSaveInstanceState() and onRestoreInstanceState()? 具有Java集合的OnSaveInstanceState / OnRestoreInstanceState - OnSaveInstanceState/OnRestoreInstanceState with java collections onSaveInstanceState / onRestoreInstanceState用于对话框 - onSaveInstanceState/onRestoreInstanceState for a dialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM