2

When I'm using Facebook login in an Android app, the callback is called before the permissions popup is shown. In my code, the expected behaviour should be:

  1. User opens app
  2. User clicks "sign in" button
  3. FB permissions dialog pops up
  4. User accepts FB permissions
  5. A "hi there!" toast message is shown

However, the actual flow is:

  1. User opens app
  2. User clicks "sign in" button
  3. A "hi there!" toast message is shown
  4. FB permissions dialog pops up

I basically used the code from this answer and added a button to perform the login when the user clicks on it, instead of on activity creation:

public class MainActivity extends Activity implements StatusCallback {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  public void signIn(View view) {
    OpenRequest open = new OpenRequest(this);
    open.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
    open.setPermissions(Arrays.asList(new String[] { "email", "user_hometown" }));
    open.setCallback(this);
    Session s = new Session(this);
    s.openForRead(open);
  }

  @Override
  public void call(Session session, SessionState state, Exception exception) {
    CharSequence text = "Hi there!";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(getApplicationContext(), text, duration);
    toast.show();
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (Session.getActiveSession() != null)
      Session.getActiveSession().onActivityResult(this, requestCode,
          resultCode, data);
  }
}

Thanks

Community
  • 1
  • 1
Jose
  • 609
  • 1
  • 7
  • 15

1 Answers1

0

You have to check when comes correct SessionState value for you.

public void call(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        if (exception == null) {
            //Success
        } else if (state == SessionState.CLOSED_LOGIN_FAILED) {
            //Login fail
        } else if (state.isClosed()) {
            //Logged out...
        }
    }
}
user123
  • 1,053
  • 15
  • 27
  • The "call" method is called before the popup and not after permissions acceptance – Jose May 20 '13 at 11:42
  • Yes `call` callback is called many times... Including before login success, thats why you have to check state – user123 May 20 '13 at 11:55
  • I've copied your code and tried it, but unfortunately, the call method is called only once. I'm using Android 4.2.2 – Jose May 20 '13 at 13:34