title: Dismiss Keyboard on button click that close fragment
body: <p>How do I close the keyboard on button click? I have a fragment which has an EditText and two buttons. One  submits the EditText content, the other simply closes the fragment. Now when the fragment is gone, the keyboard stays. However, pressing the back button closes the keyboard or clicking on "done" also closes it. But what I need is the keyboard disappear when the fragment is closed.</p>
  
  <p>I've tried solutions on similar questions <a href="https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard">here</a>,<a href="https://stackoverflow.com/questions/3400028/close-virtual-keyboard-on-button-press">here</a> or <a href="https://stackoverflow.com/questions/4551874/why-doesnt-the-virtual-keyboard-go-away?rq=1">here</a> but none seems to work. Most of them throw a <code>NullPointerException</code>. All are for activities not fragments. The code for calling the keyboard works:</p>
  
  <pre><code>editText.requestFocus();
  InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
  </code></pre>
  
  <p>However I had to add getActivity() to make it work.</p>
  
  <p>Any help will be appreciated.</p>