简体   繁体   English

Android-WebView(保存捆绑,以避免在屏幕旋转时丢失数据)

[英]Android - WebView (saving Bundle to avoid losing data on screen rotation)

So I understand that when I rotate the screen the contents will be set back to how they were originally because the activity is created and destroyed for the new layout. 因此,我了解到,当我旋转屏幕时,内容将恢复为最初的状态,因为活动是针对新布局创建和销毁的。

This is an issue for me as my web-app which I am displaying in a WebView displays unique information to the user, when they turn the phone sideways the data is lost. 这对我来说是个问题,因为我在WebView中显示的Web应用程序向用户显示独特的信息,当用户侧向翻转手机时,数据会丢失。 Now, I know the two methods I must override, but I'm not entirely sure of what I can put in the onSaveInstanceState to resurrect the exact page with the exact same data. 现在,我知道必须重写的两个方法,但是我不确定要在onSaveInstanceState中放入什么内容以使用完全相同的数据恢复完全的页面。 I would have tried storing the URL and using that but the pages use random features so it would be different. 我会尝试存储URL并使用它,但是页面使用随机功能,因此会有所不同。

So far all I have is: 到目前为止,我所拥有的是:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

Any thoughts of how to achieve this? 关于如何实现这一目标的任何想法?

It would probably be simpler if you just make your Activity not to restart during configuration changes by modifying its entry in the manifest to be similar to the following: 如果您通过修改清单中的活动条目,使其与配置类似,只是使您的活动不在配置更改期间重新启动,则可能会更简单:

    <activity android:name=".MyActivity"
        android:configChanges="orientation|keyboardHidden" />

From the reference page for WebView : WebView参考页面

The standard behavior for an Activity is to be destroyed and recreated when the device orientation or any other configuration changes. 当设备方向或任何其他配置更改时,活动的标准行为将被破坏并重新创建。 This will cause the WebView to reload the current page. 这将导致WebView重新加载当前页面。 If you don't want that, you can set your Activity to handle the orientation and keyboardHidden changes, and then just leave the WebView alone. 如果您不希望这样做,可以将Activity设置为处理方向和keyboardHidden更改,然后将WebView保留下来。 It'll automatically re-orient itself as appropriate. 它将自动根据需要自动调整方向。

UPDATE : 更新

According to this answer , we should use 根据这个答案 ,我们应该使用

    <activity android:name=".MyActivity"
        android:configChanges="orientation|screenSize" />

instead for device with Honeycomb and above version. 而不是带有Honeycomb及更高版本的设备。 Please give it a try :) 请试一试 :)

try this work with me 和我一起尝试这项工作

    if (savedInstanceState == null)
{
  web.loadUrl(webURL);
}

add over method 加法

@Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}

link: Android - Preventing WebView reload on Rotate 链接: Android-防止在旋转时重新加载WebView

As far as I understand, your problem lies on the web application, more than on the Android client implementation. 据我了解,您的问题出在Web应用程序上,而不是Android客户端实现上。 If storing the URL is not enough, you will have to find another way to send these "random datas" to your host application, maybe via cookies, or javascript variables. 如果存储URL不够,您将不得不寻找另一种方式将这些“随机数据”发送到您的主机应用程序,例如通过cookie或javascript变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM