LA_
LA_

Reputation: 20409

How to get additional (intent) data from the second activity?

I use the following code to pass data between activities:

ActivityOne.class

Intent mIntent = new Intent(getBaseContext(), ActivityTwo.class);
mIntent.putExtra("test", test_value);
startActivityForResult(mIntent, 0);

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    Log.i(TAG, "Result: "+resultCode); // OK
    final String test_value = data.getExtras().getString("test"); // fails here since data is null
    Log.i(TAG, "Test: "+test_value);

}

ActivityTwo.class

@Override
protected void onStart() {
    super.onStart();
    ...
    setResult(result); // pass the result back to ActivityOne
    finish(); // yes, I close it immediately after start ;)

So, how should I properly pass Intent data (test_value in the code above) from ActivityOne to ActivityTwo (this part works well) and then back to ActivityOne (this part doesn't work, data at onActivityResult is null)?

Upvotes: 0

Views: 55

Answers (1)

Blundell
Blundell

Reputation: 76486

You need to pass the Intent back.

Try:

 setResult(result, getIntent());

This will send the original intent that started Activity2 (i.e. created in Activity1) back to Activity1

Reference Activity.getIntent

Upvotes: 1

Related Questions