How to tell which app was selected by Intent.createChooser?

On Android 5.1+, you can use the three-parameter edition of the createChooser() method, where the last parameter is an IntentSender that you can use to find out what was chosen.

Prior to Android 5.1, there is nothing in Android to let you know what the user chose.


The answer provided by BinHe works but the problem is that a big number of apps is shown. In this solution I use the Intent.ACTION_PICK_ACTIVITY but only the apps compatible with Intent.ACTION_SEND will be shown, and you will know which option the user selected.

public void doSocialShare(String title, String text, String url){
    // First search for compatible apps with sharing (Intent.ACTION_SEND)
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    // Set title and text to share when the user selects an option.
    shareIntent.putExtra(Intent.EXTRA_TITLE, title);
    shareIntent.putExtra(Intent.EXTRA_TEXT, url);
    shareIntent.putExtra(Intent.EXTRA_TEXT, text);
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
    if (!resInfo.isEmpty()) {
        for (ResolveInfo info : resInfo) {
            Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
            targetedShare.setType("text/plain"); // put here your mime type
            targetedShare.setPackage(info.activityInfo.packageName.toLowerCase());
            targetedShareIntents.add(targetedShare);
        }
        // Then show the ACTION_PICK_ACTIVITY to let the user select it
        Intent intentPick = new Intent();
        intentPick.setAction(Intent.ACTION_PICK_ACTIVITY);
        // Set the title of the dialog
        intentPick.putExtra(Intent.EXTRA_TITLE, title);
        intentPick.putExtra(Intent.EXTRA_INTENT, shareIntent);
        intentPick.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray());
        // Call StartActivityForResult so we can get the app name selected by the user
        this.startActivityForResult(intentPick, REQUEST_CODE_MY_PICK);
    }
}

Finally, to be able to get the app selected by the user you must override the onActivityResult on your activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_CODE_MY_PICK) {
        if(data != null && data.getComponent() != null && !TextUtils.isEmpty(data.getComponent().flattenToShortString()) ) {
            String appName = data.getComponent().flattenToShortString();
            // Now you know the app being picked.
            // data is a copy of your launchIntent with this important extra info added.

            // Start the selected activity
            startActivity(data);
        }
    } 
}

This should work for early versions of Android.

Use intent PICKER instead of CHOOSER. The difference is that picker won't start the target intent automatically, but rather, it returns to onActivityResult() the target intent with the selected app's component name attached. Then you start the target intent in the callback as a 2nd step.

A little bit of code should explain,

// In MyActivity class
static final int REQUEST_CODE_MY_PICK = 1;

// Getting ready to start intent. Note: call startActivityForResult()
... launchIntent = the target intent you want to start;
Intent intentPick = new Intent();
intentPick.setAction(Intent.ACTION_PICK_ACTIVITY);
intentPick.putExtra(Intent.EXTRA_TITLE, "Launch using");
intentPick.putExtra(Intent.EXTRA_INTENT, launchIntent);
this.startActivityForResult(intentPick, REQUEST_CODE_MY_PICK);
// You have just started a picker activity, 
// let's see what user will pick in the following callback

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_CODE_MY_PICK) {
         String appName = data.getComponent().flattenToShortString();
         // Now you know the app being picked.
         // data is a copy of your launchIntent with this important extra info added.

         // Don't forget to start it!
         startActivity(data);
    }
}