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

All we need is an easy explanation of the problem, so here it is.

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!

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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;
}

Method 2

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! =]

Method 3

My dilemma of using AccessToken and AccessTokenTracker for checking login status is that when AccessToken is ready and callback function of the tracker called but profile may be not ready yet, thus I cannot get or display Facebooker’s name at that moment.

My solution is to check current profile != null and use its tracker for having Facebooker’s name at the same time:

    ProfileTracker fbProfileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
            // User logged in or changed profile
        }
    };

Check login status and then get user name:

Profile profile = Profile.getCurrentProfile();
if (profile != null) {
    Log.v(TAG, "Logged, user name=" + profile.getFirstName() + " " + profile.getLastName());
}

Method 4

You can use the same way Felipe mentioned in his answer or you can use the other two ways. But it seems AccessTokenTracker is the convenient way, since it helps you to track the access tokens (use with ProfileTracker class)

  1. If you are using a custom button for your login use LoginManager call back

For example

In your layout xml

    <Button
        android:id="@+id/my_facebook_button"
        android:background="@drawable/btnfacebook"
        android:onClick="facebookLogin"/>

In your Activity

    //Custom Button
    Button myFacebookButton = (Button) findViewById(R.id.my_facebook_button);

The button onclick Listener

public void facebookLogin(View view) {
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
    }

At the end the LoginManager Callback

 //Create callback manager to handle login response
        CallbackManager callbackManager = CallbackManager.Factory.create();

       LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
           @Override
           public void onSuccess(LoginResult loginResult) {
               Log.i(TAG, "LoginManager FacebookCallback onSuccess");
               if(loginResult.getAccessToken() != null) {
                   Log.i(TAG, "Access Token:: " + loginResult.getAccessToken());
                   facebookSuccess();
               }
           }

           @Override
           public void onCancel() {
               Log.i(TAG, "LoginManager FacebookCallback onCancel");
           }

           @Override
           public void onError(FacebookException e) {
               Log.i(TAG, "LoginManager FacebookCallback onError");
           }
       });
  1. If you are using the button (com.facebook.login.widget.LoginButton) provided in SDK use LoginButton callback (This is crealy detailed in their reference doc – https://developers.facebook.com/docs/facebook-login/android/v2.3)

For example

In your layout xml

<com.facebook.login.widget.LoginButton
                android:id="@+id/login_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"/>

In your activity

    //Facebook SDK provided LoginButton
    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends");
    //Callback registration
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // App code
            Log.i(TAG, "LoginButton FacebookCallback onSuccess");
            if(loginResult.getAccessToken() != null){
                Log.i(TAG, "Access Token:: "+loginResult.getAccessToken());
                facebookSuccess();
            }

        }

        @Override
        public void onCancel() {
            // App code
            Log.i(TAG, "LoginButton FacebookCallback onCancel");
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
            Log.i(TAG, "LoginButton FacebookCallback onError:: "+exception.getMessage());
            Log.i(TAG,"Exception:: "+exception.getStackTrace());
        }
    });

Dont forget to call callbackManager.onActivityResult(requestCode, resultCode, data); in your Activity onActivityResult()

Method 5

According to facebook documentation you can do this by:

AccessToken accessToken = AccessToken.getCurrentAccessToken();
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();

Method 6

Its late to reply, but now in version 4.25.0 of Facebook SDK there is a method:

public void retrieveLoginStatus(Context context,
                                LoginStatusCallback responseCallback)

Which states:

Retrieves the login status for the user. This will return an access
token for the app if a user is logged into the Facebook for Android
app on the same device and that user had previously logged into the
app. If an access token was retrieved then a toast will be shown
telling the user that they have been logged in.

And can be used like:

LoginManager.getInstance().retrieveLoginStatus( this, new LoginStatusCallback()
{
    @Override
    public void onCompleted( AccessToken accessToken )
    {
        GraphRequest request = GraphRequest.newMeRequest( accessToken, new GraphRequest.GraphJSONObjectCallback()
        {
            @Override
            public void onCompleted( JSONObject object, GraphResponse response )
            {
                Log.e( TAG, object.toString() );
                Log.e( TAG, response.toString() );

                try
                {
                    userId = object.getString( "id" );
                    profilePicture = new URL( "https://graph.facebook.com/" + userId + "/picture?width=500&height=500" );
                    Log.d( "PROFILE_URL", "url: " + profilePicture.toString() );
                    if ( object.has( "first_name" ) )
                    {
                        firstName = object.getString( "first_name" );
                    }
                    if ( object.has( "last_name" ) )
                    {
                        lastName = object.getString( "last_name" );
                    }
                    if ( object.has( "email" ) )
                    {
                        email = object.getString( "email" );
                    }
                    if ( object.has( "birthday" ) )
                    {
                        birthday = object.getString( "birthday" );
                    }
                    if ( object.has( "gender" ) )
                    {
                        gender = object.getString( "gender" );
                    }

                    Intent main = new Intent( LoginActivity.this, MainActivity.class );
                    main.putExtra( "name", firstName );
                    main.putExtra( "surname", lastName );
                    main.putExtra( "imageUrl", profilePicture.toString() );
                    startActivity( main );
                    finish();
                }
                catch ( JSONException e )
                {
                    e.printStackTrace();
                }
                catch ( MalformedURLException e )
                {
                    e.printStackTrace();
                }

            }
        } );
        //Here we put the requested fields to be returned from the JSONObject
        Bundle parameters = new Bundle();
        parameters.putString( "fields", "id, first_name, last_name, email, birthday, gender" );
        request.setParameters( parameters );
        request.executeAsync();
    }

    @Override
    public void onFailure()
    {
        Toast.makeText( LoginActivity.this, "Could not log in.", Toast.LENGTH_SHORT ).show();
    }

    @Override
    public void onError( Exception exception )
    {
        Toast.makeText( LoginActivity.this, "Could not log in.", Toast.LENGTH_SHORT ).show();
    }
} );

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply