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 also see how we handle Switch
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.java
file. So, click on the appropriate tab and we will add the code a bit at a time.
First, we need some member variables that will give us a working SharedPreferences
and Editor
reference. We also want a member variable to represent the user's settings option – whether they want decorative dividers or not.
Add the following member variables to SettingsActivity
:
private SharedPreferences mPrefs; private SharedPreferences.Editor mEditor; private boolean mShowDividers;
Tip
Import the SharedPreferences
class:
import android.content.SharedPreferences;
Now, in onCreate
, add the highlighted code to initialize...