25

My application - Activity with WebView was auto refresh when screen rotation and my activity back to the first 1.

Example: WebView handle 3 activity(A, B, C) when I switching from A->B or B-C then it will back to A when screen is rotating.

My question: how can we keep activity alive event screen rotation?

Buddy
  • 10,874
  • 5
  • 41
  • 58
SopheakVirak
  • 961
  • 6
  • 14
  • 36

3 Answers3

49

There is more than one approach to tackle this problem.

The easy way out is to add android:configChanges="orientation|screenSize" to the relevant Activity in your manifest. By setting this flag you tell Android to not destroy the Activity and that you're going to handle all orientation changes (if any) yourself.

The more modern approach would be to put the WebView inside a Fragment and make it retain its instance, using the setRetainInstance(true) flag. The hosting Activity will still get destroyed on orientation changes, but the Fragment containing the WebView will simply be detached and re-attached, without the need to re-create itself. You can find an example of this feature in the API demos. Keep in mind that the support library offers a pre-Honeycomb compatible implementation of fragments, so don't be fooled by the API level of the 'regular' Fragment class.

outlying
  • 578
  • 1
  • 8
  • 19
MH.
  • 45,303
  • 10
  • 103
  • 116
  • 3
    Your first "easiest" way to do this is the only way. The "more modern approach" won't work in this case (well, it'll work, but it'll leak memory) because a WebView is tied to an Activity's Context upon instantiation. Check out [Dianne Hackborn's comments on that](https://groups.google.com/forum/#!msg/android-developers/cWnxkQ8RLeY/RA1n77EDeR8J). – mkuech May 29 '13 at 16:33
  • @mkuech: The latter is the case for any view, not just `WebView`s - that's why you'll always find yourself re-inflating views on a fragment level in `onCreateView()` or `onActivityCreated()`. I didn't go into specifics in my answer above, but with `setRetainInstance(true)` you could make the 'old' `WebView` save its state to a bundle and restore it afterwards. It does mean the `WebView` will have to rebuild its content (read: reload the url), so it's visually not as fast/smooth as handling the `Activity`'s configuration changes manually. – MH. Jun 05 '13 at 00:35
  • 2
    But you don't need to use a `Fragment` to save a `WebView`'s state. You can just use the standard state management methods from the `Activity` or `Fragment` if that's all you need. `setRetainInstance(true)` isn't _normally_ needed for this simple use case. It more just replaces `onRetainNonConfigurationInstance`, and is useful for things like keeping a `Thread` active across configuration changes. I'd avoid complicating things. Besides, since merely saving a `WebView`'s state won't keep it from redrawing/reloading, the first solution is the only real way to keep it seamless when rotating. – mkuech Jun 05 '13 at 15:57
  • You rock. This is the best solution so far. – zackygaurav Jul 30 '15 at 22:07
  • @mkuench You make a very important point and provide an excellent source. You deserve more upvotes than you got. Thanks! – Lv99Zubat Mar 15 '16 at 19:21
  • This does not work for me, the webview keeps reloading. – Roel Jun 24 '16 at 12:30
10

Highlighting @kirgy comment, You have to add orientation|screenSize to your manifest if your API > 3.2 , It wont work without it in some cases.

MSaudi
  • 4,442
  • 2
  • 40
  • 65
  • 5
    Nope, nope, nope. Please allow your configuration to update when it should. – Eric Cochran Feb 26 '15 at 02:34
  • @NightlyNexus what can be the implications of doing this within a webview? – Chris Gomez Jun 16 '15 at 18:54
  • @cgomezmendez Always allow your Views to restore their state. Yes, in this case, a WebView does not have state to save and restore, but is the WebView the only part of the instance state of your Activity? Almost certainly not. – Eric Cochran Jun 16 '15 at 21:16
6

Add android:configChanges="orientation" to your manifest file to prevent restarts when the screen orientation changes.

like::

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

See this for some references.

Ricardo Saracino
  • 1,345
  • 2
  • 16
  • 37
Eight
  • 4,194
  • 5
  • 30
  • 51
  • Dear Abhina8, thank you so much..it's really greate..I tested it's working very fine for me :). – SopheakVirak Jun 07 '12 at 01:38
  • 7
    A note for Android 3.2+ (API 13) - from this version onwards a screen rotate also causes a screen size change which will refresh the UI. You need to declare "orientation|screenSize" instead of just "orientation". This is all in the above Android article suggested by @MH. http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange – kirgy Dec 05 '13 at 23:09