3

I am trying to get a WebView to stop reloading the page when the device's orientation changes. Per this answer, I was looking at WebView.saveState(). The documentation for this method says:

Please note that this method no longer restores the display data for this WebView. See savePicture(Bundle, File) and restorePicture(Bundle, File) for saving and restoring the display data.

But when I looked at savePicture(), I found that it's been deprecated:

This method is deprecated.
This method is now obsolete.

In that case, what methods should developers be using to save and restore the display data?

Community
  • 1
  • 1
Amanda S
  • 3,266
  • 4
  • 33
  • 45

2 Answers2

0

In your AndroidManifest.xml file, add this config line to your activity:

<activity
        android:name=".MyWebViewActivity"
        android:configChanges="orientation"/>
Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • Since the use of `configChanges` is somewhat [controversial](http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange), I wanted to try using `saveState()`. This does not answer the question I was asking. – Amanda S Jul 13 '11 at 23:41
  • What's controversial about it? The first line of that link clearly states that if you don't need to apply any changes during a specific configuration change and it's a performance hit to rebuild the Activity (in your case, reloading a website), then this prevents the activity from being rebuilt. It's only not recommended if you need to use different resources during a certain configuration change. Anywhere in your Activity, are you using different resources based on the orientation? – Jason Robinson Jul 14 '11 at 15:28
  • And even if it were case that you used different resources based on the orientation, you could still easily handle it by overriding the `onConfigurationChanged()` method. In your case, there's no reason why your activity needs to be destroyed and reinitialized all because the orientation changed. – Jason Robinson Jul 14 '11 at 15:30
0

I had a very similar problem but had to make a couple of additions to get it to work with various version (including ICS).

In the main app activity I added a slightly different version of what Jason offered.

<activity
android:name=".MyMainActivity"
android:configChanges="orientation|keyboardHidden|screenSize" 
android:label="@string/app_name" >

I had this working on pre-Honeycomb with:

           <activity
        ....
        android:configChanges="orientation|keyboardHidden" 
        .... >

I had to make the first example to get it running on all versions. I'm currently using fragments and ActionBarSherlock for backwards compatibility.

Hope this helps.

JustLearningAgain
  • 2,227
  • 4
  • 34
  • 50