Search code examples
androidandroid-intentandroid-activityandroid-implicit-intent

What is the difference between queryIntentActivities() and resolveActivity().Which one is the best approach to know about existing apps for a intent?


As I can see in Android documentation when trying to build implicit intents when sending the user to another app. These are the two approaches to avoid ActivityNotFoundException.

First one :

Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);

PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent,
    PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size() > 0;

Second one :

Intent chooser = Intent.createChooser(intent, title);
if (intent.resolveActivity(getPackageManager()) != null) {

}

Now my doubt is whats the differnece and which one should i use ?


Solution

  • Depends what you want to do.

    If you just want to prevent 'ActivityNotFoundException', either method will work. Neither is "best". They do basically the same thing. You want to know if there is at least 1 Activity that can handle your Intent.

    Otherwise:

    • queryIntentActivities() returns a list of all activities that can handle the Intent.
    • resolveActivity() returns the "best" Activity that can handle the Intent

    Therefore, if you want to know all the activities that can handle your Intent, you would use queryIntentActivities() and if you want to know what Android thinks is the "best" Activity, then you would use resolveActivity().