lam_gam
lam_gam

Reputation: 31

How to save an Integer's value after the app closes?

I have an Integer on screen set to 100. When I click a button, that value goes down by one (99). However, when I restart the app, how can I get the same value as before which is 99, without it resetting to 100?

Upvotes: 2

Views: 3756

Answers (4)

mmoghrabi
mmoghrabi

Reputation: 1291

You need to use SharedPreferences in this way: saving the value and then get it again

private final String NUMBER = "Number";
private final String PROFILE = "Profile";

SharedPreferences a = FirstActivity.this.getSharedPreferences("a", MODE_PRIVATE);
SharedPreferences.Editor prefsEditorProfiles = a.edit();
prefsEditorProfiles.putInt(Profile, 1);
prefsEditorProfiles.putInt(Number, 1);
prefsEditorProfiles.commit();

Then restore SharedPreferences in other Activity:

SharedPreferences a = SecondActivity.this.getSharedPreferences("a", MODE_PRIVATE);
int ab = a.getInt(Number, 0);

Upvotes: 0

Siddharth Lele
Siddharth Lele

Reputation: 27748

You can do something like this:

In the onPause(), use this code to save the counter's value to a SharedPreference file

SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putInt(KEY_NAME, THE_INTEGER_VALUE);
// Replace `putInt` with `putString` if your value is a String and not an Integer.
editor.commit();
  1. Replace the PREFERENCE_FILE_NAME used above to choose the XML file that will be created to store the value in.
  2. KEY_NAME used above is the KEY that will be used to access (for saving and reading from the SharedPreference file named in point 1.) It is part of the Key-Value pair used in SharedPreferences.
  3. THE_INTEGER_VALUE is the actual value.

And in the onResume(), you can retreive the value back and display it:

SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
int counter = sharedPrefs.getInt(KEY_NAME, 0);
// Replace the `int counter` with `String counter` if your value is a String and not an Integer.
// Also, replace the `getInt` with `getString`

You can use the int counter later to display in a TextView perhaps.

Upvotes: 2

madlymad
madlymad

Reputation: 6530

Use share preference as the doc example changes to read/write int value:

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       int lastIndex = settings.getInt("yournumbername", 100);
       setLastIndex(lastIndex);
    }

    @Override
    protected void onStop(){
       super.onStop();

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putInt("yournumbername", mlastIndex);

      // Commit the edits!
      editor.commit();
    }
}

Upvotes: 0

Rahul
Rahul

Reputation: 45060

You can use SharedPreferences to achieve what you want. Set the count, while exiting the app and when re-opening it, fetch it from there.

For example on how to go about using it, check this out.

Upvotes: 2

Related Questions