I have a web view with the following configurations:
getSettings().setJavaScriptEnabled(true);
getSettings().setSupportMultipleWindows(false);
getSettings().setNeedInitialFocus(false);
getSettings().setSupportZoom(false);
getSettings().setAllowFileAccess(true);
getSettings().setAppCacheEnabled(true);
getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
String cachePath = getContext().getCacheDir().getAbsolutePath();
getSettings().setAppCachePath(cachePath);
I load an html page into it, which in turn loads some assets: some static images, a couple of js files, a css and a font.
These assets are returned with cache-control headers:
HTTP/1.1 200 OK
Date: Wed, 01 Jan 2014 18:40:36 GMT
Last-Modified: Mon, 30 Dec 2013 09:23:55 GMT
Cache-Control: public, max-age=604800
I reload the url, and I can see in fiddler that all the assets are cached locally and nothing is reloaded.
BUT, if I close the app and open it again - everything is downloaded again.
How can I make sure that the assets (js\css\images) are cached on the device and not retrieved from my servers every time?
Turns out this code works well.
The problem was that I was calling clearCache
on another webview, and as it turns out:
the cache is per-application, so this will clear the cache for all WebViews used.
As soon as I removed the call to clearCache
from the other webview, everything was working well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With