Reputation: 2956
I have a big and frustrating problem with a simple application. I have two Activities - A and B. Activity A opens activity B on a button click. Activity B has two "ends": If user clicks B.Back if just finish and goes back to A, if user clicks B.OK button, activity B is finished but before that it sets result "OK" so activity A can be closed too.
This is the pseudo code for A:
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//some code...
startAct(data);
}
});
private void startAct(Intent inte) {
startActivityForResult(inte, -999);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println(resultCode);
if(requestCode != -999)
return;
if(resultCode == RESULT_OK)
finish();
}
Activity B does like this:
btnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//some other code...
finishOK();
}
});
private void finishOK() {
setResult(RESULT_OK, new Intent());
finish();
}
The problem is that this won't work - when i press the "btnOK" B should finish, A should "catch" the result and finish too, but it simply won't happen. What am i missing?
Upvotes: 2
Views: 175
Reputation: 18151
You have to use a non-negative requestCode
. If the requestCode
is negative then startActivityForResult
is the same as startActivity
. See official document http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,%20int)
Upvotes: 3