Skip to content

Explicit Intent – Start Another activity example in Kotlin

  • by

Explicit Intent in Android you can use for launch a specific app component, like a particular Service, Activity or Broadcast. For example, if you want to go to one activity to another one then you can use Explicit Intent and send data putExtra("value""eyehunt Tutorial") to second activity. In this example, you will learn how to pass data one activity to another activity using kotlin language.

Explicit Intent Start Another activity

In Android, there are 2 types of Intent.

  • Implicit Intent
  • Explicit Intent

Some use case of Explicit Intent is :

  1. Start the service
  2. Launch a new activity
  3. Broadcast a message

Android Intent and Types of the Intent tutorial you can read about all basic of about

Let’s Build an App for Explicit intent in kotlin :

In this tutorial, you’ll add Button and EditText widget code to that MainActivity starts a new activity to display the message when the user taps sendMessage.

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. Create a new Activity ” SecondActivity.kt ” class

Choose an option auto to create resource file or create my own. Add the following code in in “activity_second.xml” res layout file.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/ed_name"
        android:layout_width="359dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Enter name "
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:text="Send Data"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ed_name"
        android:onClick="sendMessage"
        tools:ignore="OnClick" />
</android.support.constraint.ConstraintLayout>

Now add code in SecondActivity.class and set resource layout file.

package `in`.eyehunt.explicitintentkotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_second.*

class SecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val intent = intent
        val strName= intent.getStringExtra("name")
        tv_name.text = strName
    }
}
Step 3. Add Buttons and EditText widget in an activity_main.xml resource file

EditText for taking user input as a string and button will perform an action to pass data and start a new activity.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/ed_name"
        android:layout_width="359dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Enter name "
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:text="Send Data"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ed_name"
        android:onClick="sendMessage"
        tools:ignore="OnClick" />
</android.support.constraint.ConstraintLayout>
Step 3. Add the following code in MainActivity.class

In this code using the button, it’s very easy just pass the intent with putExtra() to the target component.

package `in`.eyehunt.explicitintentkotlin

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    fun sendMessage(view: View){
        val intent = Intent(this@MainActivity,SecondActivity::class.java).apply {
            putExtra("name",ed_name.text.toString())
        }
        startActivity(intent)
    }
}
Step 4. Run the application, in emulator or On you android device

Output screenshot Android Explicit Intent example :

Video Output

Download source code Android Explicit Intent example in kotlin

https://github.com/EyeHunts/ExplicitIntentKotlin

Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 26: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin


Bonus : 

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading