How to remove some key/value pair from SharedPreferences?

前端 未结 6 1019
滥情空心
滥情空心 2020-12-12 23:26

How to remove some key/value pair from SharedPreferences ? I have put and I to remove that from prefs.

6条回答
  •  悲&欢浪女
    2020-12-13 00:10

    Here is how I tacked this issue.

    First I created an instance of SharedPreference as

    SharedPreferences mobilePreference;
    

    then I used this sharedPreference as

    mobilePreference = this.getSharedPreferences("in.bhartisoftwares.amit.allamitappsthree", Context.MODE_PRIVATE);
    

    Here "in.bhartisoftwares.amit.allamitappsthree" is my package name and I am using Context.MODE_PRIVATE, because I want to manipulate this shared preference only for this package name.

    Then I am deleting the selected sharedPreference (key of my sharedPreference is mobileString) as follows:

    mobilePreference.edit().remove("mobileString").commit();
    

    See the code as full below:

    SharedPreferences mobilePreference = this.getSharedPreferences("in.bhartisoftwares.amit.allamitappsthree", Context.MODE_PRIVATE);
        mobilePreference.edit().remove("mobileString").commit();
    

提交回复
热议问题