If you are building an Android application, you must understand how intents work
Today we`ll learn about the two types of Intents in Android: Explicit and Implicit intents It is easy to understand the language
What is an Intent?
An Intent is a way to request an action from another app
- Open Camera
- Start a new Activity
- Share a message
Β
Explicit Intent
An Explicit intent is used to invoke the activity class, such as sending data from one activity to another activity
Example:
val intent = Intent(this, CheckBoxActivity::class.java)
intent.putExtra("title", "Welcome To Second Screen")
startActivity(intent)
Passing data between activities and Kotlin
Project Structure:
MainActivity
: The entry screen is where you click a button to go to another screen.
SecondActivity
: A screen that receives a message and displays it. It also lets you navigate back to MainActivity
.
XML Design:-
<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id=”@+id/main”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<Button
android:id=”@+id/textView”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_margin=”20dp”
android:gravity=”center”
android:text=”Go To Next”
android:textColor=”@color/white”
android:textSize=”25sp”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintHorizontal_bias=”1.0″
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />
</androidx.constraintlayout.widget.ConstraintLayout>
Code MainActivity:-
package com.programmingkeeda.kotlindemo
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var textView: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById<Button>(R.id.textView)
val title = intent.getStringExtra("title")
if (title != null) {
textView.text = title
}
textView.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("title", "Welcome To Second Screen")
startActivity(intent)
}
}
}
XML Design:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Welcome to Second Activity"
android:textColor="@color/black"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/btnGoBack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.544" />
<Button
android:id="@+id/btnGoBack"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:gravity="center"
android:text="Go Back"
android:textColor="@color/white"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.318" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code SecondActivity:-
package com.programmingkeeda.kotlindemo
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class SecondActivity : AppCompatActivity() {
lateinit var textView2: TextView
lateinit var btnGoBack: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
textView2 = findViewById<TextView>(R.id.textView2)
btnGoBack = findViewById<Button>(R.id.btnGoBack)
val title = intent.getStringExtra("title")
textView2.text = title
btnGoBack.setOnClickListener {
val secondActivity = Intent(this, MainActivity::class.java)
secondActivity.putExtra("title", "Go To Second Activity")
startActivity(secondActivity)
}
}
}
Implicit Intent
The Implicit intent is used to invoke the system components. like send email, send SMS, etc
Example:
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(“https://www.programmingkeeda.com/”))
startActivity(intent)