How to Send Data Back to MainActivity in Android using Kotlin?
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 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 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:
Please Login to comment...