Question
When using the Chat SDK, sometimes if I leave the chat activity while typing, the keyboard stays open and I have to manually close it. How can I avoid that?
Answer
When this occurs, the easiest solution is to forcefully close the keyboard every time you come back to the main activity in the Chat UI.
In the example below, the MainActivity is starting the Chat UI. This will be the activity that you come back to once you close the Chat UI. In this example, the script creates a method in which MainActivity hides the keyboard when the view starts. See the code example below in Kotlin.
class MainActivity : AppCompatActivity() {
private fun hideKeyboard() {
val view = currentFocus ?: View(this)
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.rootView.windowToken, 0)
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
view.clearFocus()
}
override fun onStart() {
super.onStart()
if (Chat.INSTANCE.providers()?.connectionProvider()?.connectionStatus == ConnectionStatus.DISCONNECTED) {
hideKeyboard()
}
}
}
Make sure to check the connectionStatus
on the onStart
activity callback and see if the Chat is in state ConnectionStatus.DISCONNECTED
, which means that the MessagingActivity
is dismissed, and then the integrator can hideKeyboard
.