Latest News

Pass Data Between Activities Using Intent (Kotlin) - Android Studio Tutorial

In this tutorial i'll show you how to pass data between different activities using intent.
For Example in this tutorial 
Activity 1: Input data using EditText and click Save Button to start second activity with the data that was input.
Activity 2: Show the input data of previous activity using Intent.


Step 2: Create New Activity File>New>Activity>EmptyActivity

Step 3: Code

acitivity_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:orientation="vertical"android:gravity="center"android:padding="10dp"tools:context=".MainActivity"><EditTextandroid:id="@+id/nameEt"android:hint="Enter Name"android:inputType="text"android:layout_width="match_parent"android:layout_height="wrap_content" /><EditTextandroid:id="@+id/emailEt"android:hint="Enter Email"android:inputType="textEmailAddress"android:layout_width="match_parent"android:layout_height="wrap_content" /><EditTextandroid:id="@+id/phoneEt"android:hint="Enter Phone"android:inputType="phone"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/saveBtn"android:text="save"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

MainActivity.kt
package com.blogspot.atifsoftwares.myapplicationimport android.content.Intentimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.widget.Buttonimport android.widget.EditTextclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val nameEt = findViewById<EditText>(R.id.nameEt)val emailEt = findViewById<EditText>(R.id.emailEt)val phoneEt = findViewById<EditText>(R.id.phoneEt)val saveBtn = findViewById<Button>(R.id.saveBtn)//handle button clicksaveBtn.setOnClickListener {//get text from edittextsval name = nameEt.text.toString()val email = emailEt.text.toString()val phone = phoneEt.text.toString()//intent to start activityval intent = Intent(this@MainActivity , SecondActivity::class.java)intent.putExtra("Name" , name)intent.putExtra("Email" , email)intent.putExtra("Phone" , phone)startActivity(intent)}}}

activity_second.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:gravity="center"android:padding="10dp"tools:context=".SecondActivity"><TextViewandroid:id="@+id/resultTv"android:textSize="30sp"android:textStyle="bold"android:textColor="#000"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>

SecondActivity.kt
package com.blogspot.atifsoftwares.myapplicationimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.widget.TextViewclass SecondActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_second)//get data from intentval intent = intentval name = intent.getStringExtra("Name")val email = intent.getStringExtra("Email")val phone = intent.getStringExtra("Phone")//textviewval resultTv = findViewById<TextView>(R.id.resultTv)//setTextresultTv.text = "Name: "+name+"\nEmail: "+email+"\nPhone: "+phone}}

Step 4: Run Project

Output
Pass Data between Activities using intent (Kotlin) - Android Studio Tutorial

0 Response to "Pass Data Between Activities Using Intent (Kotlin) - Android Studio Tutorial"