Blog Archive

Android passing data between activities example code

In order to pass the data or parameters to the new activity we create a bundle resource and put all the data in name and value pairs in the bundle. Then we pass the bundle using the putExtras() method of the intent created for the new activity that needs to be called.

To receive the data from the called activity you have to make the call using startActivityForResult() method and then the new activity can pass all the data in a bundle attached to the intent. The calling activity can then receive the data in the onActivityResult() method and read the bundle name and value pairs.

Layout for the Calling Actvity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal" android:padding="10dip">
    <LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:orientation="vertical"
        android:layout_width="match_parent" android:layout_gravity="center|top">
        <TextView android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:textSize="24.5sp"
            android:layout_marginBottom="25dip" android:gravity="center"
            android:text="Calling Activity" android:id="@+id/textView1" />
        <TableLayout android:id="@+id/tableLayout1"
            android:layout_height="match_parent" android:layout_width="match_parent"
            android:stretchColumns="1">
            <TableRow android:layout_width="match_parent" android:id="@+id/tableRow1"
                android:layout_height="wrap_content">
                <TextView android:layout_height="wrap_content"
                    android:text="First Name" android:id="@+id/textView2"
                    android:layout_width="wrap_content" />
                <EditText android:id="@+id/firstName"
                    android:layout_height="wrap_content" android:layout_width="match_parent" />
            </TableRow>
            <TableRow android:layout_width="match_parent" android:id="@+id/tableRow2"
                android:layout_height="wrap_content">
                <TextView android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:text="Last Name"
                    android:id="@+id/textView3" />
                <EditText android:layout_height="wrap_content"
                    android:id="@+id/lastName" android:layout_width="match_parent" />
            </TableRow>
            <TableRow android:layout_width="match_parent" android:id="@+id/tableRow3"
                android:layout_height="wrap_content">
                <TextView android:layout_height="wrap_content"
                    android:text="Age" android:id="@+id/textView4"
                    android:layout_width="match_parent" />
                <EditText android:id="@+id/age"
                    android:layout_height="wrap_content" android:layout_width="match_parent" 
     android:inputType="number"/>
            </TableRow>
        </TableLayout>
        <Button android:layout_height="wrap_content" android:id="@+id/button1"
            android:text="Call Next Activity" android:gravity="center"
            android:layout_width="match_parent" />
    </LinearLayout>
</LinearLayout>

Sample Java code for the Calling Activity

package com.as400samplecode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CallingActivity extends Activity {
   
    public static final int CALLED_ACTIVITY = 1;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.callingactivity);

        Button nextActivity = (Button) findViewById(R.id.button1);
        nextActivity.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                Intent intent = new Intent(CallingActivity.this, CalledActivity.class); 
                Bundle b = new Bundle();

                EditText firstName = (EditText) findViewById(R.id.firstName);
                EditText lastName = (EditText) findViewById(R.id.lastName);
                EditText age = (EditText) findViewById(R.id.age);

                b.putString("firstName", firstName.getText().toString());
                b.putString("lastName", lastName.getText().toString());
                b.putInt("age", Integer.parseInt(age.getText().toString())); 
               
                //Add the set of extended data to the intent and start it
                intent.putExtras(b);
                startActivityForResult(intent,CALLED_ACTIVITY);
            }
        });
    }
   
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        switch(requestCode) {
        case CALLED_ACTIVITY: 
            if (resultCode == RESULT_OK) {
               
                Bundle bundle = data.getExtras();
               
                EditText firstName = (EditText) findViewById(R.id.firstName);
                firstName.setText(bundle.getString("firstName"));
                EditText lastName = (EditText) findViewById(R.id.lastName);
                lastName.setText(bundle.getString("lastName"));
                EditText age = (EditText) findViewById(R.id.age);
                age.setText(Integer.toString(bundle.getInt("age")));
                break;
            }
        }

    }  
}

Layout for the Called Actvity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal" android:padding="10dip">
    <LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout2" android:orientation="vertical"
        android:layout_width="match_parent" android:layout_gravity="center|top">
        <TextView android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:textSize="24.5sp"
            android:layout_marginBottom="25dip" android:gravity="center"
            android:text="Called Activity" android:id="@+id/textView8" />
        <TableLayout android:id="@+id/tableLayout2"
            android:layout_height="match_parent" android:layout_width="match_parent"
            android:stretchColumns="1">
            <TableRow android:layout_width="match_parent" android:id="@+id/tableRow1"
                android:layout_height="wrap_content">
                <TextView android:layout_height="wrap_content"
                    android:text="First Name" android:id="@+id/textView5"
                    android:layout_width="wrap_content" />
                <EditText android:id="@+id/firstName2"
                    android:layout_height="wrap_content" android:layout_width="match_parent" />
            </TableRow>
            <TableRow android:layout_width="match_parent" android:id="@+id/tableRow2"
                android:layout_height="wrap_content">
                <TextView android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:text="Last Name"
                    android:id="@+id/textView6" />
                <EditText android:layout_height="wrap_content"
                    android:id="@+id/lastName2" android:layout_width="match_parent" />
            </TableRow>
            <TableRow android:layout_width="match_parent" android:id="@+id/tableRow3"
                android:layout_height="wrap_content">
                <TextView android:layout_height="wrap_content"
                    android:text="Age" android:id="@+id/textView7"
                    android:layout_width="match_parent" />
                <EditText android:id="@+id/age2"
                    android:layout_height="wrap_content" android:layout_width="match_parent" 
     android:inputType="number"/>
            </TableRow>
        </TableLayout>
        <Button android:text="Back to Previous Activity" android:layout_width="match_parent" 
   android:id="@+id/button1" android:layout_height="wrap_content">
  </Button>
    </LinearLayout>
</LinearLayout>

Sample Java code for the Called Activity

package com.as400samplecode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CalledActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calledactivity);
        getParameters();
       
        Button nextActivity = (Button) findViewById(R.id.button1);
        nextActivity.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                Bundle b = new Bundle();
                EditText firstName = (EditText) findViewById(R.id.firstName2);
                EditText lastName = (EditText) findViewById(R.id.lastName2);
                EditText age = (EditText) findViewById(R.id.age2);

                b.putString("firstName", firstName.getText().toString());
                b.putString("lastName", lastName.getText().toString());
                b.putInt("age", Integer.parseInt(age.getText().toString())); 
               
                //Add the set of extended data to the intent and start it
                Intent intent = new Intent();
                intent.putExtras(b);
                setResult(RESULT_OK,intent);       
                finish();
            }
        });
    }

    public void getParameters() {

        if (CalledActivity.this.getIntent().getExtras() != null)
        {
            Bundle bundle = this.getIntent().getExtras();
           
            EditText firstName = (EditText) findViewById(R.id.firstName2);
            firstName.setText(bundle.getString("firstName"));
            EditText lastName = (EditText) findViewById(R.id.lastName2);
            lastName.setText(bundle.getString("lastName"));
            EditText age = (EditText) findViewById(R.id.age2);
            age.setText(Integer.toString(bundle.getInt("age")));
        }

    }

}