本文介绍了从活动A-> B-> C导航-如何在onBackPressed()中将数据从C传递到A?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. Activity A呼叫Activity B
  2. 从B到C并同时调用Activity C
  3. 我为Activity B调用完成,(从堆栈历史记录Activity B中清除)
  4. Activity B onBackPressed()中,我通过setResult()Activity BActivity A传递数据,但是在Activity A中,数据是nullresultCode 0.
  1. From Activity A call Activity B
  2. from B to C and while calling Activity C
  3. I call finish for Activity B, (to clear from stack history Activity B)
  4. and in Activity B onBackPressed(), I pass data via setResult() from Activity B to Activity A, but in Activity A the data is null and resultCode 0.

如何从Activity A中的Activity B获取数据?

推荐答案

打开C时不要从B调用finish().相反,请使用startActivityforResult()调用C,然后在onActivityResult()时将数据通过B传回.在B中被触发.像这样:

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();
    }
}

显然,在调用B时添加标记Intent.FLAG_ACTIVITY_FORWARD_RESULT实际上也应该达到您想要的结果.

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...

这篇关于从活动A-> B-> C导航-如何在onBackPressed()中将数据从C传递到A?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:57