Passing Data Between Activities in Android Application— For Beginners

Transferring data from an activity to another activity is a common use case when it comes to Android applications. This blog article describes the steps to be followed to develop a simple chat application that transfer messages from one user activity to another.

Prerequisites

  • Basic knowledge of XML and Java
  • Android Emulator

Okay Lets begin!!

  1. Open Android Studio and Start a new project

2. Choose the empty activity and then click next.

3. Configure your project. Give a project name, package name and the save location to your project. Then, Select the Android minimum SDK. After you select the minimum SDK, it will show the approximate percentage of people who use that SDK. Then, click Finish.

Okay now we are ready to start our implementation.

4. Navigate to res -> values directory and open string.xml file.Copy and paste the below code into the strings.xml file.

<resources>
<string name="app_name">Activity2</string>
<string name="message_txt">Enter Your message</string>
<string name="send_button_text">Send</string>
<string name="title_activity_second">SecondActivity</string>
</resources>

4. Navigate to “activity_main.xml” file and click “Text” tab that is placed in the bottom pane. This XML file contains the designing code for the Android application. In the activity_main.xml, copy and paste the below code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/message_text"
android:hint="@string/message_txt"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/send_button_text"
android:onClick="sendmessage"
/>
</LinearLayout>

When you switch to design tab, the view will look like the below image

5. As a next step, navigate to MainActivity.java, copy and paste the below code. Java programming is the back-end language for Android.

package com.example.activity2;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText message_text;

public final static String
MESSAGE_KEY ="com.example.message_key";

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void sendmessage (View view)
{
message_text = (EditText) findViewById(R.id.message_text);

String message = message_text.getText().toString();

Intent intent= new Intent(this ,SecondActivity.class);

intent.putExtra(MESSAGE_KEY,message);

startActivity(intent);
}}

6. Okay now, you need to create another activity for transferring the message from one activity to another. So, you need to create the SecondActivity.java and activity_second.xml.

Right click from the package and navigate to New->activity and select basic activity. Give the activity, layout, title name, and provide the hierarchical parent path. That means give your main activity path. Then click Finish button.

7. Open SecondActivity.java, and copy and paste the below code.

package com.example.activity2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {

public final static String MESSAGE_KEY ="com.example.message_key";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(45);
textView.setText(message);
setContentView(textView);

}

}

8. Navigate to “activity_second.xml” file and click “Text” tab that is placed in the bottom pane. This XML file contains the designing code for the Android application. In the activity_second.xml, copy and paste the below code.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.activity2.SecondActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_second" />

</android.support.design.widget.CoordinatorLayout>

9. Navigate to Build tab and click ‘Make Project’.

10. Finally, Run the application, then choose the virtual machine and click OK.

Put your message and click send button. The message will display in the another activity.

Hooray we did it !! we have successfully sent the data from one activity to another activity in Android application :)

Software Engineer @ hSenid Mobile | Former Software Engineering Intern @ WSO2 | Software Engineering Undergraduate @ IIT