Reputation: 31
I'd like to pre-load WebView's cache with a webpage from the internet. This would need to be done asynchronously and hidden as to not disturb other UI interactions. The purpose of the pre-load is to avoid subsequent network calls if the WebView is presented by way of optional button click (ultimately for speedier display).
The webpage contains Javascript and AJAX so I don't think pre-loading the cache using something like HttpClient would work. I also looked at using WebView's loadUrl() in an AsyncTask but I am seeing this conflicts with the main UI thread.
Has anyone with a similar situation found a way to accomplish the background pre-loading/caching?
Upvotes: 3
Views: 2864
Reputation: 855
WebView
is loading data in a separate thread by default, so no need to use AsyncTask. Just create a hidden WebView and call loadUrl()
.
If you want to pre-load more than one URL you can use a WebViewClient
and override onPageFinished()
to detect when the first one is completed. When you are done with the WebView
make sure you remove references to it so that it can be garbage collected.
Finally, if you want to make sure that the visible WebView
always uses the cached version of the url's when available, you can call:
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK).
Upvotes: 6