I’ve been trying to implement the Facebook SDK into my application in order to let users post messages on our fanwall through the app. However, I’ve been unsuccessful at even logging the user in through the SDK.
In the SDK examples, a simple sample has been given which uses an Activity to try and authorize the user using Single Sign-On. I’ve tried this example myself, and it works. I’m able to log in, I had to authorize the app to use my Facebook data and I could see the requests being made and received in LogCat.
Now, I’ve tried adding the same code to my own app. This app is Fragment based using the Compatibility package. There is one central FragmentActivity and the rest of my classes are simple Fragments. When adding the sample code to one of these Fragments, the Facebook app starts up for half a second when trying to authorize, but afterwards closes and nothing has happened. I’m back in my regular Fragment again.
When checking the LogCat after this, nothing suggests that the Facebook app even opened or made any requests other than the fact it shows some printing checks I added and the fact it says it’s starting the Facebook intent :
01-12 13:19:40.874: I/System.out(6087): Calling authorize
01-12 13:19:40.874: I/ActivityManager(1380): Starting activity: Intent { cmp=com.facebook.katana/.ProxyAuth (has extras) } from pid 6087
01-12 13:19:40.874: I/System.out(6087): Called authorize
Other than that, nothing gets returned. No Facebook-checks, no statements saying my keys are wrong or anything, just nothing. The Facebook intent was called, but closed almost immediately and nothing else shows that it was even open.
This has been boggling my mind for a few hours now, and I’m starting to think the regular , sample-provided approach just doesn’t work in Fragments due to the way Fragments work.
The code I’ve been using is posted below. The Fragment gets fired by a button which calls a FragmentTransaction. Am I doing something fundamentally wrong here, or does the Facebook SDK really just not work with Fragments? I’ve tried searching for this problem but I haven’t been able to find anyone else with the same sort of situation.
public class FanWallFacebook extends Fragment {
Facebook facebook = new Facebook("294678133912628");
public FanWallFacebook() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.filler, container, false);
}
@Override
public void onStart() {
super.onStart();
System.out.println("Calling authorize");
facebook.authorize(getActivity(), new DialogListener() {
@Override
public void onComplete(Bundle values) {
System.out.println("Completed");
}
@Override
public void onFacebookError(FacebookError error) {
System.out.println("Facebook error: "+error.getMessage());
}
@Override
public void onError(DialogError e) {
System.out.println("General error: "+e.getMessage());
}
@Override
public void onCancel() {
System.out.println("Cancelled");
}
});
System.out.println("Called authorize");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
System.out.println("Authorize callback'd");
}
}
Edit
Just tried using a FragmentActivity, and lo and behold, that does work. It successfully logs in. Seems like the SDK really only works with classes that explicitly extend ...Activity. Could anyone give me an idea why that might be the case? I always thought Fragments somewhere down the line extended Activity as well.
I ended up using a
FragmentActivityfor my Facebook interaction. Not the ideal solution, but it works.