Intent And Its Types In Android

Introduction

Intent is used to call an internal or external component from an activity to another activity of another app. 

Types of intent

  • Explicit Intent
  • Implicit Intent

Now let's understand them one by one.

Explicit Intent

Explicit Intent is used to call by name. It is simply an app or activity that calls another activity from that activity and because we know the name we can call it by name.

                                    

 

Steps of Explicit Intent

  1. Initialization of Button on java file
  2. set Id(R.id.ID_NAME), & onClicklistener on java file
  3. Onclick listener action on java file
  4. Intent Initialization
  5. Start Activity by passing initialized object on Intent

Example of Explicit Intent

Here we create an app that will open another activity on the same app and we will move to the next window of the app by this. We will create two activities under this app and code the same in both files except the button id we assign.

Below we have defined the steps that I have added to the file. You can refer full code snippet at the end of the article

Step 1

On java file under on Main class

Button button;

Step  2

On java file under onCreate

button = findViewById(R.id.button);
button.setOnClickListener(this);

Step 3,4 & 5

On java file under onClick(View view)

@Override
public void onClick(View view) {
    Intent call1 = new Intent(this,MainActivity.class);
    startActivity(call1);
}

Full Code Snippet

activityMain.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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is activity 1"
        android:textSize="34sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.377"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.255" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.447"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.196" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.explicit_intent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
       Intent call1 = new Intent(this,MainActivity2.class);
       startActivity(call1);
    }
}

Output

Implicit Intent

It is also called as call android component (otherapp/activity) without name.

When a component needs to be called which has an unknown name then you can call it using its URI address and OS will generate an intent to call this component internally. This intent is known as implicit Intent. In the below diagram we have an Activity A where we are calling an implicit intent by starting the activity by calling URI of the application This will start the internal activity of intent and intent will go to OS for the access to the App or component then if OS check the permission is given to the app is true then it proceeds further and again on the other or acessing app it will have an intent which we call onCreate method and starts the Activity B.

hey

Steps of Implicit Intent

  1. Initialization of Button on java file
  2. set Id(R.id.ID_NAME), & onClicklistener on java file
  3. Onclick listener action on java file
  4. Under onClick listener perform app permission and Intent initialization
  5. Start Activity by passing initialized object on Intent

Example of Implicit Intent

Here we create a calling app where it will open the call app and call the person whom we will define

Below we have defined the steps that I have added to the file. You can refer full code snippet at the end of the article

Step 1

On java file under on Main class

Button button;

Step  2

On java file under onCreate

button = findViewById(R.id.button);
button.setOnClickListener(this);

Step 3,4 & 5

On java file under onClick(View view)

@Override
public void onClick(View view) {
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != getPackageManager().PERMISSION_GRANTED)
  {
      String[] permissions = new String[1];
      permissions[0] = Manifest.permission.CALL_PHONE;
      requestPermissions(permissions,111);
  }
  else {
      Intent calling = new Intent(Intent.ACTION_CALL,Uri.parse("tel:8076363682"));
      startActivity(calling);
  }
}

Full Code Snippet

activityMain.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:layout_marginBottom="210dp"
        android:text="call to ravi"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2"
        app:layout_constraintVertical_bias="0.692" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="252dp"
        android:layout_height="294dp"
        android:layout_marginTop="205dp"
        android:layout_marginBottom="73dp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:drawable/ic_menu_call" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.callingapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.Activity;
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 implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != getPackageManager().PERMISSION_GRANTED)
      {
          String[] permissions = new String[1];
          permissions[0] = Manifest.permission.CALL_PHONE;
          requestPermissions(permissions,111);
      }
      else {
          Intent calling = new Intent(Intent.ACTION_CALL,Uri.parse("tel:8076363682"));
          startActivity(calling);
      }
    }
}

Output

Conclusion

Here we have discussed what is intent why it is used and what are the types of intent. Examples of implicit and explicit intent are also covered.


Similar Articles