11

I have an activity that users type inside and then click the ok button. When it is finished, the activity closes and goes back to the old activity but the soft keyboard is still on the screen! I've tried android:windowSoftInputMode="stateHidden" and

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

But it does nothing.

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Sean Pan
  • 473
  • 2
  • 6
  • 21

1 Answers1

29

In OnPause of your activity, you should do the following

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(et.getWindowToken(), 0);

Where et is an instance of your EditText.

CommonMan
  • 3,760
  • 2
  • 20
  • 35
  • I've used this approach to implement my "Search Activity" within "Start activity for result" to hide keyboard. Thank you! – Roman Dec 28 '14 at 19:58
  • 3
    I've a similar situation where an activity opened by an SDK is not dismissing the keyboard when the activity finishes. I end up with a keyboard that's covering half my activity. Any idea how to dismiss the keyboard shown by a different activity? – hitmaneidos Jan 06 '16 at 12:42
  • 2
    What if you have multiple EditText ? how do you know from which to call getWindowToken() ? – Janx from Venezuela Mar 03 '17 at 20:21
  • 3
    i used `imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);`, when there are more than one EditText on my layout. – Ric17101 Aug 06 '18 at 09:13