I am using startActivityForResult
in my application. But I have some doubts/queries about its behavior.
Scenario: In my application. I have two activities, say Activity1 and Activity2. I am calling startActivityForResult
from my Activity1 to Activity2 twice with different request codes.
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
@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.
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.
Problem: From Activity1, I execute the 1st code ie with REQUEST_CODE_1
and in my Activity2, the else block is executed. In this case, I expect REQUEST_CODE_2
to come back to Activity, but instead, I get REQUEST_CODE_1
back to Activity. Clearly, the setResult
of else
block didn't execute as intended.
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. You are supplying the request code instead of RESULT_OK or 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.
The request_code is not part of the arguments, so you need to add it to the 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")
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.