I am building a native app, where (at least) one part is a web app, disguised as a native app. Where Facebook showed us how important it is to use good caching, I’m caching the webpage, so it feels like a native part of the app when you load it for the second time. However, the way I’ve set it up, the cached content is permanent. So if I change something on the server, the old cached content still shows.
Here’s what I’d like the achieve in the order of preferability, 1 being the most preferable.
- EITHER The cached content shows first. In the background, the page checks for a newer version, but only reloads if the content is different.
- OR The cached content shows first. In the background the page refreshes and reloads.
- OR The cached content is erased after X time (a week?), forcing the page to reload.
The last option is the least preferable, since everytime the page reloads, it feels slow and horrible. The second option is the way my native activities behave (show cache first, refresh in background. However, this jumps users to the top of a list if they’ve scrolled down in the meantime.). First would be absolutely perfect!
Here’s the code I’m currently using.
WebView wv = (WebView) v.findViewById(ref_id);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setDatabaseEnabled(true);
String appDBPath = v.getContext().getApplicationContext().getDatabasePath("AppConstructor").getAbsolutePath();
wv.setWebChromeClient(new WebChromeClient() {
@Override
public void onReachedMaxAppCacheSize(long spaceNeeded,
long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
quotaUpdater.updateQuota(spaceNeeded * 2);
}
@Override
public void onExceededDatabaseQuota(String url,
String databaseIdentifier, long currentQuota,
long estimatedSize, long totalUsedQuota,
WebStorage.QuotaUpdater quotaUpdater) {
quotaUpdater.updateQuota(estimatedSize * 2);
}
});
wv.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
String appCachePath = v.getContext().getApplicationContext().getCacheDir().getAbsolutePath();
wv.getSettings().setAppCachePath(appCachePath);
wv.getSettings().setAllowFileAccess(true);
wv.getSettings().setAppCacheEnabled(true);
wv.loadUrl(ref_arg1);
Core x = new Core();
wv.addJavascriptInterface(x.new JavaScriptInterface(v.getContext()),"Android");
Any help would be greatly appreciated!
OK, after searching for ages and even getting the tumbleweed achievement on StackOverflow, here’s how I managed to get situation 1.
After the
WebViewhas loaded it’s cache, it starts a newASyncTask. This ASyncTask appends something to the URL, which is my own, custom last-update variable. Ofcourse you could replace this with theHTTP Header Last-Update, but that didn’t work for me. If the resulting variable is different from the cached variable (I’ve stored inSharedPreferences) the ASyncTask forces a refresh on the WebView’s cache using the following code.