TykiMikk
TykiMikk

Reputation: 1068

Android Intent createChooser()

My goal is to create an Implicit Intent and use the Intent.createChooser() method to select an application for viewing a web page I was told i needed to create a chooser intent, for choosing which Activity will carry out.

My first question is why do i need 2 intents for this? Why can't I just use baseIntent and put it inside the createChooser() method?

My second question is why does my application not show the menu where I can choose which application to open my URL?

Can someone also check my intent filters to see if I got them all.

MainActivity.java

static private final String URL = "http://www.google.com";
static private final String TAG = "Lab-Intents";

// For use with app chooser
static private final String CHOOSER_TEXT = "Load " + URL + " with:";



private void startImplicitActivation() {


Log.i(TAG, "Entered startImplicitActivation()");

Uri webpage = Uri.parse(URL);
Intent baseIntent = new Intent(Intent.ACTION_VIEW,webpage);

Intent chooserIntent = null;


startActivity(Intent.createChooser(baseIntent,CHOOSER_TEXT));

}

AndroidManifest.xml

   <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT"></category>
        <category android:name="android.intent.category.BROWSABLE"></category>
        <action android:name="android.intent.action.VIEW" />

        <data android:mimeType="string"/>
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

Upvotes: 2

Views: 3192

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006604

I was told i needed to create a chooser intent, for choosing which Activity will carry out.

Not necessarily.

My first question is why do i need 2 intents for this?

You don't.

Why can't I just use baseIntent and put it inside the createChooser() method?

You certainly can do that. What createChooser() does, mostly, is force a chooser.

Suppose the user has both Chrome and Firefox installed on their Android device, and you use ACTION_VIEW to try to bring up a Web page in a browser.

If you use the ACTION_VIEW Intent directly, and if the user has chosen one of those browsers as the default browser (from some previous ACTION_VIEW request), the Intent will be routed to the default browser. If the user has not chosen a default browser, the user is presented with a chooser, so the user can choose which of the two Web browsers to use. That chooser will have a way for the user to make their choice the default browser for future requests.

If, on the other hand, you use createChooser() to wrap your ACTION_VIEW Intent, your startActivity() call will always bring up a chooser, even if the user has chosen a default Web browser. And, this chooser will not have an option for the user to set a default.

Hence, usually, you do not use createChooser(), unless you feel that there is significant risk that the user's default will not be the right choice for this circumstance. You will typically see createChooser() used with ACTION_SEND, as that is a very generic request, one where defaults may not be the appropriate course of action.

My second question is why does my application not show the menu where I can choose which application to open my URL?

If, when you use createChooser(), the user is taken directly to an activity, that is because that activity is the one-and-only match for the Intent. So, for example, if the user only has one Web browser installed, and you use ACTION_VIEW to bring up a URL, the system is not going to present a chooser for just one Web browser, but instead will just route the request to that one browser.

Upvotes: 5

Related Questions