开发者

Good way to cache data during Android application lifecycle?

开发者 https://www.devze.com 2022-12-27 07:23 出处:网络
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,

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.

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.

Would storing all received data in main activity A (HashMaps? Conten开发者_如何学运维tProviders?) and then passing to B and C (if they get same request that was before) be a good idea?


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

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

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

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

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


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.

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:

/** 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消