Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SharedPreferences , how to save a simple int variable [duplicate]

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?

like image 887
donparalias Avatar asked Apr 24 '13 14:04

donparalias


People also ask

Is SharedPreferences Singleton?

I've noticed that a lot of projects have their SharedPreferences code scattered all over the project. The reason for this mostly is that fetching SharedPreference and reading/writing preferences as and when needed is the easiest thing to do when writing an app.

How do I save something in SharedPreferences?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Is SQLite better than SharedPreferences?

To give an example, SharedPreferences are useful for storing user preferences, where there are just a handful of variables that need storing. SQLite on the other hand would be better for storing data where there is a large set of items, such as song titles in a music library which need to be searched through.


3 Answers

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

like image 85
Blackbelt Avatar answered Oct 03 '22 20:10

Blackbelt


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)

like image 44
Jani Bela Avatar answered Oct 03 '22 20:10

Jani Bela


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.

like image 32
Chintan Soni Avatar answered Oct 03 '22 21:10

Chintan Soni