Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

101 ways to clear a WebView cache - all of which don't work

I have a very simple WebView in my app which displays a "Welcome" dialog.

I send across the language for the server to respond with like so:

Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
noCacheHeaders.put("Pragma", "no-cache");
noCacheHeaders.put("Cache-Control", "no-cache");
noCacheHeaders.put("Accept-Language", Locale.getDefault().toString());
mWebView.loadUrl("www.fake.com", noCacheHeaders);

This works fine - however, when I change the devices language, I want the WebView language to change as well, I can see the value for "Accept-Language" is changing correctly when debugging but the content isn't changing in the WebView. I've looked for ways to stop caching which I describe below. None of them have the effect of making the site load differently after a language change.

To ensure that the server isn't the caching problem I ensure the WebView content changes after I clear the "App Data" and restart. Pretty conclusive proof that the App is storing the WebView data.

So, I've followed many different suggestions which I've listed below. Not one of them is actually changing the content of my WebView:

From here which causes a crash:

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

Just this on it's own (both before the loadURL, after it, and onProgress=100):

mWebView.clearCache(true);

From here:

mWebView.getSettings().setAppCacheMaxSize(0);

I've also tried this:

mWebView.getSettings().setAppCacheEnabled(false);

All result in the same behaviour. My WebView, when switching device language to English from German, still retained the German Website content.

Here is all of the code together:

    Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
    noCacheHeaders.put("Pragma", "no-cache");
    noCacheHeaders.put("Cache-Control", "no-cache");
    noCacheHeaders.put("Accept-Language", Locale.getDefault().toString());

    mWebView.getSettings().setAppCacheEnabled(false); 
    mWebView.clearCache(true);
    mWebView.getSettings().setAppCacheMaxSize(0);

    mWebView.setWebChromeClient(new WebChromeClient() {
           public void onProgressChanged(WebView view, int progress) {                 
               mProgress.setProgress(progress);
               try {
                   getActivity().setProgress(progress);
                   if(progress == 100) {
                       mProgress.setVisibility(View.GONE);
                       mWebView.clearCache(true);
                   }
               } catch(Exception e) {
                   e.printStackTrace();
               }
           }
    });
    mWebView.loadUrl("www.fake.com", noCacheHeaders);
    mWebView.clearCache(true);      

Repeatable steps:

  1. Install app
  2. Open Webview - English Content.
  3. Force Stop App
  4. Change Device Language to German
  5. Open App & WebView - English Content.
  6. Force Stop App
  7. Clear App Data
  8. Open App & WebView - German Content.
like image 273
Graeme Avatar asked Oct 22 '12 10:10

Graeme


People also ask

How do I check my WebView cache?

webview cache is saved into /data/data/[your_package_name]/cache/org.

How do I disable cache in WebView?

Just after creating your webview, before loading any pages, you can clear the cache. myBrowser. clearCache(true) - the boolean indicates if you wish to delete the cached files on disk as well.


1 Answers

 CookieSyncManager.createInstance(this);         
 CookieManager cookieManager = CookieManager.getInstance();        
 cookieManager.removeAllCookie();
like image 133
mukesh Avatar answered Oct 12 '22 19:10

mukesh