Answer the question
In order to leave comments, you need to log in
How to wait for a function to execute in Kotlin?
I have a function that is supposed to return the result of whether the biometric check passed or not
fun fingerPrintResult():Boolean
{
val executor = Executors.newSingleThreadExecutor()
val activity: FragmentActivity = this // reference to activity
var resultReturn:Boolean = false
val biometricPrompt = BiometricPrompt(activity, executor, object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
resultReturn = false
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
resultReturn = true
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
resultReturn = false
}
})
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("Подтвердите личность")
.setNegativeButtonText("Negative Button")
.build()
biometricPrompt.authenticate(promptInfo)
return resultReturn
}
ad.setNegativeButton("Удалить", object : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface, arg1: Int) {
var isTrue: Boolean = false
isTrue = fingerPrintResult()
if(!isTrue)
{
Toast.makeText(applicationContext,"Error!", Toast.LENGTH_SHORT).show()
return
}
var value = item["id"].toString()
var db = DBHandler(parent.context)
db.deleteCountry(Integer.parseInt(value))
}
})
Answer the question
In order to leave comments, you need to log in
Doesn't it bother you at all that you are waiting for the same dialogue and doing something in the callback? The same meaning. In the methods you get the result.
Something like this:
suspend fun fingerPrintResult(): Boolean {
val executor = Executors.newSingleThreadExecutor()
val activity: FragmentActivity = this // reference to activity
return suspendCoroutine { coroutine ->
val biometricPrompt = BiometricPrompt(activity, executor, object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
coroutine.resume(false)
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
coroutine.resume(true)
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
coroutine.resume(false)
}
})
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("Подтвердите личность")
.setNegativeButtonText("Negative Button")
.build()
biometricPrompt.authenticate(promptInfo)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question