Caching in Android Webview

Caching in Android webview

Don't use these:

viewer.getSettings().setAppCacheMaxSize(1024*1024*8);   
viewer.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache"‌​);
viewer.getSettings().setAppCacheEnabled(true);

These have nothing to do with the default webview internal cache. Appcache is an entirely different feature mean to make you able to run the website w/o an internet connection. It does not work that great and probably you do not want to use it.

With setting this: viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT) is enough.

How to Load Cache Page in Webview

Just include enableHTML5AppCache()

private void enableHTML5AppCache() {
mWebview.getSettings().setDomStorageEnabled(true);
mWebview.getSettings().setAppCachePath("/data/data/" + getPackageName() + "/cache");
mWebview.getSettings().setAppCacheEnabled(true);
mWebview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}

webview not caching

So after a series of detailed tests on our iframe and Android device, I came to realize that WebView does not cache files which are bigger than a particular size.

One of my pages which were being loaded by the iframe had a video of 30mb which was not getting cached and was resulting in huge data consumption. (as it was getting rendered every 2 mins)

I reduced it to 10mb and it still did not cache it finally I reduced it to 980kb and it started caching.

I am not sure about the caching limit per file for WebView but I came across an interesting bug here which states after Android 4.3 the total cache limit was hardcoded to 20mb which we can find in this file on line 98.

If anyone knows where these limits are documented do let the community know by replying here.

Android WebView, load from cache and attempt to refresh

Base on the documentation for WebSettings.LOAD_CACHE_ELSE_NETWORK:

Use cached resources when they are available, even if they have
expired. Otherwise load resources from the network

Because of this you view is always loaded from cache.

You can use following method:
1. Check connectivity:

private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

return cm.getActiveNetworkInfo() != null;
}

  1. Before instead of setting:

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setAppCachePath(getBaseContext().getCacheDir().getPath());

    webSettings.setCacheMode(isNetworkConnected()?WebSettings.LOAD_NO_CACHE: WebSettings.LOAD_CACHE_ONLY);
  2. On Swipe to refresh, you can do the same:

    swipeRefreshLayout.setOnRefreshListener(
    new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
    if(isNetworkConnected()){
    ...
    myWebView.reload();
    } else {
    //Do something for non-connectivity
    }
    }
    });

Check if cached url and current url are the same or different

Refer this link

Set webview cache mode LOAD_CACHE_ELSE_NETWORK.

Override WebViewClient methods :

@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (view.getUrl().equals(failingUrl)){
showInternetConnectionError();
}
super.onReceivedError(view, errorCode, description, failingUrl);
}

@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
onReceivedError(view, error.getErrorCode(), error.getDescription().toString(), request.getUrl().toString());
}


Related Topics



Leave a reply



Submit