Login button facebook android doesn't redirect to new activity

橙三吉。 提交于 2019-12-04 10:50:08

For the login to work, you have to remove the onActivityResult() method from inside the FacebookCallback anomymous class. The correct code should look like this:

private CallbackManager callbackManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    FacebookSdk.sdkInitialize(getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("public_profile", "email", "user_friends");

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Intent i = new Intent(Login.this, MainActivity.class);
            startActivity(i);
            System.out.print("Logged in");
        }

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

        }

        @Override
        public void onError(FacebookException exception) {
            // App code
            Log.i("Error" , "Error");
        }
    });
}

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

First of all, insert private private CallbackManager callbackManager; into your code. It should look something like this:

callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);

Then add FacebookSdk.sdkInitialize(getApplicationContext());

before super.onCreate(savedInstanceState);

Try this Hope this works

Aman Dharna

The following worked for me:

Added:

FacebookSdk.sdkInitialize(getApplicationContext());

Before:

super.onCreate(savedInstanceState);

It was strange, for everything was working fine earlier; but suddenly, I had this issue. Now, it's fixed with the aforementioned solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!