子活动返回的结果代码不正常

时间:2015-02-03 11:10:10

标签: android onactivityresult start-activity

我有一个活动,可以为结果启动子活动。启动子活动时,在其finish()方法中,我将一些数据添加到包bunConnAssets

然后将此捆绑包添加到意图intConnAssets,最后调用setResult(RESULT_OK, intConnAssets);

问题是,在运行时,当我关闭子活动,因此调用其finish()方法时,mainActivity中的onActivityResult被调用,但其中的switch-case被调用不叫。但是

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

被调用,这意味着从subActivity返回的resutCode不正常

为什么会这样?

子活动

@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;
    }
}

2 个答案:

答案 0 :(得分:2)

您在第一行调用super.finish(),因此可能无法执行其余代码。

在代码末尾调用super方法。

答案 1 :(得分:1)

尝试使用以下代码

@Override
public void 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.");
    }
    super.finish();
}