Eventually you're going to have to write the the back/history to persistent storage. In the case of the device being shut down as one example, as well as avoiding a contrived (in my opinion) example where you call onSaveInstanceState
in onPause
(what you'll need to do to get the bundle you want passed into onRestoreInstanceState
which you can then pass into restoreState
).
Therefore, besides reconsidering your design (if the user closed your application and you're not making a browser, do they really want to keep the same history?), you should look up SharedPreferences
and how to use them.
Unfortunately you cannot put a bundle in shared preferences, nor would I recommend trying to write the bundle via Parcelable
(it's only meant for IPC, not persistent storage). But what you can do is store all the primitives that the bundle stores. This might be contrived too, but it seems to me the only way to get permanent storage.
And then rebuild your bundle via the primitives stored in SharedPreferences, and pass that into restoreState()
. You can check the Android source code to see what webView.saveState()
actually does (I looked up the 2.1 code and it seems to write a int, a serializeable object, and then another bundle for the SSL cert. The SSL cert bundle itself is just four integers). Off the top of my head, I'd just write all the primitives you can, and then store the location (string) of the local file you write the serialized data too.
Even if you have some ordered collection of the entire BackForward list, I'm not sure how to translate that into the goBack()
and goForward()
using those values (there's a protected method that's involved in adding items to the list but you can't access that). UNLESS you do use restoreState()
, which is why we're going to all the work to rebuild the bundle correctly.
Seriously though, unless my search skills are abysmal (entirely possible), storing the WebView
history in places beyond where saveInstanceState/restoreInstanceState
can do your work for you doesn't seem to be a problem a lot of people have run into. I.e. not a lot of people are trying to persist the WebView history when the user explicitly closes their application, so you have to ask yourself why are YOU doing this?
Apologies if there is a really easy way to persistently store this information and reload into a WebView btw!