hasan
hasan

Reputation: 24185

Open for read, Facebook Session, already opened session exception

I use the following method to login to Facebook using the Facebook sdk. I added there an exception try/catch handler that caught the following exception:

Exception ::::::﹕ Session: an attempt was made to open an already opened session.

As you can see I check if the session is closed before I open it. and it passes it which means its closed and it is safe to open it. even that an exception is caught.

I run the debugger and stopped at the line marked below and checked the currentSession state and it is closed!!!!!

private Session openActiveSession(Activity activity, boolean allowLoginUI, List permissions, Session.StatusCallback callback) {
        Session currentSession = Session.getActiveSession();
        if (currentSession == null) {
            Session session = new Session.Builder(activity).build();
            Session.setActiveSession(session);
            currentSession = session;
        }

        if (!currentSession.isOpened() && (SessionState.CREATED_TOKEN_LOADED.equals(currentSession.getState()) || allowLoginUI)) {
            try {
                Session.OpenRequest openRequest = new Session.OpenRequest(activity).setPermissions(permissions).setCallback(callback);
                currentSession.openForRead(openRequest); // I stopped here..
            } catch (Exception e) {
                Log.d("Exception :::::: ", e.getMessage()); // and stopped here.
            }
            return currentSession;
        }
        return null;
    }

Any explanation?

Could that mean that there is another session that is open? my could always gets an active session if exists I don't see how that could happen. I always check for active session in all methods and use it!

How Session.getActiveSession() method really works? what if another session has been initialised in a previous activity? this method should get it right?

Upvotes: 0

Views: 250

Answers (1)

Ming Li
Ming Li

Reputation: 15662

The error message could be improved, I think. It should actually say: "an attempt was made to open a session that's already open, or has already been closed".

You should check to see if currentSession.isClosed() is true, and if so, create a new session.

A session can only be opened once. It can't be re-opened.

Upvotes: 1

Related Questions