开始活动以获得结果 [英] Start Activity for result

查看:80
本文介绍了开始活动以获得结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个菜鸟问题,但我有一些疑问.我在Google上搜索了很多,但什么都没找到.在开始结果的活动中,我们传递请求代码,在结果中,我们使用相同的请求代码和结果代码进行检查.我想知道是否有一种方法可以通过使用请求代码来执行不同的任务并从被调用的活动中获得不同的结果,即如果同一活动被不同的请求代码多次调用,那么它将返回不同的结果.请告诉我该怎么做.我发现没有办法使用switch语句或其他任何方式来做到这一点.

It may be a noob question but I have some doubt. I googled a lot but found nothing. In starting activity for result we pass request code and on result we check with the same request code and result code. I want to know Is there a way to Implement to perform different tasks and get different results from called activity by using request code i.e if the same activity is called many times with different request code then it returns different result. Please tell me how to do that. I found no way to have a switch statement or any other way to do this.

我已经知道答案了,所以编辑一下.我想知道是否可以使用以下场景:

I already know the answers so editing this. I want to know If I can use the scenario like:

Intent intent = new Intent(this, yourClass.class); 
intent.putExtras(b);
if(condition1)
startActivityForResult(intent, 1);
else
startActivityForResult(intent, 2);

我的调用Activity返回请求代码1和2的两个不同结果,所以我可以拥有

And my called Activity returns two differnt results for request code 1 and 2 , So I can have

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    //could replace it with a switch
    if (requestCode == 1){
        //condition 1
            }
        else if(requestCode == 2){
          //condition2
        }
}

即使用不同的请求代码调用同一活动,以从同一活动中获得不同的结果.

i.e calling the same activity with different request code to get different results from the same activity.

谢谢

推荐答案

发布的问题对我来说还不是很清楚,您可以随时切换activityForResult并在onActivityResult方法中检查活动结果,以检查不同的请求代码.这是一个代码演示,如何执行:

The question posted was not so clear to me, You can always switch an activityForResult and check for activity result in onActivityResult method checking different request codes. Here is a code demonstration, how to do it:

使用此按钮切换活动:

Intent intent = new Intent(this, yourClass.class); 
intent.putExtras(b);    // here
startActivityForResult(intent, 2); //put your code along : positive integer

用这种方法检查结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    //could replace it with a switch
    if (requestCode == 1){
        //put your code here
            }
}

被调用的活动不需要知道您的请求代码,但是,如果您想执行类似的操作,则可以通过故意传递您的请求代码来做到这一点,例如:

The called activity does not need to know your requestcode, however, if you want to do something like that, you can do that by passing your request code in intent, like this:

intent.putExtra("requestCode", requestCode); 

因此,请访问您切换到的活动类中的intent变量.

Hence, access the intent variable in the activity class you switched onto..

这篇关于开始活动以获得结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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