gpt4 book ai didi

android - 从android中的共享首选项中删除数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:29:00 24 4
gpt4 key购买 nike

我正在尝试从共享首选项中删除数据。但我不能那样做。为了跟踪数据是否被删除,我使用了这段代码:

btnLogout.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
SharedPreferences prefs = getSharedPreferences(share_pref_file, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.remove(share_pref_file);
editor.clear();
editor.commit();
getApplicationContext().getSharedPreferences(share_pref_file, 0).edit().clear().commit();
String strJson = prefs.getString("jsondata","");
if(strJson != null)
{
Log.d("CLEAR", "cccccccccccccccccccccccccccc");
}
userFunctions.logoutUser(getApplicationContext());
Intent login = new Intent(getApplicationContext(),LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
});

但在 logcat 窗口中,它每次都显示“cccccccccccccccccccccccccccc”值。

那么谁能帮我解决如何从共享首选项中删除/删除数据,以便在我单击“注销”按钮时它会删除所有存储的数据?提前致谢。

最佳答案

空字符串仍然是字符串(String("") != null 将返回 true)。试试这个:

if(!strJson.equals("")) 

这首先假定空字符串永远不会成为您的 SharedPreferences 中的有效输入。

关于android - 从android中的共享首选项中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15757673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com