In my previous post we introduce the concept of "fragments" in Building a Dynamic UI with Fragments in Android.  Now see how Android app communicate with each other.

An android app comprises of several activities. Each activity display a GUI that allows the user to perform a specific task. To take the user from one activity to another, app must use an Intent to define our app’s intent to do something.

   An intent can be explicit in order to start a specific component (a specific Activity instance) or implicit in order to start any component that can handle the intended action. 

Sending the user to another app

We generally used intents to navigate between activities in our own app. We generally do so with an explicit intent, which defines the exact class name of the components we want to start. But, when we have separate app perform an action we must use an implicit intent.

Build an Implicit Intent:

Implicit intents do not declare the class name of the component to start, but instead declare an action to perform. The action specifies the thing we want to do, like view, edit, send and get. Intents often also include data associated with the action, such as address we want to view, or the mail message we want to send. Depending on the intent, data might be Uri, one of the several other data types, or the intent might not need data at all.

If the data is a Uri, there’s a simple Intent() constructor we can use define action and data.

For Example, here’s how to create an intent to initiate a phone call using the Uri data to specify the telephone number:

Uri number = Uri.parse(“tel: 5551234”);

Intent callIntent = new Intent(Intent.ACTION_DIAL, number);


When your app invokes this intent by calling startActivity(), the Phone app initiates a call to the given phone number.

Some kind of implicit intents require “extra” data that provide different data types, such as string. We can add various pieces of extra data using the various putExtra() methods.

By Default, the system determines the appropriate MIME type required by intent based on the URI data that’s included. If we don’t include Uri in the intent, we should usually use setType() to specify the type of data associated with the intent .Setting MIME type further specifies which kind of activates should receive the intent.

For example,

Intent emailIntent = new Intent(Intent.ACTION_SEND);

// The intent does not have a URI, so declare the “text/plain” MIME type.
emailIntent.putExtra(intent.EXTRA_EMAIL, new String[] {});
emailIntent.putExtra(intent.EXTRA_SUBJECT, “Email subject”);
emailIntent.putExtra(intent.EXTRA_TEXT “Email message text”);
emailIntent.putExtra(intent.EXTRA_STREAM, Uri.parse(“content: // path/to/email/attachment”));
//We can also attach multiple items by passing an ArrayList of Uris


Verify there is an App to Receive the Intent

Android platform guarantees that certain intents will resolve to one of the built-in apps (such as the Phone, Email or Calendar app) you should always include a verification step before invoking an intent.

To verify there is an activity available that can respond to the intent, the call queryIntentActivities() to get a list of activities capable of handling your Intent.  If the return list is not empty we can safely use the intent :

PackageManager packageManager = getPackageManager();

List <ResloveInfo> activites = packageManger.queryIntentActivites(intent,0);

Boolean isIntentSafe = activies.size() > 0;


If isIntentSafe() is true, then at least one app will respond to the intent. If it is false, then there aren’t any apps to handle the intent

Start an Activity with an intent

Once we have created an intent and set the extra info, call startActivity() to send it to the system. IF the system identifies more than one activity that can handle intent, it displays dialog to user to select which app to use, if there is only one activity that handles the intent, the system immediately starts it.


 For example, here we show how to create an intent to view a map, verify that an app exists to handle the intent, then start it:

//Build the intent
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
//Verify it resolves
PackageManager packageManger = getPackageManager();
List<ResolveInfo> activities =
  packageManager.queryIntnetActivites(mapIntent, 0);
Boolean isIntentSafe = activities.size() > 0;
//Start an activity if it is safe
If(isIntentSafe) {


Show an App Chooser

If the activity to be performed by an intent could be handled by multiple apps and the user might prefer a different app each time – such as a “share” action, for which user might have several apps through which they might share an item – we should explicitly show a chooser dialog Chooser dialog forces the user to select which app to use for the action every time.

To show the chooser, create an Intent using createChooser() and pass it to startActivity()

Intent intent = new Intent(Intent.ACTION_SEND); 
//Always use string resources for UI text.
// This says something like “Share this photo with”
String title = getResources().getString(R.string.chooser_title);
//Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
//Verify the intent will resolve to at least one activity
If(intent.resolveActivity(getPackageManager()) ! = null) {

 This display a dialog with a list of apps that respond to the intent passed to the createChooser() method and uses the supplied text as the dialog title.

This post is referred from Google docs Interacting with Other Apps

Next, we see how to get the result from an activity in Getting a Result from an Activity

  Modified On May-31-2019 09:43:44 PM

Leave Comment