Introduction
Shared Preferences is an Android feature that allows you to store small amounts of data in a persistent manner. It provides a simple key-value storage system that saves data as key-value pairs. The data is stored in a XML file within the app’s internal storage, which is private to the app and cannot be accessed by other apps. The data stored in Shared Preferences is available even after the app is restarted or closed.
Shared Preferences is best suited for storing small amounts of data that needs to be shared between different activities or services within the same app. For larger amounts of data, it is recommended to use a database such as SQLite or Room.
Setting Up Shared Preferences in Kotlin
To use Shared Preferences in an Android app, you first need to get an instance of SharedPreferences using the getSharedPreferences() method of the Context class. The getSharedPreferences() method takes two arguments: the name of the Shared Preferences file and the mode in which the data will be stored.
Here’s an example of how you can get an instance of SharedPreferences in Kotlin:
In this example, we are getting an instance of Shared Preferences with the file name “app_preferences” and the mode set to MODE_PRIVATE. This means that the data will be stored in a private file that can only be accessed by the app that created it.
Storing Data in Shared Preferences
Once you have an instance of Shared Preferences, you can use the edit() method to start editing the data. The edit() method returns an instance of SharedPreferences.Editor, which you can use to add, update or remove data from the Shared Preferences file.
Here’s an example of how you can store a simple string in Shared Preferences:
In this example, we are storing a string value with the key “username” and the value “John Doe”. To save the data, we call the apply() method on the editor. The apply() method saves the data asynchronously, so it does not block the main thread of the app.
Reading Data from Shared Preferences
Reading data from Shared Preferences is very simple. You can use the get*() methods of SharedPreferences to retrieve the data based on the type of data you have stored.
Here’s an example of how you can retrieve a string value from Shared Preferences:
In this example, we are retrieving the value stored under the key “username”. If the key does not exist, the method will return the default value, which in this case is an empty string.
Removing Data from Shared Preferences
To remove data from Shared Preferences, you can use the remove() method of the SharedPreferences.Editor class.
Here’s an example of how you can remove a key-value pair from Shared Preferences:
In this example, we are removing the key-value pair with the key “username” from the Shared Preferences file.
Using Shared Preferences with Classes
In a real-world app, it is often useful to store data in Shared Preferences as objects rather than simple key-value pairs. To do this, you need to serialize the objects into strings before storing them in Shared Preferences and deserialize them back into objects when retrieving the data.
One way to achieve this is by using the Gson library, which is a popular Java library for converting Java objects to and from JSON. With Gson, you can convert a Java object to a JSON string and vice versa, making it easy to store objects in Shared Preferences.
Here’s an example of how you can store and retrieve a User class in Shared Preferences using Gson:
In this example, we are creating a User class with a name and an age. We then convert the User object to a JSON string using the toJson() method of the Gson class. We store the JSON string in Shared Preferences under the key “user”. When retrieving the data, we get the JSON string from Shared Preferences and convert it back into a User object using the fromJson() method of the Gson class.
Conclusion
Shared Preferences is a useful feature in Android for storing small amounts of data that persist even after the app is closed or restarted. It provides a simple key-value storage system that is easy to use and efficient for storing simple data. With the use of libraries like Gson, you can store complex objects in Shared Preferences, making it even more versatile. By following the steps outlined in this article, you can easily incorporate Shared Preferences into your Android app using Kotlin programming language.
val sharedPreferences = getSharedPreferences("app_preferences", Context.MODE_PRIVATE)
In this example, we are getting an instance of Shared Preferences with the file name “app_preferences” and the mode set to MODE_PRIVATE. This means that the data will be stored in a private file that can only be accessed by the app that created it.
Storing Data in Shared Preferences
Once you have an instance of Shared Preferences, you can use the edit() method to start editing the data. The edit() method returns an instance of SharedPreferences.Editor, which you can use to add, update or remove data from the Shared Preferences file.
Here’s an example of how you can store a simple string in Shared Preferences:
val editor = sharedPreferences.edit()
editor.putString("username", "John Doe")
editor.apply()
In this example, we are storing a string value with the key “username” and the value “John Doe”. To save the data, we call the apply() method on the editor. The apply() method saves the data asynchronously, so it does not block the main thread of the app.
Reading Data from Shared Preferences
Reading data from Shared Preferences is very simple. You can use the get*() methods of SharedPreferences to retrieve the data based on the type of data you have stored.
Here’s an example of how you can retrieve a string value from Shared Preferences:
val username = sharedPreferences.getString("username", "")
In this example, we are retrieving the value stored under the key “username”. If the key does not exist, the method will return the default value, which in this case is an empty string.
Removing Data from Shared Preferences
To remove data from Shared Preferences, you can use the remove() method of the SharedPreferences.Editor class.
Here’s an example of how you can remove a key-value pair from Shared Preferences:
val editor = sharedPreferences.edit()
editor.remove("username")
editor.apply()
In this example, we are removing the key-value pair with the key “username” from the Shared Preferences file.
Using Shared Preferences with Classes
In a real-world app, it is often useful to store data in Shared Preferences as objects rather than simple key-value pairs. To do this, you need to serialize the objects into strings before storing them in Shared Preferences and deserialize them back into objects when retrieving the data.
One way to achieve this is by using the Gson library, which is a popular Java library for converting Java objects to and from JSON. With Gson, you can convert a Java object to a JSON string and vice versa, making it easy to store objects in Shared Preferences.
Here’s an example of how you can store and retrieve a User class in Shared Preferences using Gson:
class User(val name: String, val age: Int)
val user = User("John Doe", 30)
val gson = Gson()
val userJson = gson.toJson(user)
val editor = sharedPreferences.edit()
editor.putString("user", userJson)
editor.apply()
val userJson = sharedPreferences.getString("user", "")
val user = gson.fromJson(userJson, User::class.java)
In this example, we are creating a User class with a name and an age. We then convert the User object to a JSON string using the toJson() method of the Gson class. We store the JSON string in Shared Preferences under the key “user”. When retrieving the data, we get the JSON string from Shared Preferences and convert it back into a User object using the fromJson() method of the Gson class.
Conclusion
Shared Preferences is a useful feature in Android for storing small amounts of data that persist even after the app is closed or restarted. It provides a simple key-value storage system that is easy to use and efficient for storing simple data. With the use of libraries like Gson, you can store complex objects in Shared Preferences, making it even more versatile. By following the steps outlined in this article, you can easily incorporate Shared Preferences into your Android app using Kotlin programming language.