我有一个正在其中导航的 WebView 。我已经实现了后退和前进。我的问题是,当我在访问另一个 Activity 后返回到 webview 时,历史记录丢失了,即 webview 从第一个 url 开始。
如何编写代码,以便在我调用 webview 上的 finish 以切换到返回 webview 时的另一个 Activity 后,恢复 WebBackForwardList 并将我带到离开 webview 时所在的页面?
我尝试使用 webview.save/restoreState(bundle) 覆盖 onSaveInstanceState 和 onRestoreInstanceState 但没有成功。
我通过在首选项中保存最后一个 url 实现了返回到最后一个页面,但无法恢复整个历史记录。
最佳答案
您是否重用了 WebView ?
restore() 的 javadoc 说:
This method should be called to restore the state of the WebView before using the object. If it is called after the WebView has had a chance to build state (load pages, create a back/forward list, etc.) there may be undesirable side-effects.
因此重新生成 webview 对象可能会有所帮助。
其他研究途径:
WebView.restore() 会在不清除列表的情况下调用 list.addHistoryItem(item),因此如果您有一个正在被重用的 webView,调用 webView.clearHistory( ) 首先在 restore() 之前。
这SO answer表示有一条消息可以发送到历史列表以添加项目。
祝你好运,请回传你的进步。
--拉里
关于android - 在恢复 webview 时恢复历史记录和当前页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4581406/