简体   繁体   English

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

[英]Android Login button not calling onActivityResult or callback methods

I'm trying to implement the Facebook login button in my Android app, this is what I've done so far: 我正在尝试在我的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);
    }

I also added the Facebook activity and Id in the application manifest. 我还在应用程序清单中添加了Facebook活动和Id。 The app runs fine and when I click the Login button it takes me to the Facebook dialog. 应用程序运行正常,当我单击“登录”按钮时,它会转到Facebook对话框。 The problem here is that none of the callback methods get executed ie onSuccess(), onCancel() and onError(). 这里的问题是没有任何回调方法被执行,即onSuccess(),onCancel()和onError()。 Also the onActivityResult() is not executed either. 此外,onActivityResult()也不会被执行。 Please tell me if I missed something 请告诉我,如果我错过了什么

After a long inspection, I found out that I set the noHistory for the activity holding the fragment to true. 经过长时间的检查,我发现我为保持片段的活动设置了noHistory为true。 This prevented from going back to the activity again once the authentication was successful which led to onActivityResult() not being called. 一旦身份验证成功导致onActivityResult()未被调用,这就无法再次返回活动。

I too have this issue,But is solved by adding method in parent activities OnActivityResult function,to fragment method. 我也有这个问题,但是通过在父活动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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM