Sep 082011

Here’s the scenario. You’re playing my game, in which I have a Facebook button. You click on that button. Ideally, I’d like that to open up the Facebook app, and take you to my page. Why? Because you’re probably logged in with the Facebook app, which increases the chances that you’ll click the “Like” button and start following me. If I just open the link in the phone’s browser, chances are high that you’ll arrive at my page and not be logged in. Which means the chances are high that you won’t bother logging in just to like that page. On the other hand, if you don’t have the Facebook app installed, then I do want to open up the browser, rather than present a “Sorry, can’t do this, no Facebook” error message. So, how do I make this work? It’s pretty easy, but it took me a bit of searching on Google to figure this out. I’m putting this down here in case anyone else finds it helpful.

It’ll be easiest to explain if I just start with the code:

    public final void launchFacebook() {
        final String urlFb = "fb://page/192338590805862";
        Intent intent = new Intent(Intent.ACTION_VIEW);

        // If a Facebook app is installed, use it. Otherwise, launch
        // a browser
        final PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list =
        if (list.size() == 0) {
            final String urlBrowser = "";

The first thing I learned is, there’s an “fb://” URL format that the Facebook app recognizes, but the web browser doesn’t. The URL for a page is “fb://page/{id}”. There are other URL formats for a person’s wall, and so on.

So you start out by creating an Intent with this URL. Then you call “queryIntentActivities” to see if there’s any Activity that can handle this Intent. If one exists (list size is non-zero), then you start a new activity with this Intent. If there’s no Activity that can handle the “fb://” URL, then you create a regular http or https URL. Launch that, and it will get picked up by the browser.

This turned out to be quite easy to do, once I figured out how to do it. Run this on my phone, and it launches the Facebook app. Run it in the emulator, and it launches the browser. I’d say that constitutes “Test passed” :)

Next: See if something similar can be done for Twitter.

  5 Responses to “Launching a Facebook link with the Facebook app”

  1. Hi Ziggy,

    I like your solution, I think it’s good practice to jump in an app when it’s available.
    Still, Facebook uses a mechanism they call ‘single sign on’ for its app, apps that use the Facebook API and its website. This means in your case: When a user is signed in at the Facebook app, he is also already signed in at the website, so no aditional signing in would be necessary. Facebook does this by using cookies. Still, I think the official app should also react on intents containing an URI starting with ‘’.

    Best regards

    • Ah, I didn’t know that. Thanks for that comment. Still, as you said, I’d prefer to see a Facebook link open up in the Facebook app, and I’m sure there are others who would feel the same way.

      Unfortunately, I haven’t been able to do the same thing with Twitter. Some Twitter clients will react to an “…” URL, but not all of them. I have TweetCaster on my phone, and that doesn’t, so all I get are the installed browsers.

  2. If you want to leverage your functions, then you can use Facebook’s official SDK for Android. Here it is:

  3. Thanks so much for this!
    Works great :)

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>