1

Background

i'm trying to open a specific profile page on the facebook app (when facebook app is installed of course).

The problem

I've noticed that if I, as an end user, go to the app info screen (where i can uninstall the app, clear its cache etc...) of facebook app and choose "force stop", and then try to open the profile page, the facebook app itself either crashes or doesn't even start.

if the facebook app was already running, the code works fine.

The code

context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
final String facebookScheme = String.format("fb://profile/%s", socialNetworkUid);
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY
        | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
return intent;

BTW, the scheme was found from this link.

The question

  • why does it occur?
  • can i somehow avoid it?
  • is it a bug on facebook app or maybe i didn't implement it correctly?
  • do I really need now to check if FB app is active and only then allow to use this intent? if so, how?
Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • The quick answer is, there's currently NO supported method to link directly to a specific piece of content inside the Facebook app. Any mechanisms you see on stackoverflow or elsewhere are based on trial and error, and can break from one release to another. – Ming Li Oct 10 '13 at 17:52

1 Answers1

2

The following code works for me with no issue, whether the Facebook app is open or not.

public void goToFacebook(String id) {

    try {
        getPackageManager().getPackageInfo("com.facebook.katana", 0);
        Intent facebookPage = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/"+id));
        startActivity(facebookPage);
    } catch (Exception e) {
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/"+id));
        startActivity(launchBrowser);
    }

}

As you can see, I am using try/catch to detect whether or not the FB app is installed. If the Facebook app is already launched, it will move to the profile, otherwise it will launch the app and then open the profile.

If the Facebook app isn't installed, I send the user to the profile page in a browser.

EDIT

I've managed to reproduce the same thing you are seeing on my test device after multiple opens of the Facebook app from within my app. I can see the following error in LogCat, so it does appear to be a bug with the Facebook app:

10-10 14:43:01.406: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.facebook.katana/com.facebook.katana.IntentUriHandler}: java.lang.IllegalStateException: FbSharedPreferences used before initialized

Jay
  • 156
  • 6
  • so what can i do? should i really do a check if the FB app is active ? at least till they fix it? if so, how can i do it? – android developer Oct 10 '13 at 13:53
  • That would be a different question - but see [this answer](http://stackoverflow.com/a/4213851/1537347) for a suggestion. If I power off my device things return back to normal, until I have opened the FB app from within my app and then closed the FB app again. – Jay Oct 10 '13 at 13:58
  • that's a good way to find out if FB is active, at least according to my checks. i was hoping though for a better solution, that maybe i was wrong of how to access FB app. – android developer Oct 10 '13 at 14:07
  • Your method of accessing the FB app is correct, although I would still do a check to make sure that the FB app is installed before sending the intent, otherwise send the user to a web browser (see above code). I guess it depends how critical this part of the app is - in my case it's just an 'about' section, so I can cope if it goes wrong in the short term, until FB create a fix. – Jay Oct 10 '13 at 14:29
  • i have checked for the existence of FB (hence i've written in the question "when facebook app is installed of course"), just didn't write it in code here. so you say i should check if the app is also "running" , till FB fixes their app? if so, please update the answer since currently it only says that they have a bug. – android developer Oct 10 '13 at 15:07
  • I _personally_ wouldn't check if it was running, unless opening the profile page in the app is absolutely critical to your application. If you are already checking that the FB app is installed in your code, then I would say that this is sufficient for the average user who may never experience the bug in question. – Jay Oct 10 '13 at 15:17
  • the thing is that i've experienced it even without using "force close". i don't know what is the scenario though. maybe after a long time of usage, or using apps that require a lot of memory. – android developer Oct 10 '13 at 16:39