0

My app makes use of a singleton object implemented like this

enum MySingleton extends Something{

    INSTANCE{}

    <a bunch of attributes declared here>

    public void method1();

    public void method2();

    (...)

}

What I've watched is after my app is closed and I run the app again, my singleton attributes weren't reset

Because I'm using this pattern's implementation from Java 6 (if I'm not wrong), can I say this is equivalent to the private static singleton implementation from the previous Java releases, and in this I have to manually reset his state inside app's onDestroy()?

  • "What I've watched is after my app is closed and I run the app again, my singleton attributes weren't reset" -- that is because [your process was not terminated](https://developer.android.com/guide/components/processes-and-threads.html#Processes). "I have to manually reset his state inside app's onDestroy()?" -- an "app" does not have `onDestroy()`. – CommonsWare Sep 24 '16 at 18:19
  • As app I mean app's first activity, in my case MapsActivity, where my SupportMapFragment is – Tadeu Arias Villares Sep 24 '16 at 18:23
  • In Android, the lifecycle of activities and the lifecycle of processes are not related. When activities come and go is independent of when processes come and go. – CommonsWare Sep 24 '16 at 18:29
  • "my singleton attributes weren't reset" I'd just like to point out that mutable state in an enum is [a pretty bad idea](http://stackoverflow.com/q/540293/3788176). – Andy Turner Sep 24 '16 at 18:33
  • @CommonsWare, got it =) – Tadeu Arias Villares Sep 24 '16 at 18:37
  • @Andy, so after Java 6 the main idea about implementing a singleton using enum is for stateless singletons? In the case of stateful singletons should I use the old way to implement it? – Tadeu Arias Villares Sep 24 '16 at 18:43
  • @TadeuAriasVillares I don't know what you mean by "the old way", but you shouldn't use enums for this - and I would also suggest [you might not want to use a singleton, really](http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons). – Andy Turner Sep 24 '16 at 18:47
  • Thanks for your support guys =) – Tadeu Arias Villares Sep 24 '16 at 18:53
  • Btw what's the best alternative for singleton? – Tadeu Arias Villares Sep 24 '16 at 19:30

0 Answers0