0Pricing
Android Academy · Lesson

Preferences DataStore

Store key-value settings reactively.

Preferences DataStore

Preferences DataStore stores simple key-value pairs, just like SharedPreferences, but with the modern async API. It does not enforce a schema or type-safety on the whole object.

You access values through typed keys.

Creating the DataStore

You create a single DataStore instance per file using the preferencesDataStore property delegate at the top level of a Kotlin file. The name becomes the on-disk filename.

import androidx.datastore.preferences.preferencesDataStore
import androidx.datastore.preferences.core.Preferences

val Context.dataStore: DataStore<Preferences> by
    preferencesDataStore(name = "settings")

All lessons in this course

  1. DataStore vs SharedPreferences
  2. Preferences DataStore
  3. Reading and Writing Settings
  4. Proto DataStore
← Back to Android Academy