Facebook 处理程序未被调用

Facebook handler not get called

从主 activity 开始 activity:

Intent intent = new Intent(MainActivity.this, FacebookActivity.class);
startActivity(intent);

正在定义处理程序:

public class FacebookActivity extends Activity {

    LoginButton loginButton;
    CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_facebook);

        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("public_profile"));
        callbackManager = CallbackManager.Factory.create();

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

但是没有调用处理程序。我在控制台中看到了这个:

calling onResume for activity

知道了,问题是什么?在早期版本中,我没有使用单独的 activity 进行 facebook 登录,并且登录处理程序有效。奇怪。

setContentView

之前调用以下代码
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();

您必须初始化 Facebook-Sdk

在此行之前添加这些行

setContentView(R.layout.activity_facebook);

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

步骤 1

在初始化之前先初始化facebook SDKCallbackManager

FacebookSdk.sdkInitialize(getApplicationContext());

步骤 2

然后在onActivityResult()中将登录结果转发给onCreate()中创建的callbackManager

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