Remove Shared preferences key/value pairs

夙愿已清 提交于 2020-06-24 08:26:47

问题


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.


回答1:


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();



回答2:


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();



回答3:


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();



回答4:


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



回答5:


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();



回答6:


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.



来源:https://stackoverflow.com/questions/31580593/remove-shared-preferences-key-value-pairs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!