title: Can't Change Variable in onClick
body: <p>I'm trying to use an <code>int</code> to open up different graphs. I want this <code>int</code> to change values when a button is pressed, but it isn't changing. Here's my code:</p>
  
  <pre><code>public class PatientDemographics extends Activity
  {
      private GraphicalView mChartView;
      int num = 1;
      int tabnum = 0;
      int finalnum;
  
      public void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.tabviews);
  
          LinearLayout ll = (LinearLayout) findViewById(R.id.buttons);
  
          Button button1 = new Button(this);
          button1.setText("Disease Type");
          ll.addView(button1);
          button1.setOnClickListener( new View.OnClickListener()
          {
              public void onClick(View v)
              {
                  num = 1;
                  Intent i = new Intent(PatientDemographics.this, TabHelper.class);
                  i.putExtra("num", tabnum);
                  startActivity(i);   
              }
          });
  
          Button button2 = new Button(this);
          button2.setText("Gender");
          ll.addView(button2);
          button2.setOnClickListener(new View.OnClickListener()
          {
              public void onClick(View v)
              {
                  num = 2;
                  Intent i = new Intent(PatientDemographics.this, TabHelper.class);
                  i.putExtra("num", tabnum);
                  startActivity(i);
              }
          });
  
          Button button3 = new Button(this);
          button3.setText("Age at Diagnosis");
          ll.addView(button3);
          button3.setOnClickListener(new View.OnClickListener()
          {
              public void onClick(View v)
              {
                  num = 3;
                  Intent i = new Intent(PatientDemographics.this, TabHelper.class);
                  i.putExtra("num", tabnum);
                  startActivity(i);
              }
          });
  
          Button button4 = new Button(this);
          button4.setText("Treatment Status");
          ll.addView(button4);
          button4.setOnClickListener(new View.OnClickListener()
          {
              public void onClick(View v)
              {
                  num = 4;
                  Intent i = new Intent(PatientDemographics.this, TabHelper.class);
                  i.putExtra("num", tabnum);
                  startActivity(i);
              }
          });     
      }
  }
  </code></pre>
  
  <p>Please ignore <code>tabnum</code>. It is used to determine the tab that everything is in, which is the same. Also, I originally created the buttons with xml, but each tab needed a different number of buttons and I didn't know how to do that so I just made them with code.</p>
  
  <p>You can see that I initialize <code>num</code> to <code>1</code>, then try to change it inside the <code>onClick</code> methods, but it never changes. Please let me know if you need any more information.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>protected void onSaveInstanceState(Bundle outState)
  {
        outState.putInt("num", num);
        super.onSaveInstanceState(outState);
  }
  </code></pre>
  
  <p>And then in onCreate:</p>
  
  <pre><code>    super.onCreate(savedInstanceState);
      if(savedInstanceState != null)
      {
          num = savedInstanceState.getInt("num");
      }
  </code></pre>