Extending application class and good practice

I was recently informed that extending the class of applications to use as Singleton was bad practice, but without any explanation.
So what are the potential problems associated with using this class? I saw how it was used in many projects.

Also, if using an application class is a bad idea, what are the alternatives for storing application level variables?

+5
source share
2 answers

The problem with using the Singleton class, as well as the class extension Application, is that if the application process is killed, which is very likely when the application remains too long in the background, the object loses all your data.

However, using a class Applicationcan be a good option in cases where your application is in the foreground or does not remain in the background (although not 100% without risk).

Alternatively, you can save the data to SharedPreferences, or if your object is more complex, save it to database.

Another option is to combine as use Application, plus SharedPreferences, for example. First, try extracting the variable from the instance Applicationif the variable is null, and then extracting it from SharedPreferences.

+2

Singleton , , , , - .

, / , Application AndroidManifest.xml , ( ) , , .

In addition, you can use your application class as Singleton, as it will be created only once at startup.

+7
source

All Articles