title: Hiding softkeyboard reliably
body: <p>I have a application, and need to close the softkeyboard on <em>a rather large amount of actions</em>. For example, when <strong>clicking a button</strong>, when a new <strong>layout is drawn</strong>, on <strong>screen orientation change</strong>, when the <strong>controller tells</strong> the UI to, et cetera.
  I use the optionsMenuButton to flip view with a ViewFlipper, and obviously I want the keyboard to hide in the flipped view (there is no input field there).</p>
  
  <p>I've tried these so far and tell why these aren't reliable:</p>
  
  <p>This one didn't work because I have lots of editTexts, and other views. I need a more generic one, one that does not require a view as argument, if possible.</p>
  
  <pre><code>InputMethodManager imm = (InputMethodManager)getSystemService(
    Context.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
  </code></pre>
  
  <p>This one does not work at all for me:</p>
  
  <pre><code>getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  </code></pre>
  
  <p>This one works, but immediately pops the keyboard up again when the view is flipped. </p>
  
  <pre><code>InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
  imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
  </code></pre>
  
  <p>This one works sometimes, but getCurrentFocus() returns null most of the time.</p>
  
  <pre><code>InputMethodManager inputManager = (InputMethodManager)            
  Context.getSystemService(Context.INPUT_METHOD_SERVICE); 
  inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),      
  InputMethodManager.HIDE_NOT_ALWAYS);
  </code></pre>
  
  <p>This one works only if the keyboard is shown:</p>
  
  <pre><code>getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
  </code></pre>
  
  <p>This one does not work with the EditText like the first piece of code, but with the root Layout, which changes on orientation change and everytime the oncreate is called. I have different layout XML for landscape/portrait and normal/large. All the root Layouts have the ID <code>root</code>. This works nicely the first time, but after that, it doesn't work anymore.</p>
  
  <pre><code>InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  inputManager.hideSoftInputFromWindow(findViewById(R.id.root).getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
  </code></pre>
  
  <p>Bottomline: I've tried hella lot of softkeyboard hiding methods, but none of them seems to work reliably. <strong>Is there <em>any</em> method for hiding the soft keyboard reliably?</strong></p>