Listen for Facebook logout with the LoginButton. (

2020-04-06 03:04发布

I've been reading and haven't found a solution yet. So here goes.

I'm trying to upgrade to Facebook's new SDK 4.1 Android SDK. I can successfully log in a user using the Facebook's LoginButton using this code: facebookLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>()....

Once a user is logged in, I want to know when a user logs using the same button. The login button changes to say "Log out" when a user is logged in. What I want to know, is how can I listen for a successful log out from the LoginButton? ( I know thatLoginManager.getInstance().logOut(); can log a user out. But what I'm looking for is some sort of callBack which I can handle accordingly.)

1条回答
▲ chillily
2楼-- · 2020-04-06 03:49

Quoting the Facebook SDK Upgrading the Android SDK from 4.0.1 to 4.1.0 docs: "Replacement Classes - Session and UserSettingsFragment have been removed and replaced by the LoginManager and AccessToken classes. LoginActivity is replaced by FacebookActivity."

Because I wanted to mimic the old Sessions changed listener logic, I looked further into the AccessTokenTracker. Here's how I solved my problem. I still use Facebook's LoginButton to Log out the user, but I also include this to listen for a change to a null token. Meaning the user logged out.

private AccessTokenTracker fbTracker;
    ...
    fbTracker = new AccessTokenTracker() {
                @Override
                protected void onCurrentAccessTokenChanged(AccessToken accessToken, AccessToken accessToken2) {
                    if (accessToken2 == null) {
                        Log.d("FB", "User Logged Out.");
                    }
                }
            };

Hope this helps others in the future.

查看更多
登录 后发表回答