Thursday, October 21, 2010

Stuck on Sub-Activities

So I have hit a wall that I can't seem to get around. I am launching a sub-activity and attempting to get a result. I have a couple weird things happening, but everything I find online tells me I am doing this correctly.

I even created a simple little test project called SubActivityTest (you can download the zip file with all the source/xml files). It simply launches a sub-activity and waits for the response and writes to a Text View. I have simplified the code to the bare minimum and not only is it NOT working, but it is also returning before the activity even launches.

My code for launching the sub-activity:
Intent myIntent = new Intent(SubActivityTest.this, SubActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(myIntent, 1);

My Code for setting the result from my sub-activity:
setResult(RESULT_OK);
finish();

My Code for catching the return:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
TxtView.setText("OK Pressed");
}
else {
TxtView.setText("resultCode = " + Integer.toString(resultCode));
}
}
}

As I said before, it returns immediately and my text view updates to "resultCode = 0" and then the sub-activity opens. I set the result and finish() from the subactivity and the text view text doesn't change. It remains "resultCode = 0". I guess I am not even sure the routine is running when control returns from the sub-activity. I need this to work and will be scouring the forums to find a fix.

If anybody comes across this and can identify my mistake I would greatly appreciate it.

No comments:

Post a Comment