Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android FacebookSDK V4.20 Login and Logout using LoginManager

After spending days trying to figure this out, I couldn't come up with anything that worked. Using my code, it shows me the permission form which I accept and log in but it doesn't get to the onSuccess, onError or onCancel. Anytime I click the button, it just doesn't do anything. And no errors on the LogCat. I don't know where i'm going wrong.

fb = (Button) findViewById(R.id.fb_button);
    fb.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
           LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds);
            callbackManager = CallbackManager.Factory.create();

            LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.d("kkkkkk","kkllkl");
                }

                @Override
                public void onCancel() {
                    Log.d("kkkkkk","kkllkl2");
                }

                @Override
                public void onError(FacebookException error) {
                    Log.d("kkkkkk","kkllkl4");
                }
            });


        }
    });

The FacebookSdk.sdkInitialize(this.getApplicationContext()); is initialized after the super.onCreate(savedInstanceState);

like image 232
uchman21 Avatar asked Nov 27 '25 05:11

uchman21


1 Answers

I finally figured it out. I changed the code a bit from the initial code to this:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d("kkkkkk","kkllkl");
        }

        @Override
        public void onCancel() {
            Log.d("kkkkkk","kkllkl2");
        }

        @Override
        public void onError(FacebookException error) {
            Log.d("kkkkkk","kkllkl4");
        }
    });

and then in the button's on click listener, i just had to make the call for the login.

 fb.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds);

        }
    });

also don't forget to add the onActivityResult. I made the mistake also.

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}
like image 174
uchman21 Avatar answered Nov 29 '25 19:11

uchman21



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!