Saving Variables (Android)

I have a variable that I would like to save and can restore when the viewer opens the application. I call this variable, count

private int count=0;

He changes from time to time to my main activity. How to save it after editing, change it and restore it?

+3
source share
2 answers

Search SharedPreferencesthe documentation.

+3
source

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();
}
+4
source

Source: https://habr.com/ru/post/1769836/


All Articles