How to clear cookies and cache of webview on Android when not in webview?

AndroidAndroid Webview

Android Problem Overview


Upon a user's sign out from my app I am clearing everything that may have been cached previously from the webview by calling this method:

 public void clearCookiesAndCache(Context context){
    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.removeAllCookies(null);
    }
    else {
        cookieManager.removeAllCookie();
    }        
}

CookieSyncManager is marked as deprecated, however. CookieSyncManager.createInstance(context) is necessary to be called, however, if you have not loaded the webview previously. So how are we supposed to clear the cookies and cache without using the deprecated CookieSyncManager in cases where the webview may not have been previously loaded?

Android Solutions


Solution 1 - Android

I use the following approach in my app:

    @SuppressWarnings("deprecation")
    public static void clearCookies(Context context)
    {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            Log.d(C.TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } else
        {
            Log.d(C.TAG, "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
            CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
            cookieSyncMngr.startSync();
            CookieManager cookieManager=CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        }
    }

Or in Kotlin

@SuppressWarnings("deprecation")
fun clearCookies(context: Context?) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        CookieManager.getInstance().removeAllCookies(null)
        CookieManager.getInstance().flush()
    } else if (context != null) {
        val cookieSyncManager = CookieSyncManager.createInstance(context)
        cookieSyncManager.startSync()
        val cookieManager: CookieManager = CookieManager.getInstance()
        cookieManager.removeAllCookie()
        cookieManager.removeSessionCookie()
        cookieSyncManager.stopSync()
        cookieSyncManager.sync()
    }
}

I call this method in the following manner from my fragment:

mWebView.clearCache(true);
mWebView.clearHistory();
    
U.clearCookies(getActivity());
    
mWebView.loadUrl(authorizeURL);

It is possible to dump the cookies for a domain before and after the call to clearCookies by

String yahooCookies = CookieManager.getInstance().getCookie("https://yahoo.com");
Log.d(C.TAG, "Cookies for yahoo.com:" + yahooCookies);

After calling clearCookies yahooCookies will be null.

This implementation feeds my needs, I have tested it on several emulators and a prehistoric Samsung Galaxy Gio with Android 2.3.3 and Nexus 5 with Android 5.1.1.

Solution 2 - Android

Working and tested ..

android.webkit.CookieManager cookieManager = CookieManager.getInstance();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
         // a callback which is executed when the cookies have been removed
         @Override
         public void onReceiveValue(Boolean aBoolean) {
               Log.d(TAG, "Cookie removed: " + aBoolean);
         }
       });
}
else cookieManager.removeAllCookie();

Solution 3 - Android

To clear all the stored info from Webview,

// Clear all the Application Cache, Web SQL Database and the HTML5 Web Storage
    WebStorage.getInstance().deleteAllData();

    // Clear all the cookies
    CookieManager.getInstance().removeAllCookies(null);
    CookieManager.getInstance().flush();

    webView.clearCache(true);
    webView.clearFormData();
    webView.clearHistory();
    webView.clearSslPreferences();

Solution 4 - Android

I'm using this...

@WorkerThread
public static void clearCache() {
    new WebView(getApplicationContext()).clearCache(true);
}

Be careful to call it on a UI Thread otherwise you get an exception.

Solution 5 - Android

I just had the same problem. I needed to delete all cookies because I didn't want to keep old ones and didn't want to use the deprecated method.

Here the documentation:

http://developer.android.com/reference/android/webkit/CookieManager.html

And here the method:

> public abstract void removeAllCookies (ValueCallback > callback)

In your case:

cookieManager.removeAllCookies(null);

should do the job. If with "null" it doesn't work, then you have to use a callback*...

Eventually you may find an answer here: https://stackoverflow.com/questions/1652850/android-webview-cookie-problem

Hope this helps. Cheers

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnRockView Question on Stackoverflow
Solution 1 - AndroidAdamVeView Answer on Stackoverflow
Solution 2 - AndroidGayan WeerakuttiView Answer on Stackoverflow
Solution 3 - AndroidSrinivasanView Answer on Stackoverflow
Solution 4 - AndroidPollizzioView Answer on Stackoverflow
Solution 5 - AndroidfirepolView Answer on Stackoverflow