Answer the question
In order to leave comments, you need to log in
Cons of Android singletons?
Good afternoon.
Here is the singleton that I am using
public class singleton
{
private static singleton instance;
public static synchronized singleton getInstance() {
if (instance == null) {
instance = new singleton();
}
return instance;
}
}
Answer the question
In order to leave comments, you need to log in
What are its disadvantages, can you give advice on how to properly implement singletonsIt's better not to use singletons, it's an antipattern.
As already mentioned - problems during testing, strong connectivity, it is difficult to maintain such code. There is a desire to store some data there, this is wrong, because the application can be killed, the activity will be restored, but the data will not.
Then there are memory leaks (in the event that references to views or context somehow remain in the singleton).
In your particular implementation, the constant overhead for each access to the instance (synchronization). Better to do double-check or instance holder.
Everything is detailed here.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question