1. 2015 is going to be a great year for Android! Why wait??
    Samsung Galaxy Note 5 | Samsung Galaxy S6 | HTC One M9
  2. New Forum Created: Samsung Gear VR!

Refresh a webview's cache


  1. Duveaux

    Duveaux New Member

    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:
    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 VERY much appreciated!

    Advertisement
    :
Loading...

Share This Page