I have 3 classes in my example: Class A, the main activity. Class A calls a startActivityForResult:
Intent intent = new Intent(this, ClassB.class);
startActivityForResult(intent, "STRING");
Class B, this class is a TabActivity:
Intent intent = new Intent(this, ClassC.class);
tabHost.addTab...
Class C, this class is a regular Activity:
Intent intent = this.getIntent();
intent.putExtra("SOMETHING", "EXTRAS");
this.setResult(RESULT_OK, intent);
finish();
onActivityResult is called in Class A, but the resultCode is RESULT_CANCELED
instead of RESULT_OK
and the returned intent is null. How do I return something from the Activity inside a TabHost?
I realize that the problem is that my Class C is actually running inside of Class B, and Class B is what is returning the RESULT_CANCELED
back to Class A. I just don't know a work around yet.
For start Activity 2 from Activity 1 and get result, you could use startActivityForResult and implement onActivityResult in Activity 1 and use setResult in Activity2.
Oh, god! After spending several hours and downloading the Android sources, I have finally come to a solution.
If you look at the Activity class, you will see, that
finish()
method only sends back the result if there is amParent
property set tonull
. Otherwise the result is lost.So my solution is to set result to the parent activity if present, like that:
I hope that will be helpful if someone looks for this problem workaround again.
http://tylenoly.wordpress.com/2010/10/27/how-to-finish-activity-with-results/
With a slight modification for "param_result"
You could implement a onActivityResult in Class B as well and launch Class C using startActivityForResult. Once you get the result in Class B then set the result there (for Class A) based on the result from Class C. I haven't tried this out but I think this should work.
Another thing to look out for is that Activity A should not be a singleInstance activity. For startActivityForResult to work your Class B needs to be a sub activity to Activity A and that is not possible in a single instance activity, the new Activity (Class B) starts in a new task.
Intent.FLAG_ACTIVITY_FORWARD_RESULT?