How to close a SearchView programmatically on back pressed?
<p>I have the same question which I found <a href="https://stackoverflow.com/questions/17506230/how-do-i-close-a-searchview-programmatically">here</a> which I will re-iterate because the solution is not 100% exactly what I need:</p>

<blockquote>
  <p>I currently have a SearchView in the action bar of my app. When I click the search icon, the SearchView expands and the keyboard pops up as expected. Clicking the "X" in the SearchView box closes the SearchView as expected. However, when the SearchView is activated and I press the "back" button, my app is exited. This is the correct behavior, but what I am trying to do now is to capture back button press and just have it close the SearchView (not my app) when the SearchView is visible. Is there a way to invoke the SearchView OnCloseListener() programmatically on a back button press? For example, something like this:</p>
</blockquote>

<p>Here is the solution:</p>

<pre><code>@Override
public void onBackPressed() {
  if (!searchView.isIconified()) {
    searchView.setIconified(true);
  } else {
    super.onBackPressed();
  }
}
</code></pre>

<p>The problem is the solution requires the user to press back <strong>not once, but twice</strong>: <strong>once* to exit the keyboard and **once</strong> to close the SearchView.</p>

<p>How can I close the keyboard <strong>AND</strong> the SearchView at the same time by pressing back <strong>once</strong>?</p>

<p><strong>edit:</strong></p>

<p>Somone had a similar problem with <a href="https://stackoverflow.com/questions/3425932/detecting-when-user-has-dismissed-the-soft-keyboard">EditText</a> and the solution was to subclass the EditText view.</p>