Navigating from Activity A->B->C - How to pass data from C to A in onBackPressed()?

前端 未结 3 1335
春和景丽
春和景丽 2021-01-15 18:37
  1. From Activity A call Activity B
  2. from B to C and while calling Activity C
  3. I call finish for Activity B
3条回答
  •  难免孤独
    2021-01-15 18:50

    Don't call finish() from B when opening C. Instead, call C with startActivityforResult() and then pass the data back through B when onActivityResult() in B is triggered. Something like:

    ActivityB {
        onCreate(Bundle) {
            startActivityForResult(ActivityC, 0);
        }
    
        onActivityResult(int, int, Intent){
            setResult(resultCode, data);
            finish();
        }
    }
    

    Edit:

    Apparently adding the flag Intent.FLAG_ACTIVITY_FORWARD_RESULT when calling B should actually achieve your desired result as well.

    Learn somethin' new every day...

提交回复
热议问题