Fix currentFocus

Looks like currentFocus does not always exist even after setContentView,
so I hereby use another way to check if setContentView is called
This commit is contained in:
LoveSy 2023-03-08 16:59:25 +08:00 committed by John Wu
parent 382568bd3c
commit 3cf66d1c57
2 changed files with 4 additions and 4 deletions

View File

@ -20,13 +20,11 @@ abstract class NavigationActivity<Binding : ViewDataBinding> : UIActivity<Bindin
val navigation: NavController get() = navHostFragment.navController val navigation: NavController get() = navHostFragment.navController
override fun dispatchKeyEvent(event: KeyEvent): Boolean { override fun dispatchKeyEvent(event: KeyEvent): Boolean {
return currentFocus?.let { return if (binded && currentFragment?.onKeyEvent(event) == true) true else super.dispatchKeyEvent(event)
currentFragment?.onKeyEvent(event).takeIf { it == true }
} ?: super.dispatchKeyEvent(event)
} }
override fun onBackPressed() { override fun onBackPressed() {
currentFocus?.let { if (binded) {
if (currentFragment?.onBackPressed() == false) { if (currentFragment?.onBackPressed() == false) {
super.onBackPressed() super.onBackPressed()
} }

View File

@ -23,6 +23,8 @@ abstract class UIActivity<Binding : ViewDataBinding> : BaseActivity(), ViewModel
protected lateinit var binding: Binding protected lateinit var binding: Binding
protected abstract val layoutRes: Int protected abstract val layoutRes: Int
protected val binded get() = ::binding.isInitialized
open val snackbarView get() = binding.root open val snackbarView get() = binding.root
open val snackbarAnchorView: View? get() = null open val snackbarAnchorView: View? get() = null