ascu ascu - 1 year ago 64
Android Question

Find out if Android WebView is showing cached page

I'm making an Android application that, among other things, shows websites in a webview. As far as I understand, the webview automatically shows a cached version of the page if a connection can't be established.

Is there any way to find out if the page shown has been fetched from the server or cache?

Maybe even how old the cached page is.

This is to be able to notify the user if he/she is viewing old information.

Answer Source

You could try a hack - at first set WebView's cache mode to WebSettings.NO_CACHE and load the URL. Then, create a custom WebChromeClient like this:

final String urlToLoad = ""; 
webview.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl)
        if (view.getSettings().getCacheMode() == WebSettings.LOAD_NO_CACHE && urlToLoad.equals(failingUrl))
        if (urlToLoad.equals(failingUrl))
            // cache failed as well, load a local resource as last resort
            // or inform the user
        super.onReceivedError(view, errorCode, description, failingUrl);