简体   繁体   English

如何连接 2 个 onActivityResult?

[英]How to connect 2 onActivityResult?

I have problem with adding a Facebook Authentication because I'm using Google Authentication too and this is why I have error我在添加 Facebook 身份验证时遇到问题,因为我也在使用 Google 身份验证,这就是我出错的原因

         public class SignUpActivity extends AppCompatActivity {
                callbackManager.onActivityResult(requestCode, resultCode, data);
                super.onActivityResult(requestCode, resultCode, data);
            //Google
            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);

                // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
                if (requestCode == RC_SIGN_IN) {
                    Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
                    try {
                        // Google Sign In was successful, authenticate with Firebase
                        GoogleSignInAccount account = task.getResult(ApiException.class);
                        firebaseAuthWithGoogle(account);
                    } catch (ApiException e) {
                        // Google Sign In failed, update UI appropriately
                        Log.w(TAG, "Google sign in failed", e);
                        // ...
                    }
                }
            }

            //and Facebook
            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            callbackManager.onActivityResult(requestCode, resultCode, data);
            super.onActivityResult(requestCode, resultCode, data);
    }

How you can see I have two onActivityResult methods.你怎么看我有两个 onActivityResult 方法。 Is there any way to connect them and get rid of an error ?有什么方法可以连接它们并消除错误吗? This is how looks like my error这就是我的错误

method onActivityResult(int,int,Intent) is already defined in class SignUpActivity方法 onActivityResult(int,int,Intent) 已在类 SignUpActivity 中定义

It's just a communicate of existing two the same methods.它只是现有两种相同方法的交流。 Thanks.谢谢。

onActivityResult is a Android method, it receives results from activities you started with startActivityForResult and returns the request_code int you provided. onActivityResult是一个 Android 方法,它接收你从startActivityForResult开始的活动的结果,并返回你提供的 request_code int。

The solution is to use different REQUEST_CODES at startActivityForResult , so you can compare then in onActivityResult解决方案是在startActivityForResult使用不同的 REQUEST_CODES ,因此您可以在onActivityResult进行比较

like:喜欢:

private static final int FACEBOOK_REQUEST_CODE = 1;
private static final int GOOGLE_REQUEST_CODE = 0;

startActivityForResult(googleLoginIntent, GOOGLE_REQUEST_CODE)
startActivityForResult(facebookLoginIntent, FACEBOOK_REQUEST_CODE)

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GOOGLE_REQUEST_CODE) {
        //do the code for google result
    } else if (requestCode == FACEBOOK_REQUEST_CODE) {
        // do the code for facebook result
    }
}

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

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