The code snippet below is from my onOptionsItemSelected function. Lines 2-5 below are suppose to hide the soft keyboard if it is showing OR call the activity's finish function otherwise.
I got this piece of code from one of the answers to 'How to Hide a SoftKeyboard' on stack overflow. It works fine on my phone but when I recently submitted my app for internal testing on playstore, I found out that it is throwing NPE sometimes. Can someone explain the logic behind why this might be happening please?
R.id.done -> {
val view:View? = this.currentFocus!! // throws null pointer exception
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (bool && view!=null) {
imm.hideSoftInputFromWindow(view.windowToken, 0)
} else{
finish()
}
return true