Remove Shared preferences key/value pairs
Asked Answered
O

7

21

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.

Omidyar answered 23/7, 2015 at 7:17 Comment(1)
Seems you mistyped the storing part. Should have a SharedPreferences.Editor usage thereClinkscales
C
40

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();
Cooperstein answered 23/7, 2015 at 7:23 Comment(2)
Better use apply() than commit().Mather
Clearing something can lead to data inconsistency. Apply is asynchronous. @MatherCooperstein
G
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();
Gladdy answered 23/7, 2015 at 7:33 Comment(0)
K
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();
Kirschner answered 23/7, 2015 at 7:23 Comment(1)
Yeah I tried this SharedPreferences.Editor.remove(orderId). But the compiler doesn't recognise the remove(String value) method:(Omidyar
W
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();
Warmth answered 23/7, 2015 at 7:26 Comment(2)
I have other shared preferences values in my second activity. I only want to delete 3 of them. Not all of them.Omidyar
Alright, then remove() would be the way to go. Remember to commit()Warmth
P
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.

Pm answered 23/7, 2015 at 7:22 Comment(0)
P
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
Priory answered 23/7, 2015 at 7:25 Comment(0)
D
0
SharedPreferences preferences = getSharedPreferences("myPref",MODE_PRIVATE);
    preferences.edit().remove("productId").commit();
Divisible answered 3/7, 2023 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.