-2

I have 2 int in my navigation drawer, the value of whom changes upon clicking different button on different locations in the app.

I got the code to successfully increment and update them, but the problem is that they got reset when I open the app after closing or exiting it.

How can I make them stay there after getting updated?

If you want any code from my app, then please tell me.

Sorry for bad formatting of the question, but I have no idea how to do this and hence I haven't posted any code.

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133

2 Answers2

1

You must save the info in a persistent storage.

You can use SharedPreferences.

SharedPreferences prefs= getSharedPreferences("aName", MODE_PRIVATE);
 //save the value   
        prefs.edit()
                .putInt("nameOfTheValue", theValue).apply();        
        // get the data       
        prefs.getInt("nameOfTheValue", aDefaultValue);
giannisf
  • 2,479
  • 1
  • 17
  • 29
0

You should save them as User SharedPreferences in onDestroy method.

  public void onDestroy() {
     super.onDestroy();
     SharedPreferences settings;
     settings = getSharedPreferences("TWO_INT_SAVING", Context.MODE_PRIVATE);
     //set the sharedpref
      Editor editor = settings.edit();
      editor.putInt("FIRST_INT", firstIntValue);
      editor.putInt("SECOND_INT", secondIntValue);
      editor.commit();
    }

And then you can get them back wen needed:

  SharedPreferences settings;
  settings = getSharedPreferences("TWO_INT_SAVING", Context.MODE_PRIVATE);

  //get the sharepref
  int firstInt = settings.getInt("FIRST_INT", 0);
  int secondInt = settings.getInt("SECOND_INT", 0);
LHIOUI
  • 3,287
  • 2
  • 24
  • 37