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. Accessing java SIM card information from android

My query is regarding access to USIM card information from an android
application. My requirement is to read certain pre populated paramters
on the sim card at application startup.

Is this use case possible? I found an api in the developer reference
that interacts with the sim card, but it is limited to only sms and
geolocation information..

i wish to read the MSISDN, and a shared secret key stored on the sim.

thanks in advance

2. Where does the location of the database


In my activity, I create a database using the following:

  db.execSQL("CREATE TABLE favorites (" +    "_id INTEGER PRIMARY KEY)'" );

But can you please tell me where will android create the database file?
I try 'adb shell' and then 'cd /data/data/com.mycompany.myactivity',
but there is no database there.

Thank you.


3. Data Typists Needed! Work from home, Great Income!

4. Install issue w/non-market app OTA install to T-Mobile G1鈥

5. Extremely frustrating = TextView in ScrollView -> Scroll to bottom?

6. Android Supported Development Environment Roadmap

7. Android like Sponsor