开发者

Android muliple WebView save instance

开发者 https://www.devze.com 2023-01-25 03:05 出处:网络
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 WebV

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?


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.

This is why when you restore the state of the 3rd WebView gets restored to the 1st WebView.

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)

In onCreate() get the Bundles based on the 3 keys and restore the states.

0

精彩评论

暂无评论...
验证码 换一张
取 消