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?
startActivityForResultis meant to be used for situations where you want to select a piece of data, or perform some sort of action that yourActivityor 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
Activityyou’re launching will not send you the result until thatActivityhas completed, i.e.finish()has been called.So in your case, you need to call this in
SecondActivity:before
FirstActivitywill receive the result in itsonActivityResultmethod. Of course, this means thatSecondActivityis now gone andFirstActivityis top of the stack again.It’s not possible to send the result to
FirstActivitythen close it while keepingSecondActivitystill active. In this case you should just handle whatever this ‘result’ is inSecondActivity, or send it off to aServiceyou define to do whatever processing it is you want.