Intent in Android :
The purpose of intent is to take action. Mainly used to start an action, send a broadcast receiver, start a service, and send a message between two actions. There are two kinds of intents in Android: implicit intent and explicit intent.
The intent is a message object that can be used to request an operation from another application component. Although the intention is to facilitate communication between components in many ways, there are three main use cases: Start operations. The action is a single screen in the application.
There are two types of intent:
- Implicit intent
- Explicit intent
Syntax of Intent:
val intent = Intent(this,Demo::class.java) startActivity(intent)
What are Intent Flags in Android?
Flag defined in the Intent class and used as a flag for Intent metadata. Flags can tell the Android system how to start an Activity and how to deal with it once it is started. Before passing the Intent to startActivity (Intent), you can change this behavior by specifying the Intent flag on the Intent instance.
example:
val intent = Intent(context, DemoActivity::class.java) intent.flags = (Intent.FLAG_ACTIVITY_NEW_TASK)
Difference between Explicit and Implicit intent :
Explicit intent | Implicit intent |
---|---|
It is used to open explicit certain Activity | It is used external messaging between apps |
It is used in internal messaging between your app | It is used to open some Activities with certain service |
It is in the designated target class |
Files:
- MainActivity.kt
- activity_main.xml
- AnotherActivity.kt
- another_activity.xml
Preview
File: MainActivity.kt
package com.sagar.intentinandroid import android.content.Intent import android.net.Uri import android.os.Bundle import android.view.View import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { var explicit_btn: Button? = null var implicit_btn: Button? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) explicit_btn = findViewById<View>(R.id.Explicit_btn) as Button implicit_btn = findViewById<View>(R.id.implicit_btn) as Button explicit_btn!!.setOnClickListener { val intent = Intent(this, AnotherActivity::class.java) startActivity(intent) } implicit_btn!!.setOnClickListener { val intent = Intent(Intent.ACTION_VIEW) intent.data = Uri.parse("https://developersdome.com") startActivity(intent) } } }
File: activity_main.xml
<?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:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="30dp" android:paddingTop="30dp" android:paddingRight="30dp" android:paddingBottom="30dp" tools:context=".MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="87dp" android:layout_marginEnd="4dp" android:layout_marginRight="4dp" android:background="@color/white" android:fontFamily="sans-serif-black" android:gravity="center_horizontal" android:text="If you use implicit intent button then you will be redirect to the DevelopersDome" android:textAlignment="center" android:textColor="@color/black" android:textSize="24sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="240dp" android:layout_marginEnd="4dp" android:layout_marginRight="4dp" android:background="@color/white" android:fontFamily="sans-serif-black" android:gravity="center_horizontal" android:text="If you use Explicit intent button then you will be navigate to the Another Activity" android:textAlignment="center" android:textColor="@color/black" android:textSize="24sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/Explicit_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="572dp" android:backgroundTint="@color/teal_200" android:text="Explicit Intent " android:textColor="@color/black" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/implicit_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="189dp" android:backgroundTint="@color/teal_200" android:textColor="@color/black" android:text="Implicit Intent " app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
File: AnotherActivity.kt
package com.sagar.intentinandroid import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast class AnotherActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_another) Toast.makeText(this, "Another Activity\nDevelopers Dome",Toast.LENGTH_LONG).show(); } }
File: another_activity.xml
<?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:layout_width="match_parent" android:layout_height="match_parent" android:background="#4A148C" android:paddingLeft="30dp" android:paddingTop="30dp" android:paddingRight="30dp" android:paddingBottom="30dp" tools:context=".AnotherActivity"> <TextView android:id="@+id/textView_AA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="159dp" android:text="This is Another Activity\nDevelopers Dome" android:textAlignment="center" android:textAppearance="?android:attr/textAppearanceLarge" android:textSize="30sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:gravity="center_horizontal" /> </androidx.constraintlayout.widget.ConstraintLayout>