1. Download our Official Android App: Forums for Android!

Apps Refresh a webview's cache

Discussion in 'Android Development' started by Duveaux, Nov 8, 2012.

  1. Duveaux

    Duveaux Lurker
    Thread Starter
    Nov 8, 2012

    Nov 8, 2012
    Hi there.

    I'm working on an app, and although this may seem like a minor issue, I can't seem to get it fixed. Also, no reply over at my usual go-to Q&A site StackOverflow. I really need an answer, so any thoughts would be VERY much appreciated!

    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.

    1. EITHER The cached content shows first. In the background, the page checks for a newer version, but only reloads if the content is different.
    2. OR The cached content shows first. In the background the page refreshes and reloads.
    3. 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!
    EDIT > But I'm desperate and would go for 2 in a heartbeat!
    Here's the code I'm currently using.

    Code (Text):
    1. WebView wv = (WebView) v.findViewById(ref_id);
    2. wv.getSettings().setJavaScriptEnabled(true);
    3. wv.getSettings().setDatabaseEnabled(true);
    4. String appDBPath = v.getContext().getApplicationContext().getDatabasePath("AppConstructor").getAbsolutePath();
    6.     wv.setWebChromeClient(new WebChromeClient() {
    7.         @Override
    8.         public void onReachedMaxAppCacheSize(long spaceNeeded,
    9.                 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
    10.             quotaUpdater.updateQuota(spaceNeeded * 2);
    11.         }
    13.         @Override
    14.         public void onExceededDatabaseQuota(String url,
    15.                 String databaseIdentifier, long currentQuota,
    16.                 long estimatedSize, long totalUsedQuota,
    17.                 WebStorage.QuotaUpdater quotaUpdater) {
    18.             quotaUpdater.updateQuota(estimatedSize * 2);
    19.         }
    20.     });
    22. wv.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    23. wv.getSettings().setDomStorageEnabled(true);
    24. wv.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
    25. String appCachePath = v.getContext().getApplicationContext().getCacheDir().getAbsolutePath();
    26. wv.getSettings().setAppCachePath(appCachePath);
    27. wv.getSettings().setAllowFileAccess(true);
    28. wv.getSettings().setAppCacheEnabled(true);
    29. wv.loadUrl(ref_arg1);
    30. Core x = new Core();
    31. wv.addJavascriptInterface(x.new JavaScriptInterface(v.getContext()),"Android");
    Any help would be VERY much appreciated!


Share This Page