35

I am trying for the last hour to save an integer in my Android application. I read that this can be done using the SharedPreferences. However i dont understand why it seems so confusing to do so.

How can i simply save an int variable ? And then when i run the application again , how can i interact with this variable again?

donparalias
  • 1,834
  • 16
  • 37
  • 60
  • A **simplified approach** would be by using this library: http://github.com/viralypatel/Android-SharedPreferences-Helper ... extended technical details in my [answer here](http://stackoverflow.com/a/35232248/1957401) ... – Viral Patel Feb 06 '16 at 12:29

3 Answers3

66
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", yourIntValue);
editor.commit();

the you can get it as:

 SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
 int myIntValue = sp.getInt("your_int_key", -1);

The SharedPreference interface gives you access to the an xml file, and a easy way to modify it through its editor. The file is stored in /data/data/com.your.package/shared_prefs/ and you can access it onlu through this SharedPreference API

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 2
    hmm can you put some comments to understand whats going on there , cause i dont understand the example? Which is the variable? What is the "your_prefs". What is the 0 next to it. What is the "your_int_key" – donparalias Apr 24 '13 at 14:27
  • 2
    Actually, you also need to provide a default value to `getInt` which is returned when an `int` with the key `your_int_key` could not be found. Something like this: `int myIntValue = sp.getInt("your_int_key", -1);` where -1 is the default value. – zbr Apr 24 '13 at 14:29
  • 2
    I guess that `clearNotificationsSP` in the first example should be just `sp`, right? – Scorchio Feb 12 '14 at 20:51
  • 1
    If you use API 9 or above you should use apply() instead of commit() – Georgi Koemdzhiev Jun 13 '15 at 10:36
11
public void SaveInt(String key, int value){
       sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
       SharedPreferences.Editor editor = sharedPreferences.edit();
       editor.putInt(key, value);
       editor.commit();
}
public void LoadInt(){
       sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
       savedValue = sharedPreferences.getInt("key", 0);
}

If you want to save the variable somewhere, you have to write SaveInt("key", 5); With this you will save the value 5, while the first default value is 0. If you want to load it and use it in another activity, you have to write both of these methods there, and call LoadInt(); where you need the variable. The savedValue is a predefined integer (this needs to be declared everywhere you would like to use the saved variable)

Jani Bela
  • 1,660
  • 4
  • 27
  • 50
7

This is the example of setting Boolean preferences. You can go with Integer also.

SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(this);
            if (!prefs.getBoolean("firstTime", false)) {

                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("firstTime", true);
                editor.commit();
            }

Hope this might be helpful.

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174