1

I try to clear all the history in WebView at WebAppInterface but it is not getting clear.

Code:-

    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);
        }
    });
    }
}

Only in showMessage I want to clear all the history of webview so that in back press it will not go to previous page.

halfer
  • 19,824
  • 17
  • 99
  • 186
Atul Dhanuka
  • 1,453
  • 5
  • 20
  • 56

1 Answers1

4

try override this function in WebViewClient

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