Reputation: 4999
I'm not sure if this is expected behavior, but if I do the following in OneActivity
to launch TwoActivity
:
Intent intent = new Intent(this, TwoActivity.class);
startActivityForResult(intent, RESULT_OK);
And in TwoActivity
when I pass back to OneActivity
:
Intent resultIntent = new Intent();
resultIntent.putExtra(SOURCE, TAG);
setResult(RESULT_OK, resultIntent);
finish();
With the above code and after overriding onActivityResult
in OneActivity
nothing happens. onActivityResult
doesn't even seem to be called. If, however, I change RESULT_OK
to 0
, it works.
Is this expected? Has anyone else experienced that?
Upvotes: 1
Views: 6728
Reputation: 1034
please check your code with this sample:
MainActivity:
public class MainActivity extends Activity {
TextView textView1;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
});
}
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
}
and then SecondActivity:
public class SecondActivity extends Activity {
EditText editText1;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText1=(EditText)findViewById(R.id.editText1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
}
});
}
Upvotes: 0
Reputation: 199795
You are confusing two different concepts:
requestCode
(the second parameter to startActivityForResult
) is a unique ID you assign that can be any positive integer.resultCode
(the first parameter to setResult
) must be one of the constants in the Activity class as seen in the setResult documentationYou'll note that your onActivityResult
receives both the requestCode
you pass into startActivityForResult
and the resultCode
that you've set in setResult
- make sure you are comparing the right numbers in your onActivityResult
Upvotes: 3
Reputation: 719
Check out the docs definition of the startActivityForResult method. It says:
requestCode int: If >= 0, this code will be returned in onActivityResult() when the activity exits.
So your request code should be >= 0. If you check the value of the RESULT_OK response code, it's -1. It's important to note that the request code isn't the same as the result code. The request code serves you to identify the request the result is for, and the result code tells you if the request was successful or not.
Upvotes: 8