使用startActivityForResult,如何在子活动中获取requestCode? [英] Using startActivityForResult, how to get requestCode in child activity?

查看:548
本文介绍了使用startActivityForResult,如何在子活动中获取requestCode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个活动,例如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);

在其他情况下,我将与其他情况一起发生.像

In other situation i will B with other situation. like

 startActivityForResult(new Intent(this,B.class),TWO);

在B中,我需要根据requestCode调用C或D.即如果一个人需要启动C否则D.
因此,我需要知道如何检查子活动(此处为B)中的requestCode.
换句话说,我想获取活动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.

推荐答案

您可以通过放置多余的内容来传递请求代码.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆