onActivityResult() has Intent data as null after an Activity has finished

后端 未结 2 1696
旧时难觅i
旧时难觅i 2021-01-04 07:56

Hi there I am calling an startActivityForResult() and trying to process the result in the onAcvityResult() method. However, the Intent data is null and result is RESULT_CANC

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 08:51

    Alex,

    I think you want to remove the called to finish() in your onBackPressed() method, and replace it with the call to super.onBackPressed(). I believe the call to super.onBackPressed() is calling finish and you are never getting a chance to call setResult().

    Try...

    @Override
    public void onBackPressed() {
    
        Intent data = new Intent();
        Bundle bundle = new Bundle();
    
        bundle.putParcelable("name", la);
        data.putExtras(bundle);
    
        if (getParent() == null) {
            setResult(Activity.RESULT_OK, data);
        } else {
            getParent().setResult(Activity.RESULT_OK, data);
        }
    
        super.onBackPressed();
    }
    

提交回复
热议问题