开发者

Android: What activity lifecycle method to clean cache in app?

I'm storing my images in subdirectories under the cache directory. I have a cache clea开发者_JAVA技巧ning task I want to run, that checks the size of the cache and deletes files if necessary (according to some rules I've set up). My question is, what's the best time to run this? I was thinking in one of the Activity lifecycle methods. Any suggestions?


As @binnyb said, OS will automatically clear the cache.

However, if what you call as cache is not the cache of OS (eg. You trying to store some data internal to application), then I suggest, do the check at onPause() or onResume() methods. I will not rely on onDestroy of a service.


I'd probably fire off a Service with a separate thread to do this in the onDestroy() of key activities, maybe keeping track of a last-clean-started time in the service to make sure the hard work is only done every few minutes. onDestroy() isn't guaranteed to be called for every kind of Activity termination, but it reliably is during normal activity destruction due to backing out of an Activity, for example. Just make sure anything it does that touches the disk happens outside the UI thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜