jelic98
jelic98

Reputation: 713

startActivityForResult() returns RESULT_CANCELED

I have 2 Activity classes and 1 Non-Activity class which calls startActivityForResult() from Context passed in constructor. This is how it looks: FirstActivity -> NonActivity -> SecondActivity -> FirstActivity. In SecondActivity there is ArrayList of custom objects that needs to be passed to FirstActivity as a result. There is a problem. When onActivityResult() is called resultCode is RESULT_CANCELED, but not RESULT_OK even if setResult(RESULT_OK, intent) is called. Here is my code:

NonActivity

public void showActivity() {
    Intent intent = new Intent(request, ActivityKorak.class);
    intent.putExtra("data", fields);

    request.startActivityForResult(intent, 1);
}

SecondActivity

@Override
public void onBackPressed() {
    super.onBackPressed();

    Intent intent = new Intent();
    intent.putExtra("data", fields);
    setResult(Activity.RESULT_OK, intent);

    finish();
}

FirstActivity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    if(resultCode != Activity.RESULT_CANCELED){
        if(requestCode == 1) {
            Bundle extras = intent.getExtras();
            ArrayList<CustomInput> fields = (ArrayList<CustomInput>) extras.getSerializable("data");
        }
    }
}

Upvotes: 2

Views: 4199

Answers (2)

FractalBob
FractalBob

Reputation: 3554

In my case, the problem was fixed by adding the SHA-1 and SHA-256 certificate fingerprints given in Android Studio (click on Gradle on the right side of the AS window, then run configurations and signingReport) to your Firebase project settings->General->SDK setup and configuration.

Upvotes: 0

Danny Yassine
Danny Yassine

Reputation: 681

You must simply remove

super.onBackPressed();

in the onBackPressed Method

What is happening is that "super.onBackPressed()" is setting the result code to "RESULT_CANCELED" and finishing your activity.

Upvotes: 4

Related Questions