9

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.
2
  • It's bad practice generally to modify static variables at all. Commented Nov 4, 2013 at 18:03
  • It is perfectly okay to store a static reference to your Application instance. Commented Nov 4, 2013 at 18:06

1 Answer 1

19

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.

Sign up to request clarification or add additional context in comments.

10 Comments

A good post for further reading on the Context is this one from Dave Smith at Double Encore. The table at the end is particularly useful; knowing which types of context you should use to inflate layouts is nice.
Thanks for the nice answer. Just adding, I am currently saving all my data in a sigle object's fields. The object is centrally stored in my Application so it can be read by any activity, but the activities then store a reference to it in a non-static member.
Well it's not bad to have it that way. Just remember that the object may be null overnight (or even after a few minutes of your app in the back) so be prepared to restore the Data. Some devices obliterate your app's heap minutes after being stopped in the background. That being said, storing a reference to YourApplication.getInstance().getYourAppData() is not bad, just don't throw everything in there, use it wisely :)
Is it different to use ApplicationClass.getInstance() and anActivity.getApplication() (which I am using currently)?
@amitav13 nope, I just have the experience. When android runs low on memory, things get cleared and I've seen Static references pointing to null. I didn't investigate much, but I simply rearchitected it around not having to use static stuff :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.