androidkotlinandroid-softkeyboardandroid-bottomsheetdialog

requestFocus() on EditText, dismiss BotSheetDialog w/o hide keyboard and keyboard strangely show up


I have an EditText inside BotSheetDialog, requestFocus() on it on onViewCreated(). And then I can dismiss the dialog in two way: click the SAVE button or click to the outside of the dialog. If i manually press hide keyboard button, it will work as expected. But if I don't do that, keyboard will show from nowhere.

// BottomDialog.kt
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loadArgs()
        setClickListeners()
        binding.etTitle.requestFocus()
    }

I have tried to clearFocus() on it before Destroy View but it doesn't work.

// BottomDialog.kt
override fun onDestroyView() {
        binding.etTitle.clearFocus()
        super.onDestroyView()
    }

Thank you in advance! Here are the figures:

https://i.sstatic.net/TGBQG.png
https://i.sstatic.net/eUn5s.png

UPDATE

// My fun to hide keyboard
fun Activity.hideSoftKeyboard(view: View? = null) : Boolean {
    val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as
            InputMethodManager
    if (inputMethodManager.isAcceptingText) {
        if (view == null) {
            inputMethodManager.hideSoftInputFromWindow(this.currentFocus?.windowToken, 0)
        } else {
            inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
        }
        return true
    }
    return false
}
// BottomDialog.kt
override fun onPause() {
        val res = requireActivity().hideSoftKeyboard(binding.etTitle)
        log("onPause, $res")
        super.onPause()
    }

    override fun onStop() {
        val res = requireActivity().hideSoftKeyboard(binding.etTitle)
        log("onStop, $res")
        super.onStop()
    }

    override fun onDestroyView() {
        val res = requireActivity().hideSoftKeyboard(binding.etTitle)
        log("onDestroyView, $res")
        super.onDestroyView()
    }

    override fun onDestroy() {
        val res = requireActivity().hideSoftKeyboard(binding.etTitle)
        log("onDestroy, $res")
        super.onDestroy()
    }
/*
    Logcat:
    onPause, false
    onStop, false
    onDestroyView, false
    onDestroy, false
 */

Solution

  • I solved it:

    // AndroidManifest.xml
    <activity
        ...
    // Change this
        android:windowSoftInputMode="stateVisible|adjustPan"
    
    // Into this
        android:windowSoftInputMode="adjustPan"
        ...
    </activity>