我正在创建一个待办事项应用。当我关闭应用程序并在几小时后重新打开应用程序时,我可以看到我的文本视图正在保存,但是当您在关闭应用程序几个小时后重新打开应用程序时,它们会消失。关于问题是什么的任何想法?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_main);
setResult(RESULT_CANCELED);
layout = (LinearLayout) findViewById(R.id.main);
if (savedInstanceState != null) {
int counter = PreferenceManager.getDefaultSharedPreferences(this).getInt("key2", 0);
Log.i("counter on create", Integer.toString(counter));
for (int i = 0; i < counter; i++) {
layout.addView(createToDo(PreferenceManager.getDefaultSharedPreferences(this).getString(Integer.toString(i), "")), i);
}
}
}
protected void onPause() {
super.onPause();
SharedPreferences sharedPreferencesSettings2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE);
SharedPreferences.Editor editor2 = sharedPreferencesSettings2.edit();
editor2.putInt("key2", 0);
editor2.commit();
}
protected void onResume() {
super.onResume();
layout = (LinearLayout) findViewById(R.id.main);
SharedPreferences prefs2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE);
int counter = prefs2.getInt("key2", 0);
SharedPreferences prefs = this.getSharedPreferences("PREFERENCE", MODE_PRIVATE);
for (int i = 0; i < counter; i++) {
layout.addView(createToDo(prefs.getString(Integer.toString(i), "")));
textList.add(prefs.getString(Integer.toString(i), ""));
}
}
protected void onDestroy() {
super.onDestroy();
int counter = 0;
SharedPreferences.Editor editor1 = getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit();
for (String t: textList) {
editor1.putString(Integer.toString(counter), t).commit();
counter++;
}
editor1.commit();
SharedPreferences sharedPreferencesSettings2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE);
SharedPreferences.Editor editor2 = sharedPreferencesSettings2.edit();
editor2.putInt("key2", counter);
editor2.commit();
}
private TextView createToDo(String subject) {
final TextView todo = new TextView(this);
final LinearLayout layout = (LinearLayout) findViewById(R.id.main);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT , 180);
todo.setLayoutParams(params);
todo.setId(counter1);
counter1++;
params.setMargins(0, 1, 0, 1);
todo.setText(subject);
todo.setTextSize(18);
todo.setTextColor(Color.WHITE);
todo.setBackgroundColor(getResources().getColor(R.color.blue2));
todo.setGravity(Gravity.CENTER_VERTICAL);
todo.setPadding(25, 0, 0, 0);
todo.setTypeface(null, Typeface.BOLD);
todo.setOnTouchListener(new OnSwipeTouchListener(this, todo, layout) {
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void onSwipeRight() {
todo.animate().translationX(500).alpha(0f).setDuration(250).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
layout.removeView(todo);
}
})
.start();
}
});
return todo;
}