如何从共享中删除值

How to remove values from Shared

我正在 android 使用 kotlin,我已将 userId 保存在共享首选项中,但是当我尝试将其删除时,它并没有被删除。我做对了吗?

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId

        if (id == R.id.action_settings) {
            val pref = applicationContext.getSharedPreferences("Auth", Context.MODE_PRIVATE)
            
            val editor: SharedPreferences.Editor = pref.edit()
            editor.remove("UserId")
            editor.commit()
  
            Toast.makeText(this, "Sign out",  Toast.LENGTH_SHORT).show()
        }
        return super.onOptionsItemSelected(item)
    }

登录代码,这是我的乐趣,我在用户登录时调用它,它是这样调用的:

saveUserIdSession(mAuth.currentUser.uid.toString())
fun saveUserIdSession(id: String) {
        val sharedPreferences: SharedPreferences? =
            this.activity?.getSharedPreferences("Auth", Context.MODE_PRIVATE)
        var editor: SharedPreferences.Editor = sharedPreferences!!.edit()
        editor.apply{
            putString("UserId", id)
        }.apply()
    }

在一行中删除 Android 共享首选项:-)

context.getSharedPreferences("Auth", 0).edit().clear().commit();

谢谢, 程序员

注意:- 如果有帮助请点赞。