I have a very simple WebView in my app which displays a "Welcome" dialog.
I send across the language for the server to respond with like so:
Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
noCacheHeaders.put("Pragma", "no-cache");
noCacheHeaders.put("Cache-Control", "no-cache");
noCacheHeaders.put("Accept-Language", Locale.getDefault().toString());
mWebView.loadUrl("www.fake.com", noCacheHeaders);
This works fine - however, when I change the devices language, I want the WebView
language to change as well, I can see the value for "Accept-Language"
is changing correctly when debugging but the content isn't changing in the WebView. I've looked for ways to stop caching which I describe below. None of them have the effect of making the site load differently after a language change.
To ensure that the server isn't the caching problem I ensure the WebView content changes after I clear the "App Data" and restart. Pretty conclusive proof that the App is storing the WebView data.
So, I've followed many different suggestions which I've listed below. Not one of them is actually changing the content of my WebView
:
From here which causes a crash:
mWebView.clearCache(true);
mContext.deleteDatabase("webview.db");
mContext.deleteDatabase("webviewCache.db");
Just this on it's own (both before the loadURL, after it, and onProgress=100):
mWebView.clearCache(true);
From here:
mWebView.getSettings().setAppCacheMaxSize(0);
I've also tried this:
mWebView.getSettings().setAppCacheEnabled(false);
All result in the same behaviour. My WebView, when switching device language to English from German, still retained the German Website content.
Here is all of the code together:
Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
noCacheHeaders.put("Pragma", "no-cache");
noCacheHeaders.put("Cache-Control", "no-cache");
noCacheHeaders.put("Accept-Language", Locale.getDefault().toString());
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.clearCache(true);
mWebView.getSettings().setAppCacheMaxSize(0);
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
mProgress.setProgress(progress);
try {
getActivity().setProgress(progress);
if(progress == 100) {
mProgress.setVisibility(View.GONE);
mWebView.clearCache(true);
}
} catch(Exception e) {
e.printStackTrace();
}
}
});
mWebView.loadUrl("www.fake.com", noCacheHeaders);
mWebView.clearCache(true);
Repeatable steps:
webview cache is saved into /data/data/[your_package_name]/cache/org.
Just after creating your webview, before loading any pages, you can clear the cache. myBrowser. clearCache(true) - the boolean indicates if you wish to delete the cached files on disk as well.
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With