Android 屏幕旋转时如何防止WebView自动刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10924291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:29:57  来源:igfitidea点击:

How to prevent WebView auto refresh when screen rotation

androidandroid-webview

提问by SopheakVirak

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

我的应用程序 - 当屏幕旋转和我的活动回到第一个 1 时,WebView 的活动会自动刷新。

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.

示例:当我从 A->B 或 BC 切换时,WebView 处理 3 个活动(A、B、C)然后当屏幕旋转时它将返回到 A。

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

我的问题:我们如何让活动保持活动事件屏幕旋转?

采纳答案by MSaudi

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

突出显示@kirgy 评论,orientation|screenSize如果您的 API > 3.2 ,您必须添加 到您的清单中,在某些情况下,如果没有它,它将无法工作。

回答by MH.

There is more than one approach to tackle this problem.

解决这个问题的方法不止一种。

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

简单的方法是在清单中添加android:configChanges="orientation|screenSize"相关内容Activity。通过设置这个标志,你告诉 Android 不要破坏Activity并且你将自己处理所有的方向变化(如果有的话)。

The more modern approach would be to put the WebViewinside a Fragmentand make it retain its instance, using the setRetainInstance(true)flag. The hosting Activitywill still get destroyed on orientation changes, but the Fragmentcontaining the WebViewwill simply be detached and re-attached, without the need to re-create itself. You can find an exampleof 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' Fragmentclass.

更现代的方法是使用标志将WebViewa放入内部Fragment并使其保留其实例setRetainInstance(true)。主机Activity仍然会在方向更改时被破坏,但Fragment包含的WebView将被简单地分离和重新连接,而无需重新创建自己。您可以在 API 演示中找到此功能的示例。请记住,支持库提供了一个与蜂窝前兼容的片段实现,所以不要被“常规”Fragment类的 API 级别所迷惑 。

回答by Eight

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

添加android:configChanges="orientation"到您的清单文件以防止在屏幕方向更改时重新启动。

like::

喜欢::

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

See thisfor some references.

请参阅this以获取一些参考。