I have a variable that I would like to save and be able to restore when the viewer opens the app back up. I call this variable, count
private int count=0;
It changes every now and then through out my main activity. How can I save this after editing and changing it and be able to restore it?
Using this...
protected void onResume(){
super.onResume();
SharedPreferences settings = getSharedPreferences(PREFS_COUNT, 0);
count = settings.getInt("count", count);
}
protected void onPause(){
super.onPause();
SharedPreferences settings = getSharedPreferences(PREFS_COUNT, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("count", count);
editor.commit();
}
Lookup SharedPreferences in the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With