Android StartActivityForResult Example in Kotlin

Android StartActivityForResult Example in Kotlin

MainActivity.kt
package com.example.espl.passingdatabetweenactivities
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import android.content.Intent
import android.app.Activity

class MainActivity : AppCompatActivity() {

    private val SECOND_ACTIVITY_REQUEST_CODE = 0

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

        // "Go to Second Activity" button click
        btn_send_data_to_another_activity.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            // start the SecondActivity
            startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE)  // Activity is started with requestCode 0
        }
    }

    // This method is called when the second activity finishes
    // Call Back method  to get the Message form other Activity
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        // check if the request code is same as what is passed  here it is 0
        if (requestCode === SECOND_ACTIVITY_REQUEST_CODE) {
            // check that it is the SecondActivity with an OK result
            if (resultCode === Activity.RESULT_OK) {
                // get String data from Intent
                val returnString = data?.getStringExtra("MESSAGE")
                // set text view with string
                tv_RecivedData_fromSecondActivity.text ="Recived String:: "+ returnString
            }
        }
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:layout_margin="15dp"
            android:id="@+id/tv_RecivedData_fromSecondActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Default String data MainActivity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button
            android:layout_margin="10dp"
            android:layout_below="@id/tv_RecivedData_fromSecondActivity"
            android:text="GO TO SecondActivity"
            android:id="@+id/btn_send_data_to_another_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</RelativeLayout>

SecondActivity.kt
package com.example.espl.passingdatabetweenactivities
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_second.*
import android.content.Intent

class SecondActivity : AppCompatActivity() {

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

        // "Send text back" button click
        btn_ToPassingDataBackonActivityResult.setOnClickListener {

           // get the text from the EditText
           val stringToPassBack : String =et_ToPassingDataBack.getText().toString();

           // put the String to pass back into an Intent and close this activity
           val intent : Intent=Intent();
                intent.putExtra("MESSAGE", stringToPassBack);
                setResult(RESULT_OK, intent);
                finish();
           }
        }
}

activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".SecondActivity">

    <EditText
            android:id="@+id/et_ToPassingDataBack"
            android:layout_margin="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Default String data SecondActivity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button
            android:layout_below="@id/et_ToPassingDataBack"
            android:layout_margin="10dp"
            android:text="SEND TEXT BACK"
            android:id="@+id/btn_ToPassingDataBackonActivityResult"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</RelativeLayout>

No comments:

Post a Comment

Popular Posts