[英]Android Biometrics not navigating when using Device Credentials
I'm using Biometrics library to lock the app.我正在使用生物识别库来锁定应用程序。 Everything works fine and when I'm unlocking with fingerprint onAuthenticationSucceeded() get's called and device navigates from the lock screen.
一切正常,当我使用指纹解锁时 onAuthenticationSucceeded() 被调用并且设备从锁定屏幕导航。 However if unlock with pattern the onAuthenticationSucceeded() get's called but navigation doesn't initialise and I'm left stuck on the lock screen fragment.
但是,如果使用模式解锁 onAuthenticationSucceeded() 会被调用,但导航不会初始化,我会卡在锁定屏幕片段上。
EDIT: This only affects API29 with ANY device credentials编辑:这仅影响具有任何设备凭据的 API29
EDIT2: I'm also getting EDIT2:我也得到
FragmentNavigator: Ignoring popBackStack() call: FragmentManager has already saved its state
FragmentNavigator:忽略 popBackStack() 调用:FragmentManager 已经保存了它的状态
FragmentNavigator: Ignoring navigate() call: FragmentManager has already saved its state
FragmentNavigator:忽略导航()调用:FragmentManager 已经保存了它的状态
private lateinit var biometricPrompt: BiometricPrompt
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
biometricPrompt = createBiometricPrompt()
return inflater.inflate(R.layout.lock_screen_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val isAppLockEnabled: Boolean = PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean("lock_app_preference", false)
// If app locks is not set go to home fragment else display app lock screen
if (!isAppLockEnabled) {
findNavController().navigate(R.id.action_lock_screen_fragment_dest_to_home_fragment_dest)
} else {
// Prompt appears when user clicks "Unlock".
unlock_button.setOnClickListener {
val promptInfo = createPromptInfo()
biometricPrompt.authenticate(promptInfo)
}
}
}
private fun createBiometricPrompt(): BiometricPrompt {
val executor = ContextCompat.getMainExecutor(context)
val callback = object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Log.d("AuthenticationError()", "$errorCode :: $errString")
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Log.d("AuthenticationFailed()", "Authentication failed for an unknown reason")
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
lock_icon.setImageResource(R.drawable.ic_unlock)
lock_screen_text_view.text = getString(R.string.app_unlocked)
//This doesn't work when using pattern unlock findNavController().navigate(R.id.action_lock_screen_fragment_dest_to_home_fragment_dest)
}
}
return BiometricPrompt(this, executor, callback)
}
private fun createPromptInfo(): BiometricPrompt.PromptInfo {
return BiometricPrompt.PromptInfo.Builder()
.setTitle("Unlock App")
.setConfirmationRequired(false)
.setDeviceCredentialAllowed(true)
.build()
}
}
Ok, so I solved this issue.好的,所以我解决了这个问题。 Moved navigation from onAuthenticationSucceeded() to fragments onResume().
将导航从 onAuthenticationSucceeded() 移动到 onResume() 片段。 Device credentials window pauses my app and somehow navigation cannot be called after that.
设备凭据窗口暂停了我的应用程序,之后无法调用导航。
Solution code:解决方案代码:
private var isAppUnlocked : Boolean = false
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
isAppUnlocked = true
unlockApp()
Log.d("AuthenticationSuccess", "Authentication succeeded")
}
override fun onResume() {
super.onResume()
if(isAppUnlocked){
unlockApp()
}
}
private fun unlockApp(){
lock_icon.setImageResource(R.drawable.ic_unlock)
lock_screen_text_view.text = getString(R.string.app_unlocked)
findNavController().navigate(R.id.action_lock_screen_fragment_dest_to_home_fragment_dest)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.