How do you stop a value saved in Preferences Datastore from resetting when the app is relaunched in Android Studio?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật