Empty intent extras in onActivityResult

2020-03-26 06:04发布

问题:

I have two activities.

First calls second like this:

Intent intent = new Intent(this, Second.class);
startActivityForResult(intent, 1);

returning data in second one:

Intent intent = new Intent();
intent.putExtra("a", "la-la-la");
setResult(RESULT_OK, intent);
finish();

and trying to receive this data in first activity:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

        if ((resultCode == Activity.RESULT_OK) && (currentTask != null)) {
            //Here is empty intent extas!!!

            Log.d("OrderActivity", "RESULT!!");
        }
    }

So, request code and response code returned, but intent extra is empty..??

回答1:

Just call this lines before calling super.onFinish() or wherever you exit the previous activity

Intent intent = getIntent();
intent.putExtra("Date",dateSelected);
setResult(RESULT_OK, intent);