![]() |
VOOZH | about |
This article aims to tell and show how to "Send the data from one activity to second activity using Intent" . In this example, we have two activities, activity_first which are the source activity, and activity_second which is the destination activity. We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
Pre-requisites:
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 the following methods will run:
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio . The code for that has been given in both Java and Kotlin Programming Language for Android. This will create an XML file and a Java File. Please refer to the prerequisites to learn more about this step.
Next, go to the activity_main.xml file , which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail. Open the "activity_first_activity.xml" file and add the following widgets in a Relative Layout .
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/Kotlin files.
android:id="@+id/id_name"Here the given IDs are as follows:
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. Now, after the UI, this step will create the Backend of the App. For this, open the "first_activity" 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.
ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);Button send_button= findViewById(R.id.send_button_id);
send_text = findViewById(R.id.send_text_id);
Add the listener to the send button and this button will send the data.
send_button.setOnClickListener(v -> {} after clicking this button following operation will be performed. Now create the String type variable to store the value of EditText which is input by the user. Get the value and convert it to a string.
String str = send_text.getText().toString();Now create the Intent object First_activity.java class to Second_activity class.
Intent intent = new Intent(getApplicationContext(), Second_activity.class);where getApplicationContext() will fetch the current activity. Put the value in the putExtra method in the key-value pair then start the activity.
intent.putExtra("message_key", str);
startActivity(intent);
where "str" is the string value and the key is "message_key" this key will use to get the str value
The steps to create the second activity are as follows:
android project > File > new > Activity > Empty Activity Add TextView to display the received messages. assign an ID to Textview.
Define the TextView variable, use findViewById() to get the TextView as shown above.
receiver_msg = (TextView) findViewById(R.id.received_value_id); Now In the second_activity.java file create the object of getIntent to receive the value in String type variable by the getStringExtra method using message_key .
Intent intent = getIntent();
String str = intent.getStringExtra("message_key");
The received value set in the TextView object of the second activity XML file
receiver_msg.setText(str);