问题描述
我有四项活动,比如 A、B、C 和 D.我的情况是 A 将通过 startActivityForResult 启动活动 B.
I have four activities, say A, B, C and D.My situation is A will start the activity B by startActivityForResult.
startActivityForResult(new Intent(this,B.class),ONE);
在其他情况下,我会在其他情况下 B.喜欢
In other situation i will B with other situation. like
startActivityForResult(new Intent(this,B.class),TWO);
在 B 中,我需要根据 requestCode 调用 C 或 D.即如果一个需要启动 C 否则 D.
所以我需要知道如何检查子活动中的 requestCode(此处为 B).
换句话说,我想获取启动 Activity B 的请求代码.
In B, I need to call C or D depending on requestCode. I.e if ONE need to start C else D.
So I need to know how to check the requestCode in the child Activity (B here).
In other words, I want to get the request code that Activity B was started with.
推荐答案
可以通过 put extra 传递请求代码.
You can pass request code by put extra.
intent.putExtra("requestCode", requestCode);
或者,如果您多次使用 startActivityForResult
,那么比编辑每个都更好,您可以在 Activity 中
override
startActivityForResult
,像这样在那里添加你的代码
Or if you have used startActivityForResult
many times, then better than editing each, you can override
the startActivityForResult
in your Activity
, add you code there like this
@Override
public void startActivityForResult(Intent intent, int requestCode) {
intent.putExtra("requestCode", requestCode);
super.startActivityForResult(intent, requestCode);
}
所以没有必要编辑你所有的 startActivityForResult
希望对你有帮助
So there is no need to edit all your startActivityForResult
Hope it helped you
这篇关于使用 startActivityForResult,如何在子活动中获取 requestCode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!