如何在一个 java activity 中添加两个重写 onActivityResult 方法?

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

我无法在一个 java activity 中使用两个重写 onActivityResult 方法。我试过循环,但在这里面使用循环没有任何意义。我附上代码帮助我解决这个问题。

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

您不能多次覆盖同一个方法,这样做实际上没有意义。

只需重写一次并做一个简单的if statements检查:

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

    }
}