I intend to show androidx biometric prompt whenever the user returns to the fragment after navigating away from screen. For this, I have placed the authentication logic in Fragment level onResume. It works fine while navigating in app. But, whenever I navigate to a different app and comeback or rotate the screen, the app crashes with exception
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)
My onResume
code:
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()
}
I resolved the issue by calling the biometric prompt on fragment instead of activity.
The change was
biometricPrompt = BiometricPrompt(this, executor, callback)
instead of
biometricPrompt = BiometricPrompt(requireActivity(), executor, callback)