简体   繁体   English

Android:OnActivityResult行为

[英]Android: OnActivityResult behavior

I am using startActivityForResult in my application. 我在应用程序中使用startActivityForResult But I have some doubts/queries about its behavior. 但是我对其行为有疑问/疑问。

Scenario: In my application. 场景:在我的应用程序中。 I have two activities, say Activity1 and Activity2. 我有两个活动,例如Activity1和Activity2。 I am calling startActivityForResult from my Activity1 to Activity2 twice with different request codes. 我使用不同的请求代码从我的Activity1到Activity2两次调用了startActivityForResult

final REQUEST_CODE_1 = 1;
final REQUEST_CODE_2 = 2;

Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, REQUEST_CODE_1);

and

Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, REQUEST_CODE_2);

And I get the data back in onActivityResult 我在onActivityResult得到了数据

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data != null) {
        switch (requestCode) {
            case REQUEST_CODE_1:
                //some piece of code
                break;
            case REQUEST_CODE_2:
                //some piece of code
                break;
        }
    }

}

And in my Activity2, I have some condition based on which it sends the data back to Activity1. 在我的Activity2中,我有一些条件,基于该条件它将数据发送回Activity1。

myStr = scanner.getValue();
if(myStr.startsWith("5901")){
     intent.putExtra("box",myStr);
     setResult(REQUEST_CODE_1,intent);
  }else{
      intent.putExtra("item",myStr+"dCbA");
      setResult(REQUEST_CODE_2,intent);
}

where mystr is a String variable. 其中mystr是一个String变量。

Problem: From Activity1, I execute the 1st code ie with REQUEST_CODE_1 and in my Activity2, the else block is executed. 问题:我从Activity1执行第一个代码,即使用REQUEST_CODE_1 ,在Activity2中,执行else块。 In this case, I expect REQUEST_CODE_2 to come back to Activity, but instead, I get REQUEST_CODE_1 back to Activity. 在这种情况下,我希望REQUEST_CODE_2返回到“活动”,但我将REQUEST_CODE_1返回到“活动”。 Clearly, the setResult of else block didn't execute as intended. 显然, else块的setResult没有按预期执行。

I hope my question is clear. 希望我的问题清楚。 Can someone please explain. 有人可以解释一下。

Thank you. 谢谢。

setResult(int) must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK. setResult(int)必须始终提供结果代码,该代码可以是标准结果RESULT_CANCELED,RESULT_OK。 You are supplying the request code instead of RESULT_OK or RESULT_CANCELED. 您正在提供请求代码,而不是RESULT_OK或RESULT_CANCELED。

Try this 尝试这个

Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, REQUEST_CODE_1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data != null) {
        switch (requestCode) {
            case REQUEST_CODE_1:
                //check if resultCode is RESULT_OK or CANCELED
                break;
            case REQUEST_CODE_2:
                //some piece of code
                break;
        }
    }

}


if(myStr.startsWith("5901")){
     setResult(RESULT_OK,intent);
  }else{
      setResult(RESLT_CANCELED,intent);
}

From what I look, you suppose that myStr is the request_code sent from Activity1. 从我的角度看,您假设myStr是从Activity1发送的request_code。

The request_code is not part of the arguments, so you need to add it to the Intent. request_code不是参数的一部分,因此您需要将其添加到Intent中。

Intent i = new Intent(this, Activity2.class);
i.putExtra("request_code", REQUEST_CODE_1);
startActivityForResult(i, REQUEST_CODE_1);

So you can get it from the Activity2 with getArguments().getIntegerExtra("request_code") 因此,您可以使用getArguments()。getIntegerExtra(“ request_code”)从Activity2获取它

And in this case, you need to compare the object with another literal without equals (or wrapper) 在这种情况下,您需要将对象与另一个不带等号(或包装)的文字进行比较

Also, Activity2 doesn't need to know the integer to setResult, as it need to be one of RESULT_OK, RESULT_CANCELED, RESULT_NONE. 同样,Activity2不需要知道setResult的整数,因为它必须是RESULT_OK,RESULT_CANCELED和RESULT_NONE之一。 The onActivityResult will receive back (requestCode, resultCode, data) where requestCode will be exactly the integer you did startActivityForResult and resultCode will be the one set in Activity2 before finishing. onActivityResult将返回(requestCode,resultCode,data),其中requestCode将是您刚完成startActivityForResult的整数,而resultCode将是Activity2中在完成之前设置的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM