Nativ
Nativ

Reputation: 3150

Android facebook sdk 3.0.1 using last session

I have the following scenario with facebook sdk 3.0.1. When user first login and chooses "FB login" then the SSO starts, a new session is open and everything works fine. But then, when the user closes the app and start it again - I don't understand how to get the last open session, currently I'm opening a new session and the user sees again the FB progress bar(while it's being connected to FB again, even so the user already approved FB in his last run). Does anybody know how to skip this operation?

Edit 1:

This is how I retrieve the session:

public void tryRetrievFacebookSession() {
    Session session = Session.getActiveSession();

    if (session != null && session.isOpened())
        return;

    session = Session.openActiveSession(this, true, new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
            MobliLog.d("SplashScreen", "Inside call() with session with state: " + session.getState());
            // onSessionStateChanged(session, state, exception);
        }
    });

}

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

    if (session != null)
        session.onActivityResult(this, requestCode, resultCode, data);

    uiHelper.onActivityResult(requestCode, resultCode, data);

}

The session is normally being created or from the LoginButton or with those lines:

session = Session.getActiveSession();

    if (session.getState().isClosed())
        session = new Session(this);

    if (session.isOpened()) {
        onAuthenticationEndListener.onSuccessfullAuthentication();
        return;
    } else {
        this.onFacebookAuthenticationEndListener = onAuthenticationEndListener;
        Session.setActiveSession(session);
        session.openForRead(new Session.OpenRequest(SocNetwksCompatScreen.this).setCallback(null));
        return;
    }

Information 1:

When I'm doing the first Session session = Session.getActiveSession(); in the logins after the sso authentication, my session has state CLOSED instead of OPENED

Information 2: I'm using uiHelper and initialize it like this:

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

    twitter = SocialPluginsUtils.getTwitterClient();

    uiHelper = new UiLifecycleHelper(this, statusCallback); 
    uiHelper.onCreate(savedInstanceState);
}

Now, I know that after I perform Session.getActiveSession(); the session state should get OPENED and after that OPEN and then the uiHelper's callback should be invoked. In my code the state is CLOSED or CLOSED_LOGIN_FAILED or CREATED(not sure why it's not stable) and the rest doesn't happens

Upvotes: 0

Views: 3605

Answers (3)

Nativ
Nativ

Reputation: 3150

Problem solved. I accidentally called session.closeAndClearTokenInformation(); in the onStop() (yeah, so stupid!)

Upvotes: 0

5agado
5agado

Reputation: 2454

From what I can see after your edit maybe the problem is related with the way in which you manage the session. I use this code in my projects:

    Session session = Session.getActiveSession();

    if (session == null){
        Session.openActiveSession(activity, true, sessionCallback);
    }

    else if (!session.getState().isOpened()){
        session.openForRead(new Session.OpenRequest(activity)
                .setCallback(sessionCallback));
                    //this will open the session with basic read permissions
    }

    else {          
        //do what you want with the opened session
    } 

Moreover if you use the UiLifecycleHelper you don't need the two line of code that I suggest to you in the comment, they are already in the method of the helper. But you must be sure to call all the methods of the helper in each related method of the activity (onResume, onPause etc.)

If there isn't a token cache the openActiveSession(activity, true, sessionCallback) will automatically call a new dialog and if the user login with success a new token cache will be available for future uses.

Upvotes: 0

5agado
5agado

Reputation: 2454

In fact the sessions are being closed every time the user closed the app.
So if Session.getActiveSession() return a null session you only need to call Session.openActiveSession(activity, true, sessionCallback).

If there is a valid token cache this method will use it in order to open a new session without the need for the user to insert any data. Otherwise this will shows the default dialog with the basic permissions.

Upvotes: 2

Related Questions