1

I'm trying to use an int to open up different graphs. I want this int to change values when a button is pressed, but it isn't changing. Here's my 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);
            }
        });     
    }
}

Please ignore tabnum. 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.

You can see that I initialize num to 1, then try to change it inside the onClick methods, but it never changes. Please let me know if you need any more information.

EDIT:

protected void onSaveInstanceState(Bundle outState)
{
      outState.putInt("num", num);
      super.onSaveInstanceState(outState);
}

And then in onCreate:

    super.onCreate(savedInstanceState);
    if(savedInstanceState != null)
    {
        num = savedInstanceState.getInt("num");
    }
Seth
  • 23
  • 4

2 Answers2

1

You start a new Activity of the same type as a result of the button clicks. In the new Activity the variable is initialized to 1. If you want to use the new value in the new Activity, you should get it from the Intent and assign the variable in the onCreate() method of the Activity.

You should add something like this to onCreate():

num = (int)this.getIntent().getExtras().get("num");
Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thanks for the suggestion, but I'm trying to use num in the same PatientDemographics class, just in the onResume method. The intent part of the button click is working fine. – Seth Jul 23 '12 at 14:33
  • If you attempt to read the num when you return to the original Activity that changed its value, it is possible that the operating system already released that object, due to memory shortage. In that case the original Activity object is re-created when you go back to that Activity, and the state of the object is lost. You can keep the value of num by implementing onSaveInstanceState(Bundle outState) with something like outState.putInt("num", num); Then you can restore that value in your onCreate method. – Eran Jul 23 '12 at 18:55
  • I think this is what I need, however I don't really understand how to implement what you are saying (I'm a beginner). I edited the question to show you what I did. Can you please tell me what I'm doing wrong? Thank you very much. – Seth Jul 23 '12 at 20:09
  • Well, the only thing that doesn't look right in the code you added is that in onSaveInstanceState you should call super before you add your variable. I'm not sure if that's the reason it didn't work, but it's possible. – Eran Jul 23 '12 at 22:18
0

I think that possibly you are not verifying that num is being changed via the debugger. Because I am doing the exact same thing pretty much as you in my code. I would recommend putting a breakpoint on the lines that change num and see if num changes. I assume the start activity is causing you to lose the num value.

David Corrado
  • 363
  • 3
  • 19
  • Yes, startActivity is causing me to lose the num value. Do you know of anything that I can do to fix this? – Seth Jul 23 '12 at 14:39
  • However, if I move the num = x statement after the startActivity(i), num changes within the method. It just never stays as that number for use in onResume() – Seth Jul 23 '12 at 15:49
  • You have a few different options. I would really look into if you need to start the activity again and you could not just run a function that updates the activity. Also you can use shared preferences: http://android-er.blogspot.com/2011/01/example-of-using-sharedpreferencesedito.html You can also pass the variable to the activity and retrieve it in the activity as shown below http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – David Corrado Jul 23 '12 at 20:03