Skip to content Skip to sidebar Skip to footer

Android Webview's Clearcache Is Very Slow

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 to achieve. The

Solution 1:

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 = newHashMap<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.

@OverridepublicvoidonPageFinished(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.

Solution 2:

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.

Post a Comment for "Android Webview's Clearcache Is Very Slow"