Android FacebookSDK V4.20 Login and Logout using LoginManager

1.9k views Asked by At

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

2

There are 2 answers

0
uchman21 On BEST ANSWER

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);
}
1
Raghav Chawla On

The complete code is below.

public class MainActivity extends ActionBarActivity {

    CallbackManager callbackManager;
    Button but, but2;
    AccessTokenTracker accessTokenTracker;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        but = (Button) findViewById(R.id.button);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();




            LoginManager.getInstance().registerCallback(callbackManager,
                    new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            // App code
                        }

                        @Override
                        public void onCancel() {
                            // App code
                            // savedInstanceState
                        }

                        @Override
                        public void onError(FacebookException exception) {
                            // App code
                        }
                    });
            LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends" ));

     //   }
            accessTokenTracker = new AccessTokenTracker() {
                @Override
                protected void onCurrentAccessTokenChanged(
                        AccessToken oldAccessToken,
                        AccessToken currentAccessToken) {

                }
            };

            but.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getUserData(AccessToken.getCurrentAccessToken());
                    Log.d("Access Token: ", AccessToken.getCurrentAccessToken().toString());
                }
            });
        }


    public void getUserData(AccessToken accessToken){
        GraphRequest request = GraphRequest.newMeRequest(
                accessToken,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {
                        //JSONParser parser=new JSONParser();
                      //  Data data = new Gson().fromJson(json, Data.class);
                        long ID = 0;

                try {
                    String name = response.getRawResponse();
                    Toast.makeText(MainActivity.this, "response is: "+object.toString(), Toast.LENGTH_LONG).show();
                }
                catch (Exception e){
                    Toast.makeText(MainActivity.this, "error is: "+e.toString(), Toast.LENGTH_LONG).show();
                }

                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link");
        request.setParameters(parameters);
        request.executeAsync();
    }

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        accessTokenTracker.stopTracking();
    }
}