1

So what I'm wanting to do is have a few different single manager classes that handle the caching of data that I've pulled down from the internet. I want the data in these classes/objects to be accessible from different activities in my application, whereby any of the activities can manipulate data or call functions as needed.

Eg, I have a UserDatabasemanager where I will store the initial raw JSON data I pull down from the internet and an array of User objects which is a distilled version of that JSON data.

My app at various times will pull data down from the web and will update the User array as data changes. Eg, initially pull down a list of users that fit a criteria. So I then cache all the usernames and IDs into the User objects. Should the application then focus in on a particular user page, it will then download that user's description text and store the new description into User object. (All of which I think I cant do currently with my knowledge of programming)

I've been reading up on sending objects via Intents with Parcelable and Serializable which seems a bit of a round about way of doing things for shared data between Activities.

How to send an object from one Android Activity to another using Intents?

Passing data through intent using Serializable

Since my intention is to use these classes/managers/objects (whatever we want to call them) as single instances I'm not too fussed about following encapsulation to the letter as their job is to provide and update my cache data based on the needs of the Activities.

I'm still very green with all this Android/Java stuff, but it seems this sort of thing would be fairly straight forward in C/C++ with pointers. It seems that even if I was programming directly with Java I could pass around references to objects fairly easily, but the structure of Activities seem to be in such a way that it doesn't lend itself to easy passing data around in my own program?

Community
  • 1
  • 1
ChiggenWingz
  • 461
  • 1
  • 8
  • 17

3 Answers3

1

One easy (but not great) way to achieve this is to use the Android Application class to store (the singleton) instances of your managers. You can always retrieve a reference to your application by calling getApplication() on any activity.

You need to provide your own implementation of the Application class so that you create the managers in the onCreate() method. You should also create strongly typed getters for your managers.

Also, consider what you need to do for the system callbacks the Application may receive such as onLowMemory().

amahfouz
  • 2,328
  • 2
  • 16
  • 21
1

So what I ended up doing in the end is making a Globals.java file with the following contents

public class Globals {

public static ArrayList<User> userObjArray = new ArrayList<User>();

//Weird magic class stuff
private static Globals instance;

private Globals() {
}

public static synchronized Globals getInstance() {
    if (instance == null) {
        instance = new Globals();
    }
    return instance;
}

Then whenever I wanted to use data from the Globals I would do something like...

Globals.getInstance().userObjArray

This seems to be working exactly how I want it to. Its not great for encapsulation, but that wasn't the goal for what I needed to get done.

ChiggenWingz
  • 461
  • 1
  • 8
  • 17
0

I think you should use android service and create binders in your activities. http://developer.android.com/guide/components/services.html

Semyon Tikhonenko
  • 3,872
  • 6
  • 36
  • 61