@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設備上的後退按鈕或退出應用程序,它似乎總是重置應用程序,並刪除所有文字意見。如何保存我的文字瀏覽,以便在按下或退出應用程序時再次顯示它們?謝謝。後退按鈕刪除我所有的textviews
我在提交中添加,它似乎仍然無法正常工作。 – dk3512
我已更新我的答案,請嘗試更新onPause方法和OnDestroy –