oregonduckman
oregonduckman

Reputation: 191

getIntExtra always returns the default value

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?

Upvotes: 19

Views: 46784

Answers (7)

oyeraghib
oyeraghib

Reputation: 1075

In my case, I converted the value inside putExtra to String from Int and Long and while receiving, received it as String only. Can't figure out why but Integer, Long were showing defaultValues while after converting to String it works.

Example :

intent.putExtra("code", response.data?.code.toString())
val code = intent.getStringExtra("code")

Upvotes: 0

Ahamadullah Saikat
Ahamadullah Saikat

Reputation: 4644

    int sub_menu_id = 0;
    int question_part = 0;

    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            sub_menu_id = -1;
            question_part = -1;
        } else {
            sub_menu_id = extras.getInt("sub_menu_id");
            question_part = extras.getInt("question_part");
        }
    }

    Log.d("DREG", "sub_menu_id: " + sub_menu_id + "\nquestion_part: " + question_part);

Upvotes: 0

grandia
grandia

Reputation: 737

In my case, it was because I created the object with mId member variable declared as string

public class Application {

private String mId;
....
}

intent.putExtra("id", myApplication.getId());

and as such, the Extra is passed as string. simply change your member variable to int, you get the idea ;)

Upvotes: 0

Leonardo Sapuy
Leonardo Sapuy

Reputation: 2740

Easy:

Bundle bundle = getIntent().getExtras();
int iSelectedItem = bundle.getInt("selectedItem", -1);   

Now, if you're using StartActivityForResult and you want to return some data, from the child activity, remind that you have to use onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
    int iSelectedItem = intent.getExtras.getInt("selectedItem", -1);             
    Log.d("DEBUG", "check iSelectedItem = " + iSelectedItem);
  }
}

Remember, is the same way like you get extras from other Activity, just using bundle.getInt, in this example, getExtras return a bundle, so, across this bundle, you can get any data that you have sent from the resultIntent.

Upvotes: 0

Grant
Grant

Reputation: 261

The problem is that info.id will be a 'long' and is not converting to an 'int'. Try

long iSelectedItem = intent.getLongExtra("selectedItem", -1)

Upvotes: 26

Dayvid Oliveira
Dayvid Oliveira

Reputation: 1217

I had this problem and it was a simple thing.

Check if you're using onActivityResult ... than, you don't have to use getIntent() to get the extras, you have to use the intent you pass as parameter.

In your case should be something like this:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (resultCode == RESULT_OK) {
        int iSelectedItem = intent.getIntExtra("selectedItem", -1);             
        Log.d("DEBUG", "check iSelectedItem = " + iSelectedItem);
    }
}

observe that I'm not using getIntent(), but the argument intent.

(PS: if you're calling a lot of activities expecting results, is better to check if the intent is != null)

I hope it helps.

Upvotes: 1

Vikas
Vikas

Reputation: 24332

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.

Upvotes: 12

Related Questions