Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the soft keyboard being hidden on resume?

I have code like the following to immediately show the soft keyboard when entering my app:

@Override
protected void onResume() {
    super.onResume();

    ...

    myEditText.requestFocus();
    myEditText.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(myEditText, InputMethodManager.SHOW_IMPLICIT);
        }   
    }, 100);

    ...

}

However, on the Android 2.1 emulator, the keyboard appears and then immediately disappears. If I make the delay longer, like 1000, it reliably appears. On an Android 4.0 emulator, a delay of 100 reliably shows the keyboard, but shorter delays do not.

Does anyone know who might be hiding the keyboard? Is there a reliable way to prevent it? If not, is there a delay I can use to guarantee that the keyboard will show?

like image 506
Travis Avatar asked Feb 20 '13 06:02

Travis


1 Answers

If I understand you correctly, I think you can remove the following code in your onResume():

myEditText.postDelayed(new Runnable() {
    @Override
    public void run() {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(myEditText, InputMethodManager.SHOW_IMPLICIT);
    }   
}, 100);

And simply use android:windowSoftInputMode="stateAlwaysVisible" for your Activity in the manifest.

like image 114
Joe Avatar answered Oct 01 '22 22:10

Joe