Android preferences

Dublin Buzz uses the preferences storage mechanism for storing users’ favourites. This is an ideal mechanism – very easy to use, persistent between application launches, and doesn’t require any special permissions or access to SD cards or external memory. In fact, for doing simple key/value storage, it is the obvious choice.

Favourites are stored when the user hits the “Make fav” button in the SightViewer activity. The click handler for the button is here:

  * Uses preferences mechanism for saving favourites. Saves the sight's name and setting 
  * @param setting
 private void setFavourite (int setting)
  SharedPreferences favs = this.getSharedPreferences("MyPrefs", MODE_WORLD_WRITEABLE);
  SharedPreferences.Editor editor = favs.edit();
  editor.putInt(theSight.getName(), setting); 

A SharedPreferences is an interface for accessing preference data. The reference to it is through the activity’s getSharedPreferences() method call. The method is passed a preference file name, in this case “MyPrefs”. Ideally, to make it unique, this should be more specific, something like “” or equivalent, to make it unique on the Android device. We also pass it a mode, in this case something to indicate that we are writing preferences.
All preferences pass via an editor, which ensures that they are in the right format before being committed to the preferences storage. Changes are batched until you call commit() or apply().
We pass in the name of the sight as the key and the setting value (either 1 or 0). A setting value of 0 takes the sight out of the favourites list, while a setting of 1 indicates that it is a favourite. The setting is obviously an integer, so we use the setInt() method to write it to the editor. And then we must commit() those changes, which writes them to the preferences file.

So, we have written out a value for the sight, as to whether it is a favourite or not. Then how do we read back that value? This code, in the SightViewer onStart() method reads the preferences, looking for the key of the sight name:

  // Test to see if this is a favourite or not?
  SharedPreferences favs = this.getSharedPreferences("MyPrefs", MODE_WORLD_READABLE);
  int result = favs.getInt(theSight.getName(), 0);
  isAFavourite = result==1;

It is the opposite process to writing preferences. This time, we get the preferences in the same way, but we are in reading mode. And the getInt() method call on the preferences uses the sight’s name as a key and a default value of zero. In other words, if the key does not exist, the method returns this default value – 0. This corresponds to a setting of off, meaning the sight is not a favourite. Which makes sense, because we only add it to the favourites by writing a setting of 1. If the sight has been added and then taken out again, it has a setting explicitly in the preferences of zero anyway. Otherwise, it has no setting. This “lazy” way to use preferences is more efficient, but cannot be generalised to all situations.

I also read the preferences to get the entire list of available favourites, in the file:

  * Gets the current list of user favourites
  * @return
 private List<Sight> getFavourites()
   List<Sight> favourites = new ArrayList<Sight>();
   SharedPreferences favs = this.getSharedPreferences("MyPrefs", MODE_WORLD_READABLE);
   for (Sight s: app.sights)
   	int result = favs.getInt(s.getName(), 0);
   	if (result==1)
   return favourites;
This entry was posted in Android, Android development, Mobile Apps. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s