Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect logout event with the Facebook Android API v4?

I post here because I've got a problem. I'm working on a new android application , and I want to know how I can detect when a user is disconnecting (facebook logout button), because I want to refresh my UI at this moment.

I have been watched the official documentation, but I found nothing.

like image 618
outstore Avatar asked May 20 '15 20:05

outstore


People also ask

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.


2 Answers

You can set a listener on the onCreate() method on your activity

AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(
                AccessToken oldAccessToken,
                AccessToken currentAccessToken) {

            if (currentAccessToken == null){
                //User logged out
            }
        }
    };

You need to import com.facebook.AccessToken and com.facebook.AccessTokenTracker

When you create the instance of AccessTokenTracker it implicitly starts tracking. For stopping tracking you should call AccessTokenTracker.stopTracking() e.g. in onDestroy() to not receive anymore events when not needed/wanted and especially to not leak memory!

You can get any time if the user is logged in/out by calling

AccessToken at = AccessToken.getCurrentAccessToken();

If the user is not logged in, you get a null value.

For further reference please check the documentation at https://developers.facebook.com/docs/reference/android/current/class/AccessTokenTracker/

like image 196
Nicolás Arias Avatar answered Oct 11 '22 18:10

Nicolás Arias


You can try this also

 if(AccessToken.getCurrentAccessToken()!=null)
 {
   Log.v("User is login","YES");

 }
else
{
         Log.v("User is not login","OK");
      LoginManager.getInstance().logInWithReadPermissions(WelcomeActivity1.this, (Arrays.asList("public_profile", "user_friends","user_birthday","user_about_me","email")));
 }
like image 42
Anny Avatar answered Oct 11 '22 18:10

Anny