BiometricPrompt with Jetpack Compose

Viewed 1687

BiometricPrompt requires either Fragment or FragmentActivity in its constructor. I cannot find out how to use BiometricPrompt from a Composable screen, not in the documentation, not in any tutorial. Has anyone in here dealt with the same problem? Or is there any other way to use biometric authentication in a fully Compose built application?

4 Answers

Ok, it was quite simple in the end, but has taken me hours, so here is the answer for anyone struggling with this.

Make sure, that your MainActivity inherits from FragmentActivity(). Then you will be able to cast LocalContext.current to FragmentActivity.

val context = LocalContext.current as FragmentActivity
val biometricPrompt = BiometricPrompt(
    context,
    authenticationCallback
)

replace ComponetActivity() with FragmentActivity() then use normal compose view from FragmentActivity()

 setContent {
            FingerPrintAppTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
                ) {
                    Greeting("Android")
                }
            }
        }

then everything works fine

For those that cannot change their activity base class, there is issuetracker.google.com/issues/178855209 to request for a biometric-compose artifact. Unfortunately, as of version 1.2.0-alpha04 no work has been done towards it.

Related