FacebookCallback.onCancel is getting called when trying to login using facebook sdk

10k Views Asked by At

I have an Android app and I am trying to use Facebook's SDK (version 4.1.0) to get a token and log in. Here is my code:

public class LoginActivity extends Activity {
    private CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        FacebookSdk.sdkInitialize(getApplicationContext());

        callbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                AccessToken accessToken = loginResult.getAccessToken();
                Log.v(TAG, "Facebook login was successful");
                String authToken = accessToken.getToken();
                // User authToken here:
            }

            @Override
            public void onCancel() {
                Log.v(TAG, "Facebook login was canceled");
            }

            @Override
            public void onError(FacebookException e) {
                Log.e(TAG, "Facebook login failed: " + e.getMessage());
            }
        });

        Button facebook_button = (Button) findViewById(R.id.fbButton);

        facebook_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                LoginManager.getInstance().logInWithReadPermissions(getActivity(), Arrays.asList("public_profile"));
            }
        });

    }

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

The code switched to the Facebook app and back and OnActivityResult() is called. However, every time the callback method that is called is onCancel(). Note that I am not using the LoginButton provided by Facebook, and I have my own button (although I tried that approach and the result was the same). I double and triple checked my app ID and the keyhash generated by the app and they look correct too. So, I don't know what else may be wrong. Any help at this point is greatly appreciated.

7

There are 7 best solutions below

1
happyhuman On

The issue was simply because the APP ID needed to be in double-quotes.

0
DoruChidean On

I had the same issue, eventually I found the problem. The activity calling facebook login fragment had android:launchMode="singleInstance" in the manifest file.

0
Peter On

@Doru's comment lead me to the solution. I used the Facebook activity rather than fragment.

Instead of

    <activity android:name="com.facebook.FacebookActivity"
        android:screenOrientation="portrait"
        android:launchMode="singleTask" />

I needed to write

    <activity android:name="com.facebook.FacebookActivity"
        android:screenOrientation="portrait" />

What's nasty about this bug, is that it only occurs on old Android versions (e.g. 4.4.2, not 5.0).

2
raisahab On

The reason behind this behaviour is that you are already logged in. So when you revoke it, oncancel() gets called instead of onsubmit(). So just perform logout on your application's logout button like this

Import -->

import com.facebook.login.LoginManager;

Implementation -->

LoginManager.getInstance().logOut();
1
Mehroz Munir On

yes i was facing the same issue, resolved it by using the below code just before login

 LoginManager.getInstance().logOut();
0
PGMacDesign On

If anyone comes across this in the future, this can also be caused by the Facebook native app installed on the device blocking login due to reason X.

In my case, this was being thrown every single time I attempted login. I finally switched over to the native Facebook app and as it opened, it required authentication and had me log in again due to some 'suspicious activity' (I think I changed my number recently).

If you are doing everything that is listed above and users are still complaining about it (and they have the app installed, you can use the code at the bottom to see if the Facebook app in installed:) you may wish to include a dialog that asks them to check the Facebook app and make sure they are still logged in, which would then prompt them to fix any errors before tabbing back into your application.

//Code to check if Facebook app is installed:
public static boolean doesUserHaveFacebookAppInstalled(Context context){
    try{
        context.getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
        return true;
    } catch( PackageManager.NameNotFoundException e ){
        return false;
    }
}
0
priti On
        @Override
        public void onCancel() {

            accessToken = AccessToken.getCurrentAccessToken();
            if (accessToken == null) {
                Toast.makeText(LoginActivity.this, "Login unSuccessful..Please contact developer... ", Toast.LENGTH_LONG).show();
            } else {

                Toast.makeText(LoginActivity.this, "Login Successful. ", Toast.LENGTH_LONG).show();

            }
        }

        @Override
        public void onError(FacebookException error) {
            Toast.makeText(getApplicationContext(), "Login attempt failed.", Toast.LENGTH_SHORT).show();
        }
  • List item