Android 关闭应用程序数小时后文本视图消失
我正在创建一个待办应用程序。我可以看到,当你关闭应用程序并在几个小时后重新打开它时,我的文本视图正在保存,但当你在关闭应用程序很多小时后重新打开应用程序时,它们将消失。你知道问题出在哪里吗Android 关闭应用程序数小时后文本视图消失,android,Android,我正在创建一个待办应用程序。我可以看到,当你关闭应用程序并在几个小时后重新打开它时,我的文本视图正在保存,但当你在关闭应用程序很多小时后重新打开应用程序时,它们将消失。你知道问题出在哪里吗 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_to_do_main);
@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;
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_main);
setResult(结果被取消);
布局=(线性布局)findViewById(R.id.main);
如果(savedInstanceState!=null){
int counter=PreferenceManager.getDefaultSharedReferences(this.getInt)(“键2”,0);
Log.i(“创建时计数器”,Integer.toString(计数器));
对于(int i=0;i
请提供一份演示您的问题的报告。在这种情况下,这将包括将数据从某个持久存储(例如数据库)加载到TextView
小部件中的代码,以及将数据放入持久存储的代码。为什么要在onPause
上的SharedReferences
中将计数器设置为0
?