Best practice Handling sharedpreferences in android

SharedPreferences is very important thing in Android development. If you want to store some small non relational data then shared preference is the best choice. You can store data like user name, number or address in shared preferences. Also you can store the whole object of any class as JSON strings. So can you understand that in order to save these kind of data in Sqlite db is too much painful work to maintain unless you have the relational, complex or big data. You need to write whole new driver of db, create table and query to get data. Itself it is a huge work. So if we are not in this kind of situation we should always stick to the SharedPreferences. Today I will show you how to create and use or handle SharedPreferences class in neat and clean way and of course following the best practice.

Creating the SharedPreferences

We are going to create a new class which contains a object references of SharedPreferences and use it in anywhere in our app with out creating it every single time. For example lets say you have a HomeActivity and SettingsActivity. What will you do? Will you initialize the shared preferences in both activity like SharedPreferences sp = getSharedPreferences() bla bla bla? Its boring. Lets do it in different way.

Lets create a package name utils. In this package create a class named SPreferences. Add this bellow codes to the class:

public class SPreferences{

    protected final String TOKEN = "token";
    protected final String LOGIN = "login";
    protected final String USERNAME = "userName";
    protected final String PHONE = "phone";

    private Context context;
    private SharedPreferences sp;

    public SPreferences(Context context){
        this.context = context;
        sp = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);

    public void setToken(String token){
        sp.edit().putString(TOKEN, token).commit();
    public String getToken(){
        return sp.getString(TOKEN, "n/a");

    public void setLogIn(boolean login){
        sp.edit().putBoolean(LOGIN, login).commit();
    public boolean isLoggedIn(){
        return sp.getBoolean(LOGIN, false);
    public void setUserName(String userName){
        sp.edit().putString(USERNAME, userName).commit();
    public String getUserName(){
        return sp.getString(USERNAME, "n/a");
    public void setPhone(String phone){
        sp.edit().putString(PHONE, token).commit();
    public String getPhone(){
        return sp.getString(PHONE, "n/a");

So here basically we made a getter and setter in a tricky way. We have named 4 String variables and storing particular data against it in SharedPreferences. Notice that we are taking the Context in the constructor to initialize the SharedPreferences.

Now in this utils package create another class. Lets name it Constants. Add the code from below:

public class Constants{

    private static SPreferences sPreferences;

    public static final SPreferences getSPreferences(Context context){
        if(sPreferences == null){
            sPreferences = new SPreferences(context);
        return sPreferences;

you see, we are creating a static object of our SPreferences class and initialize that object in a static getter method named getSp(Context context). Here we are checking the SPreferences object if it is null or not, if its null we are initializing it, otherwise just returning the object. So far so good.

How do I use it?

Now suppose you want to set the username to a textview. Just use this line: userName.setText(Constants.getSp(this).getUserName()). You see how easy and clean is this? You don’t need to initialize the whole SharedPreferences things aging and again. In all your activities, classes or services you can use this SharedPreferences in just one line. So if you want to store more data, you can add more variables in SPreferences class like above and use it through Constants static getter method.