Tag Archives: intent parameters

How to pass parameters back from intent

Tonight I ran across the issue of, "How do I send a result back to the parent that spawned an intent?"

After finding out that it is nearly impossible to pass instances of objects to the intent, I decided why not send the result back from the spawned intent.

To do something like this, use the following code to spawn the child intent:

//Starting a new Intent
Intent intent = new Intent(getApplicationContext(), whateverClass.class);

// Maybe add a param or two?
intent.putExtra("someParam", paramValue);

// starting new activity
startActivityForResult(intent,SOMEINTEGERSHOULDGOHERE);

When you are done with the intent, use this code to close it.

Intent intent=new Intent();
intent.putExtra("myParam", thiscouldbeastringvariable);
intent.putExtra("mySecondParam", thiscouldbeanintvaraible);
setResult(RESULT_OK, intent);
finish();

Lastly, here is how we process our result given from the child:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (SOMEINTEGERSHOULDGOHERE): // this was the integer you used to spawn the activity previously
if (resultCode == Activity.RESULT_OK) {
// Do whatever you want here.
// data.getStringExtra("ThisGetsOurParamBack");
}
break;
}
}

This is basically how to send data, process it in a new activity/intent, and then spit the results back to the parent.