我打算在用户离开屏幕后返回到片段时显示 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()
}