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.