A
A
Alexander2015-02-16 17:23:18
Java
Alexander, 2015-02-16 17:23:18

Singleton Andoid how right?

The application uses Singleton to store data received from the network and displayed on different Activities, I found information that if the Activity is not currently active, then the link to it is not required to be stored and the object can be deleted by the GC, but what if no one will refer on Singleton, it will also be removed. I found an implementation through inheritance from the Application class, but this causes inconvenience, because Application is only accessible from Activity. I made a regular Singleton, and in the Application heir I initialized a link to the Singleton so that the GC would not get there. Actually the question is, how much is it correct and workable?

public class Singleton{
}
public static synchronized Singleton getInstance(){
...
}

public class MyApp extends Application{
private Singleton = Singleton.getInstance();
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anyd3v, 2015-02-16
Obiedkov @aobiedkov

What does your singleton depend on? if from context then init in Application#onCreate if not then
www.oracle.com/technetwork/articles/java/singleton...
stackoverflow.com/questions/17799976/why-is-static...

O
Oleg Gamega, 2015-02-16
@gadfi

look in the direction of dagger, I looked at examples with it ─Singleton is not a problem, it injects without problems, it was even injected into the adapter in samples, I’m not sure if this is the best choice, I’m just starting to work with dagger myself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question