0

I'm using the android facebook sdk but I'm running into an issue. I created 2 classes which are:

public class FacebookSession implements Session.StatusCallback {

public interface SessionCallbacks {
    public void onSessionOpened(String token, User user, boolean isNewProfile);
    public void onSessionOpenFailed(String errorText);
}

public FacebookSession(Activity activity) {
    ...
}

@Override
public void call(Session session, SessionState sessionState, Exception e) {
    switch (sessionState) {
        case CLOSED:
            ...
            // Calling SessionCallbacks callbacks here
            break;

        case CLOSED_LOGIN_FAILED:
            ...
            // Calling SessionCallbacks callbacks here
            break;

        case OPENED:
            ...
            // Calling SessionCallbacks callbacks here
            break;

        case OPENED_TOKEN_UPDATED:
            ...
            // Calling SessionCallbacks callbacks here
            break;
    }
}
}

And in my activity calling facebook session:

public class LoginActivity extends FragmentActivity implements
    FacebookSession.SessionCallbacks {

public LoginActivity() {

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    ...
}

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

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

@Override
public void onSessionOpened(String token, User user, boolean isNewProfile) {
    ...
}
}

As you can see I HAVE TO call Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); in the onActivityResult method of every activity using my FacebookSession class if I want the result to be returned in the call callback of FacebookSession. If I don't, my FacebookSession just doesn't work. What I want to do here is not having to call this Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); for every activities which use FacebookSession. Is there a workaround for this? Thank you

E-Kami
  • 2,529
  • 5
  • 30
  • 50
  • You only need to pass the results in onActivityResult to the session if you're doing some form of SSO in your activity (login, requesting additional permissions, etc). If you're only using the session for making graph requests, then you don't need to override onActivityResult. – Ming Li Feb 19 '15 at 23:03
  • I do use SSO requests. My `FacebookSession` take care of showing the facebook webview, ask permissions for the app etc... But I need it to be independant from my activities. – E-Kami Feb 20 '15 at 03:05

1 Answers1

2

If all of your activities have the potential of either logging the user in, or requesting additional permissions, then no, there's no workaround from implementing the onActivityResult method. Normally, apps that use Facebook login features only do it from a few, limited activities so this is not a problem.

How many activities are you dealing with? If you absolutely think it's an issue, then you can either

  1. Create a BaseActivity class that all of your activities extend, and put the onActivityResult in the parent class; or

  2. Move all of your Facebook integration into a centralized activity.

Ming Li
  • 15,672
  • 3
  • 37
  • 35
  • I agree with you when you say that Facebook login is used only by few activities. But I also have tests on my `FacebookSession`, that's why I wanted it to be activities independent. I also thought about your 2 "workarounds" but I don't like the way I have to implement/use them. Looks like I don't have a choice here but to accept the way Facebook created their static methods which completely breaks the principle of objects encapsulation and single responsibility principle imo. – E-Kami Feb 24 '15 at 00:08
  • At the end of the day, we need to start an activity on the FB app and get a result back. Android's model for that is startActivityForResult/onActivityResult. While I would love for the Session object to be completely self-contained, there's not really a way around that. On iOS we have a different model because that's what the platform supports. – Ming Li Feb 24 '15 at 21:14