How to integrate Intents with other Android components?

In Android development, an Intent is a messaging object you can use to request an action from another app component. Intents are primarily used to start new activities, start services, and deliver broadcasts. This capability allows different components of an application or even different applications to communicate with one another.

There are mainly two types of Intents: explicit intents and implicit intents. Explicit intents specify the component to start by name, whereas implicit intents do not name a specific component and instead declare a general action to perform.

Integrating Intents with Other Components

To illustrate the integration of Intents, let's see how to launch an activity with an explicit intent and send data between activities using putExtra() and retrieve it using getIntent().

Here is an example of how to integrate intents in an Android application:

// MainActivity.java Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("EXTRA_MESSAGE", "Hello, Second Activity!"); startActivity(intent); // SecondActivity.java Intent intent = getIntent(); String message = intent.getStringExtra("EXTRA_MESSAGE"); TextView textView = findViewById(R.id.textView); textView.setText(message);

In this example, we create an Intent in MainActivity to start SecondActivity. We pass a string extra data using the putExtra() method. In SecondActivity, we retrieve the data using getStringExtra() and display it in a TextView.


android intents explicit intents implicit intents start activity sending data Android components