简体   繁体   中英

Life cycle of an android Activity for a ListView using cache?

I have implemented a listView which shows items downloaded on my Webserver. The listView uses 'cache'(holder), I click on one item of my listView and an intent (new activity) is called. It's works perfectly, but when I come back to my listView from this activity, the onStart() is called and the download is called again (whereas I don't need it because I use 'cache').

How can I fix this problem? I need to call the webserver in a function that it doesn't call when I come back in this activity.

Thanks

Florent

If you're trying to cache images between activities, there are two approaches:

  1. Write files out to the SD card - This might be handy if you want the images to still be there even after your activity ends and restarts. The downside is that you now have to manage the lifecycle of the images and make sure that you periodically delete them so that your cache directory doesn't keep growing.

  2. Use a service - I'm not sure how the performance with this method would be, and it tends to be a little more complicated. A service will keep on living while your activities come and go, holding your images in its memory. Read this answer for more about communicating with services.

  3. Store images in a static singleton - The quick and dirty way; Just make sure you don't store too many in memory or you'll get an out of memory exception.

I'd probably prefer method 1.

Also refer to this comprehensive answer .

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