fgroeger
fgroeger

Reputation: 432

Android onActivityResult() doesn't get called

I've got a problem I don't know how to solve. This is my code of the MainActivity.java which starts a new activity:

Intent intent = new Intent(this, AssociationActivity.class);
    intent.putExtra("data", json);
    startActivityForResult(intent, 0);

Additionaly, I have the following onActivityResult()-method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    System.out.print("works outside!");
    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            System.out.print("works inside!");
        }
        if (resultCode == RESULT_CANCELED) {

        }
    }
}

And this is where the result should be sent to the MainActivity:

Intent returnIntent = new Intent();
returnIntent.putExtra("result", string);
setResult(RESULT_OK, returnIntent);
finish();

It seems that the onActivityResult() doesn't even get called as I don't have any output on the console.

In the AndroidManifest.xml I neither use the singleIntent nor the noHistory parameter.

Any suggestions why it doesn't work?

Upvotes: 3

Views: 652

Answers (2)

fgroeger
fgroeger

Reputation: 432

Okay, I found the issue, the wasn't any issue at all, I replaced the System.out statements with the Log from Android it's working all the time, Android just delayed the System.out, but I don't know why. Thanks @Rene Juuse for that tip!

Upvotes: 1

Krishna V
Krishna V

Reputation: 1821

requestCode change from 1 to 0. as shown below

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    System.out.print("works outside!");
    if (requestCode == 0) {
        if(resultCode == RESULT_OK){
            System.out.print("works inside!");
        }
        if (resultCode == RESULT_CANCELED) {

        }
    }
}

Upvotes: 2

Related Questions