How do I check is there is logged in accounts on the phone?

I have a problem with Google Auth.

If the user has signed in Gmail account on the phone I want to open a Credential Manager so that the user could choose an account he wants to use to authenticate in my app.

If the user has no signed in gmail account on the phone I want to open a sign in page so that he could input his gmail account/password and sign in.

Using this cose: GoogleSignIn.getLastSignedInAccount(requireContext()) I try to find out if there is signed in account on the devise or not. But oogleSignIn.getLastSignedInAccount(requireContext()) still returns instance even if there is no logged in account on the phone. What can I do with it? Maybe there is another aproach?

    val account = GoogleSignIn.getLastSignedInAccount(requireContext())
    if (account == null) {
        intentLauncher?.launch((viewModel.getGoogleSignUpIntent()))
    } else {
        viewModel.openGoogleAuthWindow(credentialManager, requireContext())
    }

But when I close the app and sign out from the gmail account and relaunch the app I still have account not null. Do you have an idea how to handle this?

Kotlin

val account = GoogleSignIn.getLastSignedInAccount(requireContext())

if (account == null) {
    // No signed-in account, launch sign-in page
    intentLauncher?.launch(GoogleSignIn.getSignInIntent(requireContext()))
} else {
    // Signed-in account, launch credential manager
    intentLauncher?.launch(credentialManager.getSignInIntent(requireContext()))
}

In your onActivityResult() method:

Kotlin

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode,    resultCode, data)

    if (requestCode == GOOGLE_SIGN_IN_REQUEST_CODE) {
        val task = GoogleSignIn.getSignedInAccountFromIntent(data)   
        task.addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                val account = task.result
                // Proceed with authentication using the signed-in account
            } else {
                // Handle sign-in error
            }
        }
    }
}