SharedPreferences Clear/Save

To clear SharedPreferences use this

SharedPreferences preferences = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear(); 
editor.commit();

Hope this helped you.


Simply you can:

getSharedPreferences("PREFERENCE", 0).edit().clear().commit();

You are not using the same preferences. Take a while to read http://developer.android.com/reference/android/app/Activity.html

In your first activity you are using:

SharedPreferences prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE);

And in the other activity clearing you are only using:

SharedPreferences preferences = getPreferences(0);

Reading the docs:

Retrieve a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.

You need to use the same preference name in both activities. So in your second activity, where you do the clearing just use

SharedPreferences preferences = getSharedPreferences("PREFERENCE", MODE_PRIVATE);

// save string to SharedPreferences

public static void saveStringToSharedPreferences(Context mContext, String key, String value) {
    if(mContext != null) {
        SharedPreferences mSharedPreferences = mContext.getSharedPreferences("SHARED_PREFERENCES", 0);
        if(mSharedPreferences != null)
            mSharedPreferences.edit().putString(key, value).commit();
    }
}

// read string from SharedPreferences

public static String readStringFromSharedPreferences(Context mContext, String key) {
    if(mContext != null) {
        SharedPreferences mSharedPreferences = mContext.getSharedPreferences("SHARED_PREFERENCES", 0);
        if(mSharedPreferences != null)
            return mSharedPreferences.getString(key, null);
    }
    return null;
}

// remove from SharedPreferences

public static void removeFromSharedPreferences(Context mContext, String key) {
    if (mContext != null) {
        SharedPreferences mSharedPreferences = mContext.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, 0);
        if (mSharedPreferences != null)
            mSharedPreferences.edit().remove(key).commit();
    }
}