118

Recently, Facebook released SDK 4 with new and cool updates. I tried to switch into SDK4 to use new features, however, I am struggling with the Login feature of Facebook.

So far, to log out Facebook programmatically, I used :

Session session = Session.getActiveSession();
session.closeAndClearTokenInformation();

But SDK4 seems not to support Session anymore, and in official docs, they mention:

There are two ways to implement Facebook login on Android:

LoginButton class - Which provides a button you can add to your UI. It follows the current access token and can log people in and out.

Well, seems there's no way to log out Facebook programmatically except using LoginButton. Anyone have any idea, please share it here.

franco phong
  • 2,219
  • 3
  • 26
  • 43

6 Answers6

289

You can use LoginManager.getInstance().logOut();, even if you use LoginButton because

This UI element wraps functionality available in the LoginManager.

EDIT: Just to mention that this works for Facebook SDK v4. I don't know if they will change it in the future.

@as batoutofhell mention, don't forget to put FacebookSdk.sdkInitialize(getApplicationContext()); to initialize the facebook sdk. Please see here for the details.

stackex
  • 3,205
  • 1
  • 11
  • 16
  • 3
    I want to mention since this is the top google result, my app crashed when trying to log out until I added this line first: FacebookSdk.sdkInitialize(this.getApplicationContext()); LoginManager.getInstance().logOut(); – batoutofhell May 26 '15 at 16:18
  • 7
    After logout it won't allow me to Login again. Is there any way to login again after I did LoginManager.getInstance().logOut(); – AndroidDev Aug 03 '15 at 10:22
  • Thanks a lot @stackex , this was very helpful and time saving – Antroid Sep 27 '16 at 03:26
  • 1
    Why should we call FacebookSdk.sdkInitialize, as it has already been initialized in onCreate? – CoolMind Jan 23 '17 at 15:39
  • its not working for me ..... in facebook once login via my app but i m trying to logout user but again clicking on facebook button its showing that you are logged in ..... do u want to logout or cancel why ? – Erum Jan 26 '17 at 08:02
  • @erum did you figure out your problem? I am having the same one. – MortalMan Mar 07 '17 at 01:09
  • 1
    LoginManager.logOut() i used this , it works properly – Erum Mar 07 '17 at 05:01
  • 1
    @Erum There is no such method `LoginManager.logOut()` as of now in FB SDK v4.23.0 – Gem Jul 22 '17 at 17:45
  • That works, unlike FirebaseAuth.getInstance().signOut(). The last is working for google sign in only for some reason... – Liker777 Apr 18 '21 at 05:48
68

SDK4, if you want to completely de-couple, make sure you also remove the app from the user's facebook account. This method disconnects the user completely:

public void disconnectFromFacebook() {

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

    new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
            .Callback() {
        @Override
        public void onCompleted(GraphResponse graphResponse) {

            LoginManager.getInstance().logOut();

        }
    }).executeAsync();
}
Frank
  • 12,010
  • 8
  • 61
  • 78
  • This is exactly what i wanted! – Sash_KP Jan 14 '16 at 14:46
  • That mean I must remove Facebook App and Facebook message to completed logout for my app ? – mdtuyen Jan 25 '16 at 02:34
  • 1
    This answer ensures the user completly logged out. Just to be 100% it is needed to include FacebookSdk.sdkInitialize(getApplicationContext()); – Sandro Wiggers Aug 25 '16 at 02:59
  • In the callback I also added @Override public void onCompleted(GraphResponse graphResponse) { AccessToken.setCurrentAccessToken(null); LoginManager.getInstance().logOut(); } – Sandro Wiggers Aug 25 '16 at 03:07
  • what does .logOut() do? Is it asynchronous? Will it block my UI thread for some millisecs? I think that .logOutAsync() would make more sense – voghDev Aug 30 '16 at 09:53
  • 1
    @voghDev onCompleted does not come back on the UI thread I believe, so it does not block it when you call logOut there. – Frank Dec 28 '16 at 09:46
  • In my case `onCompleted` hasn't been called, so I used an accepted answer. – CoolMind Jan 23 '17 at 15:38
  • Thank you. This is what exactly I am looking for. – viper Aug 04 '17 at 10:02
9

You can use LoginManager.logOut()

Check out https://developers.facebook.com/docs/reference/android/current/class/LoginManager/

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Chris Pan
  • 1,903
  • 14
  • 16
8

To handle it with the loginButton:

//Check if user is currently logged in
        if (AccessToken.getCurrentAccessToken() != null && com.facebook.Profile.getCurrentProfile() != null){
            //Logged in so show the login button
            fbLogin.setVisibility(View.VISIBLE);
            fbLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//log out
                    LoginManager.getInstance().logOut();
                    gotoLogin();
                }
            });
        }
tread
  • 10,133
  • 17
  • 95
  • 170
  • Hey Stevie, When i called logOut() method as you have mentioned in your answer then it gave me error "java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0;". It is asking me to add a permission in manifest file. How can i resolve it because i dont want to give this permission as logout should be normal procedure. – Kapil Jun 08 '15 at 12:26
  • @Aditya You only need the internet permission for the App. `` – tread Jun 09 '15 at 11:29
  • But i am getting above error, if i dont add it. If possible, can you please share a facebook sample which has custom login button and custom logout button ?? – Kapil Jun 09 '15 at 11:55
  • @Aditya in this example I am using the facebook Login button – tread Jun 09 '15 at 12:15
5

You can logout by using LoginManager but you have to use graph request also. I am talking about log out completely so, that next time you can login with different account.

new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
            .Callback() {
        @Override
        public void onCompleted(GraphResponse graphResponse) {

            SharedPreferences pref = DashBoard.this.getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.clear();
            editor.commit();
            LoginManager.getInstance().logOut();

            Intent logoutint = new Intent(DashBoard.this,MainActivity.class);
            logoutint.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               startActivity(logoutint);

        }
    }).executeAsync();

By the help of shared preferences here you can logout completely, and next time you can login with different account.

user3678528
  • 1,741
  • 2
  • 18
  • 24
Ritesh Jha
  • 69
  • 1
  • 4
1

Frank version kotlin:

 fun disconnectFromFacebook() {
    if (AccessToken.getCurrentAccessToken() == null) {
        return  // already logged out
    }
    GraphRequest(
        AccessToken.getCurrentAccessToken(),
        "/me/permissions/",
        null,
        HttpMethod.DELETE,
        GraphRequest.Callback {
            LoginManager.getInstance().logOut()
        }).executeAsync()
}
Andrea Leganza
  • 447
  • 7
  • 7