DataStore vs SharedPreferences
Why DataStore is the modern choice.
Storing Small Settings
Almost every app needs to remember small bits of data: a dark-mode toggle, the last-used tab, a username. These are key-value preferences, not big relational data.
For years Android used SharedPreferences for this. Today the recommended solution is DataStore.
What SharedPreferences Is
SharedPreferences is a simple XML-backed key-value store. You get an editor, put values, and commit them.
- Easy to use
- Built into the framework
- Synchronous API that can block the UI thread
val prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE)
prefs.edit().putBoolean("dark_mode", true).apply()
val dark = prefs.getBoolean("dark_mode", false)All lessons in this course
- DataStore vs SharedPreferences
- Preferences DataStore
- Reading and Writing Settings
- Proto DataStore