我在一个 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/