For the first time when I login into the app through Facebook, I'm getting profile from Profile.getCurrentProfile();
Where as when I exit the app and launch again, It was already logged in. So I can call directly Profile.getCurrentProfile();
is returning null.
Code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_page);
Profile profile = Profile.getCurrentProfile();
// For the first launch the profile will be null
displayProfileName(profile);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile");
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(
Profile oldProfile, Profile currentProfile) {
profileTracker.stopTracking();
Profile.setCurrentProfile(currentProfile);
Profile profile = Profile.getCurrentProfile();
displayProfileName(profile);
}
};
profileTracker.startTracking();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (profileTracker != null) {
profileTracker.stopTracking();
}
}
@Override
protected void onPause() {
super.onPause();
// Logs 'app deactivate' App Event.
AppEventsLogger.deactivateApp(this);
}
@Override
protected void onRestart() {
super.onRestart();
AppEventsLogger.activateApp(this);
}
/**
*
* Method to display the Profile Name
*/
private void displayProfileName(Profile profile) {
if (profile != null) {
Toast.makeText(MainActivity.this, profile.getName(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "No Profile", Toast.LENGTH_LONG)
.show();
}
}
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
callbackManager.onActivityResult(arg0, arg1, arg2);
}