DataStore & Preferences
DATA › Storage
Modern key-value persistence: DataStore vs SharedPreferences, commit/apply, encryption, and migration.
Persistence basics show up in almost every Android interview because nearly every app stores user settings. Interviewers probe whether you know why DataStore replaces SharedPreferences, the commit() vs apply() threading trade-off, when to pick Preferences vs Proto DataStore, and how to migrate safely. Expect questions on coroutines/Flow, transactional writes, and the deprecation of EncryptedSharedPreferences.
What this covers
- Preferences vs Proto DataStore: untyped key-value vs type-safe schema-backed objects
- Why DataStore beats SharedPreferences: async Flow reads, transactional writes, no main-thread blocking
- commit() vs apply(): synchronous + returns Boolean vs asynchronous + returns void
- Migrating SharedPreferences into DataStore with SharedPreferencesMigration
- EncryptedSharedPreferences (Jetpack Security) and its deprecation status
- Error and corruption handling: CorruptionException and ReplaceFileCorruptionHandler
Study this topic
- DataStore & Preferences explained: the guided lesson
- 14 practice quiz questions
- 9 revision flashcards
- DataStore & Preferences interview questions and answers