简体   繁体   中英

Android & Facebook: How to login with facebook after buttonClick?

When I start the Login2Activity it will open the screen and after a second it will directly open a facebook login popup. I want that the facebook login popup will open after clicking on the LoginButton. How can I fix this?

    public class Login2Activity extends AppCompatActivity {

            private LoginButton loginButton;
            private CallbackManager callbackManager;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                callbackManager = CallbackManager.Factory.create();
                setContentView(R.layout.activity_login2);
                TextView mTitle = (TextView)findViewById(R.id.textView3);
                mTitle.setTypeface(Typeface.createFromAsset(getApplication().getAssets(), "fonts/HELVETICANEUELTSTD-TH.ttf"));


                loginButton = (LoginButton)findViewById(R.id.login_btn);
                LoginManager.getInstance().logInWithReadPermissions(
                        this,
                        Arrays.asList("user_friends"));

                loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        Log.d("tagger", "FB onSucces");
                        GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject object, GraphResponse response) {
                                        Intent x = new Intent(Login2Activity.this, AddActivity.class);
                                        startActivity(x);
                                    }
                                });

                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,first_name,last_name,email,gender,birthday,picture,friends");
                        request.setParameters(parameters);
                        request.executeAsync();
                    }

                    @Override
                    public void onCancel() {
                        Log.d("tagger", "FB onCancel");
                        Toast.makeText(Login2Activity.this, "Facebook login Cancel", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(FacebookException e) {
                        Log.d("tagger", "FB onError");
                        Toast.makeText(Login2Activity.this, "There is a facebook error", Toast.LENGTH_SHORT).show();
                    }
                });
            }
            @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent        data) 
        {
        callbackManager.onActivityResult(requestCode, resultCode, data);
        }

}

I don't know how your code is working without initializing the facebook sdk. Here is the code I am implementing which is implementing facebook login only on button click. Facebooksdk should be initialized before setContView. I believe your code may not be working properly because of the order of instructions.

    //inside on create
    FacebookSdk.sdkInitialize(this.getApplicationContext());
    setContentView(R.layout.activity_login);

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

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
       ...rest of the success/error/cancel code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM