I have an app that uses SharedPreferences to keep track of the Users collection of items. But I've been getting complaints that they enter their items using a checkbox and when they restart the app their collection shows as empty.
Here is the code for the checkboxs
SharedPreferences data;
data = getSharedPreferences(filename, 0);
checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(this);
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
switch (buttonView.getId()) {
case R.id.checkbox:
bHave = checkbox.isChecked();
if (bHave == true) {
SharedPreferences.Editor e = data.edit();
e.putBoolean(have, bHave);
e.commit();
} else {
SharedPreferences.Editor e = data.edit();
e.putBoolean(have, bHave);
e.commit();
}
break;
}
}
That writes to SharedPreferences as soon as the box is checked, so I know the data gets saved.
And here is the code to populate the checkbox
private void myGetChecked() {
// TODO Auto-generated method stub
bHave = data.getBoolean(have, false);
if (bHave == true) {
checkbox.setChecked(true);
}
}
Which should get the data from the SharedPreferences.
A few points...
Anyone have any idea why some users are getting their data erased? And if you need any more info, let me know.
After I asked a few of the users whether they still had the issue, one replied that it was fixed. They did not say whether they had uninstalled the app before updating or whether they cleared the app data.
Looks like this wasn't an issue at all, which was why I couldn't replicate it.