Ahmad Hassan
Ahmad Hassan

Reputation: 588

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();
}

Upvotes: 0

Views: 271

Answers (1)

Hasan Bou Taam
Hasan Bou Taam

Reputation: 4035

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();

    }
}

Upvotes: 2

Related Questions