On Sun, Oct 31, 2010 at 2:08 PM, greg <sep...@eduneer.com> wrote: > I confess to using an Application subclass to clean up statics in my > application and I would be glad to repent if I knew how. Dianne, is > there a sample application in the SDK (or elsewhere) showing the best > practice for collecting statics in a static singleton? The SDK > documentation about singletons at > http://developer.android.com/resources/faq/framework.html > seems to suggest the use of an Application subclass: "There are > advantages to using a static Singleton, such as you can refer to them > without casting getApplication() to an application-specific class, or > going to the trouble of hanging an interface on all your Application > subclasses so that your various modules can refer to that interface > instead. But, the life cycle of a static is not well under your > control; so to abide by the life-cycle model, the application class > should initiate and tear down these static objects in the onCreate() > and onTerminate() methods of the Application Class." >
Gah I'll get that removed from the documentation. It is just totally wrong to rely on onTerminate. There is just fundamentally no reason to worry about "cleaning up your statics" when the way your process goes away is by killing it. There isn't anything to clean up. And anyway, onTerminate never gets called. Implementing a singleton is not special on Android; you just do it the same way you would elsewhere. Have a static method that returns the singleton: class Singleton { static final Object sLock = new Object(); static final Singleton sInstance; static getInstance() { synchronized (sLock) { if (sInstance == null) { sInstance = new Singleton(); } return sInstance; } } -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en