Using SharedPreferences in Android Jetpack Compose

Using SharedPreferences in Android Jetpack Compose: A Step-by-Step Guide

Chintan Joshi
3 min readJul 10, 2023
Jetpack Compose

Introduction:

Android Jetpack Compose is a modern toolkit for building native Android user interfaces. It provides a declarative approach to UI development, making it easier and more efficient to create interactive and dynamic user interfaces. While the recommended way to handle data persistence in Android Jetpack Compose is through the DataStore API, there might be scenarios where you still need to use SharedPreferences. In this article, we will explore how to integrate SharedPreferences into an Android Jetpack Compose project and effectively use it for data persistence.

Step 1: Add Dependencies : First, let’s add the necessary dependencies to your app’s build.gradle file. Open the build.gradle file and include the following line in the dependencies section:

implementation "androidx.datastore:datastore-preferences:1.0.0"

This will provide the required dependencies to use SharedPreferences in Android Jetpack Compose.

Step 2: Create a PreferencesManager : Next, let’s create a PreferencesManager class that will encapsulate the read and write operations with SharedPreferences. The class will have methods to save and retrieve data from SharedPreferences. Here's an example of a PreferencesManager class:

import android.content.Context
import android.content.SharedPreferences

class PreferencesManager(context: Context) {
private val sharedPreferences: SharedPreferences =
context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)

fun saveData(key: String, value: String) {
val editor = sharedPreferences.edit()
editor.putString(key, value)
editor.apply()
}

fun getData(key: String, defaultValue: String): String {
return sharedPreferences.getString(key, defaultValue) ?: defaultValue
}
}

In the above code, we create a PreferencesManager class that takes a Context as a parameter in the constructor. It initializes a SharedPreferences instance with the desired name and mode. The saveData method saves the data to SharedPreferences, and the getData method retrieves the data associated with the provided key.

Step 3: Use SharedPreferences in Composable: Now that we have our PreferencesManager class, let's integrate it into our Composable function. We can use the remember function to store the PreferencesManager instance and ensure that it retains its value across recompositions. Here's an example of a Composable function that interacts with SharedPreferences:

import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember

@Composable
fun MyComposableFunction() {
val preferencesManager = remember { PreferencesManager(ContextAmbient.current) }
val data = remember { mutableStateOf(preferencesManager.getData("myKey", "")) }

// Use the data variable in your Composable

// Update data and save to SharedPreferences
preferencesManager.saveData("myKey", newDataValue)
data.value = newDataValue
}

In the above code, we create a Composable function called MyComposableFunction. Inside the function, we use the remember function to store an instance of PreferencesManager and ensure it retains its value across recompositions. We also use mutableStateOf to store the data retrieved from SharedPreferences and enable it to be updated and recomposed when necessary. Finally, we can use the data variable in our Composable function to display or manipulate the stored data.

Conclusion:

In this article, we learned how to integrate SharedPreferences into an Android Jetpack Compose project. Although the recommended approach for data persistence in Android Jetpack Compose is through the DataStore API, there might be situations where using SharedPreferences is necessary. By creating a PreferencesManager class and using it in our Composable function, we can effectively save and retrieve data from SharedPreferences. However, it's important to note that DataStore provides improved performance and type safety and is the preferred choice for data persistence in Android Jetpack Compose.

Remember to adapt the code to your specific use case and explore the official Android documentation for more details on SharedPreferences and Android Jetpack Compose.

References:

--

--

Chintan Joshi

Android developer. Proficient in Java/Kotlin. Passionate about creating user-friendly apps. Constantly exploring new tech. Open-source contributor.