It is necessary that when the on-screen keyboard appears (when you click on EditText ), one code is executed, and when it is hidden, another one. When creating an activity keyboard is hidden.
The variant with OnFocusChangeListener does not roll, because EditText is the only field on the activity and it is always in focus.