Keeping WebView history through orientation change?

by Joseph S » Sun, 20 Mar 2011 05:00:42 GMT

Sponsored Links
 Hello All,

Working on my first Android project here...

I have an Activity that has a WebView and my own WebViewClient. To
deal with orientation changes, I have configChanges in the manifest
and onConfigurationChanged.

During onCreate and onConfigurationChanged I set up the views by
calling my own setupViews where I set the WebViewClient.

For the onCreate I load a particular URL and the Activity does it's

When the user changes the orientation of the device later on after
viewing a few pages, the onConfigurationChanged gets called, and I
load the URL the user was looking at (I saved it in a variable from my
WebViewClient's onPageStarted). This works, except the history is lost
because it's a new WebView.

What's a better way of doing this? I want my WebViewClient to behave
like a normal browser when the user tilts the phone, just reloading
the current page and preserving the history.




Re: Keeping WebView history through orientation change?

by Mark Murphy » Sun, 20 Mar 2011 05:22:13 GMT


If you are using onConfigurationChanged(), leave the existing WebView
alone. Use Java code to adjust your layout parameters and such to
reflect the new orientation rather than inflate a new layout. If you
leave the WebView alone, it should retain its history.

This particular problem is one of the few good reasons to use
onConfigurationChanged(). However, pretty much all of those reasons
mean you really shouldn't load in a fresh layout file. Loading a fresh
layout file might be used in demonstrations in some books (ahem) but
isn't really the right production strategy.

:: quick makes a note to really cover this scenario better in next
update to that book ::

Mark Murphy (a Commons Guy)  |  | 

Android Training in Oslo: 


Sponsored Links

Re: Keeping WebView history through orientation change?

by Miguel Morales » Sun, 20 Mar 2011 10:47:18 GMT

 A simple way is to override onSaveInstanceState on your activity and call
saveState on your webview.

Then on your onCreate if the saved state bundle isn't null call restoreState
on the webview.

It should reset its layout, and remember its history.

~ Jeremiah:9:23-24
Android 2D MMORPG: http ://


Re: Keeping WebView history through orientation change?

by Joseph S » Sun, 20 Mar 2011 16:27:50 GMT


Thanks for the tip!

Just doing nothing (other than kicking it back upstairs with super) in
onConfigurationChanged seems to have solved my problem. The examples
dealing with onConfigurationChanged that I'd seen had UI's that needed
fiddling after orientation changes so that's why they broke lots of
stuff out of onCreate. But in my case that was just extra.



Other Threads

1. unicode chars not being dispatched from IM


I'm opening the ImputMethod to be able to catch keys into my app.

When the user press standard keys, its ok. I get things like:
0 33 101
1 33 101
(action type, keycode, unicode)

However, when i long-press a char like A and choose  (ã), i
get only:
2 0 0

Any tips of what's going on?




2. Unable to parse hprof dump with MAT?

I'm having trouble analyzing a hprof dum using the Memory Analyzer..
Anyone have any ideas? Here is the error message I get.

org.eclipse.mat.SnapshotException: Error opening heap dump
''. Check the error log for further details.
Caused by: org.eclipse.core.runtime.CoreException: Error opening heap
... 5 more

Any ideas?  I just used the "Dump HPROF file" button, and picked it up
from the sdcard and opened it with MAT...



3. rogue setting of device_provisioned, issue 4358

4. TextView scrollHorizontally doesn't work

5. Root Hero virgi

6. What is latest Android device and best device?

7. bug in Spinner.setSelection when called twice?