Persisting data with SharedPreferences

In Android, there are a number of ways to make data persist. By persist, I mean that if the user quits the app, when they come back to it, their data is still available. The correct method to use is dependent upon the app and type of data.

In this book, we will look at three ways to make data persist. For saving our users' settings, we only need a really simple method. After all, we just need to know if they want sound and at which speed they want their animations.

Let's take a look at how we can make our apps save and reload variables to the internal storage of the device. We need to use the SharedPreferences class. SharedPreferences is a class that provides access to data that can be accessed and edited by all activities of an app. Let's look at how we can use it:

// A SharedPreferences for reading data
SharedPreferences prefs;

// A SharedPreferences.Editor for writing data
SharedPreferences.Editor editor; 

As with all objects, we need to initialize them before we can use them. We can initialize the prefs object using the getSharedPreferences method and by passing in String, which will be used to refer to all the data read and written using this object. Typically, we could use the name of the app as this string. In the next code, Mode_Private means that any class, in this app only, can access it:

prefs = getSharedPreferences("My App", MODE_PRIVATE);

We then use our newly initialized prefs object to initialize our editor object by calling the edit method:

editor = prefs.edit();

Let's say we wanted to save the user's name that we have in a string called username. We can then write the data to the internal memory of the device like this:

editor.putString("username", username);
editor.commit();

The first argument used in the putString method is a label that can be used to refer to the data, and the second is the actual variable that holds the data we want to save. The second line in the previous code initiates the saving process. So, we could write multiple variables to disk like this:

editor.putString("username", username);
editor.putInt("age", age);
editor.putBoolean("newsletter-subscriber", subscribed);

// Save all the above data
editor.commit();

The preceding code demonstrates that you can save other variable types, and it of course assumes that the username, age, and subscribed variables have previously been initialized with appropriate values.

Once editor.commit() is executed, the data is stored. We can quit the app and even turn off the device and the data will persist.

Let's see how we can reload our data the next time the app is run. This code will reload the three values that the previous code saved. We could even declare our variables and initialize them with the stored values:

String username  = 
  prefs.getString("username", "new user");

int age = prefs.getInt("age", -1);

boolean subscribed = 
  prefs.getBoolean("newsletter-subscriber", false)

In the previous code, we load the data from disk using the method appropriate for the data type and the same label we used to save the data in the first place. What is less clear is the second argument to each of the method calls. The getString, getInt, and getBoolean methods require a default value as the second parameter. If there is no data stored with that label, it will then return the default value. We could then check for these default values in our code and go about trying to obtain the real values. For example:

if (age == -1){
  // Ask the user for his age
}

We now know enough to save our users' settings in the Note To Self app.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.197.26