android - 删除共享首选项键/值对

标签 android sharedpreferences

我在一个 Activity 中存储了一些支付值

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 中删除它们。谢谢。

最佳答案

使用SharedPreferences.Editor remove (String key)做同样的事情。

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.


所以在你的情况下你可以像这样使用它

SharedPreferences.Editor editor = spreferences.edit();
editor.remove("productId");
editor.remove("purchaseToken");
editor.remove("orderId");
editor.commit();

关于android - 删除共享首选项键/值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31580593/

相关文章:

android - 如何通过 SharedPreferences 保存解析的推送通知

android - 无法在 Android 中将 SharedPreferences 值从一个 Activity 获取到另一个 Activity

android - Sharepreference来存储int值

android - 如何恢复 SharedPreferences 更改?

android - 通过 Intent 将 JSONObject 传递给另一个 Activity

android - 没有服务和任何 UI 显示的无休止的后台进程

android - 窗口中可用空间的尺寸(不包括操作栏和边距)

android - android :isScrollContainer do? 是什么意思

Android ListView 实时更新

android - 没有得到共享首选项 : 的确切值