How to Check If User Is Logged in with Fb Sdk 4.0 for Android

How to check if user is logged in with FB SDK 4.0 for Android?

I got it!

First, make sure you have initialized your FB SDK. Second, add the following:

accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
updateWithToken(newAccessToken);
}
};

This will be called when there's a change on the Current Access Tokes. Meaning, this will only help you if the user is already logged in.

Next, we add this to our onCreate() method:

updateWithToken(AccessToken.getCurrentAccessToken());

Then of course, our updateWithToken() method:

private void updateWithToken(AccessToken currentAccessToken) {

if (currentAccessToken != null) {
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
Intent i = new Intent(SplashScreen.this, GeekTrivia.class);
startActivity(i);

finish();
}
}, SPLASH_TIME_OUT);
} else {
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
Intent i = new Intent(SplashScreen.this, Login.class);
startActivity(i);

finish();
}
}, SPLASH_TIME_OUT);
}
}

That did it for me! =]

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

I struggled to find a simple answer to this in the FB docs. Using the Facebook SDK version 3.0 I think there are two ways to check if a user is logged in.

1) Use Session.isOpened()

To use this method you need to retrieve the active session with getActiveSession() and then (here's the confusing part) decipher if the session is in a state where the user is logged in or not. I think the only thing that matters for a logged in user is if the session isOpened(). So if the session is not null and it is open then the user is logged in. In all other cases the user is logged out (keep in mind Session can have states other than opened and closed).

public boolean isLoggedIn() {
Session session = Session.getActiveSession();
return (session != null && session.isOpened());
}

There's another way to write this function, detailed in this answer, but I'm not sure which approach is more clear or "best practice".

2) Constantly monitor status changes with Session.StatusCallback and UiLifecycleHelper

If you follow this tutorial you'll setup the UiLifecycleHelper and register a Session.StatusCallback object with it upon instantiation. There's a callback method, call(), which you override in Session.StatusCallback which will supposedly be called anytime the user logs in/out. Within that method maybe you can keep track of whether the user is logged in or not. Maybe something like this:

private boolean isLoggedIn = false; // by default assume not logged in

private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) { //note: I think session.isOpened() is the same
isLoggedIn = true;
} else if (state.isClosed()) {
isLoggedIn = false;
}
}
};

public boolean isLoggedIn() {
return isLoggedIn;
}

I think method 1 is simpler and probably the better choice.

As a side note can anyone shed light on why the tutorial likes to call state.isOpened() instead of session.isOpened() since both seem to be interchangeable (session.isOpened() seems to just call through to the state version anyway).

Android Facebook SDK 4.0 Login: Error when user is logged in Facebook App

Go to your Facebook Apps and select your APP and click on the Status & review then select and change it to YES for Do you want to make this app and all its live features available to the general public?

Sample Image

How i get, is user login or logout in facebook sdk 4.0.1

You could check when the token changes, and if the new access token is null, the user just logged out.

new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
if (newAccessToken==null)
//your code here!
}
};


Related Topics



Leave a reply



Submit