title: Cleaner way to maintain virtual keyboard on orientation change
body: <p>I have the following EditText in my XML:</p>
  
  <pre><code>&lt;EditText
    android:id="@+id/searchField"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="What do you want to eat?"
    android:imeOptions="actionSearch"
    android:inputType="text"
    android:maxLines="1"
    ... /&gt;
  </code></pre>
  
  <p>On orientation change, I will record the focused state of the EditText, <code>searchField</code>. I record this state using <code>override fun onSaveInstanceState(...)</code> like below:</p>
  
  <pre><code>override fun onSaveInstanceState(outState: Bundle) {
      super.onSaveInstanceState(outState)
      outState.putBoolean("SEARCH_FOCUSED", searchField.hasFocus())
  }
  </code></pre>
  
  <p>If <code>searchField.hasFocus()</code> is equal to <code>true</code>, I want the virtual keyboard to be showed when device orientation changes. To do that, I set this on my <code>override fun onRestoreInstaceState(...)</code> like below:</p>
  
  <pre><code>override fun onRestoreInstanceState(savedInstanceState: Bundle) {
      super.onRestoreInstanceState(savedInstanceState)
      val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
      val searchFocused = savedInstanceState.getBoolean("SEARCH_FOCUSED")
      if (searchFocused) {
          searchField.requestFocus()
          searchField.postDelayed({ imm.showSoftInput(searchField,
              SHOW_FORCED)
          }, 200)
      }
  }
  </code></pre>
  
  <p>The above <strong>works</strong>. </p>
  
  <h2>Concern #1</h2>
  
  <p>However, the above way is not ideal. I had to set a <code>postDelayed(Runnable, time)</code> to make the virtual keyboard appear, which doesn't seem like the best solution to me. <strong>Ideally</strong>, I want to be able to simply do the following. The problem, though, with the below code is that the keyboard <em>does not</em> show at all on orientation change. </p>
  
  <pre><code>override fun onRestoreInstanceState(savedInstanceState: Bundle) {
      super.onRestoreInstanceState(savedInstanceState)
      val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
      val searchFocused = savedInstanceState.getBoolean("SEARCH_FOCUSED")
      if (searchFocused) {
          searchField.requestFocus()
          imm.showSoftInput(searchField, SHOW_FORCED)
      }
  }
  </code></pre>
  
  <h2>Concern #2</h2>
  
  <p>Furthermore, notice that I used <code>SHOW_FORCED</code> instead of <code>SHOW_IMPLICIT</code>. This is really bad because when I tap on my home button (the <code>Activity</code> is now <code>onStop()</code>), the keyboard is still visible. I know this is due to <code>SHOW_FORCED</code> indicating that the keyboard must be hidden explicitly by the user him/herself. <strong>Ideally</strong>, I want to use <code>SHOW_IMPLICIT</code>. The problem with using <code>SHOW_IMPLICIT</code> is that the keyboard does not appear on landscape orientation, but does appear on portrait orientation.</p>
  
  <p>So, my questions are:</p>
  
  <ul>
  <li>Why can't I immediately set <code>imm.showSoftInput(...)</code> without using <code>postDelayed(...)</code> for the virtual keyboard to show? Am I missing some other prerequisite steps in using <code>showSoftInput</code>?</li>
  <li>How can I use <code>SHOW_IMPLICIT</code> to make sure that the keyboard is hidden automatically when the Activity is in the background (<code>onPause()</code>/<code>onStop()</code>)?</li>
  </ul>