简体   繁体   中英

How to add two override onActivityResult methods in one java activity?

I am unable to use two override onActivityResult methods in one java activity. I tried loop but that doesn't make any sense to use loop in this. I am attaching the code help me out in this.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Intent home = new Intent(this, HomeActivity.class);
        startActivity(home);
    } else {
        Toast.makeText(getApplicationContext(), "Unable to login please check your internet connection", Toast.LENGTH_LONG).show();
    }
}};

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);
        startActivity(new Intent(LoginActivity.this, HomeActivity.class));
    } catch (ApiException e) {
        Log.w("Google Sign In Error", "signInResult:failed code=" + e.getStatusCode());
        Toast.makeText(LoginActivity.this, "Failed", Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onStart() {
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    if(account != null) {
        startActivity(new Intent(LoginActivity.this, HomeActivity.class));
    }
    super.onStart();
}

You can't override the same method more than one time, and it actually doesn't make sense to do so.

Just override once and do a simple if statements to check:

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

    if (resultCode == RESULT_OK && requestCode != RC_SIGN_IN) {
        Intent home = new Intent(this, HomeActivity.class);
        startActivity(home);

    }else if(requestCode == RC_SIGN_IN){

        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);

    } else {

        Toast.makeText(getApplicationContext(), "Unable to login please check your internet connection", Toast.LENGTH_LONG).show();

    }
}

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