For some reason Profile.getCurrentProfile() shows up null at times right after logging into FaceBook, using FB API v4.0.
This is causing problems for me
I have a simpler suggestion than the other answers here.
There is no need to register a login callback with:
registerCallback(mCallbackManager, new FacebookCallback() { ... }
Instead, whenever you need user data (like on button click) - start tracking profile changes with:
new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile profile) {
stopTracking();
Log.d(TAG, profile.getFirstName());
Log.d(TAG, profile.getLastName());
Log.d(TAG, String.format("https://graph.facebook.com/%s/picture?type=large", profile.getId()));
}
}.startTracking();
And then start the Facebook login process by:
LoginManager.getInstance().logInWithReadPermissions(getContext(), Arrays.asList("public_profile"));
For the above code to work you of course need to prepare the usual stuff in your Activity:
private CallbackManager mCallbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mCallbackManager = CallbackManager.Factory.create();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (FacebookSdk.isFacebookRequestCode(requestCode) &&
mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
// do nothing
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Also do not forget to modify the AndroidManifest.xml file.