Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

A few days ago I implemented FB Login to my APP, and today I found out that most of the things I have implemented are now deprecated.

Before, I was using Session to see if the user was logged in or not. However, that doesn't work with the new SDK.

According to their docs, we can use AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile() to check if the user is already logged in, but I could not make use of those.

I tried something like this:

if(AccessToken.getCurrentAccessToken() == null) 

I wonder if that would work if I could use it inside of this (which is also provided by FB):

LoginManager.getInstance().registerCallback(callbackManager, new LoginManager.Callback() {...}); 

However, I get a "Cannot resolve symbol 'Callback'".

EDIT!!!!!!

Alright, so I was able to check if the user is logged in by using the following:

On onCreate:

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

Then, that calles my updateWithToken method:

private void updateWithToken(AccessToken currentAccessToken) {     if (currentAccessToken != null) {              LOAD ACTIVITY A!      } else {              LOAD ACTIVITY B!     } } 

Now, the problem is: If the user has used the application and hasn logged in before, I can check for that! But if it is the first time that the user is using the app, updateWithToken is never called by my AccessTokenTracker.

I'd really appreciate if someone could help.

Thanks!

like image 312
Felipe Avatar asked Mar 27 '15 05:03

Felipe


People also ask

How do I logout of Facebook SDK on Android?

So far, to log out Facebook programmatically, I used : Session session = Session. getActiveSession(); session. closeAndClearTokenInformation();

How do I check my Facebook login status?

Click on "Active Sessions," which is located towards the bottom of the Privacy settings page. This brings up a list of your current and past Facebook logins, including the location where the login took place, the type of device that was used to access the site, and the day and time of the login.

How can I tell who is logged into an android app?

To check if the user is signed-in, call isConnected(). if (mGoogleApiClient != null && mGoogleApiClient. isConnected()) { // signed in.

How did you integrate the Facebook SDK in Android app?

To use the Facebook SDK in an Android Studio project, add the SDK as a build dependency and import the SDK. Go to Android Studio | New Project | Minimum SDK. Select API 15: Android 4.0. 3 (IceCreamSandwich) or higher and create your new project.


1 Answers

A much simpler solution worked for my case (I don't know if this is the more elegant way though):

public boolean isLoggedIn() {     AccessToken accessToken = AccessToken.getCurrentAccessToken();     return accessToken != null; } 
like image 109
Felipe Mosso Avatar answered Oct 30 '22 22:10

Felipe Mosso