Skip to content Skip to sidebar Skip to footer

Application Class Variables Gets Uninitialised In Android

I have created an android app in which i'm using an application class to store and access the global variables. But i came across a strange behavior that all the variables in my ap

Solution 1:

At last i got it sorted out by saving the variable state using shared preferences and storing the object in application files directory. So if the application recreates at any time (sometimes if the app goes in background) i restore the state of the variables and read the object back. So the variables don't go uninitialised at an point of time.

Storing values,

SharedPreferencespref= getSharedPreferences("appstate", Context.MODE_PRIVATE);
Editoreditor= pref.edit();
editor.putBoolean("flagone", flagOne);
editor.putBoolean("flagtwo", flagTwo);

Retrieving values,

SharedPreferencespref= getSharedPreferences("appstate", Context.MODE_PRIVATE);
flagOne = pref.getBoolean("flagone", true);
flagTwo = pref.getBoolean("flagtwo", false);

Post a Comment for "Application Class Variables Gets Uninitialised In Android"