sand_storm_of_code.txt sand_storm_of_code.txt - 1 year ago 960
Android Question

Profile.getCurrentProfile() returns null after logging in (FB API v4.0)

For some reason

shows up null at times right after logging into FaceBook, using FB API v4.0.

This is causing problems for me in my app because I can't display my next
being null.

The reason I say it's null sometimes is because if I close out of my app and re-open it, I'm able to get to my next
, but if I'm not already logged in, and then login,
is null. It seems it is for a short period.

Is there a work around or fix to this?

Answer Source

Like HardiBen Patel said, you have to create an instance of ProfileTracker which will start tracking profile updates, (i.e ProfileTracker.onCurrentProfileChanged() will be called when the user's profile finishes being fetched).

Following is the complete code that you'd need to login to FB and get the user's profile:

LoginButton loginButton = (LoginButton) findViewById(;
mCallbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {

    private ProfileTracker mProfileTracker;

    public void onSuccess(LoginResult loginResult) {
        if(Profile.getCurrentProfile() == null) {
            mProfileTracker = new ProfileTracker() {
                protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
                    // profile2 is the new profile
                    Log.v("facebook - profile", profile2.getFirstName());
            // no need to call startTracking() on mProfileTracker
            // because it is called by its constructor, internally.
        else {
            Profile profile = Profile.getCurrentProfile();
            Log.v("facebook - profile", profile.getFirstName());

    public void onCancel() {
        Log.v("facebook - onCancel", "cancelled");

    public void onError(FacebookException e) {
        Log.v("facebook - onError", e.getMessage());

You must override your Activity's or Fragment's onActivityResult() as below:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if you don't add following block,
    // your registered `FacebookCallback` won't be called
    if (mCallbackManager.onActivityResult(requestCode, resultCode, data)) {


Code updated with Alex Zezekalo's suggestion to only call mProfileTracker.startTracking(); if Profile.getCurrentProfile() returns null.