gpt4 book ai didi

android - Session.getActiveSession() 在启动应用程序时返回 null,尽管我已连接

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:39 27 4
gpt4 key购买 nike

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

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");

在另一个 Fragment(但相同的 Activity)中,在 Facebook 上发布之前,我检查我是否使用

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

当我启动我的应用程序并尝试发布时,尽管我之前登录过,但我总是得到一个 null Session。当我启动 UserSettingsFragment 时,它会显示我已连接。

如果我真的连接了,Session.getActiveSession(); 怎么会是 null

此外,如果我在 UserSettingsFragment 之后直接开始发布 Fragment,我会得到正确的 Session,即:

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

有人可以帮忙吗?

最佳答案

好的,我刚刚读到有关 openActiveSessionFromCache 的内容(虽然不是来自 Facebook 文档):

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();
}

关于android - Session.getActiveSession() 在启动应用程序时返回 null,尽管我已连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18295998/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com