如何保存 &恢复 Webview 状态? [英] how to save & restore Webview State?

查看:26
本文介绍了如何保存 &恢复 Webview 状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用备份&恢复 Webview 状态,我收到此消息:x 地址处的网页可能暂时关闭或可能已永久移至新网址.

when i use backup & restore Webview state ,i recive this message : the webpage at x Address might be temporarity down or may have moved permanently to a new web address.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    webViewShowPoll.saveState(savedInstanceState);                                    
}

@Override
public void onRestoreInstanceState(Bundle outState) {
  super.onRestoreInstanceState(outState);
    webViewShowPoll.restoreState(outState);
}

和 Androidmanifest.xml

and Androidmanifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_DEBUG_APP"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    


    <activity
        android:name="com.omid.epoll.mobile.Poll"
        android:launchMode="singleInstance"
        android:label="@string/title_activity_poll" 
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
    </activity>

推荐答案

restoreState 从不可靠.这也许就是文档现在这样说的原因.

restoreState was never reliable. That's perhaps why the documentation now says this.

如果在这个 WebView 有机会建立状态之后调用它(加载页面,创建后退/前进列表等)可能有不良的副作用.请注意,此方法不再恢复此 WebView 的显示数据.

If it is called after this WebView has had a chance to build state (load pages, create a back/forward list, etc.) there may be undesirable side-effects. Please note that this method no longer restores the display data for this WebView.

saveState() 的对应条目是这样说的:

And the corresponding entry for saveState() speaks thus:

请注意,此方法不再存储显示数据这个 WebView.

Please note that this method no longer stores the display data for this WebView.

你真正应该在 onCreate 方法中做的是调用 webView.loadUrl() 如果你想显示最后访问的 url,请看这个 答案:

what you really should do inside the onCreate method is to call webView.loadUrl() if you want to display the last visited url, please see this answer:

如果您担心 webview 在方向更改等时重新加载

If you are concerned about the webview reloading on orientation change etc.

您可以设置您的 Activity 来处理方向和键盘隐藏的变化,然后只管不理会 WebView

you can set your Activity to handle the orientation and keyboardHidden changes, and then just leave the WebView alone

这篇关于如何保存 &amp;恢复 Webview 状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆