casolorz
casolorz

Reputation: 9634

How to clear all WebView stored information?

I have an Android browser and I have the option to clear cache, storage, cookies, etc.

The code looks like this:

webView.clearCache(true);
webView.clearFormData();
webView.clearHistory();
webView.clearSslPreferences();
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();

And this seems to work on all my tests but when I go to google.com my old searches are still there. What am I not clearing?

Thanks.

Upvotes: 7

Views: 12804

Answers (2)

user8690908
user8690908

Reputation:

I have got a root-access granted device and found that calling WebStorage.getInstance().deleteAllData(); and similar codes doesn't clear the cache created by the WebView at applicationDatadir/app_webview.

Also, that codes sometimes causes fatal errors like A/libc: Send stop signal to pid:16145 in void debuggerd_signal_handler(int, siginfo_t*, void*)

And it's (the cache) not so small in size.

To achieve that you can use this following code snippet :

public static void clearWebViewCachesCustom(Context context) {
    try {
        String dataDir = mContext.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.dataDir;
        new File(dataDir + "/app_webview/").delete();
    } catch (Exception e) {
        if (!MainActivity.deBugTest) Crashlytics.logException(e);
        e.printStackTrace();
        e.getSuppressed();
    }
}

Upvotes: 1

casolorz
casolorz

Reputation: 9634

Found the solution:

WebStorage.getInstance().deleteAllData();

Upvotes: 17

Related Questions