tony9099
tony9099

Reputation: 4727

Cannot send or retrieve extras using startactivityforresult

Activity myActivity1:

Intent i = new Intent(this, myActivity2.class);
i.putExtra("type", type);
if(type == 1)
{
    i.putExtra("namesArray", myArray);
}
startActivityForResult(i, 1);

Activity myActivity2:

//inside onResume()
if(getIntent().hasExtra("type"))
{
    Log.i(tag, "Has extra");
}
else
{
    Log.i(tag, "No extra type");
}

I always get "No extra type" why ?

Upvotes: 0

Views: 102

Answers (1)

Jay
Jay

Reputation: 156

If myActivity2 was created previously before receiving the intent, it's possible that getIntent() is returning the original intent that created the activity in the first place, which probably didn't have any extras set.

Try the following to refresh the intent:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

Upvotes: 1

Related Questions