This post explains further the example code on the Android Developer site:
http://developer.android.com/guide/topics/data/data-storage.html
I am trying to dumb this down as best as I can, so some useful options may be left out. Here is the code found on that page as of July 14, 2011:
public class Calc extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; @Override protected void onCreate(Bundle state){ super.onCreate(state); . . . // Restore preferences SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean silent = settings.getBoolean("silentMode", false); setSilent(silent); } @Override protected void onStop(){ super.onStop(); // We need an Editor object to make preference changes. // All objects are from android.context.Context SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); // Commit the edits! editor.commit(); } } |
Android has made storing application settings as simple as possible. Using the SharedPreferences object and a few methods build into the Activity class, we can quickly handle application settings.
First, we need to give our settings a name. In the example, Google names them MyPrefsFile. Changing the name will result in not finding your existing settings, so don’t change this ever.
public static final String PREFS_NAME = "MyPrefsFile"; |
Let’s look at the following line of code…
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); |
We need to create a SharedPreferences object. This is basically just a list of key-value pairs similar to a java HashTable. Think of it as a named array list. Our Activity has a built in method called getSharedPreferences() that will retrieve our existing settings for us. If they are not there already, Android will create one for us. All the heavy lifting is done without our help. We pass it the name of the preferences “file” we chose earlier. Remember, Android handles the “file”, all we do is pretend it’s there and Android will do the rest. The getSharedPreferences() method returns our key-value pairs object so calling…
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); |
.. creates a SharedPreferences object based on the settings named “MyPrefsFile”. We now have a list of all our existing settings. Lets get a setting.
boolean silent = settings.getBoolean("silentMode", false); setSilent(silent); |
We need the setting “silentMode”. We create a boolean object named “silent” and set it equal to the setting “silentMode” in our SharedPreferences we created earlier. The “false” is the default value…meaning if “silentMode” has not already been set, “silent” will be set to false.
The line “setSilent(silent)” is a call to a method we create in the Activity that tells our application to be silent. It is just an example of using the setting that we retrieved.
Now Lets look at how to store settings. In the example given by the Android team, settings are stored in onClose(). This means that any time an activity is closed the settings are stored. When we collected settings last time, we used SharedPreferences. That is a key/value pair holder. We need to do things a little differently to store/update new data… but we need to get the current settings and add them to any settings we are going to add or change. Lets grab the current application settings.
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); |
Now we need to create an object that handles storing and changing of settings. We create this “settings editor” using our existing settings. Let’s look at the code:
SharedPreferences.Editor editor = settings.edit(); |
Our SharedPreferences has built in functionality to create a “settings editor”. Explaining this line thoroughly would require a discussion on builders. Just know that we are creating an object of the form “SharedPreferences.Editor”. We name it “editor” and we use the edit() method of “settings” to create it with all of our current application settings ready to go. We now have a “settings editor”. Let’s use it.
Sometime during the life of our program, we set our “silentMode” setting to true. We want it to stay that way the next time the application opens. We call “putBoolean()” in our editor, passing in the name of our setting, “silentMode”. The mSilentMode is the boolean value that we need our setting set to, in this case it is equal to “true”.
editor.putBoolean("silentMode", mSilentMode); |
With our new value stored in our settings editor, lets commit the changes.
editor.commit(); |
We write that one line of code, Android does the rest. Our settings have been stored. The next time this Activity runs, it will go to the top of the code discussed in the post, only this time it will find the value of silentMode is set to true and calling setSilent(silent) will be passing in true.