S
S
swba2015-05-29 08:44:18
Android
swba, 2015-05-29 08:44:18

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

2 answer(s)
E
Ernesto Guevara, 2015-05-29
@guevara

Singletons vs. Application Context in Android?

O
OnYourLips, 2015-05-29
@OnYourLips

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 question

Ask a Question

731 491 924 answers to any question