18

I am facing with a problem related startActivityForResult()

To start SecondActivity from FirstActivity :

Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("key1", "12345");
startActivityForResult(intent, 0);

And handles result :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //TODO handle here. 
}

To send the message to FirstActivity from SecondActivity :

in SecondActivity :

setResult(0);

I can't handle the result on onActivityResult in FirstActivity. It never works for my application.

My OS is : 1.5

What is wrong here?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
AndroiDBeginner
  • 3,571
  • 11
  • 40
  • 43
  • what happens when you finish the second Activity? Is the callback method onActivityResult() invoked? – Samuh Dec 31 '09 at 10:31
  • Thanks for respose, I am trying to make it be keeping second activity and First activity would be finished. Is it possible? – AndroiDBeginner Dec 31 '09 at 11:06

6 Answers6

30

startActivityForResult is meant to be used for situations where you want to select a piece of data, or perform some sort of action that your Activity or application cannot do.

For example, you want to pick a contact, so you launch the contacts application, the user chooses the person they want, and you get sent the result. Or you want to take a photo, so you launch the camera application and ask it to send you the photo once it's done. This action is completely separate from your first activity that calls startActivityForResult.

The Activity you're launching will not send you the result until that Activity has completed, i.e. finish() has been called.

So in your case, you need to call this in SecondActivity:

setResult(...);
finish();

before FirstActivity will receive the result in its onActivityResult method. Of course, this means that SecondActivity is now gone and FirstActivity is top of the stack again.


It's not possible to send the result to FirstActivity then close it while keeping SecondActivity still active. In this case you should just handle whatever this 'result' is in SecondActivity, or send it off to a Service you define to do whatever processing it is you want.

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • I had some issues with my codes ...the onActivityResult was not being invoked..at last figured out that I was using startActivity(newIntent); instead of setResult(resultCode,intent);...thanks for this post.. – rahul Jan 18 '12 at 17:29
19

I was stuck here for a while. Adding my problem here to make sure that you don't scratch your head as well.

The second parameter of this function has to be 0 or higher.

startActivityForResult(intent, 0); // <- this is OK

I was setting the second parameter to RESULT_OK, which is -1, and my onActivityResult callback was never getting called. So if you get stuck like me, you can also check if your second parameter is correct.

startActivityForResult(intent, RESULT_OK); // <- this is wrong

The above line will fail to call onActivityResult.

matangs
  • 441
  • 5
  • 8
7

I was also stuck on the same problem - but due to a different reason as matangs. Apparently startActivityForResult only works if you have android:launchMode set to standard for main activity (in manifest). Hope it helps someone.

johndodo
  • 17,247
  • 15
  • 96
  • 113
5

Your code seems ok, but do you stop your second activity ?

Try this in it :

setResult(0);
finish();
tbruyelle
  • 12,895
  • 9
  • 60
  • 74
  • Thanks for respose, I am trying to make it be keeping second activity and First activity would be finished. Is it possible? – AndroiDBeginner Dec 31 '09 at 11:04
  • Sorry for my bad English. I am trying to make that : when setResult(MY_RESULT) to FirstActivity, I can handle it on onActivityResult. Then finish FirstActivity. such as FirstActivity.this.finish(); How to do it ? – AndroiDBeginner Dec 31 '09 at 11:38
  • If you want FirstActivity finish after getting the result of the SecondActivity, you can of course execute a finish() on FirstActivity too. – tbruyelle Dec 31 '09 at 13:01
  • setResult(0) results in a failed result. I think the result needs to be positive. – GrkEngineer Jul 20 '10 at 04:20
1

If you are doing actions on onPause (like unbinding a service) try to comment it and see if onActivityResult is called (I wasted few good hours on this..)

NBApps
  • 511
  • 5
  • 12
0

Thanks to @johndodo (that point to the manifiest) - I find my solution for the same problem.

removing android:noHistory=true at the manifiest" solved this problem for me.

Dadep
  • 2,796
  • 5
  • 27
  • 40
Shai Epstein
  • 179
  • 1
  • 3