20

I store some payment values in one Activity

SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
productId = spreferences.getString("productId", "");
purchaseToken = spreferences.getString("purchaseToken", "");
orderId = spreferences.getString("orderId", "");

Now I retrieve them in another one as

SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
productId = spreferences.getString("productId", "");
purchaseToken = spreferences.getString("purchaseToken", "");
orderId = spreferences.getString("orderId", "");

My question is to delete them in the second Activity after retrieving them.Thanks.

Hayley Guillou
  • 3,953
  • 4
  • 24
  • 34
Theo
  • 3,099
  • 12
  • 53
  • 94

7 Answers7

39

Use SharedPreferences.Editor remove (String key) to do the same.

where it marks in the editor that a preference value should be removed, which will be done in the actual preferences once commit() is called.

Note that when committing back to the preferences, all removals are done first, regardless of whether you called remove before or after put methods on this editor.


So in your case you can use it like

SharedPreferences.Editor editor = spreferences.edit();
editor.remove("productId");
editor.remove("purchaseToken");
editor.remove("orderId");
editor.commit();
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
8

To store values in SharedPreference, use below code:

SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor spreferencesEditor = spreferences.edit();
spreferencesEditor.putString("productId", "value of prodId");
spreferencesEditor.putString("purchaseToken", "value of purchaseToken");
spreferencesEditor.putString("orderId", "value of orderId");
spreferencesEditor.commit();

To remove specific value from SharedPreference, use below code:

SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor spreferencesEditor = spreferences.edit();
spreferencesEditor.remove("productId"); //we are removing prodId by key
spreferencesEditor.commit();

To remove All values from SharedPreference, use below code:

SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor spreferencesEditor = spreferences.edit();
spreferencesEditor.clear();
spreferencesEditor.commit();
Durgesh Patel
  • 1,035
  • 8
  • 15
2

You can remove any values associated with a specific key using this,

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.remove("your_key");
editor.commit();

or

SharedPreferences prefs = context.getSharedPreferences(name, mode);
SharedPreferences.Editor editor = prefs.edit();
editor.remove(your_key) 
editor.commit();
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
  • Yeah I tried this SharedPreferences.Editor.remove(orderId). But the compiler doesn't recognise the remove(String value) method:( – Theo Jul 23 '15 at 07:26
2

To clear the SharedPreferences, use the SharedPreferences Editor In your case:

SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = spreferences.edit();
editor.clear(); 
editor.commit();
krekle
  • 135
  • 6
  • I have other shared preferences values in my second activity. I only want to delete 3 of them. Not all of them. – Theo Jul 23 '15 at 07:27
  • 1
    Alright, then [remove()](http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#remove(java.lang.String)) would be the way to go. Remember to [commit()](http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#commit()) – krekle Jul 23 '15 at 07:30
0

You need to do same like I am removing my preferences.

    SharedPreferences preferences = contextAct.getSharedPreferences("PREF_KEY", 0);
                    preferences.edit().remove("productId").commit();
                    preferences.edit().remove("purchaseToken").commit();
                    preferences.edit().remove("orderId").commit();


    Format : preferences.edit().remove("Your Key").commit();

This will clear your preferences.

0
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor=spreferences.edit();
 editor.remove("productId");
 editor.remove("purchaseToken");
 editor.remove("orderId");
 editor.commit();
 // you can also use editor.apply(); instead of editor.commit(); using apply will handle the removing in the background
Ivano Donadi
  • 377
  • 1
  • 13
0
SharedPreferences preferences = getSharedPreferences("myPref",MODE_PRIVATE);
    preferences.edit().remove("productId").commit();
Mohammad Faizan
  • 191
  • 1
  • 4