Using static variables in Android

JavaAndroidDalvik

Java Problem Overview


In android, are using static variables a recommended practice? E.g, implementing a Singleton pattern in Java, I usually do:

private static A the_instance;
public static A getInstance() {
    if (the_instance == null) {
       the_instance = new A();
    }
    return the_instance;
}

Also, when does this get cleaned up by the Android JVM?

Java Solutions


Solution 1 - Java

static fields are attached to the Class instance as a whole, which is in turn attached to the ClassLoader which loaded the class. the_instance would be unloaded when the entire ClassLoader is reclaimed. I am 90% sure this happens when Android destroys the app (not when it goes into the background, or pauses, but is completely shut down.)

So, think of it as living as long as your app runs. Is Singleton a good idea? People have different views. I think it's fine when used appropriately, myself. I don't think the answer changes much on Android. Memory usage isn't the issue per se; if you need to load a bunch of stuff in memory, that's either a problem or it isn't, regardless of whether you encapsulate the data in a Singleton.

Solution 2 - Java

I think static variables are OK.

This is what Android doc says:

http://developer.android.com/guide/appendix/faq/framework.html

How do I pass data between Activities/Services within a single application?

A public static field/method

An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.

Solution 3 - Java

Contrary to what other people say - it is more than ok. Granted, it has some structure to it. In the official googlesamples/android-architecture repo it is used under todo-mvp-clean (Todo app implementing MVP pattern and following Clean Architecture principles). Check out this file.

What you can see is a lot of static methods referencing singleton getters.

Modern, less error prone and convenient alternative is the Dagger DI framework.

Solution 4 - Java

I'm not sure if such approach is good for mobile platform where you have limited memory available to you. Not to mention that the application will be run on a multi-tasking enabled device.

I think, this approach may hog memory from the device but I have no document to support this. Perhaps someone who's more educated than me can share their thoughts.

Solution 5 - Java

No. Don't do it! http://www.michaelsafyan.com/tech/design/patterns/singleton">Singleton is an anti-patern!. Instead, use dependency injection, whether via a framework (such as via http://google.github.io/dagger/">Dagger</a> or https://github.com/roboguice/roboguice/">Roboguice</a>;) or by explicitly passing the instantiated object.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmichaelView Question on Stackoverflow
Solution 1 - JavaSean OwenView Answer on Stackoverflow
Solution 2 - Javauser256239View Answer on Stackoverflow
Solution 3 - JavaMikelView Answer on Stackoverflow
Solution 4 - JavaRobGThaiView Answer on Stackoverflow
Solution 5 - JavaMichael Aaron SafyanView Answer on Stackoverflow