Google+

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. //

// ParentActivity.java

public class ParentActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parent_layout);

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

            @Override
            public void onClick(View v) {

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

    // find the Step 4 Below
}





Steps : 2          CallingActivity.java (getting values of Parent Activity)
  • This activity called by ParentActivity.java
  • 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          CallingActivity.java (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





// CallingActivity.java


public class CallingActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.child_layout);

        TextView resultDataTv = (TextView) findViewById(R.id.resultData);

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

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

            @Override
            public void onClick(View v) {

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

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


Steps : 4          ParentActivity.java (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 ParentActivity.java
  • 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.

@Override
    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(R.id.resultDataFrmChild);
            tv.setText(str);
            Toast.makeText(this, str, Toast.LENGTH_SHORT)
                    .show();
        }
    }




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


No comments:

Google+