Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook LoginButton callback not getting called

I am trying to implement a simple facebook login on my android app. When I click the login button, the app is redirected to the facebook page, after entering the credentials, it is again returned to my app. But the LoginButton callbacks functions are not called. Though there were a few questions similar to this, but they had a separate fragment class. However, I am doing everything from the main Activity. The following is my code:

package com.example.ankur.facebookdemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

public class MainActivity extends AppCompatActivity {
    private LoginButton loginButton;
    CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_main);

        loginButton = (LoginButton) findViewById(R.id.login_button);
        if (loginButton == null) {
            Log.v("CheckLogin", "null");
        }
        else {
            Log.v("CheckLogin", "not null");
        }


        loginButton.setReadPermissions("user_friends");

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                Toast.makeText(getApplicationContext(),"Fb Login Success", Toast.LENGTH_LONG);
                Log.v("CheckLogin", "successfully connected to facebook");
            }

            @Override
            public void onCancel() {
                // App code
                Toast.makeText(getApplicationContext(),"Fb on cancel",Toast.LENGTH_LONG);
                Log.v("CheckLogin", " connection to facebook cancelled");

            }

            @Override
            public void onError(FacebookException exception) {
                // App code
                Toast.makeText(getApplicationContext(),"Fb Login Error",Toast.LENGTH_LONG);
                Log.v("CheckLogin", "Error on  connection to facebook");
            }
        });

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
like image 468
Ankur Bhatia Avatar asked Jan 08 '16 17:01

Ankur Bhatia


2 Answers

You forgot to override the onActivityResult() method. onActivityResult() results in the callback methods being called.

Do something like this :

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

Hope this helps! All the best :)

like image 68
Narayan Acharya Avatar answered Oct 02 '22 01:10

Narayan Acharya


You're missing onActivityResult hook:

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

The documentation clearly states that:

Every activity and fragment that you integrate with the FacebookSDK Login or Share should forward onActivityResult to the callbackManager.

like image 25
miensol Avatar answered Oct 02 '22 03:10

miensol