views:

719

answers:

6

Hi,

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?

Thank you.

A: 

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.

RobGThai
I don't think that this might actually be a problem since the DVM manages it's memory pretty well.
Octavian Damiean
+5  A: 

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.

Sean Owen
Confirmed, it'll be retained until your entire process is destroyed. When your process is revived, your singleton will reappear!
Jesse Wilson
A: 

No. Don't do it! Use Google Guice (it works on Android, Java SE, Java EE, GWT, and is used in many Google applications, so you know it definitely works). To get started with Guice, watch the following:

Singleton is a design anti-pattern, and you should consider using dependency injection, instead. These videos will explain why singletons are bad, and how you can avoid them using Guice.

Michael Aaron Safyan
Bear in mind that the core Android team advocates the use of your "anti-pattern", and that Google Guice appears to require about 800KB of JARs, which is bad for a mobile app.
CommonsWare
Guice is great, but it might be overkill for many applications. But if you are going to dive into Guice, take a look at this fine project: http://code.google.com/p/roboguice/
Jesse Wilson
@Jesse, thanks for pointing out RoboGuice.
Michael Aaron Safyan
+2  A: 

In android, is it recommend to use static variable?

No (unless it's primitive finals of course).
If you absolutely need a Singleton, make a custom android.app.Application and store it there.

alex
Hi Alex, could you explain why.I think it is OK to use static variable to pass datahttp://developer.android.com/guide/appendix/faq/framework.htmlI defined some static variables in my Activities to pass data between Activities. Why do I have to define static variable in a custom android.app.Application? Thanks.
A: 

...and if you do go with this approach, the method should be declared "synchronized" unless you can guarantee that the first call won't be made from two different threads simultaneously.

fadden
A: 

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.