2

After looking at various info regarding singletons, global variables and extending application class, I have come up with what I believe is a workable solution, BUT I'd like to know if there is a reason NOT to do this.

I've created a class with:

public class GlobalVariables {
    private static GlobalVariables instance;
    private static Boolean testVar;
    //other private static variables.

    //setters
    public void setTestVar(Boolean vAR)
    {
       GlobalVariables.testVar = vAR;
    }
    //other similar setters from private static variables above.

    //getters
    public Boolean getTestVar() {
        return GlobalVariables.testVar;
    }
    //other similar getters from private static variables above.

    public static synchronized GlobalVariables getInstatnce() {
        if(instance==null) {
            instance=new GlobalVariables();
        }
        return instance;
    }
}

In my activity, service, broadcast receiver...:

private GlobalVariables g = GlobalVariables.getInstance();

// getting the value
Boolean bVar = g.getTestVar();

// do somethings with bVar.
...

bVar = true;

// set GlobalVariables.testVar
g.setTestVar(bVar);

Again, is there a reason NOT to use this for passing variables between activities, services, broadcastReceivers and whatever other classes?

John Smith
  • 3,493
  • 3
  • 25
  • 52
  • There are good reasons not to do this (read about principles of OOP and Google for why global variables are bad news) but sometimes, they are necessary. However, if they are holding some state of your application, that usually points to bad design. – Simon Nov 16 '14 at 22:29

2 Answers2

3

Using Singletons on Android in and of itself is nothing bad, the Android guide even suggests it as one way to share data between activities.

However, one thing you should be very careful with in the Android framework is passing Context items through Singletons. Unlike your application itself, Activities and Fragments get reclaimed by the system for various reasons (explicit activity destruction, low memory, or simply the user leaving an app). However, a Singleton maintaining a reference to one of these Contexts isn't destroyed, meaning you just created a possible memory leak, since the item is no longer of use to the system and a new one has been created already. One way to work around the Context leaking is by using the ApplicationContext, which is the Context for the entire process, and only gets reclaimed if the entire process (along with your Singleton) gets reclaimed. Using the ApplicationContext in a Singleton doesn't hurt, though there are usually other ways of obtaining a reference to a Context.

Alex Kolpa
  • 187
  • 1
  • 4
0

yes you should avoid from passing information in this method because u can easily cause to memory leak. For passing information between activities u can use the Intent extras. You should put extra in the intent and from the next activity you get the extra. Example: How do I get extra data from intent on Android?

Community
  • 1
  • 1
Liran Peretz
  • 580
  • 8
  • 11