0

I am developing a PreferenceActivity where I can add CheckBoxPreference instances according to some dynamic content. I also maintain a count of the checked preferences in the activity. Everything is good when I load the application for the first time. But when I select some 2-3 check boxes and deploy the application code again on emulator, the previously selected check boxes just remain checked when I come back to same activity. The same happens when I move to and from some activities within the same application!

To prevent this, every time when I create an instance of CheckBoxPreference, I explicitly call .setChecked(false)!!! But still those previously checked preferences remain checked only!

I can't understand the behavior! Does it bring those values checked from cache! But I explicitly call .setChecked(false), what about that then?

Thank you very much,

Ketan

EDIT : Here is my code:

    String content = null;  
    for (String title : titles) {  
        checkBoxPref = new CheckBoxPreference(this);  
        checkBoxPref.setKey(title);  
        checkBoxPref.setTitle(title);  
        <b>checkBoxPref.setDefaultValue(Boolean.FALSE); //Line # 1 set default to false   
        checkBoxPref.setChecked(false);//Line # 2 set checked = false  </b>
        content = config.getString(title, "Yet to define!");  
        if (content.length() > 30) {  
            content = content.substring(0, 30);  
        }  
        checkBoxPref.setSummary(content);  
        checkBoxPref.setOnPreferenceChangeListener(new MyCheckBoxChangeListener());  
        checkBoxPreferenceList.add(checkBoxPref);  
        inlinePrefCat.addPreference(checkBoxPref);  
    }    

This code is executed everytime I app comes back to this activity! But still had I checked 2-3 checkboxes before leaving this activity, they will remain in the same state when app comes back to this! Please have a look at the code, I am already setting setChecked(false) explicitly, but still can't get them unchecked!

Please suggest some solution! I am stuck!

Ketan
  • 738
  • 2
  • 9
  • 20
  • While navigating to other activities, do you call `finish(...)` method of your Preferences Activity ? – Salman Khakwani Oct 18 '13 at 18:49
  • Thanks... Yes, after your suggestion, I tried this, finish(), while going out from this activity! Still the result is the same only. – Ketan Oct 19 '13 at 07:53

1 Answers1

0

You will probably have to post some code to get a better answer. I had this same issue when I had my checkbox inside my adapter. I got around the issue by handling the checkbox with an onClickListener:

CheckBox menuOption = (CheckBox) v.findViewById(R.id.list_checkbox);
menuOption.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v)
    {            
        CompoundButton cb = (CompoundButton) v;
        // code to set preference goes here 
        // can check if true or false with cb.isChecked()
    }});
tim.paetz
  • 2,635
  • 20
  • 23