Android 未清除的共享首选项,

Android 未清除的共享首选项,,android,sharedpreferences,Android,Sharedpreferences,我无法从应用程序点击事件中删除SharedReferences 下面是我如何将值存储到UserInfoActivitySharedReferences: SharedPreferences notificationCountSP = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor notificationEditor = p

我无法从应用程序点击事件中删除
SharedReferences

下面是我如何将值存储到UserInfoActivity
SharedReferences

SharedPreferences notificationCountSP = PreferenceManager
             .getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = perkBalance.edit();
notificationEditor.putString("notificationCount",notificationCountValue);
notificationEditor.commit();
下面是我如何从MainActivity中清除
SharedReferences
中的所有数据:

SharedPreferences clearNotificationSP = getSharedPreferences(
                "notificationCountSP", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.remove("notificationCount");
editor.clear();
editor.commit();
请告诉我这有什么不对


任何帮助都将不胜感激。

您正在使用
首选项管理器.GetDefaultSharedReferences
存储但从
GetSharedReferences(“notificationCountSP”)
检索。除非您将默认文件设置为“notificationCountSP”,否则它们是不同的文件。

您可以按如下方式执行此操作

SharedPreferences userPref = getSharedPreferences(
                                    MyActivity.SHARED_PREFERENCES_FILENAME,MODE_PRIVATE);


第一个解决方案使用默认的应用程序首选项文件,第二个解决方案使用自定义通知首选项文件。

尝试调用notificationEditor上的删除操作(在第一个块中)。。我猜,您编辑了两个不同的首选项文件。请尝试以相同的方式在相同的上下文中检索SharedReferences.Editor。@Hovanesy可以为您做一些我可以看到的事情。我找不到你。这是用来存储值的,对吗?如何根据您的要求存储和删除这些值。可能会帮助您
PreferenceManager.setDefaultValues(context,resId,true)
,其中resId是首选项文件的资源ID。如果您不想创建自己的pref文件,您可以使用
getApplicationContext().getSharedReferences(“notificationCountSP”,mode)
并存储该值。嘿,这里有一个疑问,在执行第二个方法后,我正试图打印
编辑器的值。删除(“notificationCount”)并且它正在日志中打印。为什么会这样?
SharedPreferences notificationCountSP = PreferenceManager
                     .getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = notificationCountSP.edit();
notificationEditor.putString("notificationCount", notificationCountValue);
notificationEditor.commit();

notificationEditor.remove("notificationCount");
notificationEditor.commit();
SharedPreferences clearNotificationSP = getSharedPreferences("notification_prefs", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.putString("notificationCount", notificationCountValue);
editor.commit();

editor.remove("notificationCount");
editor.commit();