Skip to content

Android Implicit, Explicit Intent & Type of Intent with Examples

  • by

Implicit Intent and Explicit Intent is a type of Intent in Android. The intent is the main component of Android app development. The intent is the medium to pass between components such as activities, content providers, broadcast receivers, services, etc.

Some use case of Intent is :

  • Start the service
  • Launch an activity
  • Display a web page
  • Broadcast a message
  • Dial a phone call.
  • Map GEO location

Implicit Intent

Implicit Intent doesn’t specify the component in the app. In such a case, intent provides information on available components provided by the system that is to be invoked.

Implicit intent example: A button on click of which you will redirect to a web page. If your Device has multiple browsers then the options popup (bottom sheet) will open and show them all.

Explicit Intent

Explicit intent specifies the component in an application, that is which class to be invoked. You can pass the information from one activity to another using explicit intent.

Explicit intent example: On click button go to another Activity. As a definition, we know about the target component.

For much better understanding, you should read “Intent Filters in Android” and “What is Android Intent“.

Let’s Build an App for Implicit intent and Explicit intent

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. Create a new Activity ” SecondActivity.class ” for an Explication intent example

Add the following code 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=".SecondActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Explicit Example"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

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

package in.eyehunt.intenttypesimplicitandexplicit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}
Step 3. Add Buttons widget in an activity_main.xml resource file

One button for Implicit intent and another one for Explicit intent example.

<?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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Types of Intent - Implicit and Explicit Intent in Android "
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_im"
        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="20dp"
        android:text="Implicit Intent Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button_ex"
        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="40dp"
        android:text="Explicit Intent Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_im" />

</android.support.constraint.ConstraintLayout>
Step 3. Add the following code in MainActivity.class

In this code using 2 buttons, Where you can see for Explicit intent in btn_Explicit, its very easy just pass the intent to target component.

And Implicit intent in btn_Implicit used <action> and <data> ,

package in.eyehunt.intenttypesimplicitandexplicit;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Implicit intent example
        Button btn_Implicit = (Button)findViewById(R.id.button_im);
        btn_Implicit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = "http://tutorial.eyehunts.com";
                //action
                Intent i = new Intent(Intent.ACTION_VIEW);
                //data
                i.setData(Uri.parse(url));
                startActivity(i);

            }
        });

        //Explicit intent example
        Button btn_Explicit = (Button)findViewById(R.id.button_ex);
        btn_Explicit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}
Step 6. Run the application, in an emulator or On you android device

Output screenshot Android Implicit and Explicit Intent example :

Types of Intent - Implicit and Explicit Intent in Android example output

Video Output

Download source code Android Implicit and Explicit Intent

https://github.com/EyeHunts/IntentTypesImplicitAndExplicit

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 Java


Bonus : 

Leave a Reply

Discover more from Tutorial

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

Continue reading