Android Intent: What is intent in android, Android Intent Example

Android Intent: What is intent in android, Android Intent Example


What is Intent in Android or What is Android Intent

Android Intent is a messaging object that facilitates communication between app components in several ways. Android Intent is an object to pass information among activities or one app to another.

Three Fundamental use cases of intent in android are:

  • Starting an Activity
  • Starting a Service
  • Delivering broadcasts

Intents are used to give a signal to the android operating system when a certain event has occurred. Android’s intent is to perform an action on the screen.

Types of Intent in android or Android Intent types

There are two types of Android Intents.

  1. Implicit Intent in android
  2. Explicit Intent in android
android intent types, types of intent in android

What is Implicit Intent in Android

Implicit Intent doesn’t specify the component name but instead declares a general type action like you want to share content or copy the content from one app to another. For Example, if you want to save a number from your app, you can use implicit intent to request another capable app like your phone call app to save a number.

Steps to create an Intent in android

  • First, we import the Intent class following with intent name by which we call it. Then we initialize it using the new Intent keyword.
  • Then we will pass the parameter which action we want to do like action view etc. (If it is an implicit intent). For explicit intent, we pass the specific component name or package name.
  • After that, we will set the intent for that specific purpose we want to do.
  • To call the intent we use the startActivity() keyword, following with the argument of the intent name that we want to start or initialize.

Let’s understand it more precisely:

Click on the image to enlarge it.

You may write the following code to view a webpage:

Intent intent = new Intent(Intent.ACTION_VIEW);  
intent.setData(Uri.parse("https://startechcity29.com"));  
startActivity(intent);  

Now let’s see a practical example of implicit intent in android.

In this example, no component is specified, instead, an action is performed (ACTION_VIEW) i.e. a webpage is going to be opened. As you click on the ‘Click to Load webview’ button, the given name of the desired webpage is opened. 

actvitiy_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to load webview"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
Button button;

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

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://startechcity29.com"));
                startActivity(intent);
            }
        });
    }
}

Result/Output

android intent, implicit intent
android intent, implicit intent

What is Explicit Intent in Android

Explicit Intent does specify the component name on which application satisfies the intent by supplying the target app’s package name or a fully-qualified component class name. For Example, you want to start a new activity within your app or to start a service to download a file in the background.

You may write the following code to change activity within the app.

Intent changeActivity = new Intent(MainActivity.this, MainActivity2.class);
startActivity(changeActivity);

In the above example, we imported the Intent class following the intent name and assignment operator then initialized it with the new Keyword and passed the argument as the components name from one activity to another. Then started the intent with the startActivity() keyword.

Practical Example of Explicit intent in android click here


When to use implicit intent and explicit intent

It is a little bit confusing when to use those, but there is a minute difference and that is, when you want to perform the action within the same app then use explicit intent like changing from one activity to another or to fragments. If you want the action from your app to another then use implicit intent like sharing data, copying data, or opening the browser, etc. I know it’s still confusing but don’t worry as you work with them you will know automatically when to use which one.

Youtube Tutorial on android in Hindi click here

Android Intent basically acts as a messenger.

Guys, please do support us, by sharing it with others too, so that they can also understand it in simple and easy words.

Leave a Comment