Whenever I relaunch an app in Android Studio, values that are saved in Preferences Datastore are reset.
In particular, I’m trying to save whether the user selected light mode or dark mode for their preferred background theme. However it doesn’t maintain when the app is relaunched.
I’m using a boolean to determine the background theme and then save it using Preferences Datastore.
Here is the datastore class that holds the value. It’s called “ThemeDatastore”
import android.content.Context
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
private val Context.dataStore by preferencesDataStore("ThemeDatastore")
class ThemeDatastore(private val context: Context) {`
Here is the key that holds the boolean that determines if the background theme is light or dark
`val themeKey = booleanPreferencesKey("Theme")
Here is the function that loads the preferred background theme. It takes a boolean preferences key as a parameter.
fun loadTheme(key: Preferences.Key<Boolean>): Flow<Boolean> = context.dataStore.data.map { preferences ->
preferences[key] ?: false
}
Here is the function that saves the preferred background theme. It takes a boolean preferences key and a boolean variable as parameters.
suspend fun saveTheme(key: Preferences.Key<Boolean>, data: Boolean) {
context.dataStore.edit { preferences ->
preferences[key] = data
}
}
Here is the code in Main Activity
This is the instance of “ThemeDatastore”. It has the current local context in the context parameter field.
val themeDatastore = ThemeDatastore(LocalContext.current)
Here is the variable that retrieves the preferred background theme. It is initially set to false.
val savedTheme = themeDatastore.loadTheme(key = themeDatastore.themeKey).collectAsState(initial = false)
Here is the boolean variable that determines the background theme. if false, light mode is active, if true, dark mode is active. It is initially set to false.
var isDarkModeActive by remember { mutableStateOf(false) }
Here is the function that calls the function from “ThemeDatastore” the saves the preferred background theme.
fun saveTheme(){
CoroutineScope(Dispatchers.IO).launch {
themeDatastore.saveTheme(themeDatastore.themeKey, isDarkModeActive)
}
}
Here is the app layout. Is is a box that takes up the entire screen. The background color animates to dark gray if “isDarkModeActive” and “saveTheme.value” are both equal to true.
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.background(
color = animateColorAsState(
targetValue = if (isDarkModeActive && savedTheme.value)
Color.DarkGray else Color.White
).value
)
.fillMaxSize()
){
In the center of the screen there is a button. When the button is pressed, “isDarkModeActive” is toggled between true and false, and the saveTheme function is triggered.
Button(onClick = {
isDarkModeActive = !isDarkModeActive
saveTheme() }) {
Text(text = "Toggle Theme")
}
I tried setting the initial value of the “savedTheme” variable to true. It didn’t work.
I don’t know what to try next. Thanks.