67

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!

Felipe
  • 5,126
  • 5
  • 18
  • 21

6 Answers6

142

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;
}
Felipe Mosso
  • 3,907
  • 11
  • 38
  • 61
  • 8
    This should be the accepted solution. Also, I've double checked that it's not expired (`.isExpired()`). – Andre Figueiredo May 11 '15 at 03:58
  • 8
    This doesn't seem to work for me -- it is `null` even when I am logged in. – u3l May 26 '15 at 20:55
  • This is null even when I am logged in too – S-K' Jun 03 '15 at 17:11
  • 2
    The reason AccessToken.getCurrentAccessToken() is null sometimes is onCurrentAccessTokenChanged is called async. You have to wait for it for the first time. Then you can use AccessToken.getCurrentAccessToken() – klimat Jul 01 '15 at 09:51
  • If null, you need to Track Access Tokens https://developers.facebook.com/docs/facebook-login/android/v2.2 – SpyZip Aug 12 '15 at 08:13
  • Knowing that AcessToken.getCurrentAccessToken(); will return null if the user cleared device memory, for example via long press on Menu button and selecting closing all, or any other method. – Ashraf Alshahawy Mar 17 '16 at 00:03
  • Well although this did work for moving to new activity. I don't seem to quite understand why the email and other details don't seem to be displayed. Nevertheless, the details are passed when i login for the first time. – sanjeev May 24 '18 at 07:08
35

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

Felipe
  • 5,126
  • 5
  • 18
  • 21
  • 2
    I am using a different fragment for login purpose and a different fragment for posting, I wonder if I have to save the access token as a global state. – Skynet Apr 30 '15 at 07:34
  • This doesn't works for me. I'm using `LoginButton`but can't receive no one FB callback. – Konstantin Konopko Nov 19 '15 at 15:10
  • @sirvon the problem solved by setting up FB login activity as launch activity – Konstantin Konopko Nov 23 '15 at 08:52
  • thanks - i had a bit of a problem trying to get the fb login accessToken to stick properly and this piece of code allowed it to remember i have logged in. – Simon Nov 28 '15 at 09:16
  • accessTokenTracker code has to be added before onCreate() and FB SDK has to initialized before setContectView() of onCreate()? – Tara Apr 20 '16 at 11:28
9

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());
}
Tony
  • 1,551
  • 20
  • 21
3

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()

binary
  • 1,364
  • 1
  • 15
  • 20
  • 1
    I've done everything in option 2. After I click the FB login button and click ok on their activity my app is relaunched. It hits my splash screen first which moves me to the activity where the login button is. The FB login button still says Log in with Facebook. Any ideas? – RogerSmith Apr 09 '15 at 03:42
  • But it didn't work when I tried to hide a button after getting logout from Facebook. – Anshul Tyagi Oct 06 '15 at 06:19
  • @RogerSmith Also can't get any FB callbacks – Konstantin Konopko Nov 19 '15 at 15:16
  • Dont forget to call callbackManager.onActivityResult(requestCode, resultCode, data); in your Activity onActivityResult() – Mohamed ALOUANE Dec 06 '15 at 14:51
3

According to facebook documentation you can do this by:

AccessToken accessToken = AccessToken.getCurrentAccessToken();
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
Asad
  • 1,241
  • 3
  • 19
  • 32
1

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();
    }
} );
Talha
  • 903
  • 8
  • 31
  • 1
    This only seems to work, according to provided doc, when "user is logged into the Facebook for Android app on the same device". I am afraid we cannot rely on this fact. – zelig74 May 23 '20 at 16:26