Sending data from one Activity to another Activity

This article aims to tell and show about how to “Send the data from one activity to second activity using Intent”. In this Example, we have two activities, activity_first which is the source activity and activity_second which is the destination activity. We can send the data using putExtra() method from one activity and get the data from the second activity using getStringExtra() methods.

Example:

In this Example, one EditText is used to input the text. This text is sent to the second activity when the “Send” button is clicked. For this, Intent will start and following methods will run:

  • putExtra() method is use for send the data, data in key value pair key is variable name and value can be Int, String, Float etc.
  • getStringExtra() method is for get the data(key) which is send by above method. according the data type of value there are other methods like getIntExtra(), getFloatExtra()

How to create an Android App to send and receive the data between two activity

  • Step 1: Firstly create a new Android Application. This will create an XML file and a Java File. Please refer the pre-requisites to learn more about this step.

cStep 2: Open “activity_first_activity.xml” file and add following widgets in a Relative Layout:

  • A EditText to Input the message
  • A Button to send the data

Also, Assign the ID to each component along with other attributes as shown in the image and the code below. The assigned ID on a component helps that component to be easily found and used in the Java files.
Syntax:

android:id="@+id/id_name"

Here the given IDs are as follows:

  • Send Button: send_button_id
  • input EditText: send_text_id

This will make the UI of the Application.

  • Step 3: Now, after the UI, this step will create the Backend of the App. For this, open the “first_activity.java” file and instantiate the components made in the XML file (EditText, send Button) using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.
    General Syntax:

    ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);

    Syntax for components used:

    Button send_button= (Button)findViewById(R.id.send_button_id);
    send_text = (EditText) findViewById(R.id.send_text_id);

  • Step 4: This step involves setting up the operations on the sending and received the data. These operations are as follows:
    1. first Add the listener on send button and this button will send the data. This is done as follows:

      send_button.setOnClickListener(new View.OnClickListener() {}

      after clicked this button following operation will be performed.

    2. Now create the String type variable for store the value of EditText which is input by user. Get the value and convert it to string. This is done as follows:

      String str = send_text.getText().toString();

    3. Now create the Intent object First_activity.java class to Second_activity class. This is done as follows:

      Intent intent = new Intent(getApplicationContext(), Second_activity.class);

      where getApplicationContext() will fetch the current activity.

    4. Put the value in putExtra method in key value pair then start the activity. This is done as follows:

      intent.putExtra(“message_key”, str);
      startActivity(intent);

      where “str” is the string value and the key is “message_key” this key will use to getting the str value

  • Step 5: Now we have to create a Second_Activity to receive the data.
    The steps to create the second activity is as follows:

    android project > File > new > Activity > Empty Activity

Step 6: Now open your second xml file.
Add TexView for display the receive messages. assign ID to Textview. Second Activity is shown below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".first_activity">
  
    <EditText
        android:id="@+id/send_text_id"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:hint="Input"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="40dp"/>
    <Button
        android:id="@+id/send_button_id"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="send"
        android:textStyle="bold"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="150dp"/>
  
</RelativeLayout>

Output: