0

In my application i have to share various java-beans class among the activities.

In order to do that, i extended the Application class, in which i create an HashMap filled with all the java-beans. Each java-beans has its own Key.

Example code:

public class MyApplication extends Application {

    public static final String CLASSROOM_KEY = "Classroom";

    private HashMap<String, Object> myObjects;

    @Override
    public void onCreate() {
        myObjects = new HashMap<String, Object>();
        myObjects.put(CLASSROOM_KEY, new Classroom());
    }

    public HashMap<String, Object> getMyObjects() {
        return myObjects;
    }
}

This is usable in all the activities, and this is ok. BUT, i have two problems:

1) I need to get myObjets also in non-activity classes, like utils classes, but in these classes i can't do "getApplicationContext()" because they don't extend Activity.

For example, from my main activity i start a service (but it is in a normal class), and the service calls a query that in turn is in another normal class.

The query needs an object that is in myObjects!

I can't make myObjects public static i think.

2) In MyApplication i have to create all my java-beans in advance.

What if in the future i wanted to create a new classroom object in addition to the already present one? I should create a new key for it, but it is impossible!

Thanks for your help.

UDPATE

I change the question:

In this class:

public class ClassroomUtils {

    private static String result = null;
    private static String studentObjectID = null;

    public static void queryClassroom(String UUID, final Classroom classroom) {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Classroom");
        query.whereEqualTo("BeaconUUID", UUID);
        query.getFirstInBackground(new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                if (e == null) {                        
                    try {
                        result = object.getString("Label");
                    } catch(Exception e1){
                        System.out.println("Vuota");
                    }

                    if(result != null) {
                        Log.i("Classroom", "Retrieved " + result );
                        classroom.setClassroom(result);
                        sendNotification(result);
                        addStudentClassroomRelation(object);
                    }
                } else {
                    Log.e("Classroom", "Error: " + e.getMessage());
                }    
            }
        }); 
    }

i want to avoid to pass the classroom to this method (called from another normal class). How can i access to global objects from this class?

user3075478
  • 127
  • 11
  • You need to do some research into Java and Android as it is. It seems as though you are not knowledgable in the Android system and some tutorials would further help you understand how things work. For example, you can simply create a `singleton` class containing all of these objects, which means you would not need application context. – Vic Vuci May 12 '15 at 18:19
  • http://stackoverflow.com/questions/708012/how-to-declare-global-variables-in-android --> from this discussion it seems that singleton is not recommended. – user3075478 May 12 '15 at 20:12
  • Not recommended by him. I find it doesn't truly matter and its up to the developer. – Vic Vuci May 12 '15 at 20:29

2 Answers2

0

I can't make myObjects public static i think.

Why not? myObjects is effectively global in scope already. There is nothing to be gained, from a memory management standpoint, by having myObjects be a private data member of Application. If you want Classroom to be a Java singleton, do so. You just have to watch your memory management, as you do with your current implementation.

In MyApplication i have to create all my java-beans in advance

No, you do not.

What if in the future i wanted to create a new classroom object in addition to the already present one?

Then create another one. Perhaps the right singleton is a School, which holds onto a collection of Classroom objects. Again, your primary near-term issue is one of memory management, so you do not run out of memory because you are trying to keep these objects around all of the time.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Nothing to be gained from a memory management standpoint but.. it seems a "violation" of the OO paradigm imho. I have to create all my objects in advance because it is not correct to create they in some activities and then pass they through Intent. It would become very... hard to handle! – user3075478 May 12 '15 at 18:47
  • @user3075478: "it seems a "violation" of the OO paradigm imho" -- no more than is your existing implementation. Either can be a source of trouble. – CommonsWare May 12 '15 at 18:55
  • Using this method for sharing objects is recommended in many articles, but they don't consider normal classes. – user3075478 May 12 '15 at 19:50
0

1) I need to get myObjets also in non-activity classes, like utils classes, but in these classes i can't do "getApplicationContext()" because they don't extend Activity.

The best way, I think, is to create the MyApplication class as a singleton. There you can retrieve the data from anywhere by calling getInstance and the corresponding getter/setter for your attributes.

Short example:

public class MyApplication extends Application {

    private static MyApplication mInstance;

    public MyApplication getInstance(){
        // this means you have only one existing instance of this class.
        if(mInstance == null){
            // set the context to this MyApplication instance
            mInstance = this;
        }
        // return the instance of this class
        return mInstance;
    }

    // here your stuff for MyApplication
    public HashMap<String, Object> getMyObjects() {
        return myObjects;
    }
}

Then you can call it from another class like this:

public class CFoo{

    public CFoo(){
        //retrieve myObjects from MyApplication
        MyApplication.getInstance().getMyObjects();
    }
}
Lunero
  • 666
  • 1
  • 8
  • 17
  • I don't think you should instance out the Application class... This can simply be a standalone singleton not attached to the Application class. – Vic Vuci May 12 '15 at 18:20
  • Well yes, but when you want to do some things that you can only do with Application context, then this might be a simple solution. – Lunero May 12 '15 at 18:30
  • I can use this as partial solution, because it seems very... wrong to do in Android! – user3075478 May 12 '15 at 20:15
  • if you referencing to this [post](http://stackoverflow.com/questions/708012/how-to-declare-global-variables-in-android), all they have said is that you should not use singletons because you don't know if they are affected by the application lifecycle. My approach isn't affected by this, because we hold only a reference to the previously by system created MyApplication instance. And you can be sure that this instance exists, stated by the [official docs](http://developer.android.com/reference/android/app/Application.html#onCreate()) – Lunero May 13 '15 at 09:11