简体   繁体   中英

Facebook authentication not call onActivityResult from second time

I am trying to make facebook authentication by using:

public class FacebookAuthentication extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

            // callback when session changes state
            @Override
            public void call(Session session, SessionState state,
                    Exception exception) {
                if (session.isOpened()) {

                    // make request to the /me API
                    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) {

                                    }
                                }

                            });

                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);
        finish();
    }

}

I call this activity from my service because I have to do some operation related to facebook only from my service, I am doing it this way:

if (session == null) {
    Intent activ = new Intent(uploadService,
            FacebookAuthentication.class);
    activ.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    uploadService.startActivity(activ);
    for (int i = 0; i < 20; i++) {
        try {
            if (Session.getActiveSession() != null) {
                session = Session.getActiveSession();
                break;
            }
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            Log.i(TAG, "Sleep failed");
        }
    }
    if (Session.getActiveSession() == null) {

        return false;
    }
}

so this way I got my session object and I can use it. the first time works good and my onActivityResult function calls. sometimes my service is restarted, the session object turns null, so I need to make the authentication again, when doing it again the onActivityResult function never calls and I get black screen (activity screen) with my app title, and I have to press the back button to make it disappear. except this everything works good, I got my session and the operations I am making on facebook works.

Is there a way to force the black screen close? Is there a better way I can do the authentication? I know that I am doing something not so trivial, but I must do it that way, I have to make the facebook operations from service and the authentication from separate activity.

If the session has been cached (ie the user previously authorized your app, and you did not perform a session.closeAndClearTokenInformation), then the session will be transitioned to the OPENED state immediately, without starting another activity (and hence won't call onActivityResult).

In your FacebookAuthentication activity, you should do something like this after your Session.openActiveSession call:

Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
  // that means the session is opened from cache
  finish();
}

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