Home » Android » java – Is there a way to pre-cache a web page for viewing with an Android WebView?

java – Is there a way to pre-cache a web page for viewing with an Android WebView?

Posted by: admin June 15, 2020 Leave a comment


I’ve read about the HTML5 cache manifest, and I’ve seen Android does support caching websites using the cache manifest. I want to use the cache manifest to download all the required resources for my website to preload it, and then open a WebView and display the remote website using the pre-cached resources. I want to pre-cache my remote page somehow, preferably without using a WebView for the caching process.

The problem with using a WebView for the pre-caching process is that loading the webpage using a WebView renders and executes the page instead of just downloading it.

I’ve read this: http://alex.tapmania.org/2010/11/html5-cache-android-webview.html, but having a WebView support caching is not what I want. I want the loading process to be instantaneous (assuming the cache manifest / etags of the remote website are the same as the cached version) right after I finish pre-caching the resources, instead of waiting for the WebView to load up and cache everything on the first access.

What is the correct way of pre-caching web pages for viewing later?


How to&Answers:

What you want to do requires implementing a mechanism for app cache, linked resources, cookies, and local database store for HTML5 apps that use database API, and that’s an important part of what browsers do in these days. I don’t recommend doing the caching by yourself, not only because it’s so much work, but also because I can’t recall any method in WebView and it’s friends (WebViewClient, etc.) that accepts an outside cache.

But your problem has a very simpler solution: you can put a WebView in your view and set its visibility to gone. Then make it visible when it has finished loading the page. WebView also automatically keeps the cache for your app so that the next time it runs it loads the page more quickly.

For hiding your WebView and then automatically showing it you just have to override onPageFinished in WebViewClient.