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
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.hideSoftInputFromWindow(windowToken, 0);
    }

    @Override
    public void onStop() {
        super.onStop();
        closeKeyboard(getActivity(), getView().getWindowToken());
    };

You may also like...