![]() |
VOOZH | about |
Pre-requisites:
This article aims to tell about the Implicit and Explicit intents and how to use them in an android app.
The intent is a 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:
| Methods | Description |
|---|---|
| Context.startActivity() | This is to launch a new activity or get an existing activity to be action. |
| Context.startService() | This is to start a new service or deliver instructions for an existing service. |
| Context.sendBroadcast() | This is to deliver the message to broadcast receivers. |
Using implicit Intent, components canβt be specified. An action to be performed is declared by implicit intent. Then android operating system will filter out components that will respond to the action. For Example,
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 the 'CLICK' button. Your webpage is opened.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio . The code for that has been given in both Java and Kotlin Programming Language for Android. Create an XML file and Java File. Please refer to the pre-requisites to learn more about this step.
Next, go to the activity_main.xml file , which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
android:id="@+id/id_name"Now, we will create the Backend of the App. For this, Open the MainActivity file and instantiate the component (Button) created in the XML file using the findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.
ComponentType object = (ComponentType) findViewById(R.id.IdOfTheComponent);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:
In the above example, There are two activities (FirstActivity, and SecondActivity). When you click on the 'GO TO OTHER ACTIVITY' button in the first activity, then you move to the second activity. When you click on the 'GO TO HOME ACTIVITY' button in the second activity, then you move to the first activity. This is getting done through Explicit Intent.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio . The code for that has been given in both Java and Kotlin Programming Language for Android. Please refer to the pre-requisites to learn more about this step.
Next, go to the activity_main.xml file , which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
android:id="@+id/id_name"Now, we will create the Backend of the App. For this, Open the MainActivity file and instantiate the component (Button, TextView) created in the XML file using the findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.
ComponentType object = (ComponentType) findViewById(R.id.IdOfTheComponent);
Intent i = new Intent(getApplicationContext(), <className>);
startActivity(i);
Now we have to create a second activity as a destination activity. The steps to create the second activity are File > new > Activity > Empty Activity.
Next, go to the activity_main2.xml file , which represents the UI of the project. Below is the code for the activity_main2.xml file. Comments are added inside the code to understand the code in more detail.
Now, we will create the Backend of the App. For this, Open the MainActivity file and instantiate the component (Button, TextView) created in the XML file using the findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.
ComponentType object = (ComponentType) findViewById(R.id.IdOfTheComponent);
Intent i = new Intent(getApplicationContext(), <className>);
startActivity(i);