Challenge - 5 Problems
SharedPreferences / DataStore Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output when reading a missing key from SharedPreferences?
Consider this Kotlin code snippet that reads a value from SharedPreferences:
What will be printed if the key "user_name" does not exist in the preferences?
val prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE)
val username = prefs.getString("user_name", "Guest")
println(username)What will be printed if the key "user_name" does not exist in the preferences?
Android Kotlin
val prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE) val username = prefs.getString("user_name", "Guest") println(username)
Attempts:
2 left
💡 Hint
Check the default value parameter in getString method.
✗ Incorrect
The getString method returns the default value if the key is missing. Here, "Guest" is the default, so it prints "Guest".
❓ lifecycle
intermediate2:00remaining
When is it safe to read from DataStore in an Android app?
You want to read user preferences stored in DataStore in your Android app.
At which lifecycle event of an Activity is it best to start collecting DataStore data to update the UI?
At which lifecycle event of an Activity is it best to start collecting DataStore data to update the UI?
Attempts:
2 left
💡 Hint
Consider when the UI is visible but not yet interactive.
✗ Incorrect
onStart() is called when the Activity becomes visible. Starting to collect DataStore data here ensures UI updates before user interaction.
📝 Syntax
advanced2:00remaining
Which Kotlin code correctly saves a Boolean value using DataStore?
Given a DataStore instance named dataStore, which code snippet correctly saves a Boolean value with key "dark_mode" set to true?
Android Kotlin
val DARK_MODE_KEY = booleanPreferencesKey("dark_mode")
// Choose the correct snippet to save true to this keyAttempts:
2 left
💡 Hint
Check the official DataStore API for editing preferences.
✗ Incorrect
The edit function is the correct suspend function to update preferences. updateData and save do not exist, and put is not a method on Preferences.
🔧 Debug
advanced2:00remaining
Why does this SharedPreferences commit not save data?
Look at this Kotlin code:
What is the reason the data is not saved?
val prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
val editor = prefs.edit()
editor.putString("token", "abc123")
// Missing editor.commit() or editor.apply()What is the reason the data is not saved?
Attempts:
2 left
💡 Hint
Check what methods finalize changes in SharedPreferences.Editor.
✗ Incorrect
Changes made with putString are only saved after calling commit() or apply() on the editor.
🧠 Conceptual
expert2:00remaining
What is a key advantage of DataStore over SharedPreferences?
Which of the following is a key advantage of using DataStore instead of SharedPreferences in Android development?
Attempts:
2 left
💡 Hint
Think about modern Kotlin features and thread safety.
✗ Incorrect
DataStore uses Kotlin coroutines and Flow for asynchronous, safe data access, improving performance and avoiding blocking the main thread.