BCA / B.Tech 9 min read

Linking Activities Using Intents

Linking Activities Using Intents

What is an Intent?

In Android, an Intent is a messaging object you can use to request an action from another app component. In simple terms, it is a message sent from one component (like an activity) to another.

Its most common use is to start one activity from another, but it can also be used to start services or send broadcasts.


Types of Intents

There are two types of intents:

1. Explicit Intent

  • What it is: An Explicit Intent is used when you know the exact component (activity) you want to start. You specify the component's class name directly.
  • When to use: It is mostly used for navigating from one screen to another within your own application.

Example in Java:

Let's say you want to navigate from `MainActivity` to `SecondActivity`.


// Inside MainActivity.java

// 1. Create a new Intent object
// The first parameter is the context (the current activity)
// The second parameter is the target activity's class
Intent intent = new Intent(MainActivity.this, SecondActivity.class);

// 2. Start the activity
startActivity(intent);

2. Implicit Intent

  • What it is: In an Implicit Intent, you do not specify the component's name. Instead, you declare a general action to be performed (like "view a map," "send an email," or "dial a number").
  • How it works: The Android system then checks all the apps installed on the device that can handle this action. If only one app is found, it starts directly. If multiple apps are found, a dialog is shown for the user to choose.
  • When to use: When you want to use the functionality of another app from your app.

Example in Java:

An Implicit Intent to open a web page:


// Intent to open a web URL
String url = "https://www.google.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));

// Start the activity
startActivity(intent);

Running this code will make Android open the user's default web browser and navigate to google.com.


Passing Data Between Activities

You can also send data from one activity to another using Intents. This is done via `extras`.

Process of Sending Data (Sender Activity):

  1. Use the `putExtra()` method with the Intent object.
  2. `putExtra()` takes two things: a unique key (String) and the value of the data.

Example in Java (`MainActivity.java`):


Intent intent = new Intent(MainActivity.this, SecondActivity.class);

// Put data as key-value pairs
intent.putExtra("USER_NAME", "Anil Sharma");
intent.putExtra("USER_AGE", 21);

startActivity(intent);

Process of Receiving Data (Receiver Activity):

  1. Use the `getIntent()` method to get the intent that started the activity.
  2. Use the `get...Extra()` methods (like `getStringExtra()`, `getIntExtra()`) to extract the data from that intent. You must pass the same key that the sender used.

Example in Java (`SecondActivity.java`):


// Inside the onCreate() method

// Get the intent
Intent intent = getIntent();

// Extract the data using the key
String name = intent.getStringExtra("USER_NAME");
int age = intent.getIntExtra("USER_AGE", 0); // 0 is a default value

// Now you can use these variables
// For example, setting the name and age in a TextView