删除共享的首选项键/值对 [英] Remove Shared preferences key/value pairs

查看:44
本文介绍了删除共享的首选项键/值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些付款值存储在一个活动中

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.

推荐答案

使用

在编辑器中标记首选项值应为删除,一旦commit()被执行,将在实际的首选项中完成叫.

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.

请注意,当返回到首选项时,所有删除项都是无论您是在调用之前还是之后调用remove,都首先完成在此编辑器上放置方法.

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

这篇关于删除共享的首选项键/值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆