Android登录按钮不调用onActivityResult或回调方法

时间:2015-10-14 06:41:09

标签: android facebook facebook-login facebook-sdk-4.0

我正在尝试在我的Android应用中实现Facebook登录按钮,这是我到目前为止所做的:

public class LoginFragment extends BaseFragment {
    private View mView;
    private CallbackManager mCallbackManager;
    private LoginButton mFBLoginButton;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FacebookSdk.sdkInitialize(getActivity());
        mCallbackManager = CallbackManager.Factory.create();
        mView = inflater.inflate(R.layout.login, container, false);
        mFBLoginButton = (LoginButton) mView.findViewById(R.id.login_facebook_button);
        mFBLoginButton.setReadPermissions("public_profile", "user_friends","user_birthday","user_about_me","email");
        mFBLoginButton.setFragment(this);
        mFBLoginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d("LoginFragment", "UserId: " + loginResult.getAccessToken().getUserId());
        }

        @Override
        public void onCancel() {
            Log.d("LoginFragment", "Operation Canceled");
        }

        @Override
        public void onError(FacebookException e) {
            e.printStackTrace();
        }
        });
        return mView;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getBaseActivity().displayToastMessage("Hey");
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }

我还在应用程序清单中添加了Facebook活动和ID。应用程序运行正常,当我单击“登录”按钮时,它会转到Facebook对话框。这里的问题是没有任何回调方法被执行,即onSuccess(),onCancel()和onError()。此外,onActivityResult()也不会被执行。请告诉我,如果我错过了什么

2 个答案:

答案 0 :(得分:3)

经过长时间的检查,我发现我为保持片段的活动设置了noHistory为true。一旦身份验证成功导致onActivityResult()未被调用,就无法再次返回活动。

答案 1 :(得分:0)

我也有这个问题,但是通过在父活动OnActivityResult函数中添加方法来解决片段方法。

@Override
public void onAttachFragment(Fragment fragment) {
    super.onAttachFragment(fragment);
    try {
        String fragmentSimpleName = fragment.getClass().getSimpleName();
        if (fragmentSimpleName.equals("Fragment1")){ 
            frag = (Fragment1) fragment;
        }

    } catch (Exception e) { 
        e.printStackTrace();
    }
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // facebook.authorizeCallback(requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (frag != null){
            frag.myCallBack(requestCode, resultCode, data);
        }
    } catch (Exception e) { 
        e.printStackTrace();
    }
}