2

我只是在清除共享首选项数据时遇到了一点问题。

    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 框中输入值,按主页,然后返回应用程序,值在那里并且一切正常且正常工作。所以我去我的菜单->退出->弹出对话框询问用户退出与否,是的,应用程序只是关闭它,但是当我再次运行应用程序时,值仍然在文本视图中。

不知道我做错了什么:(

PS:我刚刚更新了 editor.remove("SharedP"); 与 editor.clear(); 问题仍然存在。

4

1 回答 1

1

您可以使用editor.clear(); SharedPreferences.Editor clear()

SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
于 2012-11-05T00:08:13.373 回答