setResult does not work when BACK button pressed

前端 未结 10 1914
南方客
南方客 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:49

    You need to overide the onBackPressed() method and set the result before the call to superclass, i.e

    @Override
    public void onBackPressed() {
        Bundle bundle = new Bundle();
        bundle.putString(FIELD_A, mA.getText().toString());
        
        Intent mIntent = new Intent();
        mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
        super.onBackPressed();
    }
    

提交回复
热议问题