简体   繁体   中英

Why clear webview cache in Android

As the title implies, could anybody explain if and why it is required to clear webView cache in Android?

My concern is that the cache piles up endlessly and that doesn't sound a good idea. On the other side, it helps loading pages faster, so I prefer not to clear that cache but I wonder what is the standard? What is acceptable here?

Is it the same for clearing history?

Additionally, is there a way to clear the cache/ history in a certain time interval or as it reaches to a certain size limit?

Thank you.

I don't think that cache "piles up endlessly" - every sane cache mechanism that I've ever seen maintains a maximum size - usually as an MRU cache.

So, you shouldn't have to maintain it yourself.

If, for some reason, you really do need to clear the cache, however, you can use the WebView's clearCache method

In my webview project, the webview [in a fragment] was going to the last page visited -- from the cache -- on startup. This was no good, so, I clear the cache: mWebView.clearCache(true);

Now, it would be nice to clear the cache on program exit; then, you wouldn't even be storing cache when not running the app. But, that seems not possible, because, if you put the cache clear code in onDestroy(), then it gets cleared every time the device is rotated, so you lose benefit of having the cache. So, I do the clear cache as the first operation after the webview is initialized.

I'm pretty new at this, so, don't take my experience as gospel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM