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. how to use installed apk in other apk??

hi , i have a problem.
i have two apk a.apk and b.apk.
the b just installed , i don't know any information about it , just
know  package name is "com.b"
now a.apk have a button , i wnat call b.apk on button was clicked .
how to get it??
i find the code about this. but. i don't know b.apk's class . help.

the code
Intent intent = new Intent();
intent.setClassName(ackage namehe package's class;


2. Android on Navigation systems?

I want to get into car computer development and I think it would be
cool to have android installed on a touch navigation system does
anybody have information about this sort of thing, or where can I
learn more about car computers? It would be cool to have internet in
your car which most cars are already linked up to xm radio and
navigation systems. also to be able to see some car info like boost
and those sort of things from the computer would be cool.


3. How can I know the calling time of an active call?

4. How to get a background view's drawcache?

5. Problem in Geocoder API

6. Think twice before turning on the "Copy Protection" option! -- there's a serious bug in Market

7. Why My application could play the url begin with"http" but not "rtsp"?