Android Development
Cross-Application Interaction
Intent Usage
Android Activities
Mobile App Development

How to launch an Activity from another Application in Android

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In the world of Android development, one of the most powerful features is the ability to communicate and interact with other applications. This can be particularly useful when you want your app to perform a task that's already handled by another application, such as displaying a map, capturing a photo, or simply sharing data. One of the main ways to achieve this is by launching an Activity from another application using Intents. This article will delve into the technicalities, examples, and best practices for achieving this.

What Are Intents?

Intents in Android are simple message objects that are used to communicate between different components of an application or even between different applications. They can be used to start activities, services, and broadcast receivers. There are two types of intents:

  • Explicit Intents: Used when launching an activity within the same application.
  • Implicit Intents: Used when launching an activity in another application.

Launching Activities Using Implicit Intents

Step-by-Step Guide

  1. Define the Intent: Determine the action you want to perform, such as ACTION_VIEW for viewing data, ACTION_SEND for sharing data, etc.
  2. Add Data or Extras (if needed): Attach any necessary data to the Intent. For example, if you're opening a URL in a browser, you would set the data as the URL.
  3. Specify the Category (optional): Define additional categories, which can help the system determine which activities can be started.
  4. Resolve the Activity: Before calling startActivity(), it's good practice to ensure there's an application available to handle the Intent.
  5. Start the Activity: Use startActivity() to send the Intent, causing the target activity to start.

Example

Suppose you want to open a webpage in a browser from your application. Here is a simple example:

java
1Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
2if (browserIntent.resolveActivity(getPackageManager()) != null) {
3    startActivity(browserIntent);
4}

In this example, ACTION_VIEW with a Uri is used to open a webpage. The call to resolveActivity() ensures there's at least one app that can handle this Intent, avoiding a crash if none is available.

Tables of Key Considerations

Key PointDescription
Intent TypeUse Implicit Intents to interact with other apps.
ActionDefines what type of interaction you want (e.g., ACTION_SEND).
Data & ExtrasAdditional information for the Intent like URIs or key-value pairs.
CategoriesOptional categories that provide additional info about the action.
Resolve CheckAlways verify an activity exists to handle the Intent or catch an exception.
SecurityEnsure you're not exposing sensitive data unintentionally when sharing between apps.

Handling Activity Results

You may want to receive a result from the launched activity. This can be achieved using startActivityForResult(). Here's a basic implementation:

  1. Start the Activity for a Result: Use startActivityForResult() instead of startActivity().
  2. Override onActivityResult(): Process the result in this callback.

Example

Consider launching a contact picker activity that returns a contact selected by the user:

java
1Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
2pickContactIntent.setType(Phone.CONTENT_TYPE); 
3startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
4
5// Processing the result
6@Override
7protected void onActivityResult(int requestCode, int resultCode, Intent data) {
8    if (requestCode == PICK_CONTACT_REQUEST) {
9        if (resultCode == RESULT_OK) {
10            Uri contactUri = data.getData();
11            // Handle the contact here
12        }
13    }
14}

Security Considerations

When sharing data between applications, you should be conscious of security implications:

  • Validate Data Carefully: Never trust data coming from another app blindly. Always validate it.
  • Permission Requirements: Ensure your app requests and checks the necessary permissions. If you're involving user data, inform the user why the permission is needed.
  • Avoid Sensitive Data Over Implicit Intents: Because implicit Intents can be handled by other apps, avoid sharing sensitive data like passwords or personal information via Intents.

Conclusion

Launching an activity from another application in Android is a powerful tool that can improve user experience by leveraging existing functionalities provided by other apps. By using implicit Intents, making use of best practices, and keeping security considerations in mind, you can ensure smooth integration and operation across different Android applications. Whether it's sending a simple SMS, viewing a map, or picking a contact, Intents make it possible to extend your app's capabilities beyond its own boundaries.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.