SharedPreferences 中无法解释的奇怪行为 Android
Strange unexplained behaviour in SharedPreferences Android
我对 sharedPreferences
并不陌生,我决定使用它们来存储用户创建的课程的标题,然后加载到 listView 中。课程标题存储在一个集合中,然后放入 sharedPreference
。这里的问题是当我输入第一门课程(作为用户)时,它会被永久保存,但添加更多课程也会保存,并且在应用程序处于活动状态时工作,但在应用程序重新启动时,sharedPreference returns回到集合中只有一个项目的初始值。
自然地,我查找了这个问题,并且能够通过使用从这个参考 https://looksok.wordpress.com/2012/09/21/sharedpreferences-not-saved-after-app-restart/
中得到的 editor.clear()
来解决它
请注意,解决方案被描述为 "the tricky one"。我真的很想知道这行是什么意思。我已经检查过这个问题,但仍然不明白为什么这个案例如此不同,每当我使用这些 类 时,这种异常都会吓到我。所以这是我的代码:
// Shared Preferences
SharedPreferences customPrefs;
// Editor for Shared preferences
SharedPreferences.Editor editor;
private CustomCourseDBHelper customDBHelper;
private final static int ADD_CUSTOM_COURSE_REQUEST = 1;
private final static int EDIT_CUSTOM_COURSE_REQUEST = 2;
private final static String TITLE_PREFS = "customCourseTitles";
private final static String TITLE_PREF_LIST = "customCourseList";
private ListView cListView;
private Set<String> allTitles = new LinkedHashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_course_list);
mToolbar = (Toolbar) findViewById(R.id.tool_bar_shadow);
cListView = (ListView) findViewById(R.id.customQuestionList);
addButton = (FloatingActionButton) findViewById(R.id.addButton);
cAdapter = new CustomCourseAdapter(this);
setSupportActionBar(mToolbar);
cListView.setAdapter(cAdapter);
//declare shared prefs
customPrefs = getSharedPreferences(TITLE_PREFS, MODE_PRIVATE);
//set on long press options
optionsList.add("Delete Course");
//set icon for add button
addButton.setImageDrawable(getResources()
.getDrawable(R.drawable.ic_add_white_24dp));
addButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(CustomCourseList.this, AddCustomCourseActivity.class);
startActivityForResult(i, ADD_CUSTOM_COURSE_REQUEST);
}
});
}
这就是节省的地方
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK && requestCode == ADD_CUSTOM_COURSE_REQUEST){
CustomCourseItem cci = new CustomCourseItem(data);
cAdapter.add(cci);
//save the provided details in a database
customDBHelper = new CustomCourseDBHelper(this, cci.getPath());
customDBHelper.addCustomCourse(cci);
editor = customPrefs.edit();
Set<String> mSet = customPrefs.getStringSet(TITLE_PREF_LIST, null);
if (mSet != null)
allTitles = mSet;
//store the given database names in a set in a shared preference
allTitles.add(cci.getPath());
editor.clear(); //this line here, without it, the sharedPref is not persistent,
//i didn't see any clear reason as to why this occurs, when i read the documentary
editor.putStringSet(TITLE_PREF_LIST, allTitles);
editor.commit();
}
//listen request for edits made to courses
if (resultCode == RESULT_OK && requestCode == EDIT_CUSTOM_COURSE_REQUEST){
if (data.getBooleanExtra("edited", false)){
restructureDbOnEdit();
}
}
}
PS。我省略了一些不必要的代码,如 UI 实现和其他 non-sharedPreference 相关的东西。
因此,即使该应用程序现在运行良好,我们也非常欢迎对此问题有任何进一步的见解。
好吧,经过适当的搜索,我终于找到了问题所在,请查看此处提供的答案。请注意,这是 android 中的错误,并且仅发生在字符串集上。谢谢
Set<String> in android sharedpreferences does not save on force close
我对 sharedPreferences
并不陌生,我决定使用它们来存储用户创建的课程的标题,然后加载到 listView 中。课程标题存储在一个集合中,然后放入 sharedPreference
。这里的问题是当我输入第一门课程(作为用户)时,它会被永久保存,但添加更多课程也会保存,并且在应用程序处于活动状态时工作,但在应用程序重新启动时,sharedPreference returns回到集合中只有一个项目的初始值。
自然地,我查找了这个问题,并且能够通过使用从这个参考 https://looksok.wordpress.com/2012/09/21/sharedpreferences-not-saved-after-app-restart/
中得到的editor.clear()
来解决它
请注意,解决方案被描述为 "the tricky one"。我真的很想知道这行是什么意思。我已经检查过这个问题,但仍然不明白为什么这个案例如此不同,每当我使用这些 类 时,这种异常都会吓到我。所以这是我的代码:
// Shared Preferences
SharedPreferences customPrefs;
// Editor for Shared preferences
SharedPreferences.Editor editor;
private CustomCourseDBHelper customDBHelper;
private final static int ADD_CUSTOM_COURSE_REQUEST = 1;
private final static int EDIT_CUSTOM_COURSE_REQUEST = 2;
private final static String TITLE_PREFS = "customCourseTitles";
private final static String TITLE_PREF_LIST = "customCourseList";
private ListView cListView;
private Set<String> allTitles = new LinkedHashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_course_list);
mToolbar = (Toolbar) findViewById(R.id.tool_bar_shadow);
cListView = (ListView) findViewById(R.id.customQuestionList);
addButton = (FloatingActionButton) findViewById(R.id.addButton);
cAdapter = new CustomCourseAdapter(this);
setSupportActionBar(mToolbar);
cListView.setAdapter(cAdapter);
//declare shared prefs
customPrefs = getSharedPreferences(TITLE_PREFS, MODE_PRIVATE);
//set on long press options
optionsList.add("Delete Course");
//set icon for add button
addButton.setImageDrawable(getResources()
.getDrawable(R.drawable.ic_add_white_24dp));
addButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(CustomCourseList.this, AddCustomCourseActivity.class);
startActivityForResult(i, ADD_CUSTOM_COURSE_REQUEST);
}
});
}
这就是节省的地方
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK && requestCode == ADD_CUSTOM_COURSE_REQUEST){
CustomCourseItem cci = new CustomCourseItem(data);
cAdapter.add(cci);
//save the provided details in a database
customDBHelper = new CustomCourseDBHelper(this, cci.getPath());
customDBHelper.addCustomCourse(cci);
editor = customPrefs.edit();
Set<String> mSet = customPrefs.getStringSet(TITLE_PREF_LIST, null);
if (mSet != null)
allTitles = mSet;
//store the given database names in a set in a shared preference
allTitles.add(cci.getPath());
editor.clear(); //this line here, without it, the sharedPref is not persistent,
//i didn't see any clear reason as to why this occurs, when i read the documentary
editor.putStringSet(TITLE_PREF_LIST, allTitles);
editor.commit();
}
//listen request for edits made to courses
if (resultCode == RESULT_OK && requestCode == EDIT_CUSTOM_COURSE_REQUEST){
if (data.getBooleanExtra("edited", false)){
restructureDbOnEdit();
}
}
}
PS。我省略了一些不必要的代码,如 UI 实现和其他 non-sharedPreference 相关的东西。 因此,即使该应用程序现在运行良好,我们也非常欢迎对此问题有任何进一步的见解。
好吧,经过适当的搜索,我终于找到了问题所在,请查看此处提供的答案。请注意,这是 android 中的错误,并且仅发生在字符串集上。谢谢 Set<String> in android sharedpreferences does not save on force close