onActivityResult() is not called after finish of SecondActivity

Viewed 405

I call SecondActivity on my FirstActivity. Please see below:

    Intent intent = new Intent(this, SecondActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    activity.startActivityForResult(intent, 1);

on my SecondActivity, I finish its specific screen and setResult as RESULT_OK

    activity.setResult(Activity.RESULT_OK);
    activity.finish();

Back to my FirstActivity, I override the onActivityResult(params) method

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

    if(requestCode == 1) { //to make sure request was successful
        if (resultCode == Activity.RESULT_OK) {
            finish();
        }
    }
}

But onActivityResult() wasn't called after SecondActivity is finished. Tried debugging and at first launch of FirstActivity, that's just the time that onActivityResult() is called but on return from the SecondActivity it isn't anymore.

What might be the problem?

Thank you in advance!

2 Answers

You can't use FLAG_ACTIVITY_NEW_TASK and startActivityForResult together.

It's stated in the docs of this flag:

This flag can not be used when the caller is requesting a result from the activity being launched.

You need to create the answere intent in your second Activity, it looks like this:

            Intent answIntent = new Intent();
            setResult(RESULT_OK, answIntent); 

            finish();
Related