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

I have an Android app and I am trying to use facebook‘s sdk (version 4.1.0) to get a token and login. 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.

  • Accessing Android Contact Group Names
  • how to clear heap memory of eclipse
  • Android: API level VS. Android version
  • Issue with Facebook request in Android
  • How to pop fragment off backstack
  • Android Camera Preview — How to 'freeze' the camera?
  • Related posts:

    Custom onDraw() method not called
    &ldquo;Service MeasurementBrokerService is in use&rdquo; is showing in my application proces...
    Processing Android video frame by frame while recording
    Listview - Footer at the bottom of screen
    Android Bluetooth status 133 in onCharacteristicwrite
    Android Layout : How to implement a UI similar to deck of cards?
  • How to get View in RecyclerView.Adapter for Snackbar?
  • Android GCM sent successfully but not received on some devices
  • When to clear the cache dir in Android?
  • Eclipse ADT: Project name “appcompat_v7” being created after creating every new project
  • Ignored parameters in Android SDK v4
  • how to update gallery after moving photo programmatically?
  • 6 Solutions collect form web for “FacebookCallback.onCancel is getting called when trying to login using facebook sdk”

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

     LoginManager.getInstance().logOut();
    

    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();
    

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

    @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).

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

    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;
        }
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.