Thursday, May 5, 2011

Passing Data & Returning Data between Activity in Android

Parent Activity where we Start to send & get Result

Steps : 1
  • From the ParentActivity we are calling intents
    Intnet inent = new Intent(this, CallingActivity.class);
  • Passing Values using "ComingFrom" as a Key Value with "Parent Activity" as Value
  • Use startActivityForResult(intnet, requestCode) Method by passing Intent Object & any RequestCode. //


public class ParentActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {

        Button btn = (Button) findViewById(;
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(ParentActivity.this,
                int requestCode = 1;
                intent.putExtra("ComingFrom", "This is Parent Activity Data");
                startActivityForResult(intent, requestCode);

    // find the Step 4 Below

Steps : 2 (getting values of Parent Activity)
  • This activity called by
  • Create an Intent Object and assign with getIntent() method which return Intent Object of Called Activity.
    Intnet inent = getIntent();

  • Use getExtras() method get values sent by ParentActivity
    getExtras() = retrun the bundle of all extras previously added with putExtra(), or null if none have been added.
  • then call the method of getString(KeyValue) method with KeyValue which created by ParentActivity.
    This method will return the value of Key.
  • Display Value wherever you want.

Steps : 3 (sending values to Parent Activity)
  • on the Button Click we have to pass the value of EditText to parent Activity
  • on Click Event we Create the new Intent Object
    Intnet i = new Intent();
  • same step to sending value
    i.putExtras(KeyValue, Value)
  • Now as we need to send this data to called Activity we use setResult() method
    setResult(RESULT_OK, IntentObject);
    RESULT_OK = Standard activity result: operation succeeded.
  • finish() the Calling Activity


public class CallingActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        TextView resultDataTv = (TextView) findViewById(;

        Intent intent = getIntent();
        String str = intent.getExtras().getString("ComingFrom");

        Button sendResultBtn = (Button) findViewById(;
        sendResultBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                EditText et = (EditText) findViewById(;
                String str = et.getText().toString();

                Intent i = new Intent();
                i.putExtra("childResult", str);
                setResult(RESULT_OK, i);

Steps : 4 (getting Result from Calling Activity)
  • Here we have to Catch the result of Calling Activity
  • First Overwrite the method onActivityResult(int requestCode, int resultCode, Intent data) in
  • Check for the resultCode & requestCode (You Can use Switch as Well)
  • get the result using data.getStringExtra(KeyValue) - Retrieve extended data from the intent.
  • Set resulted text wherever you want.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode==1) {

            String str = data.getStringExtra("ComingFrom");
            TextView tv = (TextView) findViewById(;
            Toast.makeText(this, str, Toast.LENGTH_SHORT)

Find the Complete Code
And don't forget to Write Comments

No comments: