Intents, Love, and Shooting Your Shot Right

Emmanuel Ututu
7 min readOct 27, 2023

--

Part I

Photo by Nathan Dumlao on Unsplash

Love, love, oh love. Universal, ubiquitous, inspirator, motivator, companion, friend, savior, and {sometimes} one hell of a daredevil in details. Multifaceted, charming, and complicated as love can be, it, without a doubt, is a universal language that cuts across borders, tribes, cultures, and religions. Emotional as it can humble us, it’s such a powerful force that makes strangers turn intimately close like they were family or previously friends for decades in just a flash of time. Spin that same coin differently and flip that force in a different direction, and we could have lovers suddenly turn back to complete strangers suspicious of each other.

Sighs. Don’t we all want a happy ending? A first love that lasts forever. A true love that stays by us no matter what event. A loyal love that will see through the depth of our soul and weakness and never betray us. A love with unambiguously clear intents that can be easily interpreted and understood.

Understandably, while some have been lucky to have captured and won over love at its pure and true nature, some have tried and made their peace with seeing love from afar but never feeling it so close to home. Or maybe you’re like Jordin Sparks; that no matter what is said about love, you keep coming back for more.

However, what if you can design and request the kind of love you desire and get that request serviced successfully?

Now, let’s take an adventure in someone’s attempt to find love and navigate through the bustling streets of Lagos with all its charisma, unpredictability, and charm. Here, love and Android Intents share an uncanny resemblance.

The Meet-cute
Imagine being Chijioke, a young handsome man in Lagos, strolling through Ikeja’s busy streets when he spots Olufe, a fascinating woman reading a book in a nearby café and is entranced by her. Chijioke wishes to communicate with Olufe but doesn’t know her language. What if there’s a way to send Olufe a ‘message’ (Intent) without directly knowing her (the receiving Activity)?

In Android, when an app wants to start an activity, send some data to another app, or perhaps wants a result back, it uses something called an Intent. Much like Chijioke’s intention to communicate with Olufe.

// Chijioke wants to send Olufe a note (Intent)
val intent = Intent(this, OlufeActivity::class.java)
intent.putExtra("Message", "Hi, I noticed you love books. Me too!")
startActivity(intent)

The Set Up
Before you can run this code in Android Studio, ensure you have set up a Kotlin project and have created an activity named ‘OlufeActivity’

Now let’s talk about the main types of Intents:

  1. The Direct Approach — Explicit Intent

Imagine Chijioke spots Olufe at a local Nigerian festival. He’s determined to walk straight up to her and start a conversation because he knows exactly where she is and wants to engage directly. This is the equivalent of an explicit intent in Android.

val directConversationIntent = Intent(this, OlufeActivity::class.java)
startActivity(directConversationIntent)

Here, Chijioke knows Olufe’s exact ‘location’ (her activity) and decides to initiate a direct conversation with her.

2. The Whisper in the Wind — Implicit Intent

Now, picture a scenario where Chijioke is at the same festival but isn’t sure where Olufe is. However, he’s heard from friends that she loves the rhythm of the talking drum. He decides to play a tune on the drum, hoping she’d hear and come towards the sound. This approach mirrors an implicit intent in Android.

val drumTuneIntent = Intent(Intent.ACTION_PLAY).apply {
type = "audio/x-wav"
putExtra(Intent.EXTRA_TITLE, "Talking Drum Melody for lovers of folklore music")
}
startActivity(drumTuneIntent)

Chijioke isn’t directing his action to Olufe specifically. Instead, he broadcasts his intent, hoping the right person (activity) picks it up.

Guided by Tradition — Intent Filters

Now, Olufe, being a cultured woman, knows her traditions well. She understands the significance of different talking drum beats. Similarly, in Android, intent filters help an activity understand and determine the kinds of intents it’s open to receiving.

<intent-filter>
<action android:name="android.intent.action.PLAY" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/x-wav" />
</intent-filter>

Another Approach…

Broadening Horizons

Photo by Ian Schneider on Unsplash

Explicit Intents — The Direct Approach

Remember when Chijioke knew exactly where Olufe was seated? That’s an Explicit Intent, where you specify the exact target component. It’s like directly walking up to someone and striking up a conversation.

In the Android world, when you know the exact Activity or Service you want to start, you craft an explicit intent by directly naming the target component.

val explicitIntent = Intent(this, OlufeActivity::class.java)
explicitIntent.putExtra("DirectMessage", "Hi Olufe, would you like some coffee?")
startActivity(explicitIntent)

It’s the equivalent of Chijioke writing a note, “For Olufe,” and handing it to her directly.

Implicit Intents — The Indirect, Hopeful Gesture

Imagine Chijioke not knowing Olufe’s exact location but knowing she loved poetry. He might recite a poem aloud, hoping she would hear and respond. That’s an Implicit Intent. You don’t specify a target; instead, you declare a general action and let fate (or Android’s Intent resolution system) find the best recipient.

val poetryIntent = Intent(Intent.ACTION_SEND)
poetryIntent.type = "text/plain"
poetryIntent.putExtra(Intent.EXTRA_TEXT, "Roses are red, Violets are blue and Pearl is love...")
startActivity(Intent.createChooser(poetryIntent, "Recite your Poem"))

It’s like Chijioke reciting a poem at a gathering, hoping Olufe would notice🙈 (sharp guy 😎).

Broadcast Intents — The Announcement

In our love story, imagine Chijioke standing atop a stage and making an announcement about a poetry competition, hoping to catch Olufe’s attention. Broadcast Intents in Android are a way to send global messages to other apps and parts of your app.

val announcement = Intent("com.example.PoetryCompetition")
sendBroadcast(announcement)

It’s akin to making a general proclamation, hoping the right ears (apps or activities with matching intent filters) catch wind of it.

Sticky Broadcasts

Think of these like Chijioke’s persistent attempts to win Olufe’s heart. Sticky Broadcasts stay around even after they’re sent, letting others collect data from them. Although they were popular once, they’re mostly deprecated now for performance and security reasons.
Such a red_flag_kind of love, good riddance 😏🧑🏾‍🦯

Handling Intent Results — The Callback

Just like how Chijioke would await a nod or a smile from Olufe after sending his poem, Android allows you to await results from an Intent.

val dateIntent = Intent(this, DateActivity::class.java)
startActivityForResult(dateIntent, REQUEST_CODE)

Later, you can handle the response using onActivityResult.

The Date and Awaited Response — Using onActivityResult

One day, emboldened, Chijioke decides to ask Olufe out on a date. It’s about time that happened 😍. Like sending out a request and hoping for a response, in Android, when you start an activity expecting a result, you’re setting the stage for onActivityResult.

Setting up the Date — startActivityForResult

Chijioke doesn’t just want to ask Olufe out; he wants to know her response. In the world of Android, when you want to start an activity and are eager for its feedback, you use startActivityForResult.

val dateRequest = Intent(this, DateActivity::class.java)
dateRequest.putExtra("Message", "Would you like to join me for dinner?")
startActivityForResult(dateRequest, REQUEST_CODE_DATE)

In this code, Chijioke (your current activity) is asking Olufe (the DateActivity) out on a date. REQUEST_CODE_DATE is a unique code you define to identify this particular request. It’s like Chijioke noting down in his diary that today, he asked Olufe out.

Awaiting the Response — onActivityResult

After the date, Chijioke anxiously awaits Olufe’s feelings about the evening. In Android, once the activity you started for a result (the date) finishes, the onActivityResult method is triggered.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == REQUEST_CODE_DATE) {
if (resultCode == Activity.RESULT_OK) {
val message = data?.getStringExtra("Reply")
Log.d("DATE_RESPONSE", "Olufe's response: $message")
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.d("DATE_RESPONSE", "Olufe chose not to reply.")
}
}
}

Here’s what’s happening:

  1. requestCode: This helps Chijioke remember which request he’s receiving a response for. Was it the date? The poetry reading invitation? Or something else?
  2. resultCode: Olufe’s reply about the date. It can be RESULT_OK (she had a great time 🫠) or RESULT_CANCELED (she doesn't want to talk about it. Ouch🥶☹️).
  3. data: Extra details about the date. Did she enjoy the dinner? Was the movie entertaining? These are packaged into the ‘data’ Intent.

Just like in relationships, where clear communication is the key, startActivityForResult and onActivityResult ensure that Android activities speak to each other effectively. By understanding the intention (or Intent) behind these methods, you pave the way for smoother app experiences, just as Chijioke and Olufe strive for clearer understanding in their budding relationship.

And Now…

Photo by Kelly Sikkema on Unsplash

Preparedness in Uncertainty

In love, as in code, there are no guarantees. Just as Olufe might not always hear Chijioke’s drum beats (probably distracted by someone else’s gist 😩), not all intents will find a matching activity.

if (drumTuneIntent.resolveActivity(packageManager) != null) {
startActivity(drumTuneIntent)
} else {
Toast.makeText(this, "Sad. No one recognized the drum's beat.", Toast.LENGTH_SHORT).show()
}

In Conclusion

Navigating the landscape of Android Intents and Love, especially in the heart of Nigeria, is a tale of intention, communication, and resolution. Just as Chijioke finds his way to Olufe’s heart, using Intents correctly can make your Android app interact seamlessly with others. Gladly, it’s Friday (TGIF! 🍾🍻); step out there bravely, have fun, and keep shooting your shots right, and soon, you’ll master the art of both love and Android Intents.

Enjoy the weekend, and see you around soon, where we would talk about The Many Facets of Love and Intents. 😊

--

--

Emmanuel Ututu

Mobile developer & writer, with several years of experience, adding creativity & imagination to projects. I love to dream of needs and their solutions.