4

I got an interview question.....

How to specify which Activity to be launched from an implicit intent, when there are multiple activities competing to execute the intent, without requiring user intervention.

My answer to this question is to use proper intent-filter within every activity, but it just sounds wrong..

Thanks in advance!

Sherman_Meow
  • 247
  • 3
  • 13

1 Answers1

8

When creating an Intent you can pass explicit component name. i.e. class name. Only that component will now receive the intent.

example:

Intent myIntent = new Intent(getApplicationContext(),RequiredActivity.class);
startActivity(myIntent);

If you don't specify the exact component, Android will smartly let user choose one of the components that handles the intent.

example:

    Intent myIntent = new Intent(Intent.ACTION_VIEW);
    startActivity(myIntent);

If you want to go through all the components that handle the intent, yourself instead of letting android show choices to user, you can do that too:

example:

    Intent myIntent = new Intent(Intent.ACTION_VIEW);

    List<ResolveInfo> infoList = getPackageManager().queryIntentActivities(myIntent, 0);

    for (ResolveInfo ri : infoList){
        ActivityInfo ai = ri.activityInfo;
        String packageName = ai.packageName;
        String componentName = ai.name;

       // you can pick up appropriate activity to start 
       // if(isAGoodMatch(packageName,componentName)){
       //     myIntent.setComponent(new ComponentName(packageName,componentName));
       //     startActivity(myIntent);
       //     break;
       // }

    }

I got six Activity matches for above code:

enter image description here

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Thanks for replying, but it denoted that "without requiring user intervention", so we cannot just createChooser and let the user to pick an activity to run... – Sherman_Meow Aug 25 '13 at 07:11
  • 3
    When you say `You can pass explicit component name`, doesn't it becomes _explicit_ intent. When I read the [Difference between explicit and implicit Intent](http://stackoverflow.com/questions/13813586/difference-between-implicit-and-explicit-intents), it said that implicit Intents don't specify any component. The [docs](http://developer.android.com/guide/components/intents-filters.html) also said `In implicit we do not name a target (i.e the field for the component name is blank).`Are you trying to say something different? I couldn't understand. Doesn't the question asks for `implicit intent`? – Shobhit Puri Aug 25 '13 at 07:14
  • So is the correct answer that android will choose itself in the intent is _implicit_? Also just curious to know that is there any predefined criteria mentioned in docs based on which it chooses? – Shobhit Puri Aug 25 '13 at 07:16
  • 1
    @ShobhitPuri I thought we can use intent-filter with specific data, category or action so that the system will pick an activity base on these criterias. While if the answer is this, the question should probably be "if we have multiple intents, how can an activity receive a specific one and perform what it is desired to". – Sherman_Meow Aug 25 '13 at 07:25
  • @S.D. i'm curious as well so one more question:Have you tested the third one ever? – Mohit Aug 25 '13 at 07:26
  • @ShobhitPuri Also the question denoted that "multiple activities competing to execute the intent", so I assume that all these activities have intent-filters that fulfill the intent's criteria. – Sherman_Meow Aug 25 '13 at 07:28
  • 1
    @S.D. I see the third option, Interesting! so the for loop will go through all possible activities and pick the most proper one to run? – Sherman_Meow Aug 25 '13 at 07:35
  • @Sherman_Meow the third example, lets you handle the selection yourself. – S.D. Aug 25 '13 at 07:36