Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android SharedReferences';s清除功能不工作_Android_Singleton_Sharedpreferences - Fatal编程技术网

android SharedReferences';s清除功能不工作

android SharedReferences';s清除功能不工作,android,singleton,sharedpreferences,Android,Singleton,Sharedpreferences,我的Pref's clear不起作用 这是我的首选Manager.java public class PreferenceManager { private static final String PREF_NAME = "my_prefs.xml"; private static PreferenceManager instance; private SharedPreferences mPrefs; private SharedPreferences.Editor mEdi

我的Pref's clear不起作用

这是我的首选Manager.java

public class PreferenceManager {
  private static final String PREF_NAME = "my_prefs.xml";

  private static PreferenceManager instance;
  private SharedPreferences mPrefs;
  private SharedPreferences.Editor mEditor;

  public static PreferenceManager getInstance(Context context) {
      if (instance == null) {
          instance = new PreferenceManager(context);
      }
      return instance;
  }


  private PreferenceManager(Context context) {
      mPrefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
      mEditor = mPrefs.edit();
  }

  private static final String UUID = "UUID";
  private String uuid = "";

  public void setUuid(String key) {
      this.uuid = key;
      clear(UUID);
      mEditor.putString(UUID, key);
      mEditor.commit();
  }

  public String getUuid() {
      if (uuid.equals("")) {
          uuid = mPrefs.getString(UUID, "");
      }
      return uuid;
  }
  //clear
  public void clear(String key) {
      mEditor.remove(key);
      mEditor.commit();
  }
  public void allClear(){
      mEditor = mPrefs.edit();
      mEditor.clear();
      mEditor.commit();
  }
}
当我在这样的活动中使用它时

PreferenceMangaer.getInstance(getApplicationContext).setUUID("ok");

当我调试时,clear(UUID)不工作,allClear也不工作。 为什么它不起作用

我尝试commit()和apply()

我在等你的帮助
谢谢

您还没有清除您的全局
uuid

  public void allClear(){
      this.uuid = ""; // Clears the global uuid.
      mEditor = mPrefs.edit();
      mEditor.clear();
      mEditor.commit();
  }
这就是为什么您的
getUuid
会得到与以前相同的结果

您不必存储
uuid
的本地引用

  private static final String UUID = "UUID";

  public void setUuid(String key) {
      mEditor.putString(UUID, key);
      mEditor.apply(); // better use apply() instead commit()
  }

  public String getUuid() {
      return mPrefs.getString(UUID, "");
  }

  //clear
  public void clear(String key) {
      mEditor.remove(key);
      mEditor.apply();
  }
  public void allClear(){
      mEditor.clear();
      mEditor.apply(); 
  }

使用键和编辑器功能

    SharedPreferences Prefs =PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = Prefs.edit();
    editor.remove(String key);
    editor.apply()

您使用“UUID”键保存值,但尝试使用不同的键删除。我甚至没有在您的
PreferenceManager
@gokhan上看到
setPushKey
getPushKey
方法。对不起,这是我的错误,我编辑了我的问题。请再检查一下。多亏了你的帮助reply@dr3k对不起,这是我的错误,我编辑了我的问题。请再检查一下。感谢您的回复虽然这段代码可能是解决方案,但确实有助于提高您的文章质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。完全正确!!非常感谢你。我明白我做错了什么:)
    SharedPreferences Prefs =PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = Prefs.edit();
    editor.remove(String key);
    editor.apply()