我只是在清除共享首選項數據時遇到了一些問題。按鈕完成時清除SharedPreferences()
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();
這是一個退出按鈕,說「是」此按鈕將退出應用程序,並清除之前輸入/保存的數據。
我測試的應用程序,我鍵入的值在文本框中,按回家,然後回到應用程序的價值是在那裏,是一切都很好,工作。所以我去我的菜單 - >退出 - >彈出對話框詢問用戶是否退出,是的,應用程序簡單地關閉它,但是當我再次運行應用程序的值仍然在文本視圖。
不知道我做錯了:(
PS:我剛剛更新了editor.remove( 「SharedP」);與editor.clear()和問題仍然
如果你想清除整個事情,使用'editor.clear();' –
感謝我曾嘗試editor.clear();以及,但似乎沒有工作: ( – Ket
我在這裏誤解了什麼?你正在設定一個價值在'onStop()'方法中調用''exitOptionsDialog()'後面的''方法',換句話說,如果在''editor'clear()''和'editor.commit() exitOptionsDialog()'作爲值在'onStop()'中設置。 – Squonk