Android Webview's ClearCache is very slow

后端 未结 2 400
再見小時候
再見小時候 2020-12-20 03:57

I am using WebViews in an Android app, and I need to prevent the WebViews from caching.

Unfortunately it seems like this seemingly simple goal is nearly impossible t

相关标签:
2条回答
  • 2020-12-20 04:19

    As you are going to the next activity finish the previous activity. So that you can free all memory occupied by that activity. Hope this helps.

    0 讨论(0)
  • 2020-12-20 04:40

    1) Try using setAppCacheEnabled and setAppCacheMaxSize to limit the cache size to very little , lower cache size will result in faster cleanup.

    Ex: wv.getSettings().setAppCacheMaxSize(1);

    OR

    2) If you don't need the cached data then simply set setCacheMode(WebSettings.LOAD_NO_CACHE); , which means "Don't use the cache, load from the network", even though data is cached.

    In-short, simply ignore the cached data, android will take care of it.

    OR

    3) you can also try the below code for no-caching,

    Note: this is only available for Android API 8+

        Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
        noCacheHeaders.put("Pragma", "no-cache");
        noCacheHeaders.put("Cache-Control", "no-cache");
        view.loadUrl(url, noCacheHeaders);
    

    OR

    4) Clear the cache every-time whenever page load finishes. Something like this in the WebViewClient.

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        view.clearCache(true);
    }
    

    OR

    5) You can try deleting whole cached database at once.

        context.deleteDatabase("webview.db");
        context.deleteDatabase("webviewCache.db");
    

    This might give a bit faster result, hope so.

    0 讨论(0)
提交回复
热议问题