I have a problem with getting Profile.getCurrentProfile()
after logging to Facebook
in my app. I am using Facebook Android SDK 4.0.0
. After logging in I would like to get user name and show it in TextView
but I'm getting profile == null
and i don't know why. I have correct app ID and HashKey
.
this is my code:
public class MyFragment extends Fragment {
CallbackManager callbackManager;
LoginButton loginButton;
FacebookCallback<LoginResult> loginResultFacebookCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Log.e("FB", String.valueOf(accessToken));
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
name.setText("Witam " + profile.getName());
Log.e("FB", "w");
}
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
};
TextView name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myfragment_facebook, container, false);
name = (TextView) view.findViewById(R.id.name);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, loginResultFacebookCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
When I check Token in Logcat
i get AccessToken token:ACCESS_TOKEN_REMOVED
The Profile is fetched asynchronously after the login happens. If you want to track it, similar to this sample: https://github.com/facebook/facebook-android-sdk/blob/b384c0655fe96db71229bfdcb981a522f3f1e675/samples/SwitchUserSample/src/com/facebook/samples/switchuser/ProfileFragment.java#L54