https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
k

Koya Sivaji

04/19/2020, 1:52 PM
One weird issue I am facing while using NavGraph. Whenever <- (back button) selected in toolbar, soft keyboard is not closed automatically. Seems a bug in AndroidX navigation. Has anyone faced this issue?
e

emmax

04/19/2020, 2:28 PM
It's not an AndroidX issue, but when you move between fragments the keyboard doesn't dismiss automatically, as it would going between activities. Look at the accepted answer to this as a way to dismiss. It could be triggered in your fragments onStop or you could register onBackPressed listener https://stackoverflow.com/questions/3553779/android-dismiss-keyboard
v

voben

04/19/2020, 2:29 PM
Intercept the back button click and dismiss the keyboard before navigating up
Copy code
fun Fragment.hideKeyboardAndNavigateUp() {
    // The keyboard is not dismissed when navigating back. This happens when keyboard is visible and user navigates up (back arrow)
    // Intercept the back button, dismiss keyboard and keep it moving :)
    hideKeyboard()
    findNavController().navigateUp()
}
k

Koya Sivaji

04/19/2020, 2:36 PM
Thank you guys. @voben, does it mean this code to be duplicated in all destination fragments?
@voben, I got it. Added a OnDestinationChangedListener() in Activity hosting NavHostFragment and called this method from `
Copy code
onDestinationChanged
Thank you
👍 1
2 Views