Dismiss Keyboard After Leaving Fragment
May 15, 2019
When switch / replace a fragment, the keyboard normally does not dismiss itself. Here is a quick way to ensure keyboard dismissed after the fragment replace (Leaving the fragment).
Utility Function
First, you will need a utility function to dismiss keyboard.
1 | class KeyboardUtil { |
Now you can call this function anywhere and pass in the current activity to easily dismiss keyboard.
Question is where do you call this utility function?
In Fragment
You want to call this utility function in fragment onPause life cycle.
1 |
|
Note that when you deactivate the app (moving to background), the keyboard will dismiss too. This is because the onPause is called when app into background as well.
