Danilo Setton Danilo Setton - 2 years ago 129
Android Question

Programmatically clear Facebook SDK 4.0 session on Android

I integrated Facebook login into my app, and It works fine. The issue is with the logout.

When I open the Facebook app on my device, I can perform a logout, so that the next time I open It, It will ask my If I want to login with my usual account, login with another account or even create a new account. Good, that is expected.

But this doesn't happen with my app. I mean, If the user opens my app, clicks in "logout" and the code below is ran

// Initialize Facebook SDK on the beginning.
FacebookSdk.sdkInitialize(this.getApplicationContext());
...
// Logout on user choice.
LoginManager.getInstance().logOut();


the next time he is back on the app and clicks to login he will be logged in directly with his account, he won't be asked with each account he wants to login.

I image that I need to clear all informations (tokens?) that are saved, which are being used into this directly login. Is this correct? If yes, how can I do it?

Thank you,

Answer Source

I am using this method. It's for SDK 4.6.0, but I guess it should be the same as 4.0. If not, just upgrade ;)

public void logoutFromFacebook(final LogoutFacebookListener listener) {

        if (AccessToken.getCurrentAccessToken() == null) {
            // already logged out
            listener.onLoggedOutFromFacebook();
            return;
        }

        new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
                .Callback() {

            @Override
            public void onCompleted(GraphResponse graphResponse) {

                LoginManager.getInstance().logOut();
                listener.onLoggedOutFromFacebook();
            }
        }).executeAsync();
    }

Listener:

public interface LogoutFromFacebookListener {

    void onLoggedOutFromFacebook();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download