So I had a project where I need to return intent data from one activity to another via OnActivityResult. I thought it was pretty straightfoward, simply roll the intent data call setResult and return control to the calling activity via finish(). Well not so fast!!! After playing around a little I found out that I actually need to call getParent() to get the parent of the current activity and add the intent data to that. I had a lot of difficulty finding code which did this so here is a snippet below that might help you out…first the calling activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case startTOCActivity:
if(resultCode == RESULT_OK)
{
int page = data.getIntExtra(“val”, 0);
}
else
{
Log.e(LOG_TAG, “Error processing return”);
}
break;
}
}
and now for the activity that rolls the data
OnClick(….)
{
Intent mIntent = new Intent();
mIntent.putExtra(“val”, page);
if (getParent() == null) {
setResult(Activity.RESULT_OK, mIntent);
}
else {
getParent().setResult(Activity.RESULT_OK, mIntent);
}
finish();
}
Pretty simple huh? I can’t tell you how long I spent trying to figure out how to handle this properly – enjoy!
Filed under: Product Engineering | Topics: android, developers, mobile, platforms