Passing Data Between Activities Android Kotlin Tutorial

Passing Data Between Activities Android Kotlin Tutorial

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)
            // get the text to pass
            intent.putExtra(Intent.EXTRA_TEXT, edittextTosend.getText().toString().trim())
            // start the SecondActivity
            startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE)
        }
    }

    // This method is called when the second activity finishes
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // check that it is the SecondActivity with an OK result
        if (requestCode === SECOND_ACTIVITY_REQUEST_CODE) {
            if (resultCode === Activity.RESULT_OK) {

                // get String data from Intent
                val returnString = data?.getStringExtra(Intent.EXTRA_TEXT)
                // set text view with string
                tv_RecivedData_fromSecondActivity.text ="Recived String:: "+ returnString
            }
        }
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:orientation="vertical"
        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">

    <EditText
            android:id="@+id/edittextTosend"
            android:layout_width="match_parent"
            android:layout_margin="10dp"
            android:layout_height="wrap_content"
            android:text="Default String data Main Activity"
            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:text="GO TO SecondActivity"
            android:id="@+id/btn_send_data_to_another_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <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"/>
</LinearLayout>

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)
       
        // get the text from MainActivity
        val intent = intent
        val text = intent.getStringExtra(Intent.EXTRA_TEXT)

        // use the text in a TextView
        tv_RecivedData_fromMainActivity.text = "Recived String:: "+text

        // "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(Intent.EXTRA_TEXT, stringToPassBack);
                setResult(RESULT_OK, intent);
                finish();
           }
        }
}

activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:orientation="vertical"
        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_margin="10dp"
            android:text="SEND TEXT BACK"
            android:id="@+id/btn_ToPassingDataBackonActivityResult"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <TextView
            android:id="@+id/tv_RecivedData_fromMainActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            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"/>
</LinearLayout>

No comments:

Post a Comment

Popular Posts