【问题标题】:Remove Shared preferences key/value pairs删除共享首选项键/值对
【发布时间】:2015-10-13 08:37:12
【问题描述】:

我将一些付款值存储在一个 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 用法

标签: android sharedpreferences


【解决方案1】:

你需要像我删除我的偏好一样做同样的事情。

    SharedPreferences preferences = contextAct.getSharedPreferences("PREF_KEY", 0);
                    preferences.edit().remove("productId").commit();
                    preferences.edit().remove("purchaseToken").commit();
                    preferences.edit().remove("orderId").commit();


    Format : preferences.edit().remove("Your Key").commit();

这将清除您的偏好。

【讨论】:

    【解决方案2】:

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

    它在编辑器中标记首选项值的位置 已删除,一旦 commit() 将在实际首选项中完成 调用。

    请注意,在提交回首选项时,所有删除都是 先完成,无论您是在之前还是之后调用 remove 把方法放在这个编辑器上。


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

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

    【讨论】:

    • 使用 apply() 比使用 commit() 更好。
    【解决方案3】:

    您可以使用此删除与特定键关联的任何值,

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = prefs.edit();
    editor.remove("your_key");
    editor.commit();
    

    SharedPreferences prefs = context.getSharedPreferences(name, mode);
    SharedPreferences.Editor editor = prefs.edit();
    editor.remove(your_key) 
    editor.commit();
    

    【讨论】:

    • 是的,我试过这个 SharedPreferences.Editor.remove(orderId)。但是编译器无法识别 remove(String value) 方法:(
    【解决方案4】:
    SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor=spreferences.edit();
     editor.remove("productId");
     editor.remove("purchaseToken");
     editor.remove("orderId");
     editor.commit();
     // you can also use editor.apply(); instead of editor.commit(); using apply will handle the removing in the background
    

    【讨论】:

      【解决方案5】:

      要清除 SharedPreferences,请使用 SharedPreferences Editor 在你的情况下:

      SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
      SharedPreferences.Editor editor = spreferences.edit();
      editor.clear(); 
      editor.commit();
      

      【讨论】:

      • 我在第二个活动中有其他共享偏好值。我只想删除其中的 3 个。不是全部。
      • 好吧,那么remove() 将是要走的路。记得commit()
      【解决方案6】:

      要将值存储在 SharedPreference 中,请使用以下代码:

      SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
      Editor spreferencesEditor = spreferences.edit();
      spreferencesEditor.putString("productId", "value of prodId");
      spreferencesEditor.putString("purchaseToken", "value of purchaseToken");
      spreferencesEditor.putString("orderId", "value of orderId");
      spreferencesEditor.commit();
      

      要从 SharedPreference 中删除特定值,请使用以下代码:

      SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
      Editor spreferencesEditor = spreferences.edit();
      spreferencesEditor.remove("productId"); //we are removing prodId by key
      spreferencesEditor.commit();
      

      要从 SharedPreference 中删除所有值,请使用以下代码:

      SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
      Editor spreferencesEditor = spreferences.edit();
      spreferencesEditor.clear();
      spreferencesEditor.commit();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多