You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Facebook Android登录:已登录状态下用户信息传递异常问题排查

问题2:长时间打开App后跳转异常的原因及修复

原因分析

当App长时间处于后台时,系统可能会回收内存,导致AccessToken.getCurrentAccessToken()或Profile.getCurrentProfile()返回null,但LoginButton的状态是基于本地缓存显示的,所以会出现“显示logout但实际登录状态返回null”的矛盾情况。另外,AccessToken也可能过期,导致判断逻辑失效。

修复方案

  1. 严格校验AccessToken有效性:
    判断登录状态时,不仅要检查token是否不为null,还要验证token是否未过期:

    if (accessToken != null && !accessToken.isExpired() && Profile.getCurrentProfile() != null) {
        // 执行跳转逻辑
    }
    
  2. 监听AccessToken变化:
    在AccessTokenTracker中处理token失效或变为null的情况,及时同步本地状态:

    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
            accessToken = currentAccessToken;
            // 如果token变为null,说明用户已登出,清空本地数据
            if (currentAccessToken == null) {
                databaseHelper.deleteAll();
                // 可以刷新UI,确保登录按钮状态正确
            }
        }
    };
    
  3. 前台恢复时重新校验状态:
    在onResume方法中再次检查登录状态,避免长时间后台后状态不一致:

    @Override
    protected void onResume() {
        super.onResume();
        accessToken = AccessToken.getCurrentAccessToken();
        Profile currentProfile = Profile.getCurrentProfile();
        
        if (accessToken != null && !accessToken.isExpired() && currentProfile != null) {
            // 执行跳转和数据传递逻辑
        }
    }
    
  4. 避免内存泄漏:
    在onDestroy方法中停止tracker,防止内存泄漏:

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (accessTokenTracker != null) {
            accessTokenTracker.stopTracking();
        }
        if (profileTracker != null) {
            profileTracker.stopTracking();
        }
    }
    

内容的提问来源于stack exchange,提问作者sanjeev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:03:27