Shared Preferences (Android)

Shared Preferences in Android are used with the instance class
android.content.SharedPreferences. You can import this class into your build path with the import statement
import android.content.SharedPreferences

This class essentially reads and writes to local storage in your app's app data. Keep in mind that this means if the user erases your app's app data (you can do that on any device in the Application Manager), the SharedPreferences are erased too. The idea of SharedPreferences are to store small information about the user, whether it be their favourite colour, their preferred settings for the app, etc. The class writes the data to a file for you, and then retrieves it for you as well. This is all done in a nice, clear and concise format. It's basically two lines of code: creating an instance of the SharedPreferences class, and then calling a method from that class to read or write a preference.

What I do (and what I recommend you do) is create methods to manipulate the SharedPreferences. So I create two methods: one to read from the SharedPreference, and one to write to it. Then I call those methods very easily with a simple method call. After you import the SharedPreference class into your build path and then create the methods to manipulate the SharedPreferences, it's simply one method call to read/write to the SharedPreference. Keep in mind that SharedPreferences can save booleans, floats, ints, longs, and strings. The example I show will be using Strings, but you can simply change the String to int, boolean, etc.

public String getPreference() {
    SharedPreferences sp = getSharedPreferences("preferenceName", 0);
    return sp.getString("preferenceKeyValue", "default value if no preference is found");
}
public void setPreference(String str) {
    SharedPreferences.Editor editor = getSharedPreferences("preferenceName", 0).edit();
    editor.putString("preferenceKeyValue", str).commit();
}

And then you could simply call it with method calls. It's nice and easy. The comments in the following code are the results of the method call.

String str = getPreference(); // "default value if no preference is found"
setPreference("Hello World!");
str = getPreference(); // "Hello World!"

Here is an example of how to use this practically. Let's say you wanted to save the high score of a user in a game.

public int getHighScore() {
    SharedPreferences sp = getSharedPreferences("High Score Preference", 0);
    return sp.getInt("High Score", -1); // returns -1 if no high score was found (never played the game maybe)
}
public void setHighScore(int i) {
    SharedPreferences.Editor editor = getSharedPreferences("High Score Preference", 0).edit();
    editor.putInt("High Score", i).commit();
}

Call it like this:

if (getHighScore() == -1) {
    // they have never gotten a high score
} else {
    String response = "Your high score is: " + getHighScore();
}

// if they get a new high score, write to it:
setHighScore(15); // new high score is 15

          Created: June 11, 2014
Completed in full by: Michael Yaworski