我尝试在WebAppInterface上清除WebView中的所有历史记录,但并不清楚。

码:-

    wvList = (WebView) view.findViewById(R.id.wv);
    wvList .setHorizontalScrollBarEnabled(false);
    wvList .getSettings().setJavaScriptEnabled(true);
    wvList .getSettings().setDomStorageEnabled(true);
    wvList .addJavascriptInterface(new WebAppInterface(mContext), "Android");
    wvList .setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return false;
        }
    });



public class WebAppInterface {
    Context mContext;

    /**
     * Instantiate the interface and set the context
     */
    WebAppInterface(Context cxt) {
        mContext = cxt;
    }

    /**
     * Show a SnackBar from the web page
     */
    @JavascriptInterface
    public void showMessage(String msg) {
        wvList .post(new Runnable() {
        @Override
        public void run() {
            wvList .clearHistory();// Not working
            wvList .loadURL(url.toString);
        }
    });
    }
}


仅在showMessage中,我想清除Webview的所有历史记录,以便在向后按时不会转到上一页。

最佳答案

尝试在WebViewClient中重写此功能

@Override
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
    super.doUpdateVisitedHistory(view, url, isReload);
    if (needClearHistory) {
        needClearHistory = false;
        webview.clearHistory();
    }
}

07-27 14:03