How to close android software keyboard?

I was researching how to close the android software keyboard when the user leaves the activity in which he is in. Here is the easiest way:

public static void closeKeyboard(Context c, IBinder windowToken) {
        InputMethodManager mgr = (InputMethodManager) c
        mgr.hideSoftInputFromWindow(windowToken, 0);

    public void onStop() {
        closeKeyboard(getActivity(), getView().getWindowToken());

You may also like...