Android webView saveState
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10258772/
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
Android webView saveState
提问by Ukjent
I have a tabhost with 3 tabs. In each of these tabs there is a webiview. When i click a tab the webview need to "reload" even when i have been there before, it have not been saved. Is there any way to save the webview ?
我有一个带有 3 个标签的 tabhost。在每个选项卡中都有一个 webiview。当我点击一个选项卡时,即使我以前去过那里,webview 也需要“重新加载”,它没有被保存。有什么办法可以保存 webview 吗?
回答by Shankar Agarwal
This can be handled by overrwriting onSaveInstanceState(Bundle outState) in your activity and calling saveState from the webview:
这可以通过在您的活动中覆盖 onSaveInstanceState(Bundle outState) 并从 webview 调用 saveState 来处理:
@Override
protected void onSaveInstanceState(Bundle outState) {
webView.saveState(outState);
super.onSaveInstanceState(outState);
}
Then recover this in your onCreate after the webview has been re-inflated of course:
然后在 webview 重新充气后在你的 onCreate 中恢复它:
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
WebView webview = (WebView)findViewById(R.id.webview);
if (savedInstanceState != null)
webview.restoreState(savedInstanceState);
else
webview.loadUrl(URLData)
}
回答by e4c5
restoreStatewas never reliable. And come 2015 the documents accept the fact. (The change was probably made to the documents around 2014). This is what it says now.
restoreState从来都不可靠。到了 2015 年,文件接受了这一事实。(更改可能是在 2014 年左右对文件进行的)。这就是它现在所说的。
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.
如果在此 WebView 有机会构建状态(加载页面、创建后退/前进列表等)之后调用它,则可能会产生不良副作用。请注意,此方法不再恢复此 WebView 的显示数据。
And the corresponding entry for saveState() speaks thus:
saveState() 的相应条目是这样说的:
Please note that this method no longer stores the display data for this WebView.
请注意,此方法不再存储此 WebView 的显示数据。
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:
如果您想显示上次访问的 url,您在 onCreate 方法中真正应该做的是调用 webView.loadUrl(),请参阅以下答案:
If you are concerned about the webview reloading on orientation change etc.
如果您担心在方向更改等时重新加载 webview。
you can set your Activity to handle the orientation and keyboardHidden changes, and then just leave the WebView alone
你可以设置你的 Activity 来处理方向和键盘隐藏的变化,然后只留下 WebView
回答by sirFunkenstine
Alternatively you can look into using fragments with your tabHost. when entering another webView you can set fragment to hide and the other to show. when you return to the previous fragment the content will not have been destroyed. Thats how i did it and it worked for me.
或者,您可以考虑在 tabHost 中使用片段。进入另一个 webView 时,您可以将片段设置为隐藏,另一个显示。当您返回上一个片段时,内容不会被破坏。这就是我如何做到的,它对我有用。