2

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?

CodingM
  • 350
  • 3
  • 18
lam_gam
  • 31
  • 1
  • 5
  • Using `SharedPreferences` for this would be the way to go – mango Mar 10 '13 at 10:07
  • @itay lael Save the sharedPreferences in `onDestroy` and get the value in `onCreate`... – Ajay S Mar 10 '13 at 10:14
  • @TGMCians: In such cases, you should not be recommending using the `onDestroy()`. There are times when it is never called. Source: http://developer.android.com/reference/android/app/Activity.html#onDestroy() – Siddharth Lele Mar 10 '13 at 10:17

4 Answers4

2

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.

Community
  • 1
  • 1
Rahul
  • 44,383
  • 11
  • 84
  • 103
2

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.

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • Dude i have one more question that i didnt understend....if i put in the SharedPreferences String value how i get my count value from it? and the String value Should be something special or just write in it something? – lam_gam Mar 11 '13 at 17:03
0

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);
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
mmoghrabi
  • 1,233
  • 1
  • 14
  • 23
0

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();
    }
}
madlymad
  • 6,367
  • 6
  • 37
  • 68