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?


Solution 1:

Subclass your MainActivity from FragmentActivity, then in composable get your context:

val context = LocalContext.current as FragmentActivity

Check out some examples on github: https://github.com/search?l=kotlin&q=BiometricPrompt%20composable&type=Code

Solution 2:

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
)