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.
Asked
Active
Viewed 1,655 times
-1
-
1You can use SharedPreferences http://developer.android.com/training/basics/data-storage/shared-preferences.html – Dheerendra Jeevani May 16 '16 at 15:41
1 Answers
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