Answer the question
In order to leave comments, you need to log in
Singleton or variable in Application: which is better?
If I need a global object, access to which is necessary from any activity (and only from them), then there are (at least) two ways to do it:
1) Inherit from Application and declare the corresponding variable inside this child class, initialized in onCreate () .
2) Make the required class a singleton and also initialize it in onCreate() of the successor from Application.
Apart from purely aesthetic preferences, are there any objective pros and cons of the first and second methods?
Answer the question
In order to leave comments, you need to log in
Singleton is an anti-pattern: it creates a strong connection between components and masks component dependencies.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question