How to Send Data Back to MainActivity in Android using Kotlin?
Last Updated :
06 Dec, 2021
As there are many methods to send the data, but in this article, we will use startActivityForResult() method. Here we need to launch a child activity using startActivityForResult() method. Then from child activity, we can easily send data back to Main Activity.
Example:

Note: To implement it in java refer to this article: How to send data from one activity to second activity using Java
Step by Step Implementation
Step 1: Create new project in android using kotlin
Step 2: Create XML layout for MainActivity
XML
<?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">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/ic_geeksforgeeks"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"
android:textColor="#7CB342"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.303" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Below is the code for MainActivity.kt file
Kotlin
package com.ayush.gfg_exit
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import kotlin.properties.Delegates
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.btnMain).setOnClickListener {
val intent = Intent(this, ChildActivity::class.java)
// 0 is request code
startActivityForResult(intent, 0)
}
}
// this called after child activity finishes.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// Get the result from intent
val result = intent.getStringExtra("result")
// set the result to the text view
findViewById<TextView>(R.id.textView).text = result
}
}
}
}
Step 4: XML for child activity
XML
<?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=".ChildActivity">
<EditText
android:id="@+id/etChild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.444"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.263" />
<Button
android:id="@+id/btnChild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send data to MainActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etChild"
app:layout_constraintVertical_bias="0.241" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 5: Following code for ChildActivity.kt file
Kotlin
package com.ayush.gfg_exit
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class ChildActivity : AppCompatActivity() {
lateinit var etChild: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_child)
etChild = findViewById(R.id.etChild)
findViewById<Button>(R.id.btnChild).setOnClickListener {
val result = etChild.text.toString()
val intent = Intent()
intent.putExtra("result", result)
setResult(Activity.RESULT_OK, intent)
finish()
}
}
}
So our app is ready.
Output:
Similar Reads
How to Create Option Menu in Android using Kotlin? In this article, we will learn how to create an options menu in the Android app using Kotlin. To have an options menu in an Activity, we need to create a new menu XML file and inflate it using menuInflator.inflate( ) method. In menu.xml we will design the options menu as the requirement of the app.
2 min read
How to Send SMS in Android using Kotlin? SMS Manager is a class in Android which is used to send the SMS to a specific contact from the android application. We can send text messages, data messages, and multimedia messages using this class. There are different methods that are provided to send different types of messages. In this article,
4 min read
Send Multiple Data From One Activity to Another in Android using Kotlin There are multiple ways for sending multiple data from one Activity to Another in Android, but in this article, we will do this using Bundle. Bundle in android is used to pass data from one activity to another, it takes data in key and value pairs. To understand this concept we will create a simple
3 min read
How to send message on WhatsApp in Android using Kotlin Whatsapp is the one of most popular messaging App. Many android applications need the functionality to share some messages directly from their app to WhatsApp. For example, if a user wants to share the app or share a message from the app then this functionality comes in use. Either user can send a t
3 min read
How to Pass Data to Destination using Safe Args in Android? SafeArgs is a gradle plugin that allows you to Pass data to destination UI components. It generates simple object and builder classes for type-safe navigation and access to any associated arguments. Safe Args is strongly recommended for navigating and passing data because it ensures type-safety. A s
6 min read
How to Post Data to API using Retrofit in Android using Jetpack Compose? APIs are used within Android Applications to interact with databases to perform various CRUD operations on data within the database such as adding new data, reading the existing data, and updating and deleting existing data. In this article, we will take a look at How to Post Data to API using Retro
5 min read