Screen blinks when user goes to another activity and comes back to MainActivity.class

Viewed 425

My user is logged in but when I call below lines of code, if a user is logged in I am getting need permission error.

  mDriveServiceHelper = new DriveServiceHelper(getGoogleDriveService(getApplicationContext(), alreadyloggedAccount, "appname"));


 public static Drive getGoogleDriveService(Context context, GoogleSignInAccount account, String appName) {
        GoogleAccountCredential credential =
                GoogleAccountCredential.usingOAuth2(
                        context, Collections.singleton(DriveScopes.DRIVE_FILE));
        credential.setSelectedAccount(account.getAccount());
        com.google.api.services.drive.Drive googleDriveService =
                new com.google.api.services.drive.Drive.Builder(
                        AndroidHttp.newCompatibleTransport(),
                        new GsonFactory(),
                        credential)
                        .setApplicationName(appName)
                        .build();
        return googleDriveService;
    }

In my case every time I have to call the below methods then only it works.

when I call requestSignIn() and then startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN). then only if user is not having permission the drive access popup is coming and next time when user logins to the app that drive access popup is not coming (which is expected). but the application is trying to open something (some activity or google drive popup) I think and as the user is having permission, it is not showing popup but the screen blinks once every time we go to another activity and come back on mainactivity.class. (this google drive code is written in main activity)

 private void requestSignIn() {

        Log.d(TAG, "Requesting sign-in");
        GoogleSignInOptions signInOptions =
                new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        .requestScopes(new Scope(DriveScopes.DRIVE_FILE))
                        .requestEmail()
                        .build();

        googleSignInClient = GoogleSignIn.getClient(this, signInOptions);


    }

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

        switch (requestCode) {
            case REQUEST_CODE_SIGN_IN:
                if (resultCode == Activity.RESULT_OK && resultData != null) {
                    handleSignInResult(resultData);
                }
                break;


        }

        super.onActivityResult(requestCode, resultCode, resultData);
    }

    String TAG = ":::: Backup User Data ::::::::::";
    private void handleSignInResult(Intent result) {
        GoogleSignIn.getSignedInAccountFromIntent(result)
                .addOnSuccessListener(googleAccount -> {
                    //System.out.println(">>>>>>>>>>>> handleSignInResult inside Mainactivity>>>>>>>>>>>>>>>. ");
                    Log.d(TAG, "Signed in as " + googleAccount.getEmail());

                    // Use the authenticated account to sign in to the Drive service.
                    GoogleAccountCredential credential =
                            GoogleAccountCredential.usingOAuth2(
                                    this, Collections.singleton(DriveScopes.DRIVE_FILE));
                    credential.setSelectedAccount(googleAccount.getAccount());
                    Drive googleDriveService =
                            new Drive.Builder(
                                    AndroidHttp.newCompatibleTransport(),
                                    new GsonFactory(),
                                    credential)
                                    .setApplicationName("Drive API Migration")
                                    .build();

                    // The DriveServiceHelper encapsulates all REST API and SAF functionality.
                    // Its instantiation is required before handling any onClick actions.
                    mDriveServiceHelper = new DriveServiceHelper(googleDriveService);
                })
                .addOnFailureListener(exception -> Log.e(TAG, "Unable to sign in.", exception));
    }

i dont want that black line should pass from my screen everytime i go to another screen and comes back to MainActivity.class. (i think the black is passing from screen because whenever i am coming to MainActivity.class my code is again checking for google drive access and trying to show popup but as user is having permission the popup is disappearing)

can you please help me in above issue.

1 Answers

Studying into the source code of related classes, I find this function GoogleSignIn.getLastSignedInAccount should be useful. Call requestSignIn() at first time. If it's signed in before, use below function instead of requestSignIn(). This avoids startActivityForResult() and the blink.

private void handleSignInWithoutActivity() { //a little modification from handleSignInResult()
    GoogleSignInAccount googleAccount = GoogleSignIn.getLastSignedInAccount(getApplicationContext());

    if(googleAccount!=null){
                Log.d(TAG, "Signed in as " + googleAccount.getEmail());

                // Use the authenticated account to sign in to the Drive service.
                GoogleAccountCredential credential =
                        GoogleAccountCredential.usingOAuth2(
                                this, Collections.singleton(DriveScopes.DRIVE_FILE));
                credential.setSelectedAccount(googleAccount.getAccount());
                Drive googleDriveService =
                        new Drive.Builder(
                                AndroidHttp.newCompatibleTransport(),
                                new GsonFactory(),
                                credential)
                                .setApplicationName(getString(R.string.app_name))
                                .build();

                // The DriveServiceHelper encapsulates all REST API and SAF functionality.
                // Its instantiation is required before handling any onClick actions.
                mDriveServiceHelper = new DriveServiceHelper(googleDriveService);
    }else {
        //impossibly because it's signed in before. Error handle if it happens.
    }
}
Related