1313 * </p>
1414 */
1515public final class KeyboardUtil {
16+
17+ /**
18+ * Flag for {@link #hideKeyboard(Activity, EditText)} to force the keyboard to hide.
19+ * This is required for non-touch devices like Fire TV where the system
20+ * flags the focus as an explicit user action.
21+ */
22+ private static final int CLEAR_SOFT_INPUT_FORCED = 0 ;
23+
1624 private KeyboardUtil () {
1725 }
1826
@@ -24,7 +32,8 @@ public static void showKeyboard(final Activity activity, final EditText editText
2432 if (editText .requestFocus ()) {
2533 final InputMethodManager imm = ContextCompat .getSystemService (activity ,
2634 InputMethodManager .class );
27- if (!imm .showSoftInput (editText , InputMethodManager .SHOW_FORCED )) {
35+
36+ if (imm != null && !imm .showSoftInput (editText , InputMethodManager .SHOW_FORCED )) {
2837 /*
2938 * Sometimes the keyboard can't be shown because Android's ImeFocusController is in
3039 * a incorrect state e.g. when animations are disabled or the unfocus event of the
@@ -38,7 +47,7 @@ public static void showKeyboard(final Activity activity, final EditText editText
3847 imm .showSoftInput (editText , InputMethodManager .SHOW_FORCED );
3948 }
4049 }
41- }
50+ } // <-- This was the missing brace in your code!
4251
4352 public static void hideKeyboard (final Activity activity , final EditText editText ) {
4453 if (activity == null || editText == null ) {
@@ -47,8 +56,9 @@ public static void hideKeyboard(final Activity activity, final EditText editText
4756
4857 final InputMethodManager imm = ContextCompat .getSystemService (activity ,
4958 InputMethodManager .class );
50- imm .hideSoftInputFromWindow (editText .getWindowToken (),
51- InputMethodManager .HIDE_NOT_ALWAYS );
59+ if (imm != null ) {
60+ imm .hideSoftInputFromWindow (editText .getWindowToken (), CLEAR_SOFT_INPUT_FORCED );
61+ }
5262
5363 editText .clearFocus ();
5464 }
0 commit comments