本文介绍了启动应用程序时,虽然我连Session.getActiveSession()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用com.facebook.widget.UserSettingsFragment在我的应用程序管理的登录/注销Facebook的:

I use com.facebook.widget.UserSettingsFragment to manage login/logout to Facebook in my app:

UserSettingsFragment userSettingsFragment = new UserSettingsFragment();
Session session = Session.getActiveSession();
if(session==null || session.isClosed()){
    userSettingsFragment.setPublishPermissions(Arrays.asList("publish_actions"));
}
fragmentTransaction.replace(R.id.content_container, userSettingsFragment, "userSettingsFragment");

在另一个片段(但同样活动),在发布在Facebook上我检查,我到底登录使用

In another Fragment (but same Activity), Before publishing on Facebook I'm checking whether I'm logged using

Session session = Session.getActiveSession();
if (session != null && session.isOpened()){
    publishOnFacebook(intent.getExtras());
}

当我开始我的应用程序,并尝试发布,我总是一 会话虽然我登录过。当我开始 UserSettingsFragment ,它让我发现,我连接。

When I start my app and try to publish, I always get a null Session although I logged in before. And when I start UserSettingsFragment, it shows me that I'm connected.

如何 Session.getActiveSession(); 如果我有实际联系

How can Session.getActiveSession(); be null if I'm actually connected?

此外,如果是Ⅱ启动片段其中,我出版后直 UserSettingsFragment ,我得到了正确的会话,即:

Moreover, if I I start the Fragment where I do the publishing straight after UserSettingsFragment, I get the correct Session, i.e.:

1 start the app
2 call `Session.getActiveSession()` in another `Fragment` returns `null`
3 start `UserSettingsFragment` shows that I'm log

1 start the app
2 start `UserSettingsFragment` shows that I'm log
3 call `Session.getActiveSession()` in another `Fragment` returns a session

任何人都可以帮忙吗?

Anybody can help?

推荐答案

好吧,我刚刚读到 openActiveSessionFromCache (而不是从Facebook DOC虽然):

Ok, I've just read about openActiveSessionFromCache (not from Facebook doc though):

Session session = Session.getActiveSession(); 

if(session==null){                      
    // try to restore from cache
    session = Session.openActiveSessionFromCache(getActivity());
}

if(session!=null && session.isOpened()){    
    publishOnFacebook(intent.getExtras());  
}
else{
    login();
}

这篇关于启动应用程序时,虽然我连Session.getActiveSession()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 12:03