Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent WebView auto refresh when screen rotation

Tags:

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?

like image 952
SopheakVirak Avatar asked Jun 07 '12 00:06

SopheakVirak


People also ask

How do you prevent the data from reloading and resetting when the screen is rotated?

Prevent Activity to recreated Most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change. Save this answer.

How do I stop my screen from automatically rotating?

To adjust the screen rotation settings: Swipe down from the top of the screen to open the Quick settings panel. Look for the screen orientation icon. Depending on your settings, you may need to look for the Portrait, Landscape, or Auto Rotate icon.

What is auto rotate?

The auto-rotate button automatically rotates your device's screen when you flip your device between landscape and portrait mode.


1 Answers

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.

like image 64
MH. Avatar answered Sep 22 '22 15:09

MH.