2

I'm trying to call the Facebook authorization from my Android activity, but for some reason it never calls the onActivityResult as it should.

I followed the official tutorial, and I even created a very simple application just in order to try this functionality:

public class SimpleFacebookActivity extends Activity {
    private EditText console;
    private Facebook facebook = new Facebook(APP_ID);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.console = (EditText)super.findViewById(R.id.console);
        this.console.append("Started\n\n");

        String text = Integer.toString(super.getIntent().getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY);
        this.console.append(text);

        this.facebook.authorize(this, new DialogListener() {
            @Override
            public void onComplete(Bundle values) {}

            @Override
            public void onFacebookError(FacebookError error) {}

            @Override
            public void onError(DialogError e) {}

            @Override
            public void onCancel() {}
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        this.console.append("onActivityResult, request code: " + requestCode + "\n\n");
        super.onActivityResult(requestCode, resultCode, data);
        this.facebook.authorizeCallback(requestCode, resultCode, data);

    }
}

I added a TextEdit widget to which I log, and when I run this application all I get is:

Started

0

I checked to see if the FLAG_ACTIVITY_NO_HISTORY is set since they mention it in the tutorial and in another post I saw here on Stack Overflow, but in my case it's not set and so can't be the problem.

How can I fix this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299

2 Answers2

5

I think this is because you need to add some key to your app in Facebook:

https://developers.facebook.com/docs/mobile/android/build/#sig

But the key is not correctly created when you follow the step-by-step guide, so you got to check for the debugger of the Facebook SDK, to throw you an error telling something like the key you are using is not the same of your app, copy paste and voila!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
desgraci
  • 1,191
  • 1
  • 11
  • 25
  • I added the key in the facebook developers app, and I know that it's working well since I can manage to successfully authenticate using the sdk, it's just that onActivityResult method is not being called when that authentication process finishes. – Nitzan Tomer Jan 26 '12 at 00:17
  • 4
    I see, the problem is that you are expecting the Dialog returns you like an extra activity, look, when you open the Dialog, it will happen 2 thing. 1 - You have facebook.katana installed on your phone (the facebook app) in which you may do your authorize (i'm assuming you're loggin in) and that external app will get you a nice code in your ActivityResult, i think it was 32665. 2. The second case is that you dont have the App installed so it will open a Web View inside your activity, a VIEW, it wont leave your activity never, so you got to recieve whatever you want in the methods of the... – desgraci Jan 26 '12 at 17:05
  • 2
    dialog Listeners, like in example, you logged correctly and it will call the onComplete(Bundle values) {} method. Now if you really, really want to use the facebook app to force your activity to make a callback in activityResult, you should be implementing an intent and starActivityForresult, but this is not recommended at all. Hope this clarify all your doubts. – desgraci Jan 26 '12 at 17:08
  • 1
    well, this kind of supports a theory i came up with, that the onActivityResult is called only on cetrain authentication flow, and in other flow(s) it does not. I'm currently on testing on an emulator, and in it I don't have the facebook application installed, and so what you say makes sense. if that's true then the onActivityResult will only be called then. I will just have to test this theory on a real device. – Nitzan Tomer Jan 27 '12 at 11:26
  • In fact is not a theory, onActivityResult is only called when you use the startActivityOnResult as @Peterdk suggested, otherway you have to use the Callbacks method as the facebook dialog ones, if you don't have the facebook App, it wont call the onActivityResult, cause you aren't going to need the startActivityForResult intent of the facebook.katana, unless of course you call for another intent that way. Without the facebook app, you only will get the callbacks normally. If this clarify everything, please mark as solved. Thank you. – desgraci Jan 27 '12 at 14:08
  • ok, so my question is this: will the facebook "mechanism" will use the startActivityForResult when it's possible? or is it up to me? – Nitzan Tomer Jan 28 '12 at 15:10
  • sorry for the delay with the answer, got sick, ok the startActivityForResult is up to you, it will only work if: 1. Call an intent with startActivityForResult. 2. Open ofc the facebook app to, dunno, login... 3. Return from the facebook app, it will return to the OnActivityResult method you made with a code for facebook. Otherwise you have to use the callbacks of the DialogListener of the facebook sdk. – desgraci Feb 07 '12 at 15:57
  • Hey @desgraci I tried implementing the FB login flow with the UILifeCycleHelper and in certain instances, the `data` param which would normally have a field `mMap`, which contains the FB response, is null. Why would this be happening? – Etienne Lawlor Aug 26 '13 at 07:07
0

I don't know the Facebook SDK, but it looks to me you should implement these DialogListener methods. Otherwise you don't get any callback of course.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peterdk
  • 15,625
  • 20
  • 101
  • 140
  • implementing the DialogListener methods does not help, I did try to add some code in the onComplete one, and it does get there, but still the onActivityResult method is not being called. – Nitzan Tomer Jan 21 '12 at 17:52
  • Isn't that normal? onActivityResult does only get called when you start an activityForResult using an intent? – Peterdk Jan 21 '12 at 18:53
  • @Peterdk , it gets there if single-sign-on login procedure gets called (which is triggered using activityForResult). – harism Jan 21 '12 at 19:07