Intent in Android

ThinhBach
3 min readApr 27, 2022

--

image source: teamvietdev

What’s intent in Android?

What do you do when you want to send letters, mails or items to another person?
Are you thinking about going to the post office, send them packages or letters, staying home and write email or tying a letter into a dove’s leg?
This is the concept of intent, sending informations between Android component.

Type of intent

There are two kinds of intent: implicit intent and explicit intent.

Explicit intent: like the examples above, when you know exactly the address that you want to send to, then your intent is explicit.
You can use explicit intent to move around activities, start services,…

val intent = Intent(this@MainActivity, DetailActivity::class.java)

Implicit intent: when you want the system to run some functions without pointing which services will handle.
For example, your app has a function that need to use camera, so you send an intent contain an action ACTION_IMAGE_CAPTURE, then system will detect which service can handle that action (like camera apps).

// remember to request the camera feature to manifest
<uses-feature android:name="android.hardware.camera"
android:required="true" />
// define an implicit intent
val REQUEST_IMAGE_CAPTURE = 1

private fun takePicture() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}

Intent structure

There are some pieces of an intent:

  • Action: the main action that your intent will be performed. Ex: ACTION_IMAGE_CAPTURE, ACTION_REVIEW, ACTION_VIDEO_CAPTURE,..
  • Data: Use a Uri variable as data for system to operate on. Data type is up to the action.
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("Thanks you everybody")
  • Category: Gives additional information about the action. For example, your MainActivity in AndroidManifest has a category named “android.intent.category.LAUNCHER”, which means, MainActivity will appear in the Launcher as a top-level application.
  • Type: Specifies an explicit type (a MIME type) of the intent data.
intent.type = "text/plain"
  • Extras: This is a bundle of every addition information. Exist as key-value variables. You can put extra datas and get it later, like an item package.
val KEY = "name"
intent.putExtra(KEY, "Jon Snow")

Moving around Activities

The most basic useful thing of intent is moving and transferring datas between activities.
Supposing we have two activities: ContactListActivity to show phone numbers in your device, and PhoneDetailActivity to show informations about each phone number. Our missions are to transfer datas between them and display the phone number information.

Firstly, we create an intent from ContactListActivity that contain the address where the intent will be sent to:

val phoneIntent = Intent(this@ContactListActivity, 
PhoneDetailActivity::class.java)

Next, we need to put extra datas such as phone number, then send this intent away. There are two ways to put extra data into intent:

val PHONE_KEY = "PHONE NUMBER"
val phoneNumber = "0123456789"
// directly put data into intent
phoneIntent.putExtra(PHONE_KEY, phoneNumber)
// or put data into a bundle, then put bundle into intent
val bundle = Bundle()
bundle.putStringExtra(PHONE_KEY, phoneNumber)
phoneIntent.putExtra(bundle)
// after you put data into intent, just send it
startActivity(phoneIntent)

Okay, the displaying activity now is PhoneDetailActivity, now we have to get datas from intent:

val PHONE_KEY = "PHONE NUMBER"
val phoneIntent = intent
val phoneNumber = phoneIntent.getStringExtra(PHONE_KEY)

In conclusion

Intent is one of the most important parts of Android because of communicating between Android system. So, learn it carefully!

Thanks for reading!

References:

https://developer.android.com/reference/android/content/Intent

https://viblo.asia/p/ban-biet-gi-ve-intent-trong-android-Do7544AB5M6

https://developer.android.com/training/camera/photobasics#kotlin

--

--