Android : Hiding keyboard - getCurrentFocus() return null

on Tuesday, September 9, 2014


I want to implement generic method to hide keyboard from any activity. My code:



public static void hide(Activity activity) {
if (null == activity) {
return;
}

View view = activity.getCurrentFocus();
if (null == view) {
return;
}

InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}


It works well for most of my activities but not for all of them. The problem is that sometimes getCurrentFocus() return null even when selected editText is focused. What can be wrong with this activity? Why this method return null?


0 comments:

Post a Comment