Tracking Access Tokens and Profiles in Facebook Login for Android

If you want your app to keep up with the current access token and profile, you can implement AccessTokenTracker and ProfileTracker classes.

These classes call your code when access token or profile changes happen. Internally they use receivers, so you need to call stopTracking() on an activity or call a fragment's onDestroy() method.

Track Access Tokens

For example to use the AccessTokenTracker instead of using a login callback:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    callbackManager = CallbackManager.Factory.create();

    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(
            AccessToken oldAccessToken,
            AccessToken currentAccessToken) {
                // Set the access token using 
                // currentAccessToken when it's loaded or set.
        }
    };
    // If the access token is available already assign it.
    accessToken = AccessToken.getCurrentAccessToken();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onDestroy() {
    super.onDestroy();
    accessTokenTracker.stopTracking();
}

Track Current Profile

You can use ProfileTracker in a similar way to track changes in the current profile:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    callbackManager = CallbackManager.Factory.create();

    profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(
                Profile oldProfile,
                Profile currentProfile) {
            // App code
        }
    };
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onDestroy() {
    super.onDestroy();
    profileTracker.stopTracking();
}