Get result from another activity using registerForActivityResult | Android Studio | Kotlin

How To Get Result From Another Activity Using the registerForActivityResult?

In this tutorial we will learn how to get result from another activity using registerForActivityResult. You might aware to use the method startActivityForResult which is now deprecated. So we will use the newer way that is registerForActivityResult.

We will create 2 Activities the MainActivity and the FormActivity. From the MainActivity we will launch the FormActivity to input some data and get that data as result back to the MainActivity.

>>Check For Java<<

Code:

activity_main.xml

<?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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/addUserBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add User" />

    <TextView
        android:id="@+id/nameTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/emailTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp" />

</LinearLayout>

MainActivity.java

package com.technifysoft.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView
import android.os.Bundle
import com.technifysoft.myapplication.R
import android.content.Intent
import com.technifysoft.myapplication.FormActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultCallback
import android.app.Activity
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts

class MainActivity : AppCompatActivity() {

    private lateinit var addUserBtn: Button
    private lateinit var nameTv: TextView
    private lateinit var emailTv: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //init UI Views
        addUserBtn = findViewById(R.id.addUserBtn)
        nameTv = findViewById(R.id.nameTv)
        emailTv = findViewById(R.id.emailTv)

        //handle addUserBtn click, start Next Activity to get data from it
        addUserBtn.setOnClickListener {
            // The launcher with the Intent you want to start
            val intent = Intent(this@MainActivity, FormActivity::class.java)
            launchSomeActivity.launch(intent)
        }
    }

    private val launchSomeActivity = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == RESULT_OK) {
            val data = result.data
            val name = data!!.getStringExtra("name")
            val email = data.getStringExtra("email")
            nameTv.text = name
            emailTv.text = email
            Toast.makeText(this@MainActivity, "$name \n$email", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(this@MainActivity, "Cancelled...", Toast.LENGTH_SHORT).show()
        }
    }

}

activity_form.xml

<?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"
    android:padding="10dp"
    tools:context=".FormActivity">

    <EditText
        android:id="@+id/nameEt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name" />

    <EditText
        android:id="@+id/emailEt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email" />

    <Button
        android:id="@+id/submitBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Submit" />

</LinearLayout>

FormActivity.java

package com.technifysoft.myapplication

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class FormActivity : AppCompatActivity() {

    private lateinit var nameEt: EditText
    private lateinit var emailEt: EditText
    private lateinit var submitBtn: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_form)

        title = "Add User"

        //init UI Views
        nameEt = findViewById(R.id.nameEt)
        emailEt = findViewById(R.id.emailEt)
        submitBtn = findViewById(R.id.submitBtn)

        //handle submitBtn click, input data and pass to previous activity
        submitBtn.setOnClickListener {
            //get data
            val name = nameEt.text.toString().trim()
            val email = emailEt.text.toString().trim()

            //put data to intent to get in previous activity
            val intent = Intent()
            intent.putExtra("name", name)
            intent.putExtra("email", email)
            setResult(RESULT_OK, intent)
            //finishing activity
            finish()
        }
    }
}

Screenshot:

Get result from another activity using registerForActivityResult | Android Studio | Kotlin


Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

Add a Back Button to Action Bar Android Studio (Kotlin)