Android Webview - Completely Clear the Cache
Web Technologies
Web Development
2 years ago
5
Star Rating
1
Rating
_x000D_
_x000D_
I have a WebView in one of my Activities, and when it loads a webpage, the page gathers some background data from Facebook.
What I'm seeing though, is the page displayed in the application is the same on each time the app is opened and refreshed.
I've tried setting the WebView not to use cache and clear the cache and history of the WebView.
I've also followed the suggestion here: How to empty cache for WebView?
But none of this works, does anyone have any ideas of I can overcome this problem because it is a vital part of my application.
mWebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
if(progress >= 100)
{
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
}
else
{
mProgressBar.setVisibility(ProgressBar.VISIBLE);
}
}
});
mWebView.setWebViewClient(new SignInFBWebViewClient(mUIHandler));
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.clearHistory();
mWebView.clearFormData();
mWebView.clearCache(true);
WebSettings webSettings = mWebView.getSettings();
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
Time time = new Time();
time.setToNow();
mWebView.loadUrl(mSocialProxy.getSignInURL()+"?time="+time.format("%Y%m%d%H%M%S"));
So I implemented the first suggestion (Although changed the code to be recursive)
private void clearApplicationCache()
{
File dir = getCacheDir();
if(dir!= null && dir.isDirectory())
{
try
{
ArrayList stack = new ArrayList();
// Initialise the list
File[] children = dir.listFiles();
for(File child:children)
{
stack.add(child);
}
while(stack.size() > 0)
{
Log.v(TAG, LOG_START+"Clearing the stack - " + stack.size());
File f = stack.get(stack.size() - 1);
if(f.isDirectory() == true)
{
boolean empty = f.delete();
if(empty == false)
{
File[] files = f.listFiles();
if(files.length != 0)
{
for(File tmp:files)
{
stack.add(tmp);
}
}
}
else
{
stack.remove(stack.size() - 1);
}
}
else
{
f.delete();
stack.remove(stack.size() - 1);
}
}
}
catch(Exception e)
{
Log.e(TAG, LOG_START+"Failed to clean the cache");
}
}
}
However this still hasn't changed what the page is displaying. On my desktop browser I am getting different html code to the web page produced in the WebView so I know the WebView must be caching somewhere.
On the IRC channel I was pointed to a fix to remove caching from a URL Connection but can't see how to apply it to a WebView yet.
http://www.androidsnippets.org/snippets/45/
If I delete my application and re-install it, I can get the webpage back up to date, i.e. a non-cached version. The main problem is the changes are made to links in the webpage, so the front end of the webpage is completely unchanged.
Posted on 16 Aug 2022, this text provides information on Web Development related to Web Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Take Quiz To Earn Credits!
Turn Your Knowledge into Earnings.