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

发布于 2024-10-21 22:19:35 字数 250 浏览 5 评论 0原文

我已经实现了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜宝宝 2024-10-28 22:19:35

如果您尝试在活动之间缓存图像,有两种方法:

  1. 将文件写入 SD 卡 - 如果您希望图像在活动结束后仍然存在,这可能会很方便。活动结束并重新开始。缺点是您现在必须管理图像的生命周期并确保定期删除它们,以便缓存目录不会继续增长。

  2. 使用服务 - 我不确定这种方法的性能如何,而且它往往会更复杂一些。当您的活动来来去去时,服务将继续存在,并将您的图像保留在其内存中。请阅读此答案,了解有关与服务。

  3. 将图像存储在静态单例中 - 快速而肮脏的方式;只需确保不要在内存中存储太多图像,否则会出现内存不足的异常。

我可能更喜欢方法 1。

另请参阅这个全面的答案

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文