Skip to content
Advertisement

Is it bad practice to keep data in static variables?

In an Android application, is it bad practice to store objects in static fields in these cases?

  1. Application data. Is it bad to keep application data in static variables in a class while the application is running? Currently, I’m storing the data in an instance variable in my Application class. Then classes that need the data can obtain the data from the Application.
  2. Context‘s etc. Is it bad practice to store a Context (e.g. a reference to an Activity or an Application) in a static field? This can be used in a class that needs e.g. a LayoutInflater or resources. Currently, I am passing the Contexts to methods needing them as arguments.

Advertisement

Answer

Yes and Yes. 🙂

Static Fields. There are a lot of problems with excessive usage of Static fields. Not only they are slower to access by an interesting margin, but also they are prone to be destroyed overnight by Android, and it’s usually hacky to check for their references all over the place or fill your getter/setters with if (sSomeStatic == null) { return new SomeStatic()}. It’s ok to store a static reference to a class called (for example) ApplicationData where you store some values, hey, we NEED some globals every now and then, but it’s so easy to abuse it, that I frown every time I inspect new Android devs’ source code.

Yes, store your Application instance in a singleton pattern and use it, but don’t add 200 static fields to your Application implementation just because you can do YOURAPP.getInstance().SomeLazyValueYouAddedHere();

That is bad. It leads to bad practices and it will be slower than having a good design where you access hard references.

I could go on forever but there are a lot of StackOverflow discussions (some heated!) about this. If you are here, I’m assuming you’re asking for experience; I’ve been doing Android for a couple of years in different projects and my experience has always been that the less Static, the merrier.

Now the context… oh the Context. Don’t store the Context in a hard reference, ever. Or you will leak memory. An activity has references to View and a multitude of other things. If you store the Context, you’re storing the activity and things go bad from there. Learn to pass the Context around, use the Application Context whenever possible and if you need to pass it around, do it for very good reasons. Most of the time the App context is enough for getting resources, strings, etc. If you’re going to store the Context, always store context.getApplicationContext(); Never store a static activity context. You can google this too and StackOverflow has some good answers.

If you can afford one and only one Android book, get the BNR one. Even though Android may release new SDKs every now and then, the concepts are completely valid and the patterns the author uses are the right way to deal with Activities, Contexts, Fragments, etc.

UPDATE Your Application should look like this:

public class YourApp extends Application {
   private static YourApp sInstance;
   public YourApp() {
      super();
      sInstance = this;
   }
   public static YourApp getInstance() {
      return sInstance;
   }
}

And in that case, yes, you are getting the same Static reference to the same App Context.

Advertisement