Complete the code to save a string value in SharedPreferences.
val sharedPref = getSharedPreferences("MyPrefs", MODE_PRIVATE) val editor = sharedPref.edit() editor.putString("username", [1]) editor.apply()
We save the string "JohnDoe" as the value for the key "username" in SharedPreferences.
Complete the code to read a string value from SharedPreferences with a default fallback.
val sharedPref = getSharedPreferences("MyPrefs", MODE_PRIVATE) val username = sharedPref.getString("username", [1])
We provide "Guest" as the default value if "username" is not found in SharedPreferences.
Fix the error in the code to correctly save an integer in SharedPreferences.
val sharedPref = getSharedPreferences("MyPrefs", MODE_PRIVATE) val editor = sharedPref.edit() editor.[1]("userAge", 25) editor.apply()
Use putInt to save an integer value in SharedPreferences.
Fill both blanks to check if a key exists and then remove it from SharedPreferences.
val sharedPref = getSharedPreferences("MyPrefs", MODE_PRIVATE) if (sharedPref.[1]("userToken")) { val editor = sharedPref.edit() editor.[2]("userToken") editor.apply() }
Use contains to check if the key exists and remove to delete it.
Fill all three blanks to save a boolean flag and then read it with a default value.
val sharedPref = getSharedPreferences("MyPrefs", MODE_PRIVATE) val editor = sharedPref.edit() editor.[1]("isLoggedIn", true) editor.apply() val isLoggedIn = sharedPref.[2]("isLoggedIn", [3])
Use putBoolean to save a boolean and getBoolean to read it with a default of false.