简体   繁体   中英

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

         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. 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

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.

The solution is to use different REQUEST_CODES at startActivityForResult , so you can compare then in 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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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