Facebook SDK - 如何正确覆盖onActivityResult()?

时间:2015-01-18 17:27:18

标签: java android facebook-sdk-4.0

我有一个简单的活动,我希望获得有关Facebook用户的一些信息:

public class FacebookActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.facebook_layout);

        // start Facebook Login
        Session.openActiveSession(this, true, new Session.StatusCallback() {

            // callback when session changes state
            public void call(Session session, SessionState state, Exception exception) {
                if (session.isOpened()) {
                    Request.executeMeRequestAsync(session, new Request.
                            GraphUserCallback() {

                        // callback after Graph API response with user object
                        @Override
                        public void onCompleted(GraphUser user, Response response) {
                            if (user != null) {
                                Log.i("facebook_id", user.getId());
                                Log.i("facebook_name", user.getName());
                            }
                        }
                    });
                }
            }

        });
    }
}

但如果未覆盖onCompleted()方法,则不会调用onActivityResult()方法。所以,我的问题是如何在我的案例中正确使用它?

感谢。

1 个答案:

答案 0 :(得分:1)

试试

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);
}