在 Android 应用程序生命周期中缓存数据的好方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2676280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:52:30  来源:igfitidea点击:

Good way to cache data during Android application lifecycle?

androidandroid-contentprovider

提问by sniurkst

keeping my question short, I have created an application with 3 activities, where A - list of categories, B - list of items, C - single item. Data displayed in B and C is parsed from online XML. But, if I go through A -> B1 -> C, then back to A and then back to B1, I would like to have its data cached somewhere so I wouldn't have to request the XML again.

保持我的问题简短,我创建了一个包含 3 个活动的应用程序,其中 A - 类别列表,B - 项目列表,C - 单个项目。B 和 C 中显示的数据是从在线 XML 中解析出来的。但是,如果我通过 A -> B1 -> C,然后返回 A,然后返回 B1,我希望将其数据缓存在某处,这样我就不必再次请求 XML。

I'm new to Android and Java programming, I've googled a lot and still can't find (or simply do not have an idea where to look) a way to do what I want.

我是 Android 和 Java 编程的新手,我在谷歌上搜索了很多,但仍然找不到(或者根本不知道去哪里找)做我想做的事情的方法。

Would storing all received data in main activity A (HashMaps? ContentProviders?) and then passing to B and C (if they get same request that was before) be a good idea?

将所有接收到的数据存储在主要活动 A(HashMaps?ContentProviders?)然后传递给 B 和 C(如果他们收到与之前相同的请求)是个好主意吗?

采纳答案by digitarald

A simple and fast way to cache information or to keep track of the application state, is to extend the Applicationas described in this blog post.

缓存信息或跟踪应用程序状态的一种简单快捷的方法是扩展应用程序,如本博客文章中所述

This blog post forgets to add that you have to set your CustomApplicationclass in the manifest, like:

这篇博文忘记添加您必须在清单中设置CustomApplication类,例如:

<application [...] android:name="CustomApplication">

In my projects I stick to the singleton style via getInstance.

在我的项目中,我通过 getInstance 坚持单例风格。

More resources: this answer, Global Variables in Android Appsand this blog post.

更多资源:此答案Android 应用程序中的全局变量此博客文章

回答by Phil

If you want to build some sort of cache on memory, consider using a Map with SoftReferences. SoftReferences are sort of references that tend to keep your data around for a while, but does not prevent it from being garbage collected.

如果您想在内存上构建某种缓存,请考虑使用带有 SoftReferences 的 Map。SoftReferences 是一种引用,它们倾向于将您的数据保留一段时间,但不会阻止它被垃圾收集。

Memory on a cell phone is scarce, so keeping everything in memory may not be practical. In that case you may want to save your caches on the device's secondary storage.

手机上的内存很少,因此将所有内容都保存在内存中可能不切实际。在这种情况下,您可能希望将缓存保存在设备的辅助存储上。

Check out MapMaker from Google's Collections, which allows you to conveniently build a 2-level cache. Consider doing this:

Google 的 Collections 中查看 MapMaker ,它可以让您方便地构建 2 级缓存。考虑这样做:

/** Function that attempts to load data from a slower medium */
Function<String, String> loadFunction = new Function<String, String>() {
    @Override
    public String apply(String key) {
        // maybe check out from a slower cache, say hard disk
        // if not available, retrieve from the internet
        return result;
    }
};

/** Thread-safe memory cache. If multiple threads are querying
*   data for one SAME key, only one of them will do further work.
*   The other threads will wait. */
Map<String, String> memCache = new MapMaker()
                                .concurrentLevel(4)
                                .softValues()
                                .makeComputingMap(loadFunction);

About cache on device's secondary storage, check out Context.getCacheDir(). If you're sloppy like me, just leave everything there and hope that the system will clean things for you when it needs more space :P

关于设备二级存储上的缓存,请查看Context.getCacheDir()。如果你像我一样马虎,把所有东西都留在那里,希望系统在需要更多空间时为你清理东西:P

回答by the100rabh

You can make use of data storage as explained in Android Reference here http://developer.android.com/guide/topics/data/data-storage.html

您可以按照 Android 参考中的说明使用数据存储http://developer.android.com/guide/topics/data/data-storage.html