Article image Shared Preferences in Android

27. Shared Preferences in Android

Page 27 | Listen in audio

When developing Android applications, one of the most common tasks is to manage user preferences and settings. In many cases, these preferences need to be stored across app sessions, which requires persistent storage. Android provides a lightweight mechanism for storing key-value pairs of primitive data types called Shared Preferences. This system is particularly useful for saving simple data, such as user settings, application states, or any configuration details that need to persist even after the application is closed.

Shared Preferences are stored in XML files within the app's private storage directory. Each application has its own set of preferences, which means that data stored in Shared Preferences is not accessible by other applications, ensuring data security and privacy. Shared Preferences offer a simple interface to store and retrieve data in the form of key-value pairs.

Using Shared Preferences in Kotlin

To use Shared Preferences in a Kotlin-based Android application, you typically follow these steps:

  1. Get the SharedPreferences Object: You can obtain a SharedPreferences instance by calling the getSharedPreferences() method, passing the name of your preference file and the mode (usually Context.MODE_PRIVATE).
  2. Read and Write Data: Use the get and put methods to read and write data. SharedPreferences supports various data types, including String, int, boolean, float, and long.
  3. Commit Changes: To save changes, you can use either apply() or commit() on the SharedPreferences.Editor object. The apply() method saves changes asynchronously, while commit() does so synchronously and returns a boolean indicating success.

Example Code

Below is a simple example of how to use Shared Preferences in a Kotlin-based Android application:


val sharedPreferences = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE)

// Writing data
val editor = sharedPreferences.edit()
editor.putString("username", "JohnDoe")
editor.putInt("user_age", 28)
editor.putBoolean("is_premium_user", true)
editor.apply()

// Reading data
val username = sharedPreferences.getString("username", null)
val userAge = sharedPreferences.getInt("user_age", 0)
val isPremiumUser = sharedPreferences.getBoolean("is_premium_user", false)

In this example, we create a Shared Preferences file named MyPreferences. We then write a string, an integer, and a boolean to the preferences. Finally, we read these values back, providing default values in case the data does not exist.

Advanced Usage of Shared Preferences

While the basic usage of Shared Preferences is straightforward, there are some advanced techniques and considerations that can enhance your implementation:

Preference Change Listener

You can register a listener to monitor changes to the Shared Preferences. This is useful if your app needs to respond to changes in the preferences dynamically. Here’s how you can implement a preference change listener:


val preferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
    when (key) {
        "username" -> {
            // Handle username change
        }
        "user_age" -> {
            // Handle user age change
        }
        "is_premium_user" -> {
            // Handle premium user status change
        }
    }
}

override fun onResume() {
    super.onResume()
    sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener)
}

override fun onPause() {
    super.onPause()
    sharedPreferences.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener)
}

In this snippet, we define a listener that reacts to changes in specific keys. We register the listener in the onResume() method and unregister it in onPause() to ensure it only listens when the activity is active.

Security Considerations

While Shared Preferences are private to the app, it’s important to note that they are stored in plain text. If your app handles sensitive information, such as passwords or personal data, consider encrypting the data before storing it in Shared Preferences. Android provides the EncryptedSharedPreferences class, which offers an easy way to encrypt and decrypt data.

Migrating to EncryptedSharedPreferences

To migrate to EncryptedSharedPreferences, you need to include the necessary library in your build.gradle file and then modify your SharedPreferences usage as follows:


// Add dependency in build.gradle
implementation "androidx.security:security-crypto:1.1.0-alpha03"

// Use EncryptedSharedPreferences
val masterKeyAlias = MasterKey.Builder(applicationContext)
    .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
    .build()

val encryptedSharedPreferences = EncryptedSharedPreferences.create(
    applicationContext,
    "MyEncryptedPreferences",
    masterKeyAlias,
    EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
    EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)

// Write and read encrypted data
val editor = encryptedSharedPreferences.edit()
editor.putString("secure_username", "EncryptedJohnDoe")
editor.apply()

val secureUsername = encryptedSharedPreferences.getString("secure_username", null)

This setup ensures that your data is encrypted using the AES256 encryption standard, providing an additional layer of security.

Best Practices

When using Shared Preferences, consider the following best practices to ensure efficient and secure usage:

  • Use the appropriate data type: Always use the most suitable data type for your values to optimize storage and retrieval efficiency.
  • Avoid storing large datasets: Shared Preferences are not designed for large datasets. Use them for small configuration settings and lightweight data.
  • Encrypt sensitive data: Always encrypt sensitive information using EncryptedSharedPreferences or other encryption methods.
  • Use default values wisely: When reading values, always provide sensible default values to handle cases where the data might not exist.
  • Manage preference keys: Keep your preference keys organized and consistent to avoid conflicts and errors.

By following these practices, you can effectively leverage Shared Preferences in your Android applications, providing a seamless and secure user experience.

In conclusion, Shared Preferences in Android offer a simple yet powerful way to manage user settings and application states. With careful implementation and consideration of security practices, they can be an invaluable tool in your Android development toolkit.

Now answer the exercise about the content:

What is the primary purpose of using Shared Preferences in Android applications?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Working with SQLite Databases

Next page of the Free Ebook:

28Working with SQLite Databases

10 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text