本文介绍了从活动A-> B-> C导航-如何在onBackPressed()中将数据从C传递到A?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 从
Activity A
呼叫Activity B
- 从B到C并同时调用
Activity C
- 我为
Activity B
调用完成,(从堆栈历史记录Activity B
中清除) 在 - 和
Activity B
onBackPressed()
中,我通过setResult()
从Activity B
到Activity A
传递数据,但是在Activity A
中,数据是null
和resultCode 0
.
- From
Activity A
callActivity B
- from B to C and while calling
Activity C
- I call finish for
Activity B
, (to clear from stack historyActivity B
) - and in
Activity B
onBackPressed()
, I pass data viasetResult()
fromActivity B
toActivity A
, but inActivity A
the data isnull
andresultCode 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!