后退按钮删除我的所有文本视图

时间:2015-12-01 20:21:35

标签: android

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    layout = (LinearLayout) findViewById(R.id.main);
    if (savedInstanceState != null) {
        int counter = PreferenceManager.getDefaultSharedPreferences(this).getInt("key2", 0);
        for (int i = 0; i < counter; i++) {
            layout.addView(createToDo(PreferenceManager.getDefaultSharedPreferences(this).getString(Integer.toString(counter), "")), counter);
        }
    }
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
    LinearLayout layout = (LinearLayout) findViewById(R.id.todoMain);
    super.onRestoreInstanceState(savedInstanceState);
    textList = savedInstanceState.getStringArrayList("key");
    for (String t: textList) {
        todoList.add(createToDo(t));
        layout.addView(createToDo(t));
    }
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState){
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putStringArrayList("key", textList);
}

protected void onPause() {
    super.onPause();
    int counter = 0;
    for (String t: textList) {
        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putString(Integer.toString(counter), t);
        counter++;
    }
    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().commit();
    SharedPreferences sharedPreferencesSettings2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE);
    SharedPreferences.Editor editor2 = sharedPreferencesSettings2.edit();
    editor2.putInt("key2", counter);
    editor2.commit();
}

protected void onResume() {
    super.onResume();
    layout = (LinearLayout) findViewById(R.id.todoMain);
    int counter = PreferenceManager.getDefaultSharedPreferences(this).getInt("key2", 0);
    for (int i = 0; i < counter; i++) {
        layout.addView(createToDo(PreferenceManager.getDefaultSharedPreferences(this).getString(Integer.toString(i), "")), i);
    }
}

protected void onDestroy() {
    super.onDestroy();
    int counter = 0;
    for (String t: textList) {
        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putString(Integer.toString(counter), t);
        counter++;
    }
    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().commit();
    SharedPreferences sharedPreferencesSettings2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE);
    SharedPreferences.Editor editor2 = sharedPreferencesSettings2.edit();
    editor2.putInt("key2", counter);
    editor2.commit();
}

因此,当我按下Android设备上的后退按钮或退出应用程序时,它似乎总是重置应用程序并删除所有文本视图。如何保存我的文本视图,以便在按下或退出应用程序时它们会再次显示?感谢。

2 个答案:

答案 0 :(得分:2)

您在更新后提交SharedPreferences吗?

看一下下面的例子。

    SharedPreferences sharedPreferencesSettings = context.getSharedPreferences("sharedPreferencesSettings", Context.MODE_PRIVATE);

    SharedPreferences.Editor editor = sharedPreferencesSettings.edit();

    editor.putBoolean("showRedColor", true);

    editor.commit();

尝试修改以下方法。

protected void onPause() {
    super.onPause();
    int counter = 0;
    //get an editor
    SharedPreferences.Editor editor1 = getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
    for (String t: textList) {
        //set key values
        editor1.putString(Integer.toString(counter), t).commit();
        counter++;
    }
    //save changes
    editor1.commit();
    SharedPreferences sharedPreferencesSettings2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE);
    SharedPreferences.Editor editor2 = sharedPreferencesSettings2.edit();
    editor2.putInt("key2", counter);
    editor2.commit();
}

protected void onDestroy() {
    super.onDestroy();
    int counter = 0;
    //get an editor
    SharedPreferences.Editor editor1 = getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
    for (String t: textList) {
        //set key values
        editor1.putString(Integer.toString(counter), t).commit();
        counter++;
    }
    //save changes
    editor1.commit();
    SharedPreferences sharedPreferencesSettings2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE);
    SharedPreferences.Editor editor2 = sharedPreferencesSettings2.edit();
    editor2.putInt("key2", counter);
    editor2.commit();
}

在您的示例中,您没有保存在正确的编辑器中。

for (String t: textList) {
    //Get editor, set the key, but you are not committing the changes, so they are lost
    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putString(Integer.toString(counter), t);
    counter++;
}
//Get a new editor and you are committing, but you haven't edit any of the key value, so you are not commint any changes
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().commit();

答案 1 :(得分:0)

您没有在SharedPreferences编辑器上调用commit(),因此没有保存任何内容。

编辑:此外,如果您定位在Lollipop之上,您可以考虑使用PersistableBundles进行保存并在重新打开应用后检索状态。