简体   繁体   English

在共享首选项中,当调用 editor.clear() 方法时,它只删除值或删除键值对

[英]In shared preference, when call editor.clear() method, It delete only values or delete key value pairs

Using shared preference, when I call editor.clear(), It delete only values (means that keys are available) or it delete key value pair.使用共享首选项,当我调用 editor.clear() 时,它只删除值(意味着键可用)或删除键值对。

check the following code.检查以下代码。

SharedPreferences userlogindetails = getSharedPreferences("userdetails", MODE_PRIVATE);
SharedPreferences.Editor edituserlogindetails = userlogindetails.edit();                
edituserlogindetails.clear();//**it delete values or key value pairs.**
edituserlogindetails.apply();

According to SharedPreferences.Editor#remove you have to call remove() 根据SharedPreferences.Editor#remove,您必须调用remove()

remove 去掉

Added in API level 1 在API级别1中添加

SharedPreferences.Editor remove (String key) SharedPreferences.Editor删除(字符串键)

Mark in the editor that a preference value should be removed, which will be done in the actual preferences once commit() is called. 在编辑器中标记应删除首选项值,一旦调用commit(),将在实际首选项中完成。

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. 请注意,在提交回首选项时,所有删除都将首先完成,无论您在此编辑器上的put方法之前还是之后调用remove。

Here is an example 这是一个例子

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.remove("key"); //Your key here
editor.apply();

It deletes/clears all the values of your shared preferences. 它删除/清除共享首选项的所有值。 And if you want to remove the key you have to use remove as mentioned in the answer by Michele. 如果要删除密钥,则必须使用Michele答案中提到的remove。

Simply add ".commit()" as like this只需像这样添加“.commit()”

sharedPreference?.edit()?.clear()?.commit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM