简体   繁体   中英

Android Facebook SDK Remember Session

I'm writing an application which requires facebook login. I can make user log in when he/she first opens the application. However, I can't retrieve the session after they restart the application. My login code is the following:

private static Session openActiveSession(Activity activity, boolean allowLoginUI, StatusCallback callback, List<String> permissions) {
    OpenRequest openRequest = new OpenRequest(activity).setPermissions(permissions).setCallback(callback);
    Session session = new Session.Builder(activity).build();
    if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
        Session.setActiveSession(session);
        session.openForRead(openRequest);
        return session;
    }
    return null;
}

And I use isLoggedIn method when the app is opened in order to understand if the user is logged in or not.

public static boolean isLoggedIn() {
    Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
        return true;
    } else {
        return false;
    }
}

But when the application is closed and reopened, this method never returns true .

How can I retrieve old session back without showing a popup window to want user's facebook account informations, if he/she logged in in the past.

您可以调用Session.openActiveSessionFromCache,只有在存在缓存的访问令牌时才会打开活动会话,否则返回null。

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