Browse By

Working with SharedPreferences – Android

 

In android, SharedPreferences is a way of storing and retrieving data. This allows us to store and retrieve data in the form of key-value pairs. What I mean is, you store a value with a unique key and when you want to retrieve the value you use that particular key to get the saved value.

So what types of values we can store using SharedPreference? We can store primitive data using this class. Like strings, ints, booleans, floats and longs. All the data will be stored on the phone until you remove it.

SharedPreference location – SharedPreference is basically an XML file, which is created automatically. We don’t have to worry about its path or other things. Location of this file is the app’s data folder, which is

 and the default location is

Now to store and retrieve data we need an object of the class SharedPreferences. There are two methods in which we can get the object. These two methods are getPreferences and getSharedPreferences, depending on our app’s requirement we can use any of the two methods.

getPreferences(int mode): – We can use this method if our app’s requirement is only one SharedPreference file. Here mode is who can access the preference file. We don’t need to supply any preference name here because we only use this method if our app requires only one preference file.

getSharedPreferences(String name,int mode) :- We use this method when our app requires more than one preferences. So along with the mode, we provide a name for the preference. Because our preference file will be identified by the name we provide here.

Now coming to modes. There are six modes in SharedPreferences. These are MODE_PRIVATE, MODE_APPEND, MODE_ENABLE_WRITE_AHEAD_LOGGING, MODE_MULTIPROCESS, MODE_WORLD_READABLE, MODE_WORLD_WRITABLE.

MODE_PRIVATE :- When we use this mode, only our app can use the preference file. This is the most secure and recommended way of storing preferences.

MODE_WORLD_READABLE :- If you use this mode, all the apps in your phone can read the preference file. This mode is deprecated in API level 17 or Android 4.2 Jelly Bean.

MODE_WORLD_WRITABLE :- As you can guess from the name, this mode allows other apps to write to the preference file. This method is also deprecated in android 4.2 Jelly Bean or API level 17.

The above two modes are deprecated because using these modes may cause security issue in our app.

MODE_APPEND :- When you write something to an existing file, the older data is erased by the new one. If you don’t want that to happen, you can use MODE_APPEND. What MODE_APPEND does is, instead of erasing the old data, it writes the data to the end of the existing file.

MODE_MULTI_PROCESS :- As you can guess from the name, if you have a preference file, more than one process can modify the file. But this mode is not process safe. That is the reason it got deprecated in Android Marshmallow or API level 23.

MODE_ENABLE_WRITE_AHEAD_LOGGING :-  This is a database open flag. When it is set, by default the database is opened with write-ahead logging.

Now enough of the theory part. Let’s move on to a practical example of how SharedPreferences work.

We are going to make a simple app with two EditTexts, two Buttons and two TextViews.

shared-preferences-android

In this app, when the user clicks the ‘Save‘ button, both the texts from EditText fields are saved in SharedPreferences. After saving the data, when the user clicks ‘Load‘, data is loaded in TextViews.

Save data

We need only one preference, so we are not providing any file name. We are performing save operation here. That means we are going to edit the file. So we need to call edit() method on sharedPreferences to get SharedPreferences.Editor. At last call commit() method on editor. The commit() method returns a boolean value, true or false. If the data is saved successfully it will return true else it will return false.

Get data

Here to load data we are using the KEY which we have provided with the data at the time of saving. You can see we are also passing a double quote. That the is default value. So what is the default value? Suppose without saving any data, someone clicks on ‘Load‘ button. Then the default value is displayed. You can pass any string value as a default value.

Here is the complete MainActivity.java class

Leave a Reply

Your email address will not be published. Required fields are marked *