Junior — Middle
What is the role and purpose of setting data storage preferences?
sobes.tech AI
Answer from AI
Setting up data storage preferences (SharedPreferences in Android) is used for saving simple key-value pairs, such as user settings, status flags, or small configuration data. It is a lightweight way to store data that should persist between app sessions, for example, selected theme, language, or login status.
Advantages:
- Easy to use
- Fast access to data
- Data is stored in an XML file, convenient for small amounts of data
Example usage:
val sharedPref = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)
with(sharedPref.edit()) {
putString("username", "user123")
putBoolean("isLoggedIn", true)
apply()
}
val username = sharedPref.getString("username", "defaultUser")