I have a public class that only contains public static members.
I know this is not the best thing to do but I was just wondering why on my Android, if I pause the application, open a few others and come back to mine, all variables seems to be (null).
Questions:
- Is it because of some kind of a memory release made by Android?
- What then could be a better way to keep such variables?
- Is a class that extends Application a good option?
Here is my code:
public class Session {
public static String ID = null;
public static String UNIQID = null;
public static String TOKEN = null;
public static String SESSIONID = null;
}
As your application process might get destroyed any time, those static instances might get garbage collected indeed.
If you put these static variables in a custom Application object, same will apply, unless you initialise them in the application’s onCreate function each time the application gets (re-)created.
You should keep track of persistent data using either SharedPreferences or a SQLite database.
If these variables are too complex to be stored like that then you might want to consider using a singleton (subclassing Application is not as recommended as it used to be).