54

In my android webview my webpage is loading even without internet because of cache, so i want to disable cache in android webview, can anyone help me how to do this?

I am using following lines in my webview, but still i am not able to disable cache.

     myBrowser.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
     myBrowser.getSettings().setAppCacheEnabled(false); 

Please suggest me if there are some other methods

user3153083
  • 571
  • 1
  • 5
  • 8
  • possible duplicate of [Android Webview - Completely Clear the Cache](http://stackoverflow.com/questions/2465432/android-webview-completely-clear-the-cache) – Arda Yigithan Orhan Jan 11 '14 at 07:45

4 Answers4

66

Please add below code

mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

Please remove app from history task and test its working.

Edric
  • 24,639
  • 13
  • 81
  • 91
Piyush
  • 5,607
  • 4
  • 27
  • 27
  • Works like a charm! I was having an issue with a payment gateway not updating the Amount in case user presses BACK and updates the order value. Now it works! – sud007 Aug 03 '16 at 10:56
  • Can't thank you enough! Too much time wasted on this! – Hobbes the Tige May 23 '18 at 16:17
  • It seems setAppCacheEnabled is now deprecated. Does anyone know what replaced it..? – Vélimir Nov 10 '21 at 22:27
  • 1
    @Sho Hi, if it is still the issue, try this - https://stackoverflow.com/questions/62354423/how-to-use-serviceworkercontroller-on-android/63491172#63491172 and here is info from javadoc: The Application Cache API is deprecated and this method will become a no-op on all Android versions once support is removed in Chromium. Consider using Service Workers instead. See https://web.dev/appcache-removal/ for more information. – Inliner Apr 07 '22 at 14:06
35

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.

The documentation is here if you wish to dig further.

Mellson
  • 2,918
  • 1
  • 21
  • 23
5

I had the same problem. Even though I had the below two lines, my webview was still loading old data.

webview.getSettings().setAppCacheEnabled(false); webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

My mistake was, i had this cache related settings after I called the loadUrl() on webview. After I moved these cache settings ahead of loadUrl(), i had everything working as expected.

MSD
  • 2,627
  • 1
  • 18
  • 12
0
 webView.clearCache(true)
 webView.clearHistory()
 webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE;

Those 3 methods before the loadUrl method worked for me. Don't forget to clear the app storage or uninstall the app completely to test for the first time.