setResult does not work when BACK button pressed

前端 未结 10 1924
南方客
南方客 2020-11-27 13:08

I am trying to setResult after the BACK button was pressed. I call in onDestroy

Intent data = new Intent();
setResult(RESULT_OK, data) 

But

10条回答
  •  一向
    一向 (楼主)
    2020-11-27 13:37

    Activity result must be set before finish() is called. Clicking BACK actually calls finish() on your activity, so you can use the following snippet:

    @Override
    public void finish() {
        Intent data = new Intent();
        setResult(RESULT_OK, data); 
    
        super.finish();
    }
    

    If you call NavUtils.navigateUpFromSameTask(); in onOptionsItemSelected(), finish() is called, but you will get the wrong result code. So you have to call finish() not navigateUpFromSameTask in onOptionsItemSelected(). wrong requestCode in onActivityResult

提交回复
热议问题