简体   繁体   English

使用缓存的ListView的android Activity的生命周期?

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

I have implemented a listView which shows items downloaded on my Webserver. 我实现了一个listView,它显示了在我的Web服务器上下载的项目。 The listView uses 'cache'(holder), I click on one item of my listView and an intent (new activity) is called. listView使用“缓存”(持有者),我单击listView的一项,然后调用一个意图(新活动)。 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'). 它的工作原理非常完美,但是当我从该活动返回到listView时,将调用onStart()并再次调用下载(而我不需要它是因为我使用了“缓存”)。

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. 我需要在此活动返回时不使用的函数中调用Web服务器。

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. 将文件写出到SD卡 -如果即使活动结束并重新启动后仍希望图像仍然存在,这可能很方便。 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. 我可能更喜欢方法1。

Also refer to this comprehensive answer . 另请参阅此综合答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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