Search code examples
androidonactivityresultstart-activity

the result code returned from sub activity is not OK


I have an activity that starts a sub activity for results. when the sub activity is launched, in its finish() method i add some data to a bundle bunConnAssets

and then add this bundle to an intent intConnAssets and finally call setResult(RESULT_OK, intConnAssets);

the problem is, at run time, when i close the sub activity, and therefore its finish() method is called, the onActivityResult in the mainActivity is called but the switch-case inside it is not called. but the

else {
            Log.d(TAG, "resultCode != RESULT_OK");
        }

is called which means the resutCode returned from the subActivity is not OK

why that is happening?

SubActivity:

@Override
public void finish() {
    // TODO Auto-generated method stub
    super.finish();
    Log.w(TAG, "@finish().");

    if (this.mqttSettingsDB != null) {
        Log.d(TAG, "this.mqttSettingsDB is not null.");

        int[] id = this.mqttSettingsDB.getIDs();
        Bundle bunConnAssets = new Bundle();
        Intent intConnAssets = new Intent();
        ....
        ....
        ....
        intConnAssets.putExtras(bunConnAssets);
        setResult(RESULT_OK, intConnAssets);

    } else {
        Log.wtf(TAG, "this.mqttSettingsDB is null.");
    }
}

onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "onActivityResult");

    switch (requestCode) {
    case REQUEST_CODE:
        if (resultCode == RESULT_OK) {
            if (data != null) {
                if (data.hasExtra("bunConnAssets")) {
                    Log.d(TAG, "Bundle exists");
                } else {
                    Log.d(TAG, "Bundle does not exist");
                }
            }
        } else {
            Log.d(TAG, "resultCode != RESULT_OK");
        }
        break;

    default:
        Log.e(TAG, "Unexpected RequestCode.");
        break;
    }
}

Solution

  • You are calling super.finish() at the first line, so the rest code may not be executed.

    Call the super method at the end of your code.