Android Intent
Android Intent is a messaging object in Android that allows components to request another component to perform a specific task. It provides a way to communicate between components of the same application, as well as different applications running on the Android platform. Android Intent enables the developer to launch a new activity, start a service, or deliver a broadcast message.
Types of Android Intent:
-
Implicit Intent: This type of intent does not specify the name of the target component. Instead, it declares a general action to be performed. The system searches for a suitable component to perform the action based on the intent filters declared in the application's manifest file. An example of implicit intent is opening a web page using a web browser application.
-
Explicit Intent: This type of intent specifies the target component by name. It is used to start a specific activity within the same application or a different application. Explicit intents are used when the developer knows which component to start. For example, starting a new activity on a button click.
Example of Android Intent:
To understand the Android Intent, let's take an example of opening a new activity using Intent.
Step 1: Create a new project in Android Studio and add two activities named MainActivity and SecondActivity.
Step 2: In the MainActivity, create a button and add an OnClickListener to it. Inside the OnClickListener, create an Intent object and set the target activity using the SecondActivity class name.
Button btnOpen = findViewById(R.id.btn_open);
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
Step 3: In the SecondActivity, create a TextView and set some text to it.
TextView tvMessage = findViewById(R.id.tv_message);
tvMessage.setText("Welcome to Second Activity!");
Step 4: Run the application on an Android device or emulator and click the button on the MainActivity. It should open the SecondActivity and display the text on the TextView.
Android Intent Lifecycle Methods:

-
onCreate() method: This method is called when the intent is created. It is used to initialize the variables and set up the layout.
-
onStart() method: This method is called when the intent is visible to the user. It is used to start animations or music playback.
-
onResume() method: This method is called when the intent is in the foreground. It is used to start the camera or microphone and register broadcast receivers.
-
onPause() method: This method is called when the intent loses focus. It is used to release the camera or microphone and unregister broadcast receivers.
-
onStop() method: This method is called when the intent is no longer visible. It is used to stop animations or music playback.
-
onDestroy() method: This method is called when the intent is destroyed. It is used to release the resources used by the intent.
In conclusion, Android Intent is an essential part of the Android platform that provides a way to communicate between different components of an application as well as different applications running on the platform. It allows developers to launch new activities, start services, or deliver broadcast messages. Android Intent can be used to create explicit or implicit intents. The activity lifecycle methods are used to manage the lifecycle of the intent and the resources used by it.