在facebook上没有调用onActivityResult登录

时间:2016-09-22 13:27:07

标签: android callback facebook-sdk-4.x

我有一个带有facebook登录的Android应用程序。我在片段中初始化了facebook sdk。但是在启动登录过程时从未调用过onActivityResult。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        FacebookSdk.sdkInitialize(getContext());
        AppEventsLogger.activateApp(getContext());
        FacebookSdk.setApplicationId(getResources().getString(R.string.facebook_app_id));
        callbackManager = CallbackManager.Factory.create();
        View view=inflater.inflate(R.layout.fragment_signin, container, false);
        gButton=(Button)view.findViewById(R.id.loginGmail);
        fButton=(LoginButton) view.findViewById(R.id.loginFacebook);
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();
        fButton.setReadPermissions("email");
        gButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });
        mGoogleApiClient = new GoogleApiClient.Builder(getContext())
                .enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
        fButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Toast.makeText(getActivity(),"Success"+loginResult.getAccessToken(),Toast.LENGTH_LONG).show();

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {
                Toast.makeText(getActivity(),"Failed",Toast.LENGTH_LONG).show();

            }
        });

        return view;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode,resultCode, data);
        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }
    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

但是当google登录时调用了onActivityResult。

2 个答案:

答案 0 :(得分:7)

最后我得到了答案。 我忘了添加

fButton.setFragment(this);
  

对于片段,此setFragment(this);方法是必需的。参考   this

答案 1 :(得分:0)

这是因为您在Fragment中集成facebook以获得片段onActivty结果,您必须从Activty OnActivity结果调用它

check this image for better understanding

你在活动OnACtivity结果中的这段代码在超级调用之前你会得到你的结果片段onActivity结果希望这个帮助