Close
Close full mode
logoAppDevAssist

How to pass data between activities in Android

How to pass data between activities in Android:

In this post, we will learn how to pass data between activities. If we are moving from one activity to another activity, we need to pass data from the first activity to the second activity. The second activity can use the data.

In this post, we will learn how to pass data from one activity to another activity with examples.

Video:

You can watch the below video to learn it in step-by-step:

Android Studio Project:

The Android Studio project that we will create in this post will have two activities. One will pass data to another.

How data is passed:

Data is passed through Intent. Intent objects can hold data and we can pass one intent while starting a different activity. This activity can read the data from the Intent.

  • Intent is an object to provide runtime bindings between two Activities.
  • We can put data in an Intent and pass it to a different activity.
  • It carries data as key-value pairs called extra
  • startActivity method is used to start a new activity. We can pass the intent in this method.
  • The second activity can read the data from the Intent.

Android Studio Project setup:

  • This project will hold two Activities.
  • The first one will have one EditText and one Button.
  • User can enter any text in the EditText and click on the Button. It will start the second Activity and the entered text will be passed to the second activity using an intent.
  • The second activity will read the text from the intent and show it in a TextView.

This project will have :

  • Two activities: MainActivity with xml file activity_main.xml and SecondActivity with xml file activity_second.xml
You can watch the above video to learn how to create these layout files

activity_main.xml

This file is the layout for the MainActivity:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="300dp"
android:text="Click Me"
android:onClick="onButtonClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/editText"
app:layout_constraintStart_toStartOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>

It has one EditText and one Button. The button calls onButtonClick method on click.

MainActivity.kt:

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
class MainActivity : AppCompatActivity() {
lateinit var editText: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editText = findViewById(R.id.editText)
}
fun onButtonClick(view: View) {
val i = Intent(this, SecondActivity::class.java).apply{
putExtra("MyKey", editText.text.toString())
}
startActivity(i)
}
}
  • This file creates the intent in onButtonClick method.
  • It reads the text entered in the EditText and puts it in the extra field of the intent. The key is MyKey for this value.
  • It starts the activity by using the startActivity method.

activity_second.xml

This is the xml file for the second activity. It has the below content:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".SecondActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

It has only one TextView. The data we are receiving from MainActivity will be shown in this textview.

SecondActivity:

This is the second activity. It holds the below content:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val intentValue = intent.getStringExtra("MyKey")
findViewById<TextView>(R.id.textView).apply{
text = intentValue.toString()
}
}
}

It is reading the string from the key MyKey and assigning it to the TextView with id textView.

If you run the app, it will show the text you entered in the SecondActivity.

Subscribe to our Newsletter

Previous
Introduction to Activity configuration changes
Next
Introduction to Fragment in Android Kotlin