Home How to programmatically log out from Facebook SDK 3.0 without using Facebook login/logout button?
Reply: 9

How to programmatically log out from Facebook SDK 3.0 without using Facebook login/logout button?

Michael Eilers Smith
Michael Eilers Smith Published in 2013-01-14 22:43:00Z

The title says it all. I'm using a custom button to fetch the user's facebook information (for "sign up" purposes). Yet, I don't want the app to remember the last registered user, neither the currently logged in person via the Facebook native app. I want the Facebook login activity to pop up each time. That is why I want to log out any previous users programmatically.

How can I do that? This is how I do the login:

private void signInWithFacebook() {

    SessionTracker sessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() 
        public void call(Session session, SessionState state, Exception exception) { 
    }, null, false);

    String applicationId = Utility.getMetadataApplicationId(getBaseContext());
    mCurrentSession = sessionTracker.getSession();

    if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
        Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
        mCurrentSession = session;

    if (!mCurrentSession.isOpened()) {
        Session.OpenRequest openRequest = null;
        openRequest = new Session.OpenRequest(RegisterActivity.this);

        if (openRequest != null) {

    }else {
        Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
              public void onCompleted(GraphUser user, Response response) {
                  fillProfileWithFacebook( user );

Ideally, I would make a call at the beginning of this method to log out any previous users.

ViliusK Reply to 2015-10-14 14:00:27Z

Update for latest SDK:

Now @zeuter's answer is correct for Facebook SDK v4.7+:


Original answer:

Please do not use SessionTracker. It is an internal (package private) class, and is not meant to be consumed as part of the public API. As such, its API may change at any time without any backwards compatibility guarantees. You should be able to get rid of all instances of SessionTracker in your code, and just use the active session instead.

To answer your question, if you don't want to keep any session data, simply call closeAndClearTokenInformation when your app closes.

jpotts18 Reply to 2013-12-19 20:35:20Z

Here is snippet that allowed me to log out programmatically from facebook. Let me know if you see anything that I might need to improve.

private void logout(){
    // clear any user information
    // find the active session which can only be facebook in my app
    Session session = Session.getActiveSession();
    // run the closeAndClearTokenInformation which does the following
    // DOCS : Closes the local in-memory Session object and clears any persistent 
    // cache related to the Session.
    // return the user to the login screen
    startActivity(new Intent(getApplicationContext(), LoginActivity.class));
    // make sure the user can not access the page after he/she is logged out
    // clear the activity stack
Rikin Prajapati
Rikin Prajapati Reply to 2013-09-03 05:41:53Z

This method will help you to logout from facebook programmatically in android

 * Logout From Facebook 
public static void callFacebookLogout(Context context) {
    Session session = Session.getActiveSession();
    if (session != null) {

        if (!session.isClosed()) {
            //clear your preferences if saved
    } else {

        session = new Session(context);

            //clear your preferences if saved


selva_pollachi Reply to 2014-08-22 10:01:51Z
private Session.StatusCallback statusCallback = new SessionStatusCallback();

logout.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Session.openActiveSession(this, true, statusCallback);  

private class SessionStatusCallback implements Session.StatusCallback {
public void call(Session session, SessionState state,
Exception exception) {
Sufian Reply to 2015-09-21 07:22:59Z

Since Facebook's Android SDK v4.0 (see changelog) you need to execute the following:

luizfelippe Reply to 2015-04-29 20:43:17Z

Session class has been removed on SDK 4.0. The login magement is done through the class LoginManager. So:

mLoginManager = LoginManager.getInstance();

As the reference Upgrading to SDK 4.0 says:

Session Removed - AccessToken, LoginManager and CallbackManager classes supercede and replace functionality in the Session class.

user2906641 Reply to 2015-07-02 21:21:11Z

Yup, As @luizfelippe mentioned Session class has been removed since SDK 4.0. We need to use LoginManager.

I just looked into LoginButton class for logout. They are making this kind of check. They logs out only if accessToken is not null. So, I think its better to have this in our code too..

AccessToken accessToken = AccessToken.getCurrentAccessToken();
if(accessToken != null){
Aniket Thakur
Aniket Thakur Reply to 2015-08-09 10:39:51Z

Since Facebook's Android SDK v4.0 you need to execute the following:


This is not sufficient. This will simply clear cached access token and profile so that AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile() will now become null.

To completely logout you need to revoke permissions and then call LoginManager.getInstance().logOut();. To revoke permission execute following graph API -

    GraphRequest delPermRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), "/{user-id}/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
        public void onCompleted(GraphResponse graphResponse) {
                FacebookRequestError error =graphResponse.getError();
                    Log.e(TAG, error.toString());
                }else {
    Log.d(TAG,"Executing revoke permissions with graph path" + delPermRequest.getGraphPath());
aravindkanna Reply to 2016-06-22 12:28:59Z

Facebook provides two ways to login and logout from an account. One is to use LoginButton and the other is to use LoginManager. LoginButton is just a button which on clicked, the logging in is accomplished. On the other side LoginManager does this on its own. In your case you have use LoginManager to logout automatically.

LoginManager.getInstance().logout() does this work for you.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.342561 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO