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. Linking to your app from external browser

I'm quite new to Android so please excuse if this is the wrong forum,
but I have searched up and down and also this list, but is there no
way to externally link from my website into the market to show my app
within a desktop browser and not on the device? I mean similar to the
iTunes links where you can view the application details right in any
browser. There must be something similar, but the only information

says it works only on the Android device.



2. Problem in parsing the package.


When I tried to download the My App  to my droid it says there is a problem
parsing the package.I am using ecllipse and api level 2.1 for development.

Please help its very urgent.

Thanks and Regards
Ajmer Singh


3. Question about APN for Browser and MMS

4. About Download manager example

5. Adobe 10.1 player

6. How to check the Foreground Process from a Service

7. So can we use Directions API or not in Android?