Shared preferences

We can say that “shared preferences” initial functionality is worth remembering and share it with other methods or activities. What is really interesting about this feature is that this value will continue recorded and remembered though we close our application or reboot the phone.
For example, many applications we install on our phones have the particularity that it is the first time I run gives us a number of options, if you’ve already performed before, we offer different alternatives. This can be done easily with “shared preferences”.
Another example of this is the recording functionality users, I mean, let us take the example that we are working on an application that will be able to be executed by some registered users and each take a particular condition. Most often in these cases is to access a database, but if we are not talking about a very high number, we can create the characteristics of each user with “shared preference” will be discussed as these permanently remembered.

For this occasion I mounted a simple interface with two buttons and a text field.
The first “Save” button serves to keep within our “shared preferred” the value that we put into our text field. The second “Show” will be in charge of recovering the value at the time we ask.
At this point we recall the important feature of this function. Say we write in our text field “Hello Android” and click the save button thereupon turned our phone, we take it on and run our application again, if directly click on the button “Show”, we will recover the previously saved value “Hello android”.

import android.content.Context;
import android.content.SharedPreferences;

public class PreferencesManager {

    private static final String PREF_NAME = "com.example.app.PREF_NAME";
    private static final String UPDATE = "com.thedeveloperworldisyours.unitconverterpro.UPDATE";

    private static PreferencesManager sInstance;
    private final SharedPreferences mSharedPreferences;

    private PreferencesManager(Context context) {
        mSharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public static synchronized void initializeInstance(Context context) {
        if (sInstance == null) {
            sInstance = new PreferencesManager(context);
        }
    }

    public static synchronized PreferencesManager getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException(PreferencesManager.class.getSimpleName() +
                    " is not initialized, call initializeInstance(..) method first.");
        }
        return sInstance;
    }

    public void setUpdate(String date) {
        mSharedPreferences.edit()
                .putString(UPDATE, date)
                .commit();
    }

    public String getUpdate() {
        return mSharedPreferences.getString(UPDATE, "");
    }

    public void remove(String key) {
        mSharedPreferences.edit()
                .remove(key)
                .commit();
    }

    public boolean clear() {
        return mSharedPreferences.edit()
                .clear()
                .commit();
    }
}

In your activity

you can set and get the string

PreferencesManager.getInstance().setUpdate("17/5/2012");

myDate = PreferencesManager.getInstance().getUpdate();

One thought on “Shared preferences

  1. Hello everyone, it’s my first go to see at this web site, and paragraph is actually fruitful for me, keep up posting such posts.

Leave a Reply

Your email address will not be published. Required fields are marked *