Android Facebook SDK: Check if the user is logged in or not

Jay Sidri picture Jay Sidri · Sep 13, 2012 · Viewed 40.4k times · Source

I'm have a feature on my Android app where the user authorizes the app and shares a link.

I also need to give an option for the user to logout of facebook and I need to conditionally disable this button if the user is not logged int (or not authorized the app).

I can't seem to find the API call on the Android SDK that would let me ask FB if the user is logged in or not.

What I have found is getAccessExpires():

Retrieve the current session's expiration time (in milliseconds since Unix epoch), or 0 if the session doesn't expire or doesn't exist.

Will checking if the session equals 0 be the way to go? Or is there something I'm missing?

Answer

Diljeet picture Diljeet · Mar 31, 2015

Facebook SDK 4.x versions have a different method now:

boolean loggedIn = AccessToken.getCurrentAccessToken() != null;

or

by using functions

boolean loggedIn;
//...
loggedIn = isFacebookLoggedIn();
//...
public boolean isFacebookLoggedIn(){
    return AccessToken.getCurrentAccessToken() != null;
}

Check this link for better reference https://developers.facebook.com/docs/facebook-login/android check this heading too "Access Tokens and Profiles" it says "You can see if a person is already logged in by checking AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile()