Search code examples
androiddialoggoogle-drive-apigoogle-drive-android-api

Google Drive Account Chooser Dialog won't cancel


I am using google drive on android to backup files. Backup is working fine. However when I am presented with the dialog to choose an account to back up to, if I hit "Cancel" the dialog just closes and reappears. It gets caught in a loop and I am forced to choose an account. While debugging the code I see that onConnectionFailed() is called everytime I hit cancel. SIGN_IN_REQUIRED is the resolution code passed to it and so the dialog gets called again.

How can I break out of this loop and allow the user to cancel? Has anybody else had this issue?

Thanks!


Solution

  • I fixed the issue by editing my onActivityResult method.

    Previously I had:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
            mGoogleApiClient.connect();
        }
    }
    

    I changed this so that if there was no resolution (which is what happens when cancel is clicked), I just finish the activity hosting the dialog:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
            mGoogleApiClient.connect();
        }
        else {
    
          finish();
        }
    }
    

    So now the activity ends if the user clicks cancel, which is fine for my purposes.