Making the Note to self settings persist
We have already learned how to save data to the device's memory. As we implement saving the user's settings, we will, again, see how we handle the Switch
widget input and where exactly the code we have just seen will go to make our app work the way we want it to.
Coding the SettingsActivity class
Most of the action will take place in the SettingsActivity.kt
file. So, click on the appropriate tab and we will add the code a bit at a time.
First, we want a property to represent the user's option on the settings screen – whether they want decorative dividers or not.
Add the following to SettingsActivity
:
private val showDividers: Boolean = true
Now, in onCreate
, add the highlighted code to initialize prefs
, which is inferred to be a SharedPreferences
instance:
val prefs = getSharedPreferences( "Note to self", Context.MODE_PRIVATE)
Tip
Import the SharedPreferences
class:
import android.content.SharedPreferences...