简体   繁体   中英

Facebook onCompleted is called twice

I did the following:

@Override
public void onResume() {
    super.onResume();
    // For scenarios where the main activity is launched and user
    // session is not null, the session state change notification
    // may not be triggered. Trigger it if it's open/closed.
    Session session = Session.getActiveSession();
    if (session != null && (session.isOpened() || session.isClosed())) {
        onSessionStateChange(session, session.getState(), null);
    }        
    uiHelper.onResume();
}

and

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        makeMeRequest(session);
    } else if (state.isClosed()) {

    }
}

and

private void makeMeRequest(final Session session) {
    Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
        @Override
        public void onCompleted(GraphUser user, Response response) {
            if (session == Session.getActiveSession()) {
                if (user != null) {
                    log.d("creating fragment");  //<<<<<<<<<<<<<<<
                    //creating a fragment when the request is complete
                    .....
                }                    
            }
            if (response.getError() != null) {

            }
        }
    });
    request.executeAsync();
} 

Please read the comment of the first code clause: as suggested by Facebook, that code works because without it, the fragment is not created in certain scenarios even though I'm logged in, but with this code, onCompleted is called twice and I get an exception. see my logging in the 3rd code clause: log.d("creating fragment") - I see it twice before the exception occurred.

Any idea what am I missing? ps: I have a main activity that calls a fragment where the user can login to facebook

Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
    onSessionStateChange(session, session.getState(), null);
}

don't use above code in onresume() ,

Activity normally called first oncreate() and then onresume() .so that it's called twice.

please check it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM