1
votes

I have a webView which changes its load url on a button click. Everything is working fine except the loaded url seems to be not cleared properly.

The previous url is loaded in webview and is displayed just before the new url appears. I tried using

webView.clearView(),

webView.clearHistory(),

webView.loadUrl("about:blank"),

webView.clearCache(true)

and also tried clearing the cookies. But nothing helps clearing the webview properly.

I also tried webView.destroy(); but the webView is destroyed fully as I am nt able to load the next url.

Please help.

2

2 Answers

0
votes

This order worked for me

        mWebView.clearCache(true);
        mWebView.clearHistory();
        clearCookies(this); // Activity context


 @SuppressWarnings("deprecation")
    private void clearCookies(Context context)
    {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } else
        {
            CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
            cookieSyncMngr.startSync();
            CookieManager cookieManager=CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        }
    }
0
votes

Do you have this function in your MainActivity.java ?

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mailto:") || url.startsWith("mms:") || url.startsWith("mmsto:") || url.startsWith("market:")) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
        else {
            view.loadUrl(url);
            return true;
        }
    }
}