2

I have a question: I like to use the singleton pattern to create a MainController which handle all my data of the app.

Is this a bad practice in android or do professional software developers do it equal?

opalenzuela
  • 3,139
  • 21
  • 41
gurehbgui
  • 14,236
  • 32
  • 106
  • 178
  • 1
    singleton is a design pattern. Why should it a "bad practice" ? – Blackbelt Oct 28 '13 at 13:23
  • @gurehbgui why do you feel it bad? – TheFlash Oct 28 '13 at 13:24
  • @blackbelt: Because some people consider it an anti-pattern. –  Oct 28 '13 at 13:26
  • @Poldie Any pattern can be misused. As is, the singleton pattern describes a state of facts in a real world so it's not an antipattern per se. – Alexander Kulyakhtin Oct 28 '13 at 13:33
  • @Alex An anti-pattern is just a pattern which ends up being considered counterproductive; what the pattern is doing/modelling isn't relevant, is it? –  Oct 28 '13 at 13:44
  • Singleton is indeed an antipattern in Android. The application can be killed and restarted at any time (e.g: memory pressure). Any data kept in the Singleton would be lost. Singleton is fine if it is stateless. Any state needs to be saved in a bundle otherwise. – JonathanC Oct 04 '18 at 05:14

2 Answers2

7

Avoid using activity context in singleton. It prevents garbage collector to clear links as for that activity. Remember that android can recreate your application any time, so be ready that your singleton would lose its state. saveInstanceState/restoreInstanceState helps in these situations.

Kayhan Asghari
  • 2,817
  • 1
  • 28
  • 47
resource8218
  • 1,445
  • 1
  • 20
  • 33
  • This is a great answer. I will only add that if your Singleton is stateless then it's fine to use it. Otherwise you may want to find another way to easily save your state in a bundle at the saveInstanceState time. There is no blanket "one size fits all" solution. – JonathanC Oct 04 '18 at 05:17
1

As mentioned, Singleton pattern is a design pattern so there is no problem in using it.

However, keep in mind that an application may be killed at anytime and therefore your singleton are killed as well. You should be able to re construct your class.

For instance, I usually use a static get(Context) instead of get() so that I can use this context to instantiate my singleton from SharedPreferences or Files in case it is not there.

Anyway, singleton is a design pattern that can be used on Android.

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • To add to this answer, check this link for an exhaustive treatment of the singleton pattern: http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton – DntFrgtDSemiCln Oct 28 '13 at 13:30
  • It is naive to use a design pattern in contexts where it is not meant to be used. Android is often that situation for Singleton pattern. With a stateful Singleton (as mentioned in the answer) you will have to find a way to persist and restore the state. This can make things more error prone, less readable and maintainable, and less testable. At the very least, use a Singleton that is injected. But I highly recommend using a different approach most of the time. – JonathanC Oct 04 '18 at 05:22