How to return a result through multiple activities

前端 未结 4 1664
南方客
南方客 2020-12-12 13:59

in some part of my application there is a structure of activities like this:

\"enter<

相关标签:
4条回答
  • 2020-12-12 14:16

    Yup, great formatting. And you can -- and probably should -- definitely call startActivityForResult() from each of Activity A, B, and C (and don't finish() right away). In B and C you can check for a successful result and finish(), passing the result on back to A.

        @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if((resultCode == RESULT_OK) && (requestCode == MY_RESULT_CODE)) {
            setResult(RESULT_OK, data);
            finish();
        }
    }
    

    If you want B and C to disappear regardless, do the following instead:

        @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        setResult(resultCode, data);
        finish();
    }
    
    0 讨论(0)
  • 2020-12-12 14:20

    I know this is a really old question but I wanted to put in a valid solution, use onNewIntent() and treat it as onActivityResult().

    In activity D you would structure your intent as

    Intent intent = new Intent(yourContext, Activity_A.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("someName", data);
    startActivity(intent);
    finish();
    

    and then in Activity_A

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // update your UI
        intent.getSerializableExtra(...
    }
    
    0 讨论(0)
  • 2020-12-12 14:23

    You could do B and C as dialogs that are fired from A and only if B and C are ok, you run D with startActivityForResult()

    0 讨论(0)
  • 2020-12-12 14:28

    You might like to make use of the intent flag FLAG_ACTIVITY_FORWARD_RESULT as described in Intent when starting activities B and C

    public static final int FLAG_ACTIVITY_FORWARD_RESULT

    Since: API Level 1

    If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.

    That way A should pick up any data sent back in the extras sent back from D

    0 讨论(0)
提交回复
热议问题