BiometricPrompt.authenticate中的java.lang.IllegalStateException错误

时间:2019-05-29 10:28:52

标签: android android-biometric-prompt

我在项目中使用androidx.biometric:biometric:1.0.0-alpha04库。但是,当我尝试使用BiometricPrompt进行身份验证时,可能会在crashlytics和Play-store中看到很多崩溃。崩溃发生在以下代码

myBiometricPrompt.authenticate(promptInfo, mcryptoObject!!)

以下是crashlytics的堆栈跟踪

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
   at androidx.fragment.app.FragmentManagerImpl.checkStateLoss(FragmentManagerImpl.java:2080)
   at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManagerImpl.java:2106)
   at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
   at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:637)
   at androidx.fragment.app.DialogFragment.show(DialogFragment.java:144)
   at androidx.biometric.BiometricPrompt.authenticateInternal(BiometricPrompt.java:499)
   at androidx.biometric.BiometricPrompt.authenticate(BiometricPrompt.java:452)
   at com.transfast.transfast.biometric.BiometricHelper$startAuth$2.run(BiometricHelper.java:132)
   at android.os.Handler.handleCallback(Handler.java:790)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:164)
   at android.app.ActivityThread.main(ActivityThread.java:7000)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

1 个答案:

答案 0 :(得分:1)

我们发现可以通过显式检查当前活动是否处于恢复状态来解决此问题:

FragmentActivity activity = ((FragmentActivity) activity);
Lifecycle lifecycle = activity.getLifecycle();
Lifecycle.State lifecycleState = lifecycle.getCurrentState();
if (lifecycleState != Lifecycle.State.RESUMED) {
    // don't
    return;
}

BiometricPrompt biometricPrompt = new BiometricPrompt(activity, mExecutor, callback);
biometricPrompt.authenticate(promptInfo);

它必须特别是RESUMED,因为STARTED仍可以允许呼叫在onSaveInstanceState之后发生

请参见https://developer.android.com/topic/libraries/architecture/lifecycle