Android | Implicit and Explicit Intents with Examples - GeeksforGeeks

06 May, 2019
Pre-requisites:
Android App Development Fundamentals for Beginners
Guide to Install and Set up Android Studio
Android | Starting with first app/android project
Android | Running your first Android app
This article aims to tell about the Implicit and Explicit intents and how to use them in an android app.
Intent is an messaging object which passes between components like services, content providers, activities etc. Normally startActivity() method is used for invoking any activity.
Some of the general functions of intent are:
Start serviceLaunch activityDisplay web pageDisplay contact listMessage broadcasting
Start service
Launch activity
Display web page
Display contact list
Message broadcasting
Methods and its Description
Intent ClassificationThere are two types of intents
1. Implicit Intent
2. Explicit Intent

Implicit Intent: Using implicit Intent, component can’t be specifying. An action to be performed is declared by implicit intent. Then android operating system will filter out component which will response to the action.For ExampleIn the above example, no component is specified, instead an action is performed i.e. a webpage is going to be opened. As you type the name of your desired webpage and click on ‘CLICK’ button. Your webpage is opened.How to create an Android App to open a webpage using implicit intent(with Example)Step 1: Create XML file and Java File. Please refer the pre-requisites to learn more about this step.Step 2: Open “activity_main.xml” file and add following widgets in a Constraint Layout.1. An EditText to input text.2. A Button to open webpage.Also, Assign IDs for each component as shown in the image and the code below. The assigned ID for a component helps in the identification of component and can be easily use in the Java files.Syntax:android:id="@+id/id_name"Here the given IDs: editText1, button1This will make the UI of the Application.Step 3: Now, after the UI, this step will create the Backend of the App. For this, Open “MainActivity.java” file and instantiate the component (Button) created in the XML file using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.General Syntax:ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);Syntax for used components (EditText, Button):EditText editText = (EditText)findViewById(R.id.editText1);Button button = (Button)findViewById(R.id.button1);Step 4: This step involves setting up the operations to display the Toast Message. These operations are as follows:a). First Add the listener on button and this button will open webpage.button.setOnClickListener(new View.OnClickListener() {});b). Create String type variable to store the value of ‘EditText’. Value is accepted and converted to string.String url = editText1.getText().toString();c). Create an Intent object MainActivity.java class to open the webpage.Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));d). The startActivity() method starts to call a webpage for opening specified by the intent.startActivity(intent);Therefore the code snippet for Implicit Intent:String url = editText1.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
Complete code of implicit Intentactivity_main.xmlMainActivity.javaactivity_main.xml<?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">  <!-- add an edittext to input text -->    <EditText        android:id="@+id/editText1"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="136dp"        android:ems="10"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />  <!-- add a button for click -->    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/editText1"        android:layout_centerHorizontal="true"        android:layout_marginTop="49dp"        android:layout_marginEnd="132dp"        android:text="Click"        app:layout_constraintEnd_toEndOf="@+id/editText1"        app:layout_constraintTop_toBottomOf="@+id/editText1" />  </android.support.constraint.ConstraintLayout>MainActivity.javapackage org.geeksforgeeks.implicitIntent_example;  import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;  public class MainActivity extends Activity {      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // Bind the components to their respective objects        // by assigning their IDs        // with the help of findViewById() method        final EditText editText1 = (EditText)findViewById(R.id.editText1);        Button button = (Button)findViewById(R.id.button1);          // implementation of onClick event for Implicit Intent        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v)            {                  // performing webpage open action                String url = editText1.getText().toString();                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));                startActivity(intent);            }        });    }}Output:Explicit Intent: Using explicit intent any other component can be specified. In other words, the targeted component is specified by explicit intent. So only the specified target component will be invoked.For Example:Explicit Intent ExampleIn the above example, There are two activities (FirstActivity, SecondActivity). When you click on ‘GO TO OTHER ACTIVITY’ button in the first activity, then you move to second activity. When you click on ‘GO TO HOME ACTIVITY’ button in the second activity, then you move to the first activity. This is getting done through Explicit Intent.How to create an Android App to move to next activity using Explicit Intent(with Example)Step 1: Create XML file and Java File. Please refer the pre-requisites to learn more about this step.Step 2: Open “activity_main.xml” file and add following widgets in a Constraint Layout.1. A Button for moving to second activity.2. A TextView for writing some text.Also, Assign IDs for each component (Button, TextView) as shown in the image and the code below. The assigned ID for a component helps in the identification of component and can be easily use in the Java files.Syntax:android:id="@+id/id_name"Here the given IDs: Button01, TextView01.This will make the UI of the Application.Step 3: Now, after the UI, this step will create the Backend of the App. For this, Open “MainActivity.java” file and instantiate the component (Button, TextView) created in the XML file using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.General Syntax:ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);Syntax for used components (Button, TextView):1. Button button = (Button)findViewById(R.id.Button01);2. TextView textView = (TextView)findViewById(R.id.TextView01);Step 4: This step involves setting up the operations to create explicit intent. These operations are as follows:a. First Add the listener on button and using this button you will move to other activity.button1.setOnClickListener(new View.OnClickListener() {});b. Now, Create an Intent.Intent i = new Intent(getApplicationContext(), SecondActivityName.class);Parameters: This is having two parameters:getApplicationContext(): Returns the context for the entire application.OtherActivityName.class: You should use your activity name here.Therefore the code to make an explicit intent is:Intent i = new Intent(getApplicationContext(), ActivityTwo.class);c. Now Start the Targeted Activity.startActivity(i):This starts target activity.The code snippet for explicit intent:Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);Step 5: Now we have to create a second activity as a destination activity.The steps to create the second activity is as follows:android project > File > new > Activity > Empty ActivityStep 6: Now open your second xml file.Add Button and TextView for moving back to home activity and to write some text on activity respectively. Assign ID to Button and Textview. Second Activity is shown below:Step 7: Now, open up the your second activity java file and perform the following operation.a. First Add the listener on button and using this button you will move to home activity.button1.setOnClickListener(new View.OnClickListener() {});b. Now, Create an Intent.Intent i = new Intent(getApplicationContext(), OtherActivityName.class);Therefore the code to make an explicit intent is:Intent i = new Intent(getApplicationContext(), MainActivity.class);
        c. Now Start the Targeted Activity.startActivity(i):This starts target activity.The code to show the Toast message:Intent i = new Intent(getApplicationContext(), MainActivity.class);startActivity(i);Step 8: Now Run the app and operate as follows:When the app is opened, it displays a “GO TO OTHER ACTIVITY” button.Click the “GO TO OTHER ACTIVITY” button.Then you will move to second activity.Also, you will get a “GO TO HOME ACTIVITY” button on the second activity, When you will click this, you will move to home activity.Complete code of Explicit Intentactivity_main.xmlMainActivity.javaactivity_two.xmlActivityTwo.javaactivity_main.xml<?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="org.geeksforgeeks.explicit_intent.MainActivity">   <!-- add a button to move to next activity -->    <Button        android:id="@+id/Button01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/TextView01"        android:layout_marginTop="209dp"        android:onClick="onClick"        android:text="Go To Other Activity"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        tools:ignore="OnClick" />   <!-- add a TextView to write some text on activity -->    <TextView        android:id="@+id/TextView01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/Button01"        android:layout_alignParentTop="true"        android:layout_marginTop="44dp"        android:minHeight="60dip"        android:text="This is First Activity"        android:textSize="20sp"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />  </android.support.constraint.ConstraintLayout>MainActivity.javapackage org.geeksforgeeks.explicit_intent;  import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;  public class MainActivity extends Activity {    // Defining the object for button    Button button1;    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // Bind the components to their respective objects        // by assigning their IDs        // with the help of findViewById() method        button1 = (Button)findViewById(R.id.Button01);          button1.setOnClickListener(new OnClickListener() {            public void onClick(View view)            {                  // Creating explicit intent                Intent i = new Intent(getApplicationContext(),                                      ActivityTwo.class);                startActivity(i);            }        });    }}activity_two.xml<?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="org.geeksforgeeks.explicit_intent.ActivityTwo">  <!-- add a button for moving to home activity -->    <Button        android:id="@+id/Button01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/TextView01"        android:layout_alignParentLeft="true"        android:layout_marginBottom="220dp"        android:onClick="onClick"        android:text="Go to Home Activity"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.533"        app:layout_constraintStart_toStartOf="parent"        tools:ignore="OnClick" />   <!-- add a TextView to write some text on activity -->    <TextView        android:id="@+id/TextView01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/Button01"        android:layout_alignParentTop="true"        android:layout_margin="71dp"        android:layout_marginTop="144dp"        android:layout_marginEnd="88dp"        android:layout_marginRight="88dp"        android:minHeight="60dip"        android:text="This is Second Activity"        android:textSize="20sp"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintTop_toTopOf="parent" />  </android.support.constraint.ConstraintLayout>ActivityTwo.javapackage org.geeksforgeeks.explicit_intent;  import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;  public class ActivityTwo extends Activity {      // Defining the object for button    Button button1;    @Override    public void onCreate(Bundle bundle)    {        super.onCreate(bundle);        setContentView(R.layout.activity_two);          // Bind the components to their respective objects        // by assigning their IDs        // with the help of findViewById() method        button1 = (Button)findViewById(R.id.Button01);          button1.setOnClickListener(new OnClickListener() {            public void onClick(View view)            {                  // Creating explicit intent                Intent i = new Intent(getApplicationContext(),                                      MainActivity.class);                startActivity(i);            }        });    }}Output:
Implicit Intent: Using implicit Intent, component can’t be specifying. An action to be performed is declared by implicit intent. Then android operating system will filter out component which will response to the action.For ExampleIn the above example, no component is specified, instead an action is performed i.e. a webpage is going to be opened. As you type the name of your desired webpage and click on ‘CLICK’ button. Your webpage is opened.How to create an Android App to open a webpage using implicit intent(with Example)Step 1: Create XML file and Java File. Please refer the pre-requisites to learn more about this step.Step 2: Open “activity_main.xml” file and add following widgets in a Constraint Layout.1. An EditText to input text.2. A Button to open webpage.Also, Assign IDs for each component as shown in the image and the code below. The assigned ID for a component helps in the identification of component and can be easily use in the Java files.Syntax:android:id="@+id/id_name"Here the given IDs: editText1, button1This will make the UI of the Application.Step 3: Now, after the UI, this step will create the Backend of the App. For this, Open “MainActivity.java” file and instantiate the component (Button) created in the XML file using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.General Syntax:ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);Syntax for used components (EditText, Button):EditText editText = (EditText)findViewById(R.id.editText1);Button button = (Button)findViewById(R.id.button1);Step 4: This step involves setting up the operations to display the Toast Message. These operations are as follows:a). First Add the listener on button and this button will open webpage.button.setOnClickListener(new View.OnClickListener() {});b). Create String type variable to store the value of ‘EditText’. Value is accepted and converted to string.String url = editText1.getText().toString();c). Create an Intent object MainActivity.java class to open the webpage.Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));d). The startActivity() method starts to call a webpage for opening specified by the intent.startActivity(intent);Therefore the code snippet for Implicit Intent:String url = editText1.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
Complete code of implicit Intentactivity_main.xmlMainActivity.javaactivity_main.xml<?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">  <!-- add an edittext to input text -->    <EditText        android:id="@+id/editText1"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="136dp"        android:ems="10"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />  <!-- add a button for click -->    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/editText1"        android:layout_centerHorizontal="true"        android:layout_marginTop="49dp"        android:layout_marginEnd="132dp"        android:text="Click"        app:layout_constraintEnd_toEndOf="@+id/editText1"        app:layout_constraintTop_toBottomOf="@+id/editText1" />  </android.support.constraint.ConstraintLayout>MainActivity.javapackage org.geeksforgeeks.implicitIntent_example;  import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;  public class MainActivity extends Activity {      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // Bind the components to their respective objects        // by assigning their IDs        // with the help of findViewById() method        final EditText editText1 = (EditText)findViewById(R.id.editText1);        Button button = (Button)findViewById(R.id.button1);          // implementation of onClick event for Implicit Intent        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v)            {                  // performing webpage open action                String url = editText1.getText().toString();                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));                startActivity(intent);            }        });    }}Output:
In the above example, no component is specified, instead an action is performed i.e. a webpage is going to be opened. As you type the name of your desired webpage and click on ‘CLICK’ button. Your webpage is opened.
Step 1: Create XML file and Java File. Please refer the pre-requisites to learn more about this step.
Step 2: Open “activity_main.xml” file and add following widgets in a Constraint Layout.1. An EditText to input text.2. A Button to open webpage.Also, Assign IDs for each component as shown in the image and the code below. The assigned ID for a component helps in the identification of component and can be easily use in the Java files.Syntax:android:id="@+id/id_name"Here the given IDs: editText1, button1This will make the UI of the Application.
android:id="@+id/id_name"
Here the given IDs: editText1, button1This will make the UI of the Application.
Step 3: Now, after the UI, this step will create the Backend of the App. For this, Open “MainActivity.java” file and instantiate the component (Button) created in the XML file using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.General Syntax:ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);Syntax for used components (EditText, Button):EditText editText = (EditText)findViewById(R.id.editText1);Button button = (Button)findViewById(R.id.button1);
ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);
Syntax for used components (EditText, Button):
EditText editText = (EditText)findViewById(R.id.editText1);Button button = (Button)findViewById(R.id.button1);
Step 4: This step involves setting up the operations to display the Toast Message. These operations are as follows:a). First Add the listener on button and this button will open webpage.button.setOnClickListener(new View.OnClickListener() {});b). Create String type variable to store the value of ‘EditText’. Value is accepted and converted to string.String url = editText1.getText().toString();c). Create an Intent object MainActivity.java class to open the webpage.Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));d). The startActivity() method starts to call a webpage for opening specified by the intent.startActivity(intent);
a). First Add the listener on button and this button will open webpage.
button.setOnClickListener(new View.OnClickListener() {});
b). Create String type variable to store the value of ‘EditText’. Value is accepted and converted to string.
String url = editText1.getText().toString();
c). Create an Intent object MainActivity.java class to open the webpage.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
d). The startActivity() method starts to call a webpage for opening specified by the intent.
startActivity(intent);
Therefore the code snippet for Implicit Intent:
String url = editText1.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

Complete code of implicit Intent
activity_main.xml
MainActivity.java
<?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">  <!-- add an edittext to input text -->    <EditText        android:id="@+id/editText1"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="136dp"        android:ems="10"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />  <!-- add a button for click -->    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/editText1"        android:layout_centerHorizontal="true"        android:layout_marginTop="49dp"        android:layout_marginEnd="132dp"        android:text="Click"        app:layout_constraintEnd_toEndOf="@+id/editText1"        app:layout_constraintTop_toBottomOf="@+id/editText1" />  </android.support.constraint.ConstraintLayout>
package org.geeksforgeeks.implicitIntent_example;  import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;  public class MainActivity extends Activity {      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // Bind the components to their respective objects        // by assigning their IDs        // with the help of findViewById() method        final EditText editText1 = (EditText)findViewById(R.id.editText1);        Button button = (Button)findViewById(R.id.button1);          // implementation of onClick event for Implicit Intent        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v)            {                  // performing webpage open action                String url = editText1.getText().toString();                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));                startActivity(intent);            }        });    }}
Output:
Explicit Intent: Using explicit intent any other component can be specified. In other words, the targeted component is specified by explicit intent. So only the specified target component will be invoked.For Example:Explicit Intent ExampleIn the above example, There are two activities (FirstActivity, SecondActivity). When you click on ‘GO TO OTHER ACTIVITY’ button in the first activity, then you move to second activity. When you click on ‘GO TO HOME ACTIVITY’ button in the second activity, then you move to the first activity. This is getting done through Explicit Intent.How to create an Android App to move to next activity using Explicit Intent(with Example)Step 1: Create XML file and Java File. Please refer the pre-requisites to learn more about this step.Step 2: Open “activity_main.xml” file and add following widgets in a Constraint Layout.1. A Button for moving to second activity.2. A TextView for writing some text.Also, Assign IDs for each component (Button, TextView) as shown in the image and the code below. The assigned ID for a component helps in the identification of component and can be easily use in the Java files.Syntax:android:id="@+id/id_name"Here the given IDs: Button01, TextView01.This will make the UI of the Application.Step 3: Now, after the UI, this step will create the Backend of the App. For this, Open “MainActivity.java” file and instantiate the component (Button, TextView) created in the XML file using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.General Syntax:ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);Syntax for used components (Button, TextView):1. Button button = (Button)findViewById(R.id.Button01);2. TextView textView = (TextView)findViewById(R.id.TextView01);Step 4: This step involves setting up the operations to create explicit intent. These operations are as follows:a. First Add the listener on button and using this button you will move to other activity.button1.setOnClickListener(new View.OnClickListener() {});b. Now, Create an Intent.Intent i = new Intent(getApplicationContext(), SecondActivityName.class);Parameters: This is having two parameters:getApplicationContext(): Returns the context for the entire application.OtherActivityName.class: You should use your activity name here.Therefore the code to make an explicit intent is:Intent i = new Intent(getApplicationContext(), ActivityTwo.class);c. Now Start the Targeted Activity.startActivity(i):This starts target activity.The code snippet for explicit intent:Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);Step 5: Now we have to create a second activity as a destination activity.The steps to create the second activity is as follows:android project > File > new > Activity > Empty ActivityStep 6: Now open your second xml file.Add Button and TextView for moving back to home activity and to write some text on activity respectively. Assign ID to Button and Textview. Second Activity is shown below:Step 7: Now, open up the your second activity java file and perform the following operation.a. First Add the listener on button and using this button you will move to home activity.button1.setOnClickListener(new View.OnClickListener() {});b. Now, Create an Intent.Intent i = new Intent(getApplicationContext(), OtherActivityName.class);Therefore the code to make an explicit intent is:Intent i = new Intent(getApplicationContext(), MainActivity.class);
        c. Now Start the Targeted Activity.startActivity(i):This starts target activity.The code to show the Toast message:Intent i = new Intent(getApplicationContext(), MainActivity.class);startActivity(i);Step 8: Now Run the app and operate as follows:When the app is opened, it displays a “GO TO OTHER ACTIVITY” button.Click the “GO TO OTHER ACTIVITY” button.Then you will move to second activity.Also, you will get a “GO TO HOME ACTIVITY” button on the second activity, When you will click this, you will move to home activity.Complete code of Explicit Intentactivity_main.xmlMainActivity.javaactivity_two.xmlActivityTwo.javaactivity_main.xml<?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="org.geeksforgeeks.explicit_intent.MainActivity">   <!-- add a button to move to next activity -->    <Button        android:id="@+id/Button01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/TextView01"        android:layout_marginTop="209dp"        android:onClick="onClick"        android:text="Go To Other Activity"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        tools:ignore="OnClick" />   <!-- add a TextView to write some text on activity -->    <TextView        android:id="@+id/TextView01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/Button01"        android:layout_alignParentTop="true"        android:layout_marginTop="44dp"        android:minHeight="60dip"        android:text="This is First Activity"        android:textSize="20sp"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />  </android.support.constraint.ConstraintLayout>MainActivity.javapackage org.geeksforgeeks.explicit_intent;  import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;  public class MainActivity extends Activity {    // Defining the object for button    Button button1;    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // Bind the components to their respective objects        // by assigning their IDs        // with the help of findViewById() method        button1 = (Button)findViewById(R.id.Button01);          button1.setOnClickListener(new OnClickListener() {            public void onClick(View view)            {                  // Creating explicit intent                Intent i = new Intent(getApplicationContext(),                                      ActivityTwo.class);                startActivity(i);            }        });    }}activity_two.xml<?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="org.geeksforgeeks.explicit_intent.ActivityTwo">  <!-- add a button for moving to home activity -->    <Button        android:id="@+id/Button01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/TextView01"        android:layout_alignParentLeft="true"        android:layout_marginBottom="220dp"        android:onClick="onClick"        android:text="Go to Home Activity"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.533"        app:layout_constraintStart_toStartOf="parent"        tools:ignore="OnClick" />   <!-- add a TextView to write some text on activity -->    <TextView        android:id="@+id/TextView01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/Button01"        android:layout_alignParentTop="true"        android:layout_margin="71dp"        android:layout_marginTop="144dp"        android:layout_marginEnd="88dp"        android:layout_marginRight="88dp"        android:minHeight="60dip"        android:text="This is Second Activity"        android:textSize="20sp"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintTop_toTopOf="parent" />  </android.support.constraint.ConstraintLayout>ActivityTwo.javapackage org.geeksforgeeks.explicit_intent;  import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;  public class ActivityTwo extends Activity {      // Defining the object for button    Button button1;    @Override    public void onCreate(Bundle bundle)    {        super.onCreate(bundle);        setContentView(R.layout.activity_two);          // Bind the components to their respective objects        // by assigning their IDs        // with the help of findViewById() method        button1 = (Button)findViewById(R.id.Button01);          button1.setOnClickListener(new OnClickListener() {            public void onClick(View view)            {                  // Creating explicit intent                Intent i = new Intent(getApplicationContext(),                                      MainActivity.class);                startActivity(i);            }        });    }}Output:
For Example:
Explicit Intent Example
In the above example, There are two activities (FirstActivity, SecondActivity). When you click on ‘GO TO OTHER ACTIVITY’ button in the first activity, then you move to second activity. When you click on ‘GO TO HOME ACTIVITY’ button in the second activity, then you move to the first activity. This is getting done through Explicit Intent.
Step 1: Create XML file and Java File. Please refer the pre-requisites to learn more about this step.
Step 2: Open “activity_main.xml” file and add following widgets in a Constraint Layout.1. A Button for moving to second activity.2. A TextView for writing some text.Also, Assign IDs for each component (Button, TextView) as shown in the image and the code below. The assigned ID for a component helps in the identification of component and can be easily use in the Java files.Syntax:android:id="@+id/id_name"Here the given IDs: Button01, TextView01.This will make the UI of the Application.
android:id="@+id/id_name"
Here the given IDs: Button01, TextView01.This will make the UI of the Application.
Step 3: Now, after the UI, this step will create the Backend of the App. For this, Open “MainActivity.java” file and instantiate the component (Button, TextView) created in the XML file using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.General Syntax:ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);Syntax for used components (Button, TextView):1. Button button = (Button)findViewById(R.id.Button01);2. TextView textView = (TextView)findViewById(R.id.TextView01);
General Syntax:
ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);
Syntax for used components (Button, TextView):
1. Button button = (Button)findViewById(R.id.Button01);2. TextView textView = (TextView)findViewById(R.id.TextView01);
Step 4: This step involves setting up the operations to create explicit intent. These operations are as follows:a. First Add the listener on button and using this button you will move to other activity.button1.setOnClickListener(new View.OnClickListener() {});b. Now, Create an Intent.Intent i = new Intent(getApplicationContext(), SecondActivityName.class);Parameters: This is having two parameters:getApplicationContext(): Returns the context for the entire application.OtherActivityName.class: You should use your activity name here.Therefore the code to make an explicit intent is:Intent i = new Intent(getApplicationContext(), ActivityTwo.class);c. Now Start the Targeted Activity.startActivity(i):This starts target activity.The code snippet for explicit intent:Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
a. First Add the listener on button and using this button you will move to other activity.
button1.setOnClickListener(new View.OnClickListener() {});
b. Now, Create an Intent.
Intent i = new Intent(getApplicationContext(), SecondActivityName.class);
Parameters: This is having two parameters:
getApplicationContext(): Returns the context for the entire application.
OtherActivityName.class: You should use your activity name here.
Therefore the code to make an explicit intent is:
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
c. Now Start the Targeted Activity.startActivity(i):This starts target activity.The code snippet for explicit intent:
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
Step 5: Now we have to create a second activity as a destination activity.The steps to create the second activity is as follows:android project > File > new > Activity > Empty Activity
android project > File > new > Activity > Empty Activity
Step 6: Now open your second xml file.Add Button and TextView for moving back to home activity and to write some text on activity respectively. Assign ID to Button and Textview. Second Activity is shown below:
Step 7: Now, open up the your second activity java file and perform the following operation.a. First Add the listener on button and using this button you will move to home activity.button1.setOnClickListener(new View.OnClickListener() {});b. Now, Create an Intent.Intent i = new Intent(getApplicationContext(), OtherActivityName.class);Therefore the code to make an explicit intent is:Intent i = new Intent(getApplicationContext(), MainActivity.class);
        c. Now Start the Targeted Activity.startActivity(i):This starts target activity.The code to show the Toast message:Intent i = new Intent(getApplicationContext(), MainActivity.class);startActivity(i);
button1.setOnClickListener(new View.OnClickListener() {});
b. Now, Create an Intent.
Intent i = new Intent(getApplicationContext(), OtherActivityName.class);
Therefore the code to make an explicit intent is:
Intent i = new Intent(getApplicationContext(), MainActivity.class);
        
c. Now Start the Targeted Activity.
startActivity(i):This starts target activity.
The code to show the Toast message:
Intent i = new Intent(getApplicationContext(), MainActivity.class);startActivity(i);
Step 8: Now Run the app and operate as follows:When the app is opened, it displays a “GO TO OTHER ACTIVITY” button.Click the “GO TO OTHER ACTIVITY” button.Then you will move to second activity.Also, you will get a “GO TO HOME ACTIVITY” button on the second activity, When you will click this, you will move to home activity.
activity_main.xml
MainActivity.java
activity_two.xml
ActivityTwo.java
<?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="org.geeksforgeeks.explicit_intent.MainActivity">   <!-- add a button to move to next activity -->    <Button        android:id="@+id/Button01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/TextView01"        android:layout_marginTop="209dp"        android:onClick="onClick"        android:text="Go To Other Activity"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        tools:ignore="OnClick" />   <!-- add a TextView to write some text on activity -->    <TextView        android:id="@+id/TextView01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/Button01"        android:layout_alignParentTop="true"        android:layout_marginTop="44dp"        android:minHeight="60dip"        android:text="This is First Activity"        android:textSize="20sp"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />  </android.support.constraint.ConstraintLayout>
package org.geeksforgeeks.explicit_intent;  import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;  public class MainActivity extends Activity {    // Defining the object for button    Button button1;    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // Bind the components to their respective objects        // by assigning their IDs        // with the help of findViewById() method        button1 = (Button)findViewById(R.id.Button01);          button1.setOnClickListener(new OnClickListener() {            public void onClick(View view)            {                  // Creating explicit intent                Intent i = new Intent(getApplicationContext(),                                      ActivityTwo.class);                startActivity(i);            }        });    }}
<?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="org.geeksforgeeks.explicit_intent.ActivityTwo">  <!-- add a button for moving to home activity -->    <Button        android:id="@+id/Button01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/TextView01"        android:layout_alignParentLeft="true"        android:layout_marginBottom="220dp"        android:onClick="onClick"        android:text="Go to Home Activity"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.533"        app:layout_constraintStart_toStartOf="parent"        tools:ignore="OnClick" />   <!-- add a TextView to write some text on activity -->    <TextView        android:id="@+id/TextView01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/Button01"        android:layout_alignParentTop="true"        android:layout_margin="71dp"        android:layout_marginTop="144dp"        android:layout_marginEnd="88dp"        android:layout_marginRight="88dp"        android:minHeight="60dip"        android:text="This is Second Activity"        android:textSize="20sp"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintTop_toTopOf="parent" />  </android.support.constraint.ConstraintLayout>
package org.geeksforgeeks.explicit_intent;  import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;  public class ActivityTwo extends Activity {      // Defining the object for button    Button button1;    @Override    public void onCreate(Bundle bundle)    {        super.onCreate(bundle);        setContentView(R.layout.activity_two);          // Bind the components to their respective objects        // by assigning their IDs        // with the help of findViewById() method        button1 = (Button)findViewById(R.id.Button01);          button1.setOnClickListener(new OnClickListener() {            public void onClick(View view)            {                  // Creating explicit intent                Intent i = new Intent(getApplicationContext(),                                      MainActivity.class);                startActivity(i);            }        });    }}
Output:
android
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multidimensional Arrays in Java