Reading and Writing Settings
Save and observe user preferences.
Reading and Writing Settings is a free Android Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Reading and Writing
With keys defined, the two core operations are reading the data flow and writing with edit. Reads are reactive, writes are suspend functions.
The data Flow
dataStore.data is a Flow<Preferences>. Every time you write, the flow emits a fresh Preferences snapshot, so collectors stay up to date automatically.
val rawData: Flow<Preferences> = dataStore.dataMapping a Single Value
You rarely want the whole Preferences object. Use map to pull out one key and apply a default.
val darkMode: Flow<Boolean> = dataStore.data
.map { prefs -> prefs[PrefKeys.DARK_MODE] ?: false }Handling Read Errors
Disk reads can fail with IOException. Use catch before map to emit empty preferences and keep the flow alive.
val darkMode: Flow<Boolean> = dataStore.data
.catch { e ->
if (e is IOException) emit(emptyPreferences())
else throw e
}
.map { it[PrefKeys.DARK_MODE] ?: false }Writing With edit
To change values you call the edit suspend function. Inside the lambda you receive a mutable preferences object; set keys on it. The whole block is one transaction.
suspend fun setDarkMode(enabled: Boolean) {
dataStore.edit { prefs ->
prefs[PrefKeys.DARK_MODE] = enabled
}
}edit Is a Suspend Function
Because edit suspends, you must call it from a coroutine, typically inside a ViewModel using viewModelScope.launch. This guarantees the write happens off the main thread.
fun toggleDarkMode(value: Boolean) {
viewModelScope.launch {
repository.setDarkMode(value)
}
}Updating Multiple Keys
One edit block can set several keys. They are committed together atomically, so observers never see a partial update.
dataStore.edit { prefs ->
prefs[PrefKeys.USERNAME] = "ada"
prefs[PrefKeys.DARK_MODE] = true
prefs[PrefKeys.FONT_SIZE] = 16
}Incrementing a Value
Inside edit you can read the current value and compute a new one, which is handy for counters like launch counts.
suspend fun incrementLaunchCount() {
dataStore.edit { prefs ->
val current = prefs[PrefKeys.LAUNCH_COUNT] ?: 0
prefs[PrefKeys.LAUNCH_COUNT] = current + 1
}
}Removing and Clearing
Remove a single key with remove, or wipe the store with clear, both inside an edit block.
dataStore.edit { prefs ->
prefs.remove(PrefKeys.USERNAME)
}
dataStore.edit { prefs ->
prefs.clear()
}Collecting in Compose
In a Composable, turn the ViewModel flow into state with collectAsStateWithLifecycle(). The UI recomposes whenever the setting changes.
val darkMode by viewModel.darkMode.collectAsStateWithLifecycle(initial = false)
Switch(
checked = darkMode,
onCheckedChange = { viewModel.toggleDarkMode(it) }
)A Read-Modify-Write Cycle
Putting it together: the UI reads via a flow, the user toggles, the ViewModel launches a coroutine that calls edit, DataStore writes, the flow re-emits, and the UI recomposes. Fully reactive end to end.
Quick Check
Test your understanding of reading and writing.
Recap
You can now read and write settings:
dataStore.datais a reactiveFlow<Preferences>mapextracts a single value,catchhandles I/O errorsdataStore.edit { }writes atomically from a coroutinecollectAsStateWithLifecycledrives the Compose UI
Next: typed structured data with Proto DataStore.
Frequently asked questions
Is the “Reading and Writing Settings” lesson free?
Yes — the full text of “Reading and Writing Settings” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reading and Writing Settings”?
Save and observe user preferences. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reading and Writing Settings” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- DataStore vs SharedPreferences
- Preferences DataStore
- Reading and Writing Settings
- Proto DataStore