Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good way to cache data during Android application lifecycle?

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? ContentProviders?) and then passing to B and C (if they get same request that was before) be a good idea?

like image 975
sniurkst Avatar asked Apr 20 '10 15:04

sniurkst


1 Answers

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.

like image 90
digitarald Avatar answered Oct 12 '22 11:10

digitarald