onResume 中的 Biometric Prompt 抛出 FragmentManager is already executing transactions

Biometric Prompt in onResume throws FragmentManager is already executing transactions

我打算在用户离开屏幕后 returns 向片段显示 androidx 生物识别提示。为此,我将身份验证逻辑放在片段级别 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()
}

我通过在片段而不是 activity 上调用生物识别提示解决了这个问题。

变化是

biometricPrompt = BiometricPrompt(this, executor, callback)

而不是

biometricPrompt = BiometricPrompt(requireActivity(), executor, callback)