Using static variables in Android

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.

Leave a Comment