Reputation: 63
I want to have more than one extra added to Intent
. One to hold a double
and one to hold long
. Is this possible?
If so, how would I do it and how would I get the information from each extra?
Upvotes: 1
Views: 3203
Reputation: 1591
https://stackoverflow.com/a/11461530/776075
Devunwired is correct. But the way i see is you can keep only one value per Type. Like one string, one int, one double etc..
You are not capable of containing 2 string values. or two integers. I have experienced this on a program and i have overcome it by using one string and one Boolean.
Upvotes: 1
Reputation: 253
intent.putExtra(@ExtraDoubleKey, @ExtraDoubleValue);
intent.putExtra(@ExtraLongKey, @ExtraLongValue);
Where @ExtraDoubleKey is a string that you will use to access the extra (i.e. "price" or something), and @ExtraDoubleValue is the value of the extra (the double variable you wish to pass). Similarly for @ExtraLongKey and @ExtraLongValue.
Then to access the extras in your next activity you can use:
double doubleValue = getIntent().getExtras().getDouble(@ExtraDoubleKey);
long longValue = getIntent().getExtras().getLong(@ExtraLongKey);
to get the value of the double extra with the key @ExtraDoubleKey.
Upvotes: 1
Reputation: 63293
You can add as many extras to an Intent
as your heart desires, they are all just key value data:
Intent intent = new Intent();
intent.putExtra("name", "MyName");
intent.putExtra("age", 35);
intent.putExtra("weight", 155.6);
And they can be retrieved using the same key names:
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
double weight = intent.getDoubleExtra("weight", 0.0);
Upvotes: 10
Reputation: 5640
You can use Bundle
and pass it as a parameter to the Intent.
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent
Upvotes: 0