title: Android - PagerAdapter not saving RadioButton's state
body: <p>I have a very peculiar problem. I understand that while in the foreground the member variables of an Activity remain untouched (don't need saving to persist). Now, I have a <code>PagerAdapter</code> that represents a quiz with a question and three answers. I've made various objects to represent the question and answers.</p>
  
  <p>A <code>Question</code> always has a list of exactly 3 <code>Answer</code>'s. This is how I do it: (note: some code was left out for readability)</p>
  
  <pre><code>public class QuizPagerAdapter
  {
      List&lt;Question&gt; questionList;
      Context context;
  
      public QuizPagerAdapter (List&lt;Question&gt; list, Context ctx) {
          questionList = list;
          context = ctx;
      }
  
      @Override
      public Object instantiateItem (View pager, int position) {
          // Create various views (ScrollView, LinearLayout, etc)
  
          final Question q = questionList.get(position);
  
          RadioGroup group = new RadioGroup(context);
          for (int i = 0; i &lt; 3; i++) {
              Answer a = q.getAnswer(i);
  
              RadioButton rb = new RadioButton(context);
              rb.setId(i);
              rb.setText(a.getText());
  
              rb.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      RadioButton btn = (RadioButton) v;
                      q.unselectAll(); // unselects all Answer's
  
  
                      q.getAnswer(btn.getId()).setSelected(true);
                  }
              });
  
              if (a.isSelected()) rb.setChecked(true);
          }
          return view;
      }
  }
  </code></pre>
  
  <p>When I use this in my Activity, I get a nice list of Questions with three Answers. Now, when I select one Answer, the RadioButton is checked. OK, now, I scroll to the next Question and check that one, then go back to the previous Question and there the answer I checked in the first place is now <strong>not</strong> checked.</p>
  
  <p>Now, this doesn't always happen, it happens sometimes, but that sometimes is enough that it can cause a problem. The Activity is never placed in the background.</p>
  
  <p>I understand that Android will destroy Views in the PagerAdapter to conserve memory, thus destroying the RadioButton, this is why I have this snippet:</p>
  
  <pre><code>if (a.isSelected()) rb.setChecked(true);
  </code></pre>
  
  <p>But it is still happening. I've tried logging everything, debugging but I cant seem to get to the source of the problem. Maybe someone here has come across a similar problem.</p>
  
  <p>Any help is appreciated,
  Thank you in advance!</p>