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?
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?
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.
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.