1

I'm trying to implement Facebook Log In Button in Android.

Currently when I click the button the facebook app opens with no problems, but when I accept or cancel the callbacks are not getting called. Here is my code.

Public class AccessOptionsFragment extends Fragment implements FacebookCallback<LoginResult> {

    private CallbackManager callbackManager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    View view = inflater.inflate(R.layout.fragment_access_options, container, false);
    callbackManager = CallbackManager.Factory.create();
    LoginButton loginButton = (LoginButton) view.findViewById(R.id.fb_button);
    loginButton.setFragment(this);
    loginButton.setReadPermissions("email");
    loginButton.registerCallback(callbackManager, this);
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, requestCode, data);
}


@Override
public void onSuccess(LoginResult loginResult) {
    AccessToken accessToken = loginResult.getAccessToken();
    AccessActivity accessActivity = (AccessActivity)this.getActivity();
    accessActivity.displaySignUpActivity(accessToken);
}

@Override
public void onCancel() {

}

@Override
public void onError(FacebookException e) {

}
}

Already checked the manifest and it seems to be ok.

If you need anything else to help me please tell me. I've been reading answers but none have worked.

EDIT --> SOLUTION

For some reason, as mentioned in the comments, the Fragment onActivityResult() was not getting called, but instead the Activity was.

So move the facebook logic to the activity and the let the fragment just manage the view. And REMEMBER TO DELETE THE LINE

loginButton.setFragment(this);

Otherwise the callback won't work.

Lightster
  • 119
  • 1
  • 9
  • 2
    I believe that the onActivityResult of the Activity is called, not the one of the fragment. – Mimmo Grottoli Jul 13 '15 at 20:23
  • I thought the idea of setFragment was that. How can I do it then? – Lightster Jul 13 '15 at 20:25
  • Look at this [post](http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment) – Mimmo Grottoli Jul 13 '15 at 20:28
  • What version of the SDK are you using, and have you tried running any of the samples from the SDK? They should mostly use Fragments, and your method should work. – Ming Li Jul 14 '15 at 17:26
  • 1
    The other possibility is that your Fragment is getting destroyed during the login, and since onActivityResult gets called before onCreateView, your registerCallback method hasn't been called yet. You can move to use LoginManager inside your onCreate method instead since that gets called before onActivityResult. – Ming Li Jul 14 '15 at 17:32
  • I edited. I still don't know why the method in the fragment wasn't getting called, but now that I moved that logic to the Activity, it works. – Lightster Jul 14 '15 at 17:35
  • my sdk 'com.facebook.android:facebook-android-sdk:4.0.0' – Lightster Jul 14 '15 at 17:35

1 Answers1

1

Replace the line

callbackManager.onActivityResult(requestCode, requestCode, data);

with

callbackManager.onActivityResult(requestCode, resultCode, data);

(note the second parameter!)

Martin
  • 770
  • 6
  • 22