1

I have a singleton in an activity. When I end my application (like pressing back button), and start it again after some time, the singleton is not recreated, but is holding previous state. Singleton is not destroyed if the application is destroyed? Do I have to null its static members in onDestroy() to avoid memory leak? Thanks.

4
  • 2
    This has nothing to do with Android. Singletons are a Java programming concept. Please read your Java book for more details on singletons, or read javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html Sep 1, 2010 at 15:32
  • This question is very broad and mostly a gimme the codez question. Please explain your problem and what you want to achieve and what you have done already. Stackoverflow is no place to get cheap sample code to stack together something that you don't understand yourself.
    – Janusz
    Sep 1, 2010 at 15:36
  • @i am using singletone class in android,once time the value set to variable,this value did not clear,how can i clear value
    – sivaraj
    Sep 1, 2010 at 16:54
  • @CommonsWare great singleton read thanks for that
    – wired00
    Jun 1, 2011 at 21:45

1 Answer 1

2

Take a look at the attached picture.

Just because the application is no longer visible doesn't mean it has shut down.

I'd have to see code to give you a definitive answer, but before you assume the application is gone, kill the application manually with a task killer or the built-in application manager. Then, see if when you restart it, the singleton is reinitialized.

If killing it manually fixes the problem, you may want to 'uninitialize' the singleton in "onStop()" if you want it to go away when the application leaves the foreground. You can also try adding an "exit" menu function?

If you give me an update either with code or whether the task killer works, I'll see if I can give you a better answer.

(sorry I can't just attach the picture--I'm a new user) http://developer.android.com/images/activity_lifecycle.png

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.