我将一些付款值存储在一个活动
中SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
productId = spreferences.getString("productId", "");
purchaseToken = spreferences.getString("purchaseToken", "");
orderId = spreferences.getString("orderId", "");
现在我在另一个中检索它们
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
productId = spreferences.getString("productId", "");
purchaseToken = spreferences.getString("purchaseToken", "");
orderId = spreferences.getString("orderId", "");
我的问题是在检索它们后在第二个Activity中删除它们。谢谢。
答案 0 :(得分:18)
使用SharedPreferences.Editor remove (String key)
执行相同的操作。
在编辑器中标记偏好值应该是 删除,这将在commit()之后在实际首选项中完成 调用。
请注意,在提交回首选项时,所有删除都是 首先完成,无论你是否在之前或之后调用删除 把方法放在这个编辑器上。
因此,在您的情况下,您可以像
一样使用它SharedPreferences.Editor editor = spreferences.edit();
editor.remove("productId");
editor.remove("purchaseToken");
editor.remove("orderId");
editor.commit();
答案 1 :(得分:5)
要在SharedPreference中存储值,请使用以下代码:
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();
要从SharedPreference中删除特定值,请使用以下代码:
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor spreferencesEditor = spreferences.edit();
spreferencesEditor.remove("productId"); //we are removing prodId by key
spreferencesEditor.commit();
要从SharedPreference中删除所有值,请使用以下代码:
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor spreferencesEditor = spreferences.edit();
spreferencesEditor.clear();
spreferencesEditor.commit();
答案 2 :(得分:1)
您可以使用此
删除与特定键关联的任何值SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.remove("your_key");
editor.commit();
或
SharedPreferences prefs = context.getSharedPreferences(name, mode);
SharedPreferences.Editor editor = prefs.edit();
editor.remove(your_key)
editor.commit();
答案 3 :(得分:0)
你需要像删除我的偏好一样。
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();
这将清除您的偏好。
答案 4 :(得分: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
答案 5 :(得分:0)
要清除SharedPreferences,请使用SharedPreferences Editor 在你的情况下:
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = spreferences.edit();
editor.clear();
editor.commit();