Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top