2

I need to pass 2 variables from one activitiy to another activity.

I have the following for the first activity:

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    Bundle bundle=new Bundle();
switch (item.getItemId()){ 

case 1: 

    bundle.putString(drinkButton, "4");
    bundle.putString(drinkType, "1");
    Intent myIntent1 = new Intent(this, DrinksList.class);
    myIntent1.putExtras(bundle);
    startActivityForResult(myIntent1, 0);
    return true;

case 2: 

    bundle.putString(drinkButton, "1");
    bundle.putString(drinkType, "2");
    Intent myIntent2 = new Intent(this, DrinksList.class);
    myIntent2.putExtras(bundle);
    startActivityForResult(myIntent2, 0);
    return true;
}
return false;

Then in the second activity i use this to get the values back, but both values are the same i.e. the same as 'drinkType' case 1 I get "1" for both and case 2 I get "2" for both when I expect to get 4,1 and 1,2.

Bundle extras = getIntent().getExtras();

        drinkButton = extras.getString(drinkButton);


        drinkType = extras.getString(drinkType);

    Toast.makeText(this, "drink Button = "+drinkButton+"  Drink Type = "+drinkType, Toast.LENGTH_LONG).show();  


} 

It seems I can't pass more than one extra. Any ideas?

user1095784
  • 117
  • 1
  • 2
  • 11
  • 1
    Double check the both `drinkButton` and `drinkType` string value, when you initialize them as `String drinkButton = "?";` – Adil Soomro Feb 11 '12 at 16:10
  • Ass Adil has suggested, check the String value for drinkButton and drinkType. I have a feeling they are the same value. It would explain why you are getting 1 for case one and 2 for case two as if they are the same the second putString(drinkType, "1/2") will be overwriting the first putString(drinkButton, "4/1"). Do you mean to have putString("drinkButton","4/1") and putString("drinkType", "1/2")? – Richard Lewin Feb 11 '12 at 16:16
  • Have you tried hardcoding a string value as a key instead of using a string variable to see if you get the same result? – bschultz Feb 11 '12 at 17:14
  • I am declaring private String drinkButton; and private String drinkType; – user1095784 Feb 11 '12 at 18:28
  • Also, just tried swapping round bundle.putString(drinkType, "2"); and bundle.putString(drinkButton, "4"); and now I get 4 for both instead of a 2 and 4. Why would one variable of a different name over write another? – user1095784 Feb 11 '12 at 18:31
  • What are you setting `drinkButton` and `drinkType` _to_? You've declared them, are you ever setting them _to_ anything? Like `drinkButton = "DrinkButton";`? – Brian Dupuis Feb 11 '12 at 19:04
  • No, I only need to set them when I putExtra . Not sure i would need to set them anywhere else. Thing is when I only use one of the variables in my putExtra there is no problem. Problem only occurs when I use more then one at a time. – user1095784 Feb 11 '12 at 19:42
  • Should I be setting drinkButton = null and drinkType = null before I them? – user1095784 Feb 11 '12 at 19:47

1 Answers1

9

If you don't assign values to the variables drinkButton and drinkType, they're both null when you use them in the first activity. In that case, your code:

bundle.putString(drinkButton, "4");
bundle.putString(drinkType, "1");

is equivalent to

bundle.putString(null, "4");
bundle.putString(null, "1");

See, you're using variables with null value as the "key" argument to putString().

The most common way to set the "key" arguments like this is to use constants. For example:

public interface IntentConstants {
    public static final String EXTRA_DRINK_BUTTON = "DrinkButton";
    public static final String EXTRA_DRINK_TYPE = "DrinkType";
}

And then in your activity, use those constants, like this:

bundle.putString(IntentConstants.EXTRA_DRINK_BUTTON, "4");
bundle.putString(IntentConstants.EXTRA_DRINK_TYPE, "1");

and to retrieve them in the second Activity:

String drinkButton = extras.getString(IntentConstants.EXTRA_DRINK_BUTTON);
String drinkType = extras.getString(IntentConstants.EXTRA_DRINK_Type);

By the way, is there a particular reason that you're passing integer values as String extras? Why not pass them as integers?

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • Worked, thanks. I have never used IntentConstants before. I will use this in future, and , yes, I have changed string to integer. It's just that early on in my project I was passing strings. Thanks again. – user1095784 Feb 12 '12 at 12:29