为什么不使用的Andr​​oid碎片的Facebook界面的工作? [英] Why doesn't Android Facebook interface work with Fragments?

本文介绍了为什么不使用的Andr​​oid碎片的Facebook界面的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我交换一些安卓的 Facebook的 $ C $从活动ç片段。在此之前的开关一切工作正常,但现在不被执行的onComplete()回调。

I'm switching some Android Facebook code from an Activity to a Fragment. Prior to the switch everything worked fine, but now the onComplete() callback is not being executed.

难道Facebook的code不能与片段,还是我做错了什么工作?

Does the Facebook code not work with Fragments, or am I doing something wrong?

下面是原来的code(在 SherlockActivity ):

Here's the original code (in a SherlockActivity):

if (!mFacebook.isSessionValid()) {
    mFacebook.authorize(MyActivity.this, permissions, new DialogListener() {
        @Override
        public void onComplete(Bundle values) { ... }    // CALLED AS EXPECTED
    }
}

这是新的code(在SherlockFragment):

And here's the new code (in a SherlockFragment):

if (!mFacebook.isSessionValid()) {
    mFacebook.authorize(getActivity(), permissions, new DialogListener() {
        @Override
        public void onComplete(Bundle values) { ... }    // DOES NOT GET CALLED
    }
}

无论是活动的片段包括相同的onActivityResult()所要求的Facebook的:

Both the Activity and the Fragment include the same onActivityResult() as required by Facebook:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mFacebook.authorizeCallback(requestCode, resultCode, data);
}

感谢您的帮助!

Thanks for your help!

编辑:类似的问题是问<一个href="http://stackoverflow.com/questions/8835094/does-the-facebook-sdk-only-work-with-activities">here,在这里接受的答案是将片段更改为 FragmentActivity 。但我看不出这会有所帮助,因为那不再是一个片段(我需要其他原因)。建议?

A similar question is asked here, where the accepted answer is to change the Fragment to a FragmentActivity. But I don't see how this helps, as then it's no longer a Fragment (which I need for other reasons). Suggestions?

编辑2:我推出自己的解决方案。见下文。

EDIT 2: I rolled my own solution. See below.

推荐答案

据我判断,Facebook的身份验证API不支持的片段。具体而言,从Facebook上的的onComplete()回调授权()通话效果是活动 ,而与片段

As far as I could determine, the Facebook auth API does not support Fragments. Specifically, the onComplete() callback from the Facebook authorize() call works with Activities, but not with Fragments.

于是,我把一个简单的解决方法片段。该解决方案依赖于一个事实,即 onActivityResult()也被称为父的活动授权()调用完成,所以你可以用它来建立一个独立的回调的机制,在片段

So I put together a simple workaround for Fragments. The solution depends on the fact that onActivityResult() is also called in the parent Activity when the authorize() call completes, so you can use it to set up a separate callback mechanism for the Fragment.

首先,建立一个变量在父的活动执行目标的片段的的名字,说

First, set up a variable in the parent Activity to carry the target Fragment's name, say

TargetFragment mTargetFragment;

您可以初始化它时,片段是第一次实例是这样的:

You can initialize it when the Fragment is first instantiated like this:

@Override
public void onAttachFragment(Fragment fragment) {
    super.onAttachFragment(fragment);

    String fragmentSimpleName = fragment.getClass().getSimpleName();

    if (fragmentSimpleName.equals("TargetFragment"))
        mTargetFragment = (TargetFragment)fragment;        
}

随后几行添加到onActivityResult()函数:

Then add a couple of lines to the onActivityResult() function:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (mTargetFragment != null)       
        mTargetFragment.myCallBack(requestCode, resultCode, data);
}

现在你可以模仿code,你通常会放入的onComplete()回调,在新myCallBack函数()函数:

Now you can just mimic the code you would ordinarily put in the onComplete() callback in the new myCallBack() function:

public void myCallBack(int requestCode, int resultCode, Intent data) {
    mFacebook.authorizeCallback(requestCode, resultCode, data);

    // other code from your onComplete() function ...
}

我希望这可以帮助别人。干杯!

I hope this can help someone else. Cheers!

这篇关于为什么不使用的Andr​​oid碎片的Facebook界面的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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