使用缓存的ListView的android Activity的生命周期?
我已经实现了一个 listView,它显示在我的网络服务器上下载的项目。 listView 使用“缓存”(holder),我单击 listView 的一项并调用一个意图(新活动)。它工作得很好,但是当我从这个活动返回到我的 listView 时,会调用 onStart() 并再次调用下载(而我不需要它,因为我使用“缓存”)。
我该如何解决这个问题?我需要在一个函数中调用网络服务器,而当我返回此活动时它不会调用该函数。
谢谢弗洛伦特
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试在活动之间缓存图像,有两种方法:
将文件写入 SD 卡 - 如果您希望图像在活动结束后仍然存在,这可能会很方便。活动结束并重新开始。缺点是您现在必须管理图像的生命周期并确保定期删除它们,以便缓存目录不会继续增长。
使用服务 - 我不确定这种方法的性能如何,而且它往往会更复杂一些。当您的活动来来去去时,服务将继续存在,并将您的图像保留在其内存中。请阅读此答案,了解有关与服务。
将图像存储在静态单例中 - 快速而肮脏的方式;只需确保不要在内存中存储太多图像,否则会出现内存不足的异常。
我可能更喜欢方法 1。
另请参阅这个全面的答案 。
If you're trying to cache images between activities, there are two approaches:
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.
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.
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.