How to use SharedPreferences in Android to store, fetch and edit values
Shared Preferences in Android provide a way to store and retrieve key-value pairs. It is commonly used to store small amounts of data that need to persist across application sessions. In this article, we will explore how to use SharedPreferences to store, fetch, and edit values in Android.
What is SharedPreferences?
SharedPreferences is an interface provided by Android, which allows you to save and retrieve persistent key-value pairs of primitive data types. It is a way to store simple data in key-value pairs similar to a Map. The data stored using SharedPreferences remains even if the app is closed or the device is restarted.
Getting started with SharedPreferences
To start using SharedPreferences, follow the steps below:
- Create an instance of SharedPreferences using the getSharedPreferences() method:
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
The first parameter of getSharedPreferences() is a unique name for the shared preferences file, and the second parameter is the mode. The MODE_PRIVATE mode ensures that the shared preferences are private and can only be accessed by the calling application.
- Use the SharedPreferences.Editor class to modify the shared preferences:
SharedPreferences.Editor editor = sharedPreferences.edit();
The SharedPreferences.Editor class provides methods to add or edit key-value pairs in the shared preferences.
Storing values in SharedPreferences
To store a value in SharedPreferences, you need to use the put
For example, to store a string value:
editor.putString("name", "John");
editor.apply();
In the above code snippet, we are storing a string value with the key "name". The apply() method is used to save the changes to the shared preferences file.
Fetching values from SharedPreferences
To retrieve a value from SharedPreferences, you can use the get
For example, to retrieve the string value we stored earlier:
String name = sharedPreferences.getString("name", "");
In the above code snippet, the getString() method is used to retrieve the value associated with the key "name". The second parameter is the default value to be returned if the key is not found in the shared preferences.
Editing values in SharedPreferences
To edit a value in SharedPreferences, you can simply store a new value using the same key. The old value will be replaced with the new one.
For example, to edit the name value:
editor.putString("name", "Tom");
editor.apply();
In the above code snippet, we are storing a new string value with the key "name", effectively editing the existing value.
Deleting values from SharedPreferences
To delete a value from SharedPreferences, you can use the remove() method of the SharedPreferences.Editor class.
For example, to delete the name value:
editor.remove("name");
editor.apply();
In the above code snippet, we are removing the value associated with the key "name". The apply() method is used to save the changes to the shared preferences file.
Conclusion
SharedPreferences in Android provides a convenient way to store, fetch, and edit key-value pairs of primitive data types. It is easy to use and can persist data across application sessions. By following the steps outlined in this article, you can effectively use SharedPreferences in your Android applications to store data.