gpt4 book ai didi

android - onResume 中的生物识别提示抛出 FragmentManager 已经在执行事务

转载 作者:行者123 更新时间:2023-12-04 03:56:24 28 4
gpt4 key购买 nike

我打算在用户离开屏幕后返回到 fragment 时显示 androidx 生物识别提示。为此,我将身份验证逻辑放置在 Fragment 级别的 onResume 中。它在应用程序中导航时工作正常。但是,每当我导航到另一个应用程序并返回或旋转屏幕时,应用程序都会崩溃并出现异常

java.lang.RuntimeException: Unable to resume activity {com.beekay.myapp/com.beekay.myapp.MainActivity}: java.lang.IllegalStateException: FragmentManager is already executing transactions
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4205)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalStateException: FragmentManager is already executing transactions
at androidx.fragment.app.FragmentManager.ensureExecReady(FragmentManager.java:1778)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1843)
at androidx.fragment.app.FragmentManager.executePendingTransactions(FragmentManager.java:489)
at com.beekay.myapp.MyFragment.onResume(MyFragment.kt:147)
我的 onResume代码:
override fun onResume() {
executor = ContextCompat.getMainExecutor(requireContext())

val callback = object : BiometricPrompt.AuthenticationCallback() {

override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
findNavController().popBackStack()
}

override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
findNavController().popBackStack()
}

override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)

//Continue the current work

}
}
val activity = activity
if (activity!=null) {
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("Investments")
.setSubtitle("Place your finger on fingerprint scanner to view investments")
.setNegativeButtonText("Cancel")
.build()
biometricPrompt = BiometricPrompt(requireActivity(), executor, callback)
biometricPrompt.authenticate(promptInfo)
}
super.onResume()
}

最佳答案

我通过在 fragment 而不是 Activity 上调用生物识别提示解决了这个问题。
变化是

biometricPrompt = BiometricPrompt(this, executor, callback)
代替
biometricPrompt = BiometricPrompt(requireActivity(), executor, callback)

关于android - onResume 中的生物识别提示抛出 FragmentManager 已经在执行事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63806437/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com