Fragment inside ViewPager incorrectly setting state
android, android-viewpager, checkbox, fragment, state
Solution
a. Try calling `setSaveEnabled(false);` on CheckBox.
b. Try adding:
answerCheckButton.setOnCheckedChangeListener(null);
before
answerCheckButton.setChecked(isChecked);
then add new listener.
Problem
I have a weird problem with a ViewPager containing multiple fragments. Each fragment shows a question with multiple possible answers (checkboxes). When a checkbox state is changed, it's value stored in a hashmap so that if the the user uses ViewPager to slide away and then back again, the checked/unchecked state of each checkbox can be restored. in onCreateView() : ``` ViewGroup answersRow = (ViewGroup) view.findViewById(R.id.answers_row); //loop over each answer and add a checkbox for(Answer answer : answers) { View answerView = inflater.inflate(R.layout.view_answer, null); final CheckBox answerCheckButton = (CheckBox) answerView.findViewById(R.id.answerCheckButton); //set correct value for answer based on datamodel String tagName = "Q" + question.getNumber() + "A" + answer.getNumber(); Integer answerValue = getDataModel().getAnswer(tagName); boolean isChecked = (answerValue != null && answerValue > 0); System.out.println("answer " + answerCheckButton.getTag() + " " + isChecked); answerCheckButton.setTag(tagName); answerCheckButton.setChecked(isChecked); answerCheckButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { System.out.println("onCheckedChanged " + buttonView.getTag() + " " + isChecked); String answerTag = (String) buttonView.getTag(); Integer answerValue = isChecked ? 1 : 0; getDataModel().setAnswer(answerTag, answerValue); } }); answersRow.addView(answerView); } ``` I'll omit the the layout files as they are trivial. The app works fine initially when paging between fragments and tapping on checkboxes. The datamodel is correctly updated. The problem arises when I do this: - select a checkbox so that it becomes "checked" - move two pages away (so that the original fragment gets destroyed) - move back to the original fragment position so that it is recreated. What happens is: the state is CORRECTLY set from my data model (i.e. to "checked"), but then an immediate subsequent call happens on onCheckedChanged and the checkbox gets set incorrectly (i.e. to "unchecked"). Interestingly, it's always gets set the value of the last checkbox in the list. It's as though Android is trying to remember the state of the checkboxes and automatically setting them, but because they all have the same ID (being inflated from xml), it is only taking the value from the last checkbox in the list. I am NOT using onSaveInstanceState or anything like that so I don't understand why the checkboxes are being automatically reset. Can I prevent Android from changing my checkbox states in this way? In other words, after step 1 (selecting checkbox), I see ``` 07-02 14:42:07.334: I/System.out(21487): onCheckedChanged Q4A1 true ``` But at step 3 I see (after initially being set to "true" from the datamodel) ``` 07-02 14:42:10.474: I/System.out(21487): onCheckedChanged Q4A1 false ``` ...this change happens immediately and without clicking on the checkbox. Any help appreciated.