Where should I globally store / cache the data needed for the entire application life cycle?

I'm new to Android / Java programming, so some of the main things are still pretty confusing. So, let's say, my application receives all the data (articles) that is required from external XML files, analyzes it on data models (article class), and these articles will be used (displayed in lists or a separate article) throughout the application for all this cycle.

Where should I store them? Can I create a singleton class with an array containing all the articles I have analyzed? Or should I store them in a database and then query it on request? (this sounds like too much work, I don't need to cache them now). What is the best practice here?

+6
android
source share
3 answers

Keep them in Application . This is the base class of any Android application and is alive for the entire duration of the application, while actions are destroyed when they are not displayed or the orientation changes.

You need to declare android:name in your AndroidManifest.xml:

 <application android:name=".YourApplication"/> 

Edited by:

This is also relevant: How to transfer data between Activity / Services in one application?

+2
source share

which depends on your lol programming style.

you can, as you said, create a singleton that will read your xml and store everything.

you can create a static hash in your class. Thus, when you create a new object, you will have access to static information from this class.

You can pass information from class to class as parameters.

if you want us to tell you which one will be the best, it will be difficult without knowing the architecture of your program.

for example, you might have a view controller that handles any changes, and then just store data at that level to pass it on when switching views.

you may have static views in which you could directly set all the values ​​when they open.

you may have views related to each other that cause each other without any controller to handle the switch, in which case you may prefer to be able to collect the necessary information from a singleton or static method ...

+1
source share

As long as you make sure that you do not have threads acting on shared data, you can simply create a static class with static members. every action will have access to it. If you use async loading for your data, then in onPostExecute of your handler you can touch GlobeVars, because it works in the user interface thread.

 public class GlobalVars { public static String userId = "?"; //public static String serverUrl = "10.0.2.2"; //localhost when developing public static String serverUrl = "192.168.1.4"; //on device to laptop //public static String serverUrl = "102.192.293.10"; //production public static Book currentBook = null; public static Chapter currentChapter = null; public static int lastclickedChapter = -1; public static Voice currentVoice = null; public static String catalogJson = ""; public static ArrayList<Book> catalogItems = null; } 

onCreate of MainActivity I can set the directory to my loaded xml list hidden for objects

 GlobeVars.catalogItems = downloaded xml to object list 

in my SubActivity, which is a list of chapters in onclicklistener, I can set:

 GlobeVars.currentChapter = items[clickeditem]; 

when you return to the main action, the values ​​will still be set.

0
source share

All Articles