Android 多个 WebView 保存实例 [英] Android muliple WebView save instance
问题描述
我尝试使用 onSaveInstanceState()
和 restoreState()
方法在我的活动中保存和恢复 3 个 WebViews
的状态我在 onCreate()
中的 WebView
.当我只有一个 WebView
时,这工作得很好,但是要保存 3 个 WebViews
,看起来它只是最后一个保存计数并覆盖其他保存;因此,当我在 onCreate()
中使用 restoreState()
方法时,第一个 WebView
具有第三个 WebView<的内容/code> 和另外两个是空的.
I try to save and restore the state of 3 WebViews
in my activity by using onSaveInstanceState()
and the restoreState()
method of my WebView
in onCreate()
. This works just fine when I had only one WebView
, but with 3 WebViews
to save, it looks like it's only the last one save which counts and overrides the other saves; and so when I use the restoreState()
method in onCreate()
, the first WebView
has the content of the third WebView
and the 2 others are empty.
这是我的活动代码示例:
Here is a sample of my activity code:
@Override
public void onSaveInstanceState(Bundle outState) {
((WebView)findViewById(R.id.webview1)).saveState(outState);
((WebView)findViewById(R.id.webview2)).saveState(outState);
((WebView)findViewById(R.id.webview3)).saveState(outState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
((WebView)findViewById(R.id.webview1)).restoreState(savedInstanceState);
((WebView)findViewById(R.id.webview2)).restoreState(savedInstanceState);
((WebView)findViewById(R.id.webview3)).restoreState(savedInstanceState);
}
我应该使用一些技巧为每个视图使用另一个包吗?我是否在代码中的其他地方做错了导致这种情况的错误,还是标准行为?
Should I use some trick to use another bundle for each view? Have I done something wrong somewhere else in my code that causes this or is it the standard behaviour?
推荐答案
您对 saveState()
的第二次调用覆盖了第一次调用中保存的内容,第三次调用覆盖了第二次调用的内容.在 onSaveInstanceState()
执行结束时,bundle 只包含第三个 WebView
的状态.
Your 2nd call of saveState()
overwrites the contents saved in the first call and the 3rd call overwrites the content of the 2nd call. At the end of the execution of onSaveInstanceState()
the bundle only contains the 3rd WebView
's state.
这就是为什么当您恢复第 3 个 WebView
的状态时,它会恢复到第一个 WebView
.
This is why when you restore the state of the 3rd WebView
gets restored to the 1st WebView
.
为每个 WebViews
创建一个新的 bundle 对象,并使用 this 为每个Bundle代码>:
Create a new bundle object of each of your WebViews
and put them into the outState Bundle
object using this giving a different key for each Bundle
:
public void putBundle (String key, Bundle value)
在onCreate()
中根据3个key获取Bundle
并恢复状态.
In onCreate()
get the Bundle
s based on the 3 keys and restore the states.
这篇关于Android 多个 WebView 保存实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!