我在清除“共享首选项”数据时遇到一点问题。
protected void onStop(){
super.onStop();
SharedPreferences settings = getSharedPreferences("SharedP", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("inputValue", et1.getText().toString());
editor.commit();
}
private void exitOptionsDialog() {
new AlertDialog.Builder(this)
.setTitle("Exit")
.setMessage("Are you sure you want to exit?")
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) { } })
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
SharedPreferences settings = getSharedPreferences("SharedP", 0);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
finish();
这是一个退出按钮,说“是”,此按钮将退出应用程序并清除之前输入/保存的数据。
我测试了该应用程序,然后在textview框中键入值,按home键,然后返回到该应用程序,该值在那里并且一切正常。因此,我进入菜单->退出->对话框弹出,询问是否退出用户,是的,应用程序只是将其关闭,但是当我再次运行该应用程序时,该值仍在textview中。
不知道我做错了什么:(
PS:我刚刚更新了editor.remove(“ SharedP”);与editor.clear();问题仍然存在。
最佳答案
您可以使用editor.clear();
SharedPreferences.Editor clear()
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
关于android - 在按钮的finish()上清除SharedPreferences,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13224135/