The problem with using a Singleton class, as well as extending the Application
class, is that if the application process is killed - which is very likely to happen when the app is left too long in background - the object will lose all your data.
However, using Application
class may be a good option in cases when your app is in foreground or does not stay to much in background (although not 100% risk free).
As an alternative, you could save the data into SharedPreferences
, or if your object is more complex, save it in a database
.
Another option would be to combine both the use of Application
, plus SharedPreferences
for example. First try to retrieve the variable from Application
instance, if the variable is null, then retrieve it from SharedPreferences
.