SepidehDev
SepidehDev

Reputation: 1

onActivityResult not called after click the button (using Google Drive API)

I use the following link to use the Google Drive API to upload a file to Drive: https://www.youtube.com/watch?v=rznYp43KLRc

When I call requestSignIn() in onCreate, it works correctly. But when I call it in setOnClickListener, it doesn't work!

My code looks like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // When I call requestSignIn here it works correctly 
    // requestSignIn();
}

private void requestSignIn() {
    GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestScopes(new Scope(DriveScopes.DRIVE_FILE))
            .build();
    GoogleSignInClient client = GoogleSignIn.getClient(this,signInOptions);
    startActivityForResult(client.getSignInIntent(), 400);
}


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

    switch (requestCode)
    {
        case 400:
            if(resultCode== RESULT_OK)
            {
                handleSignInIntent(data);
            }
    }
}

private void handleSignInIntent(Intent data) {
    GoogleSignIn.getSignedInAccountFromIntent(data)
            .addOnSuccessListener(new OnSuccessListener<GoogleSignInAccount>() {
                @Override
                public void onSuccess(GoogleSignInAccount googleSignInAccount) {
                    GoogleAccountCredential credential = GoogleAccountCredential
                            .usingOAuth2(MainActivity.this , Collections.singleton(DriveScopes.DRIVE_FILE));

                    credential.setSelectedAccount(googleSignInAccount.getAccount());

                    Drive googleDriveService= new Drive.Builder(
                            AndroidHttp.newCompatibleTransport(),
                            new GsonFactory(),
                            credential)
                            .setApplicationName("My Drive Totorial")
                            .build();
                    driveServiceHelper = new DriveServiceHelper(googleDriveService);



                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });
}


btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            
            if (signatureView.isBitmapEmpty()) {
                Toast.makeText(getApplicationContext(), "Bitte unterschreiben", Toast.LENGTH_SHORT).show();
            }
            else {
                
                // I want to sign in to Google Drive now 
                 requestSignIn(); // but it doesn't work!
            }
        }
    });

What I want is that "Sign in to client" should not be possible while the "activity is loading", but it should only be possible after "submit" has been clicked.

When I debug the code, requestSignIn() is being called correctly, but onActivityResult is not called correctly.

In my code below, after doing

startActivityForResult(client.getSignInIntent(), 400)

it never reaches onActivityResult.

I am a beginner with Android. I don't underestand where is the problem? Do you have any idea?!

Thanks in advance

++++UPDATE 18.04.2023+++

in the mainActivity worked the program without any Problemm as intended

public class MainActivity extends AppCompatActivity {
private Button btnSubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnSubmit = findViewById(R.id.submit);
    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            requestSignIn(); //it works correctly
        }
    });
}
private void requestSignIn() {
    GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestScopes(new Scope(DriveScopes.DRIVE_FILE))
            .build();
    GoogleSignInClient client = GoogleSignIn.getClient(this,signInOptions);
    startActivityForResult(client.getSignInIntent(), 400);
    // I see ++++startActivityForResult+++
    Toast.makeText(this, " ++++startActivityForResult+++", Toast.LENGTH_SHORT).show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // I see +++++++++++onActivityResult+++++++++
    Toast.makeText(this, "+++++++++++onActivityResult+++++++++ ", Toast.LENGTH_SHORT).show();
    switch (requestCode)
    {
        case 400:
            if(resultCode== RESULT_OK)
            {
                handleSignInIntent(data);
            }
    }
}

when I take the same Code to SecondActivity, it doesn't work.

public class SecondActivity extends AppCompatActivity {
private Button btnSubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnSubmit = findViewById(R.id.submit);
    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            requestSignIn(); // it doesn't work!!!!
        }
    });
}
private void requestSignIn() {
    GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestScopes(new Scope(DriveScopes.DRIVE_FILE))
            .build();
    GoogleSignInClient client = GoogleSignIn.getClient(this,signInOptions);
    startActivityForResult(client.getSignInIntent(), 400);
    // I see ++startActivityForResult++
    Toast.makeText(this, " ++startActivityForResult++", Toast.LENGTH_SHORT).show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // But here that shows no ++++++++++onActivityResult+++++++++
    Toast.makeText(this, "+++++++++++onActivityResult+++++++++ ", Toast.LENGTH_SHORT).show();
    switch (requestCode)
    {
        case 400:
            if(resultCode== RESULT_OK)
            {
                handleSignInIntent(data);
            }
    }
}

Upvotes: 0

Views: 109

Answers (0)

Related Questions