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;
}
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);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
How to Launch the 'Add Contact' Activity in Android
Sqlite Query in Android to Count Rows
Android Studio- "Sdk Tools Directory Is Missing"
Scoping States in Jetpack Compose
Android Fragmenttransaction Custom Animation (Unknown Animator Name: Translate)
Android: Changing Nfc Settings (On/Off) Programmatically
Gradle Flavors for Android with Custom Source Sets - What Should the Gradle Files Look Like
Get Latitude and Longitude with Geocoder and Android Google Maps API V2
How to Implement Pagination in Android Listview
Opencv Image Comparison in Android
What Dom Events Are Available to Webkit on Android
How to Add a Hint in Spinner in Xml
Delete File from Internal Storage
Pop the Fragment Backstack Without Playing the Pop-Animation