28

Iam trying to make a checker and I want to save a value into SharedPreferences. But i'am not sure if it works

This what I do to save the value is : *

    SharedPreferences prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
    boolean firstrun = prefs.getBoolean("firstrun", true);

    db = openOrCreateDatabase("value.db", Context.MODE_PRIVATE, null); // optional CursorFactory

    if (firstrun) {
          SharedPreferences.Editor editor = prefs.edit();

          db.execSQL("CREATE TABLE startValue (ID Integer Primary Key, myValue Integer)");

          db.execSQL("INSERT INTO startValue (myValue) VALUES (2)"); 

          editor.putBoolean("firstrun", false);
          editor.apply();

           }

    // Save the state
    getSharedPreferences("PREFERENCE", MODE_PRIVATE)
        .edit()
        .putBoolean("firstrun", false)
        .commit();

And to Clear the preferenece from another activity is :

     try{
            db = openOrCreateDatabase("value.db", Context.MODE_PRIVATE, null); // optional CursorFactory

            db.execSQL("DROP TABLE IF EXISTS startValue");
            db.close();

            SharedPreferences preferences = getPreferences(0);
            SharedPreferences.Editor editor = preferences.edit();

            editor.remove("firstrun");
            editor.clear();
            editor.commit();

            this.finish();
        }    
        catch(SQLException ex)
        {
        //catch error here
        }

Issue

But when i'am testing as I see its not clearing the preferences. Am I doing something wrong or?

Tirolel
  • 928
  • 3
  • 17
  • 43

5 Answers5

88

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.

Edison Spencer
  • 491
  • 1
  • 9
  • 24
AwadKab
  • 3,054
  • 2
  • 17
  • 17
  • 1
    Thanks this is helpful, but how to clear a part of sharedPreferences ? – alicanbatur Mar 28 '14 at 07:54
  • I know it might be late, but I recommend you to use the value ´Context.MODE_PRIVATE´ as the mode instead of ´0´. – Edison Spencer Jul 16 '15 at 17:02
  • @EdisonSpencer difference in using `0` and `Context.MODE_PRIVATE`? – August Jan 09 '17 at 08:17
  • 1
    @August, you can see [here](https://developer.android.com/reference/android/content/Context.html) that the constant Context.MODE_PRIVATE has the value 0x0, however, if you use it rather than hardcoding 0 in the code, if Google decides to change the value on a later stage, your code would be protected without need for changes and eventual release to fix the bug. – Edison Spencer Jan 10 '17 at 08:57
  • Change editor.commit(); with editor.apply(); commit() writes its data to persistent storage immediately, whereas apply() will handle it in the background. – Parth Patel Jun 26 '18 at 11:21
7

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);
David Olsson
  • 8,085
  • 3
  • 30
  • 38
4

// 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();
    }
}
2

Simply you can:

getSharedPreferences("PREFERENCE", 0).edit().clear().commit();
Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
2

For removing all preferences :

SharedPreferences mPrefs_delete = context.getSharedPreferences(GeneralFunctions.SETTING_SINGLE_MASTER_CHILD, Context.MODE_PRIVATE);
SharedPreferences.Editor editor_delete = mPrefs_delete.edit();
editor_delete.clear().commit();
Sharon Joshi
  • 498
  • 7
  • 19
  • Consider using apply() instead; commit writes its data to persistent storage immediately, whereas apply will handle it in the background. `SharedPreferences sp = context.getSharedPreferences(SharedDBName, Context.MODE_PRIVATE);` `SharedPreferences.Editor edit = sp.edit();` `edit.clear();` `edit.apply();` – snehal agrawal Apr 24 '19 at 07:42
  • GeneralFunctions showing red ...it is not identified – Sharon Joshi Sep 16 '19 at 14:15