-1

Suppose i have an integer i and whenever I press a button, the value of i increases by 1. I want the app the save the value of i when the user exits the app and when the user opens the app again, the number displayed should be the one which was saved.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

Next time you should search first: save variables after quitting application?

To your question. There is something called SharedPreferences in Android, to save data like your value.

Get the variable in your onCreate with

SharedPreferences mPrefs = getSharedPreferences("label", 0);
int mInt = mPrefs.getInteger("tag", 0); // 0 = value if var not found

and later edit the variable with

SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.putInteger("tag", value_of_var).commit();

in this case "tag" is your variable name, the name is not important as long it is the same while saving and editing.

Community
  • 1
  • 1
Luca Schimweg
  • 747
  • 5
  • 18