Looking For Anything Specific?

[Tutorial] Android Intent With Kotlin Sample Code

Hello guys,
In this tutorial, I am going to share how to work with android intent with Kotlin, for those of you who don’t know what is an intent in android application below is a small explanation which is given in the android developer's docs, if its confusing don’t worry we will make sure you understand everything

What is the intent?

An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways
As stated above intent is a messaging object which can be used to communicate between components. And we are going to use that feature in our app to do some tasks. And the most common use case for android intents starts a new activity. What is the activity?

What is an activity in the android application?

Activity in android is the screen that is a user viewing in the application when he clicks on the icon on the launcher. For example take play store, when you click the icon on the launcher, the application you opens and from there every screen you see is an activity, well the complete screen is an activity inside that you’ll see the button, texts, edit box those are called widgets. So we can say that an activity is generally, more than one widget that combines together to make a user experience is. So I hope you got a clear picture of activities, Doubt? don’t be shy to ask in the comments … So now that you learned about activity let's play with some, in this tutorial we are going to create two activities and switch from activity one to activity b, and of course we are going to use kotlin

How to switch between activities in android applications?

Let's start by creating activities, you can do that in two ways of course by using the android studio itself or by manually creating each and every file all by yourself (not recommended). When you’re creating a new project you get the first activity “MainActivity” which is created for you by android studio and for our example, we just need one more activity to start working with intent

Steps to create an activity in the android studio:

  • Open android studio create a new project
  • Don’t forget to choose language Kotlin
  • Right-click on the app folder in the project section
  • New -> Activity -> Empty Activity -> “Name you activity” or leave it as be “Main2Activity.kt”
  • Hit Finish

android studio project intent example kotlin
This is how the android studio project should be
Well now that you have created your second activity now let's move on to the coding part, start another activity in the android application with Kotlin, but before that, you need to know a few things about the Android activity life cycle you can learn about that here Android Activity Android Developers Docs, or you can refer to the below image
Got a clear picture of the Android activity life cycle? 😉 well now that you now how android activity works let's move on to the coding part, In this example, we are going to start an activity from another activity in an android application so we are going to do that in a button click action so let's create a button and label stating the current activities name or any random text to identify the activity
<!-- This is a code to create button in android. 
For this example file name : activity_main.xml-->
        android:text="Start Activity B"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
The above code will create a simple button in android with the text “Start Activity B” and if you started this project without making any modification in the file name at the start then it should probably go under the activity_main.xml otherwise paste it in the starting activity of your application. As you can see we made this button constrain to a textView you can learn more about android constrain layout here: Android Constraint Layout A Complete Basic Guide. Read this guide if you want to learn about constraint layout and some important properties because in this blog we will be completed using the android constraint layout for design also you can see our detailed guide on how to make a login screen with an android constraint layout here.
In out Make a toast in android with kotlin guide we have used setOnClickListener() to create on click action on a button. Alternatively, we will be using the onClick example attribute with a function to execute the code in this guide, after typing this line you will get a red squiggly line warning stating you have to create a function in that name, which is because this attribute requires a function / a part of code that needs to be executed after the button click so let's create the function
fun startActvityB(view: View) {
Above is the onClick function that will be executed when a user clicks the button, this code is equivalent to the button.setOnClickListener{}, why use onClick like this well its easier, and we can add this attribute to other widgets like text view and image view as well so this might be helpful for you, if you’re trying to start another activity on android on textview click, make sure to create a method as the same name from XML’s attribute value right now this is just an empty function in where our start another activity from intent code will go, but before that put some text on the second activity to make sure we started second activity from first activity,

android intent example first activity screen
This is our first activity MainActivity.kt
This is so far what we have created. All set? alright then…. add some Kotlin code
   val i = Intent(applicationContext,Main2Activity::class.java)
Above is the code to start another activity in android with Kotlin,
Intent(context: Context, class: Class) is the class that is used to set the second activity / or generally to set the activity you want to start, and if you’re from java you might be wondering where is one important part of the code the “new” keyword well
To create an instance of a class, we call the constructor as if it were a regular function. Note that Kotlin does not have a new keyword.
context: Context! : is the applicationContext from where the activity is started you can use this keyword or the applicationContext which is equivalent to java’s getApplicationContext(). In Kotlin, getters and setters are the same if it's after the equals symbol it is a getter, if it's before the equals symbol its a setter in the perspective of android, we will see about this in the later example before that make sure to read this guide Kotlin Guide Geter and Setter to not to get confusion
class: Class is the second parameter we want to pass to the Intent class which is the name of the class which you want to start and it must be an activity, meaning make sure the class extends android Activity Class and have a layout file, otherwise you might get an error. And in our case, I didn’t change the activity name while creating, so it is MainActivity::class.java well if you’re from java you might have called Main2Activity.java
startActivity(intent: Intent ) is the method that is used to start the second activity, and in this method, we pass the intent object that we just created which in our case is I (short for intent no special meaning).
Did you notice the val in the line yes in Kotlin we use val for nonmutable values which is nothing but a value that will not be modified after it is created more like static from java, it is good practice to use val to avoid your code accidentally changes the value in the intent object
Add these pieces of code in your project then run.

android intent example second activity screen
This is our second activity Main2Activity.kt
Congratulations you have creates your android application with two activities in Kotlin and you made a switch between activities. As you can see this is our second activity, which is started from MainActivity.kt and the toast message you can learn about toast message in this tutorial on how to display toast on the android application with kotlin. Before you close this tab below are some important points that I wish I knew when I started learning android intent so here they are for you

The thing you need to know about Android Activity and Intent

Below some of the things I would like to add, that you might find it interesting and maybe needed in your application because the possibilities of android applications are endless so you often need some out of the box things to know as a developer
  • Intents are not only for starting activities, they can also be used to start a service and delivering broadcasts. We will see the services later.
  • There are two types of intents Explicit Intents and Implicit Intents we will cover both with an example in the next post
  • With intent, you can pass data to share between in-app activities which will come in handy. Tutorial Here(will be updated soon)
These are the basic you need to know about android Intent, and for activity, Well from our above example if you click back on the second activity you will jump back to the first activity well this is great right but what if you have ten activities in a chain and the user must close the ten activity to exit the application which is not a good practice right? so if you don’t want your application to go back to the previous activity simply call
After you call startActivity() method .And it will now if you press back your application will exit instead of jumping to the first activity, Try that and comment on your result.
So for if your application is working fine then congratulation. But if your activities don’t start, here are the debugging tips
  • Always keep an eye on the log-cat
  • Check your AndroidManifest.xml if the activity name is present or not
  • Always check to spell (which is my common error)
Download Complete Code: GDrive Link
Still trouble, comment below of you can always use your programming friend StackOverFlow, Don’t be a stranger to comment, speak your mind
~~~Sharing is Caring~~~
keywords: android Kotlin intent activity, one activity to another activity in Kotlin, android Kotlin start another activity, how to move from one activity to another in android on button click, how to move from one activity to another in android on textview click,
how to navigate to another page in android on button click