Failed to call Facebook call LoginButton

I am trying to implement a simple facebook account in my android application. When I click on the login button, the application is redirected to the facebook page, after entering the credentials it returns to my application again. But LoginButton callbacks functions are not called. Although there were a few questions like this, they had a separate class of fragments. However, I do everything from the main activity. Below 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); } } 
+5
source share
2 answers

You forgot to override the onActivityResult() method. onActivityResult() calls the callback methods.

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

+10
source

You are missing the 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:

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

+2
source

Source: https://habr.com/ru/post/1240181/


All Articles