I am trying to pass an integer between activities using an intent. The source activity makes the calls info.id is the selected item from a ListView.
Intent intent = new Intent(myActivity.this, newClass.class);
intent.putExtra("selectedItem", info.id);
this.startActivity(intent);
The target activity retrieves the intent using getIntent then calls
int iSelectedItem = intent.getIntExtra("selectedItem", -1);
iSelectedItem is always -1 instead of the value passed to putExtra. Can someone tell me what I am doing wrong or do I misunderstand the use of intents?
getIntExtra returns only the default value #15596.
int diff = getIntent(). getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY); reads the value that was set for KEY_DIFFICULTY in the intent used to start the activity. Hence, diff now contains the user-selected value (or DIFFICULTY_EASY , if the activity is started through a different intent which did not set KEY_DIFFICULTY ).
The problem is that info.id will be a 'long' and is not converting to an 'int'. Try
long iSelectedItem = intent.getLongExtra("selectedItem", -1)
I don't find putIntExtra()
method. So I ended up with following:
intent.putExtra("jobId", 1);
and
Integer.parseInt(getIntent().getExtras().get("jobId").ToString());
Use try and catch to handle Exceptions.
UPDATE
Later I found that I was passing jobId as a string in putExtra()
method, therefore getIntExtra()
was always returning the default value.
So @Grant is correct. You must pass an Integer value in putExtra()
method to use getIntExtra()
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With