46945

Android muliple WebView save instance

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?

Answer1:

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.

Recommend

  • How to get bookmark of PDF and add bookmark to new pdf?
  • SaveState in MvvmCross is not being called in an iOS application
  • How to save WebView state and restore it in Android Lollipop?
  • Checkbox not saving state
  • Can you measure the time it takes to load a web page in a webView in Android?
  • Save pictures to custom folder in media library
  • Will jQuery ever leverage HTML5 technologies?
  • Changing Entity's State in Entity Framework 4.1
  • Keep Sql Connection open for iterating many requests? Or close each step?
  • Reading a file into a multidimensional array
  • Android application: how to use the camera and grab the image bytes?
  • Jackson Parser: ignore deserializing for type mismatch
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why is an OPTIONS request sent to the server?
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • sending/ receiving email in Java
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Load html files in TinyMce
  • Acquiring multiple attributes from .xml file in c#
  • Free memory of cv::Mat loaded using FileStorage API
  • JTable with a ScrollPane misbehaving
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to stop GridView from loading again when I press back button?
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?