user291701
user291701

Reputation: 39681

Clearing all cached items created by a WebView?

I've got a WebView in my app. When the user logs out of my app, I'd like to delete all cached resources the WebView may have created. Looking at an emulator, I see the following files:

/data
  /data
    /com.example.myapp
      /cache
        /webviewCache
          bunch of files here..
      /databases
        webview.db
        webviewCache.db

Is there any system call I can use to clear all the elements in /cache and /databases, or should we do that manually? I'm worried about doing it manually just because I don't know what new files WebView may leave behind in future versions of android so I won't be sure I'm really clearing everything for the user.

Upvotes: 8

Views: 33877

Answers (3)

amalBit
amalBit

Reputation: 12181

This is the only code that saved my day!!

 CookieSyncManager.createInstance(this);         
 CookieManager cookieManager = CookieManager.getInstance();        
 cookieManager.removeAllCookie();

My scenario:

  • Once logged in via linkedIn with the webview. The webview saves the login details. I need my app to clear this when the user signs out of the application. All the other approaches did not work for me.

Upvotes: 16

Reno
Reno

Reputation: 33792

Only posting here because commenting can be ugly

clearCache() will work because:

From the doc:

Clear the resource cache. Note that the cache is per-application, so this will clear the cache for all WebViews used.

Upvotes: 4

try this

mWebView.clearCache(true);
mContext.deleteDatabase("webview.db");
mContext.deleteDatabase("webviewCache.db");

Upvotes: 18

Related Questions