Reputation: 961
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?
Upvotes: 25
Views: 34611
Reputation: 4284
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.
Upvotes: 6
Reputation: 4652
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.
Upvotes: 10
Reputation: 45493
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.
Upvotes: 49