Hya Hya - 1 year ago 95
Android Question

Get The Facebook Friends List in Android Activity Other then where User Logged In

I am using Facebook Login in my Android Application. i am able to login successfully and display user name in a TextView but i am facing the below problem

After successful facebook login when i close the application and opens it again, i am getting logout button but the profile Name is missing. i want the activity should again call the facebook API to get the user name.

i am using the code below
in my OnCreate Event

callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.connectWithFbButton);
loginButton.setReadPermissions("email", "user_friends");
loginButton.registerCallback(callbackManager, nCallback);

protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);

private FacebookCallback<LoginResult> nCallback = new FacebookCallback<LoginResult>() {
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
if ( profile!= null)
String FirstName = "";
String LastName = "";
String ProfilePic = "";
String PId = "";
FirstName = URLEncoder.encode(profile.getFirstName(), "utf-8");
LastName = URLEncoder.encode(profile.getLastName(), "utf-8");
message.setText( "Hello " + profile.getFirstName());

public void onCancel() {


public void onError(FacebookException error) {


Answer Source

I belive you are looking for something like this

    Profile profile;
    String name;

        profile = Profile.getCurrentProfile();
        name = profile.getName();
    }catch(NullPointerException npe){

onSuccess is only called when you log in so you need to getCorrentProfile outside onSucess note that if you are not logged in this will return null

so in onCreate check if getCorrentProfile is null, if so let the user log in you will get the result in onSuccess, if not you use the profile to get the name and other info

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download