為了讓返回按鈕控制WebView“返回上一頁”,我處理了按鈕消息並加入相關邏輯。同時為了復用WebView,我需要在特定的時候調用WebView::clearHistory() ,不料WebView::clearHistory()並未起作用。
原因是WebView::clearHistory()有個奇怪的特性,那就是只清除當前頁之前的歷史記錄。假設當前頁面為A,我調用WebView::clearHistory()然後loadUrl(B),接著回退還是會退到A。所以正確的調用時機是在B完全載入之後才行,簡單的解決方案:
1
2
3
4
5
6
7
8
9
|
mWebView.loadUrl( "B" ) mWebView.postDelayed( new Runnable() { @Override public void run() { mWebView.clearHistory(); } }, 1000 ); |
參考方案:https://groups.google.com/forum/#!topic/android-developers/Onpbm7esvsM
全站熱搜
留言列表