0

I'm updating an app that use facebook sdk, but i'm facing some problems, and official documentation is too poor. On my app, user can log in with facebook or with a normal account (stored on my server), and this options are showed on app startup. Obviously user can also log out and log in with another account (facebook or not), and i have a problem with facebook logout. In fact i'm not able to logout user connected with facebook account. As i've noticed after a lot of attempts, all changes about facebook status are tracked by AccessTokenTracker and ProfileTracker, that should be instantiated only once at startup. I show (and explain) my code. This is code of my login FragmentActivity that check if user was already logged in (with facebook or with dedicated account), and if yes, show next activity, else show fragment for choose access options:

@Override
protected void onCreate(Bundle savedInstanceState) {
    FacebookSdk.sdkInitialize(this.getApplicationContext());
    callbackManager = CallbackManager.Factory.create();

        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                if (currentAccessToken != null) {
                    Log.i("LOGINACTIVITY", "token tracker, current token valid");
                    AccessToken token = AccessToken.getCurrentAccessToken();
                    //already logged with facebook, show next activity

                } else {
                    //check if current visible activity is logout activity
                    // that contains logout button
                    ActivityManager am = (ActivityManager) LoginActivity.this.getSystemService(ACTIVITY_SERVICE);
                    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);

                    String top_activity = taskInfo.get(0).topActivity.getClassName();

                    if (top_activity.equals(getApplicationContext().getPackageName() + ".LogoutActivity")) {
                        //launch new login activity
                        LoginManager.getInstance().logOut();
                        getApplicationContext().startActivity(new Intent(getApplicationContext(),
                                LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

                    }

                }
            }
        };

        accessTokenTracker.startTracking();

Now it happen that, when i press logout button, accesstoken tracker execute else branch so show login options it's showed, but for some reason app automatically login again with facebook (it's invoked if branch of TokenTracker), so user is not able to logout from facebook. What's wrong?

4
  • What does your LoginActivity do?
    – Ming Li
    Commented Apr 8, 2015 at 16:42
  • LoginActivity contains that code, and show login fragment (if user isn't logged in yet) or show next activity if user was already logged in
    – giozh
    Commented Apr 9, 2015 at 7:34
  • Can you post your full LoginActivity? I can't figure out what your code is trying to do here.
    – Ming Li
    Commented Apr 9, 2015 at 16:24
  • Thanks for the command: "LoginManager.getInstance().logOut();" you give me the life XD
    – KryNaC
    Commented Jun 16, 2015 at 12:31

1 Answer 1

2

Make sure to stop access token tracker before logging out to avoid getting onCurrentAccessTokenChanged(...) called with a null currentAccessToken, which will cause - According to your code - to execute else clause.

accessTokenTracker.stopTracking();
LoginManager.getInstance().logOut();

And BTW, you don't have to use startTracking() right after executing new AccessTokenTracker(), as AccessTokenTracker() implements startTracking()

AccessTokenTracker.class

public AccessTokenTracker() {
    Validate.sdkInitialized();
    this.receiver = new AccessTokenTracker.CurrentAccessTokenBroadcastReceiver();
    this.broadcastManager = LocalBroadcastManager.getInstance(FacebookSdk.getApplicationContext());
    this.startTracking();
}
7
  • Is the line 'accessTokenTracker.stopTracking();' necessary for logout Facebook?? Only with the line 'LoginManager.getInstance().logOut();' my facebook session log out
    – KryNaC
    Commented Jun 16, 2015 at 12:33
  • It's necessary in his scenario / code flow, but it's not a must in all cases to successfully log out. Commented Jun 16, 2015 at 14:05
  • In my scenario I log into with Facebook and I take the user-name and image profile ... when I log out from my app I must put 'accessTokenTracker.stopTracking();'?? Thanks
    – KryNaC
    Commented Jun 17, 2015 at 8:40
  • No you don't ... If this is only what you do and you don't use accessTokenTracker.StartTracking() you don't have to call stopTracking() Commented Jun 17, 2015 at 17:08
  • 1
    If you are not using / executing any code within accessTokenTracker.startTracking() then don't use it, and if you use it, make sure to call .stopTracking() when user logs out. That's it. Commented Jun 18, 2015 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.