Android switching activity and sending data - Example


There may be situation where you want to send data from one activity to another while switching from one activity to another. The following is a simple example where you can enter your name and age in the first screen and receive it at the second screen on click of a button.

So lets start .

1. Create a new project File -> Android Project. While creating a new project give activity name as FirstActivity(FirstActivity.java).
2. Now you need to create user interface for the FirstActivity.java
3. Create a new xml file in layout folder or rename the main.xml to first.xml
4. Now insert the following code in first.xml to design a small layout. This layout contains two edittext and a button.


First.xml

<?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="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Name: " />

    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Age: " />

    <EditText
        android:id="@+id/age"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:inputType="number" />

    <Button
        android:id="@+id/btnNextScreen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dip"
        android:text="Send Data" />

</LinearLayout>



5.Now open your FirstActivity.java and Type the following code. In the following code we are creating a new Intent and passing parameters on clicking button.

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

public class FirstActivity extends Activity {
 // Initializing variables
 EditText inputName;
 EditText inputAge;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.first);
  inputName = (EditText) findViewById(R.id.name);
  inputAge = (EditText) findViewById(R.id.age);
  Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
  // Listening to button event
  btnNextScreen.setOnClickListener(new View.OnClickListener() {
   public void onClick(View arg0) {
    // Starting a new Intent
    Intent nextScreen = new Intent(getApplicationContext(),
      SecondActivity.class);
    // Sending data to another Activity
    nextScreen.putExtra("name", inputName.getText().toString());
    nextScreen.putExtra("age", inputAge.getText().toString());
    startActivity(nextScreen);
   }
  });
 }
}



  1. Create a class called SecondActivity.java. And an xml file named second.xml
SecondActivity.java.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SecondActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.second);
  TextView txtName = (TextView) findViewById(R.id.txtName);
  TextView txtEmail = (TextView) findViewById(R.id.txtAge);
  Button btnBack = (Button) findViewById(R.id.btnBack);
  Intent i = getIntent();
  // Receiving the Data
  String name = i.getStringExtra("name");
  String age = i.getStringExtra("age");
  Log.e("Second Screen", name + "." + age);
  // Displaying Received data
  txtName.setText(name);
  txtEmail.setText(age);
  // Binding Click event to Button
  btnBack.setOnClickListener(new View.OnClickListener() {
   public void onClick(View arg0) {
    // Closing SecondScreen Activity
    finish();
   }
  });
 }
}


second.xml
<?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="vertical" >

    <TextView
        android:id="@+id/txtName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dip"
        android:textSize="18dip" />

    <TextView
        android:id="@+id/txtAge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dip"
        android:textSize="18dip" />

    <Button
        android:id="@+id/btnBack"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dip"
        android:text="Back" />

</LinearLayout>



  1. Open you AndroidManifest.xml file and modify the code as below in application tag

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
    
        <activity
            android:name=".FirstActivity"
            android:label="@string/app_name" >
    
            <intent-filter>
    
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" />
    
    </application>
    

    8.Run your project by right clicking on your project folder -> Run As -> 1 Android Application.

The below image is output of this example