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.
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.
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.
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.
To clear all the values in the shared preferences file, call the clear() method on the shared preferences editor and apply the changes.
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With