Why clear webview cache in Android - 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.

Related

Does glide caching works after restart by default too?

I'm new to caching and stuff. I have an app that fetches images from firebase storage by their url using Glide. Now it will cost me a lot if the user fetches a single image "every time" they use the app using the url (also a single image might be visible in couple of activities , so then fetching becomes redundant). So does glide cache is valid even after app restart and across all the activities or is it activity related( I mean for every activity does it has separate cache or is it just simply dependent on the url)?
Also is the caching enabled by default?
Sorry if the question sounds stupid, but otherwise the cost will increase significantly. Thanks.
Yes it's store some cache after app restart also the best way to invalidate a cache file is to change your identifier when the content changes (url, uri, file path etc) when possible.

Android Does admob use cache? How Cache works?

I have this aplication thats keeps building cache every time I open the app
In my app I load like 20 images from my website, but I don't save them to cache,
does the app automatically save this images to cache? If so, I don't think they load from cache, cuz I still have to have internet to load them
Admob in the other hand, if I don't have internet and I loaded the test ad before, it will load the testad.
currently I have the app to delete the cache everytime it starts, keeping cache low, but this doesn't seems smart.
My questions are:
Does admob uses cache of my app?
Knowing my app loads 20 images from my website, this images being
loaded affects my cache size even if I don't save them
programmatically to cache ?
is deleting cache expensive enough to have a dedicated thread to
do just that?
Does admob uses cache of my app?
Yes i think Admob caches its ads so they load faster. And Admob should be managing its own cache size. I don't think you need to worry about that.
Knowing my app loads 20 images from my website, this images being loaded affects my cache size even if I don't save them
programmatically to cache ?
No images or bitmaps will not be cached unless you do so, see this link for more info. I will recommend that you use the Glide or Picasso for image loading since they are memory efficient and abstract out most of the complexity.
Is deleting cache expensive enough to have a dedicated thread to do just that?
I don't think you should use dedicated thread. What you can do is override onTrimMemory and keep monitoring the memory level.When your memory level is critical or low you can delete the cache. Check this link for more info. In my opinion if you use Glide for loading your images it will already handle the memory very well and prevents OutOfMemoryError. Also, loads image much faster.

Most Efficient Way to Store Bitmap Info Android

Currently, I am using a WeakHashMap to store my BitMap and link it to my key, but I have noticed it begins to use a ton of RAM. Is there a better solution to caching the BitMaps till I need them? It is unlikely I will need the image after the application closes, so a long-term solution isn't really needed.
Thanks!
I need to keep them in memory in case the user swipes back to a
previous view to save bandwidth and make the ViewPager more responsive
FragmentStateAdapter is meant for precisely that. Also use setOffscreenPageLimit() and setRetainInstance() in conjunction with that.
Storing Bitmap objects in runtime memory is not a good idea and will sooner or later lead to OutOfMemoryErrors getting thrown.
It is unlikely I will need the image after the application closes, so a long-term solution isn't really needed.
Storing images in cache wont be a problem as system doesn't clear image from cache even after you close the application or unless you clear the app data from setting.
For image downloading you can use image libraries like:
Image loaders:
https://github.com/nostra13/Android-Universal-Image-Loader
It gives you option to save image in cache.
You can also use volley library. But in this you have to implement your own code for cache storing.
http://developer.android.com/training/volley/index.html
Otherwise you can also implement your own code for cache in which you can use memory disk for caching.
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html#disk-cache
Use an LruCache. There are plenty of online guides on the developer site.

How to cache pages in webview without opening them?

So that's about it, I want webview to download and cache multiple pages when the app is opened so that they're saved for offline use. They're only going to be a few kilobytes each and there's only going to be about 10 of them, we're talking less than 100kb all together, but I want to make sure all the info is up to date when the app is open. How would I do go about doing this? Just somewhere to start would be nice.
Edit: assume internet connection at first, but then this stops and there's no internet after the caching is complete.
You can create a WebView and make it not visible and load the resources here.

is it good idea to store bitmaps as temp files to implement undo and redo feature?

In my project i am making big changes on pixels of a bitmap and i want to add undo redo feature to this project.Sincesaving several bitmaps on VM's memory is not a good idea i thought my only choice is storing history(bitmaps) as temp files.Before starting to implement i want to be sure i am doing something not stupid here.
Depends on the processing power and the length of undo history you want to maintain. If you can apply the transformations with reasonable speed then you can just keep a copy of a bitmap x operations back and in case of an undo just apply the last x-1 transformations to it.
How fast do you need the undo to happen? Remember: memory is faster than anything else.
How many bitmaps do you need to be able to go back and undo? If it's a lot, then yes, temporary files might be best.
What if they close the app and come back later (or the app is killed to free memory)? How important is it to get to those undos? If it is important, then you need to save them to disk.
How big are the images? That could also factor in your decision of how many you could in memory.
Perhaps even consider some of both? Keep the most recent in memory and save it and all others to disk?
Whether or not you have room on the heap, storing the undo state in your persistent storage (not temp files) is your only option. The user can leave your app, at which point your process can be killed. Upon returning, you need to restart your app with the same state they last saw. Anything you had in your own heap will no longer be there; you can not put big bitmaps in the Bundle in onSaveInstanceState(). So you need to put them in persistent storage.
Just make sure you don't block the UI while doing so -- write a new state asynchronously from updating the UI.

Categories

Resources