Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Shared preferences key/value pairs

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.

like image 409
Theo Avatar asked Jul 23 '15 07:07

Theo


People also ask

How does shared preferences store key value pairs?

Have each key and value be separate entries in the SharedPreferences . After all, SharedPreferences is a key-value store. Use a dedicated preference file for your keys and values (so as not to collide with anything else you might want to store), then you know that all the keys are ones from the user.

Can shared preferences be deleted?

SharedPreferences is what Android and iOS apps use to store simple data in an allocated space. This data exists even when the app is shut down and starts up again; we can still retrieve the value as it was. The data stored in SharedPreferences can be edited and deleted.

What does the SharedPreferences editor Clear () method do?

clear(), It delete only values (means that keys are available) or it delete key value pair. check the following code. Mark in the editor to remove all values from the preferences.

What is the syntax for deleting data in shared preferences?

To clear all the values in the shared preferences file, call the clear() method on the shared preferences editor and apply the changes.


2 Answers

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();
like image 173
Pankaj Kumar Avatar answered Oct 22 '22 01:10

Pankaj Kumar


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();
like image 42
Durgesh Patel Avatar answered Oct 22 '22 01:10

Durgesh Patel