title: Fragment inside ViewPager incorrectly setting state
body: <p>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.</p>
  
  <p>in onCreateView() :</p>
  
  <pre><code>    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 &amp;&amp; answerValue &gt; 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);
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>The problem arises when I do this:</p>
  
  <ol>
  <li>select a checkbox so that it becomes "checked"</li>
  <li>move two pages away (so that the original fragment gets destroyed)</li>
  <li>move back to the original fragment position so that it is recreated.</li>
  </ol>
  
  <p>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. </p>
  
  <p>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?</p>
  
  <p>In other words, after step 1 (selecting checkbox), I see</p>
  
  <pre><code>07-02 14:42:07.334: I/System.out(21487): onCheckedChanged Q4A1 true
  </code></pre>
  
  <p>But at step 3 I see (after initially being set to "true" from the datamodel)</p>
  
  <pre><code>07-02 14:42:10.474: I/System.out(21487): onCheckedChanged Q4A1 false
  </code></pre>
  
  <p>...this change happens immediately and without clicking on the checkbox.</p>
  
  <p>Any help appreciated.</p>