Saturday, April 9, 2016

Send Data Back and forth between Two Activities Android Studio


In this tutorial I will demonstrate how to send data back and forth between two activities.

Consider two Activities

1. Home Activity        (This is the Launcher Activity)
2. Detail Activity

Our goal is to send data from Home Activity to Detail Activity, use that received data, concatenate some extra string in it and than send it back to Home Activity via Button click event

Home Activity Code

private void SendDataToDetailActivity () {
        EditText fn = (EditText) findViewById(;
        Intent obj = new Intent(this, Details.class);
        obj.putExtra("Name", fn.getText().toString());
        startActivityForResult(obj, 100);

Now override the onActivityResult Method
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        if (requestCode == 100) {

            if (resultCode == RESULT_OK) {

                Toast.makeText(Home.this, data.getStringExtra("result"), Toast.LENGTH_SHORT).show();

Now in detail Activity Define the method
private void GetDetails() {

        TextView txtF = (TextView) findViewById(;
        String Name = getIntent().getStringExtra("Name");
        Student objS = new Student(Name);
        Name = objS.getName();
        String[] ss = Name.split("!");
Now to send data back to Home Activity use the following Method
public void NavigateToBack (View view) {
        String Name = getIntent().getStringExtra("Name");
        Intent obj = getIntent().putExtra("result", Name + " You just received a Message from Another Activity");
        setResult(RESULT_OK, obj);

I hope that it was informative for you and I would like to Thank you for reading.

No comments:

Post a Comment