4

In my main activity, I have:

    Intent settings_intent = new Intent(this, SettingsActivity.class);
    settings_intent.putExtra("SomeID", some_int);
    startActivityForResult(settings_intent, 1);

And then, in my SettingsActivity, I have:

@Override
public void onBackPressed() {
    super.onBackPressed();
    // pass back settings:
    Intent data = new Intent();
    data.putExtra("SomeThing", some_number);
    setResult(200, data);
    finish();
}

And finally, I overrode the following in my Main activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    makeToast("called");
    super.onActivityResult(requestCode, resultCode, data);
}

However, the "called" toast happens as soon as my "Settings" activity starts, instead of when it finishes. I've spent quite a while on this now. Any help is appreciated. Thanks.

WSBT
  • 33,033
  • 18
  • 128
  • 133

2 Answers2

14

Most likely your settings activity has it's launch mode set to singleTask in your manifest. This leads to a immediate cancel response when calling startActivityForResult().

Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect. For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.

from the startActivityForResult() documentation

  • Thank you very much for your response. launchMode was set to "singleInstance" actually, and it was not working. Just of curiosity, I changed it to `android:launchMode="singleTask"` after reading your answer, and it surprisingly worked. But now reading the doc, I'm worried if it will work on other devices. – WSBT Dec 10 '11 at 22:15
  • I see how `singleInstance` can cause this too, since it's almost the same as `singleTask`. But to be honest, I have no idea why this works with `singleTask`, so I can't tell you if this works the same way on other devices. –  Dec 10 '11 at 22:29
-1
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==1)
        makeToast("called");
    super.onActivityResult(requestCode, resultCode, data);
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880