Android Intent Handling Between Activities Using Kotlin With Examples

In this tutorial, we’ll be discussing Android Intents and implement them using Kotlin in our application.

What Will You Learn?

  • What are Intents?
  • Types Of Intents?
  • Using Intents Between Activities
  • Sending Data Using Android Intents
  • Using Parcelable and Serializable to pass objects
  • Creating shorthand intents

Android Intents

As the name says Intent is something that’s used to perform some action with respect to the flow of the android application. Intents can be used to:

  • Starting a new activity and passing some data.
  • Starting Fragments/Communicating between fragments.
  • Start/End service.
  • Launch activities from a broadcast receiver

In this tutorial, we’ll be looking mainly at intents to handle activities.

An intent definition mainly consists of an instance of the current activity. We set the component name which can be:
The fully qualified class name of the activity to be called. This type of Intent is an explicit intent.
An action such as URL, phone number, location. It’ll display all the available applications of those types.
This falls under the implicit intent category.

In Kotlin, following is the way to create an activity.

startActivity would add OtherActivity on the activity stack and launch it.
How does our Application, realise which activity is the first to be invoked?
In the AndroidManifest.xml we set the intent filter with the action android.intent.action.MAIN and category android.intent.category.LAUNCHER on the first activity to be launched when our application opens.

finish() is used to destroy an activity and remove it from the stack.

Intent Flags

Flags are like options that can be set on intents to customise the launch process.
If you start the same activity everytime, a new instance would be created and added onto the activity stack
To prevent this, you can use the flags:
FLAG_ACTIVITY_SINGLE_TOP – If set, the activity will not be launched if it is already running at the top of the activity stack.

Similarly using a flag FLAT_ACTIVITY_CLEAR_TOP would not launch another instance of the activity if it already exists. This flag would clear all the activities above the activity that’s called and set it on the top of the stack.

Passing Data Through Intents

To pass data onto the new activities we use key value pairs inside the function putExtra, putStringArrayListExtra etc.
putExtra generally passes the basic types such as Int, Float, Char, Double, Boolean, String along with IntArray…. etc.


These Extras fields are under the hood wrapped into the Bundle object which ultimately holds all the data to be passed.

To retrieve the data in the other activity, we need to use the extras property over the bundles.

Retrieving Data in the new Activity

intent, extras are equivalent to getIntent(), getExtras() in Java.
We’ve used a nullable type Bundle? to prevent NullPointerExceptions when not data exists. Similarly, for the data that’s fetched using the keys, we’ve used the nullable types to prevent NPE that can occur when the key is incorrect.

Using Parcelable and Serializable Data

Sometimes we need to pass a complete object from one activity to another. It’s not possible to do so unless we implement the Parcelable or Serializable interface.

Difference between Parcelable and Serializable

  • Parcelable interface is a part of the Android SDK. Serializable is a standard interface of Java.
  • In Parcelable you need to set all of the data you need to pass in a Parcel object and also override the writeToParcel() methods etc. In serializable implementing the interface is sufficient to pass the data.
  • Parcelable is faster than Serializable.

Sending Parcelable Data

Kotlin comes up with some handy annotations to save us from overriding the writeToParcel() method to set the data on the Parcelable. Instead, we can use @Parcelize annotation as shown below:

Note: Currently in your build.gradle you must add the following code for the @Parcelize annotation to work:

In your Activity you do:

Sending Serializable Data

Let’s use over above knowledge in our Android Studio Project.

Project Structure


Layout Code

The code for the activity_main.xml layout is given below:

The code for the activity_other.xml layout is given below:

Activity Code

The code for the MainActivity.kt class is given below:

In the above code, we’ve used Buttons for each type of Intent.
We’ve used Kotlin’s with expression to prevent setting data over the intent object every time.
Besides, we’ve created three different intents apart from the ones already discussed above.
A browser intent is used to launch the url present in the intent in the browser app.
It uses Intent(Intent.ACTION_VIEW, uri).
A location intent is used to launch the lat,lng location in the maps application.
Both of these are implicit intents.
Lastly, we’ve used a generic intent in which we use the Kotlin’s extension functions and lambda expressions to create a shorthand function to launch an intent.
For this we use the following functions:

startActivity is an exension function which looks for a higher order function as it’s parameter.
Thanks to this, we can now launch intents in as few lines as:

The code for the OtherActivity.kt class is given below.

We’ve used let and apply to handle nullable types and prevent doing bundle.field in every line.

The output of the above application in action is given below:

This brings an end to this tutorial on Android intents in Kotlin. You can download the project from the link below.

By admin

Leave a Reply

%d bloggers like this: