简体   繁体   中英

Facebook Android SDK Automatically Logging In

I am using the following facebook-sdk version:

com.facebook.android:facebook-android-sdk:4.+

I use the default facebook login button for the login view

<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="85dp" />`

With the following Java code

 LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList(permissions));
 LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
       //details intentionally hidden
 }

But for logging out, I use a custom button where on click I do the following

LoginManager.getInstance().logOut();
AccessToken.setCurrentAccessToken(null);

But the Facebook login button re logs in without being clicked! This just loops continuously!

What should I do?

You need to register the callback with LoginButton so it only calls when button is clicked.

use below code: in OnCreate()

 loginButton = (LoginButton) findViewById(R.id.login_button);
 List<String> permissionNeeds = Arrays.asList( "public_profile", "email", "user_birthday", "user_friends");
loginButton.setReadPermissions(permissionNeeds);

loginButton.registerCallback(callbackManager,
        new FacebookCallback<LoginResult>() {

         @Override
            public void onSuccess(LoginResult loginResult) { and so on }

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