Understanding Intents in Android: A Guide to Communication Between App Components
Table of contents
Intent helps us as a communication link between different application components by providing runtime binding between different components.
Start an Activity
A new instance of an activity can be created by passing an intent to thestartActivity()
method. The intent defines the activity that needs to be started and the necessary data to be communicated.If a result is needed from the called activity when it finishes. we pass the intent in the
startActivityForResult()
method.Start a Service
A service can be initiated and carried out by passing an intent to the
startService()
method. The intent defines the service that needs to be started and the necessary data to be communicated.Delivering a Broadcast
A broadcast can be delivered to other applications by passing an intent to the
sendBroadcast()
method.
Intent Types
Explicit Intent: specifies the application that will satisfy the intent, by supplying either the target app's package name or a fully-qualified component class name.
Explicit Intent is also used to initiate the different components of our application.
Implicit Intent: doesn't satisfy a specific component, but declares a general action to perform, which can be handled by different application's components. Hence a dialog to pick the application is presented to the user if there's more than one application that can handle the intent.
Explicit Intent
Creating Intent :
//MainActivity.kt
var intent = Intent(this@MainActivity, SecondActivity::class.java)
//where this@MainActivity represents our context
//SecondActivity::class.java represents our component to activate
startActivity(intent)
//calls the startActivity method with the intent object
This method calls the SecondActivity.kt
Application from our MainActivity.kt
file which creates the second_activity.xml
during the onCreate()
method on our application screen.
Passing Data with Intent :
//MainActivity.kt
var userName = "John"
var age = 18
intent.putExtra("username", userName)
intent.putExtra("userage", age)
//where the first parameter represents the tag to the data
//second parameter represents the data that needs to be passed
Receiving Data :
//SecondActivity.kt
var receivedName = intent.getStringExtra(username).toString()
var receivedAge = intent.getIntExtra(userage,18).toInt()
//where the parameter represents the data tag
//second parameter represents the default value which is optional
Creating a Navigation Icon:
making the MainActivity parent of SecondActivity creates a navigation icon that helps us easily go back to the activity from which we came.
<!--AndroidManifest.xml-->
<activity
android:name = ".SecondActivity"
android:exported = "true"
android:parentActivityName = ".MainActivity"/>
Implicit Intent
Implicit Intent follows the following passage :
Activity A is created.
An Intent is created by the Activity A, which is passed to the
startActivity()
methodAndroid System searches all the applications for an Intent Filter that matches the Intent.
The system starts the matching Activity by calling its
onCreate()
method
Intent Filter: an expression in the application's manifest file that specifies the type of intent, an activity, service, or broadcast receiver can respond to. That is, declares the capabilities of its parent components.