
Learn How to Use SharedPreferences in Android
There are times when you want to save some data on your newly built app permanently. Now, you can resort to many third-party libraries but if the collection you want to store is relatively small, then you can use SharedPreferences provided by android. Using SharedPreferences, you can store data to validate a user session or perform some small data manipulations. In Android, the SharedPreferences object points to an xml file that contains data in key-value pairs and provides some unique sets of methods to read and write data to it. Now to make that clear, we’ll work with some simple examples.
Let’s see how to initialize SharedPreferences in Android.
You get the SharedPreferences from the context of the current activity. You can either get the context using getActivity() or you can just use this. We’re using getSharedPreferences() because we want to create our own SharedPreferences file rather than using the default which is invoked using getPreferences().
Context.MODE_PRIVATE in the above piece of code represents that the SharedPreferences we’re going to create will only be accessible from our application only. Now that we have created the SharedPreferences object, let’s see how to use it to store and retrieve data from it.
How to read and write data to SharedPreferences
The SharedPreferences objects provided us with a definitive set of methods that we can use to write data to the SharedPreferences file. To do that, first we need to create a SharedPreferences.Editor by calling the edit() method on the SharedPreferences object like this:
After you have the editor in place, you can use simple methods like putInt(), putString(), putBoolean() etc. and you use them like this:
As we stated at the beginning of the tutorial, SharedPreferences uses key-value pairs, and in the above line of code, there is a key int_key associated with a value someRandomInt. You can also use SharedPreferences to store a StringSet where you can pass a Set to the put method with a key associated to it. After putting the data into the editor, you either commit() the changes or you apply() the changes.
Here is the only difference between these two according to the documentation:
apply() changes the in-memory SharedPreferences object immediately but writes the updates to disk asynchronously. Alternatively, you can use commit() to write the data to disk synchronously. But because commit() is synchronous, you should avoid calling it from your main thread because it could pause your UI rendering.
After you’ve committed your changes, you’d probably want to use that data as your application at some point in time. And to get any data from the SharedPreferences, you don’t need to create an editor. You can just use the methods provided directly on the SharedPreferences object like this:
To get the value, we will use the same key that we used to put the value in the SharedPreferences i.e. int_key. Also, the get methods of the SharedPreferences require a default value in case the method fails to fetch the value or you provide a wrong key for the method. The default value can be anything that you want it to be.
And that is all there is to learn about SharedPreferences in Android. If you look at it, you can use SharedPreferences to store any kind of data, but you should only use it to store a relatively small collection. If you would want to save tons of data on permanent storage, you can use SQLite instead. And we will cover that next time.
If you have questions about SharedPreferences for Android, feel free to contact us at hello@dignitas.digital.