1

I have a problem about Facebook SDK for android. This code works for another activity of same application. I show to user the same FacebookLogin button in same xml file. But in the other activity, when I login on facebook, onUserInfoFetched method does not work! So I can not get the user.

The codes are in the same application. But in different activities. I can login both of them. but I can not get the user.

The codes below: This is the code which successfully work:

public class FacebookPreference extends Preference {
    private LoginButton loginButton;
    private ProfilePictureView profilePictureView;
    public static GraphUser user;
    private Context mContext;

    private enum PendingAction {
        NONE, POST_PHOTO, POST_STATUS_UPDATE
    }

    public FacebookPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setWidgetLayoutResource(R.layout.preferences_item_list_facebook);
        this.mContext = context;
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        profilePictureView = (ProfilePictureView) view.findViewById(R.id.profilePicture);
        loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
            @Override
            public void onUserInfoFetched(GraphUser user) {
                FacebookPreference.user = user;
                updateUI();

            }
        });

    }

    private void updateUI() {
        Session session = Session.getActiveSession();
        if (user != null) {
            profilePictureView.setProfileId(user.getId());
        } else {
            profilePictureView.setProfileId(null);
        }
    }
}

This is the code which does not work.

public class FacebookLoginActivity extends Activity{

    LoginButton button;
    private ProfilePictureView profilePictureView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences_item_list_facebook);


        button = (LoginButton) findViewById(R.id.login_button);
        profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);

        button.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
            @Override
            public void onUserInfoFetched(GraphUser user) {
                FacebookPreference.user = user;
                if(FacebookPreference.user != null)
                    Toast.makeText(FacebookLoginActivity.this,  FacebookPreference.user.getName().toString(), Toast.LENGTH_LONG).show();
                updateUI();

            }
        });

    };


    private void updateUI() {
        Session session = Session.getActiveSession();
        if (FacebookPreference.user != null) {
            profilePictureView.setProfileId(FacebookPreference.user.getId());
        } else {
            profilePictureView.setProfileId(null);
        }
    }
}

The xml File:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/menu_item_back"
    android:orientation="horizontal" >

    <com.facebook.widget.ProfilePictureView
        android:id="@+id/profilePicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        facebook:preset_size="normal" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="5dp"
        android:orientation="vertical" >

        <com.facebook.widget.LoginButton
            android:id="@+id/login_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            facebook:confirm_logout="false"
            facebook:fetch_user_info="true" >
        </com.facebook.widget.LoginButton>
    </LinearLayout>

</LinearLayout>

Thanks for answers.

serefakyuz
  • 273
  • 4
  • 14

2 Answers2

1

If you look at the other activity where this code works, you'll notice a UiLifecycleHelper member that shows up on each of the Activity lifecycle methods, as in:

@Override
protected void onResume() {
    super.onResume();
    uiHelper.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    uiHelper.onPause();
}

etc.

Implement this as well, and the callback should work.

josephus
  • 8,284
  • 1
  • 37
  • 57
-1

Just Look at this this working nice for me And Look At this Answer Facebook Sdk User details

 private String UserInfoDisplay(GraphUser user) {    

    StringBuilder userInfo = new StringBuilder("");              
    String Name =  user.getName();
    String Id =   user.getId();
    String lastname =    user.getLastName();
    String firstname =  user.getFirstName();
    String getUsername =  user.getUsername();             

    String get_gender = (String) user.getProperty("gender");


    String image_url = "http://graph.facebook.com/"+user.getId()+"/picture?type=square";        

    Log.i("User information == >", Name +Id + lastname+ 
            firstname + image_url + get_gender +  getUsername + "usermail"+User_mail  );

    text.setText(Name + "  mail = " +User_mail);

    return userInfo.toString();
}
Community
  • 1
  • 1
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
  • After login, onUserInfoFetched does not work for the second code(FacebookLoginActivity). So, i can not get user. How can i get user details while user is null? – serefakyuz Feb 13 '14 at 09:53
  • My problem is not about user details. Problem is i can not get user object because onUserInfoFetched method does not work. – serefakyuz Feb 13 '14 at 09:55
  • change condition like this and check `if(!FacebookPreference.user.equalls("")) ` – NagarjunaReddy Feb 13 '14 at 09:56
  • FacebookPreference.user = user; if(FacebookPreference.user != null) Toast.makeText(FacebookLoginActivity.this, FacebookPreference.user.getName().toString(), Toast.LENGTH_LONG).show(); updateUI(); This code completely does not work. The method does not work my friend. My problem is this. – serefakyuz Feb 13 '14 at 10:05
  • No error, when i run the application in debug mode, i saw the onUserInfoFetched method does not work after login is ok. – serefakyuz Feb 13 '14 at 10:10
  • This is the full activity. there is no another code in activity and in preference class. in addition, app_id is defined in manifest like that: – serefakyuz Feb 13 '14 at 10:55