FB SDK,requestNewPublishPermissions()不调用任何回调 [英] FB sdk, requestNewPublishPermissions() doesn't call any callback

查看:87
本文介绍了FB SDK,requestNewPublishPermissions()不调用任何回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着 FB 教程一步一步,我想不通这是为什么不工作。

I followed the FB tutorial step by step, and I can't figure out why is this not working.

我所有的片段继承的 BaseFragment

public class BaseFragment extends SherlockFragment {
    ...
    protected UiLifecycleHelper _uiHelper;
    protected boolean _isResumed = false;

    protected static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
    protected static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
    protected boolean _pendingPublishReauthorization = false;


    protected Session.StatusCallback _callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _uiHelper = new UiLifecycleHelper(getSherlockActivity(), _callback);
        _uiHelper.onCreate(savedInstanceState);
    }

    protected void onSessionStateChange(Session session, SessionState state, Exception exception) {}

    ...(all lifecycle stuff)
}

然后,我有一个 FacebookLoginFragment 包含登录/注销按钮:

public class FacebookLoginFragment extends BaseFragment {

    protected LoginButton _authButton;
    protected ProgressBar _loadingBar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.facebook_login, container, false);

        _loadingBar = (ProgressBar) view.findViewById(R.id.login_progressBar);

        _authButton = (LoginButton) view.findViewById(R.id.authFacebookButton);
        _authButton.setFragment(this);
        _authButton.setReadPermissions(Arrays.asList("email", "user_birthday"));

        return view;
    }

    @Override
    protected void onSessionStateChange(Session session, SessionState state, Exception exception) {
        super.onSessionStateChange(session, state, exception);

        // LogIn process
    }
    ...
}

最后我有一个 NewsFragment 包含饲料的一个ListView。每个饲料共享。
当我们点击一​​个饲料的分享按钮,适配器调用的 NewsFragment.publishFeed(饲料)的方法。

And finally I've a NewsFragment which contains a listView of feeds. Each feed is sharable. When we click on the share button of a feed, the adapter call the NewsFragment.publishFeed(feed) method.

public class NewsFragment extends BaseFragment{
    ...
    protected Feed _feedPendingPublish;

    @Override
    // => NEVER CALLED 
    protected void onSessionStateChange(Session session, SessionState state, Exception exception) {
        super.onSessionStateChange(session, state, exception);

        if (_isResumed) {

            if (_pendingPublishReauthorization && state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
                _pendingPublishReauthorization = false;
                if(_feedPendingPublish != null){
                    publishFeed(_feedPendingPublish);
                    _feedPendingPublish = null;
                }
            }
        }
    }

    public void publishFeed(Feed feed){

        if(_pendingPublishReauthorization){
            return;
        }

        Session session = Session.getActiveSession();

        if (session != null){

            // If permission doesn't exist, ask and return. Otherwise, send request

            List<String> permissions = session.getPermissions();
            if (!isSubsetOf(PERMISSIONS, permissions)) {
                _pendingPublishReauthorization = true;
                _feedPendingPublish = feed;

                Session.NewPermissionsRequest newPermissionsRequest = new Session
                    .NewPermissionsRequest(getSherlockActivity(), PERMISSIONS);
                session.requestNewPublishPermissions(newPermissionsRequest);

                return;
            }

            // DO stuff
        }
    }
}

的问题是,在NewsFragment回调未发布授权之后调用。 (既不BaseFragment,既无FacebookLogin)。
另外,在相同的活性存在两个片段。

The problem is that the NewsFragment callback is not called after the publish authorization. (neither the BaseFragment, neither the FacebookLogin). Note that both fragments exist in the same activity.

我使用Facebook的Andr​​oid的SDK-3.5.2

I'm using facebook-android-sdk-3.5.2

推荐答案

我终于想通了这一点。
正如许多人所说,这是一个的onActivityResult()的问题,但相当凶狠。

I finally figured this out. As many people said, it was an onActivityResult() problem, but quite vicious.

在登录时, FacebookLoginFragment.onSessionStateChange()被称为succefully,而 NewsFragment.onSessionStateChange()在persmission过程中从来没有被调用。

When Log In, the FacebookLoginFragment.onSessionStateChange() was called succefully, whereas the NewsFragment.onSessionStateChange() was never called during the persmission process.

经过多次试验,我认为这是由于静态和动态绑定。我的 FacebookLoginfragment 是directy在XML(静态)引用,而我NewsFragment在源$ C ​​$ C动态添加。
因此,它包含两个片段的活性仅为发送的onActivityResult 信号到 FacebokLoginFragment
因此, NewsFragment.onActivityResult()这是必要的触发回调从未被调用。

After several tests, I assume this was due to a static and dynamic binding. My FacebookLoginfragment is directy referenced in the xml (static), whereas my NewsFragment is added dynamically in the source code. Consequently, the Activity which contains both fragments was only sending the onActivityResult signal to the FacebokLoginFragment. Therefore, the NewsFragment.onActivityResult() which is necessary to trigger the callback was never called.

重写 Activity.onActivityResult()给我预期的行为:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    getSupportFragmentManager().findFragmentById(R.id.my_fragment_frame).onActivityResult(requestCode, resultCode, data);
} 

这篇关于FB SDK,requestNewPublishPermissions()不调用任何回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆