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?
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.
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.
The auto-rotate button automatically rotates your device's screen when you flip your device between landscape and portrait mode.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With