Android: Hiding keyboard from auto complete in collapse action of Action Bar Sherlock doesn't work
<p>I have an app that implements an auto complete text view inside an action bar. I'm using Action Bar Sherlock, and this auto complete is shown by the tag</p>

<pre><code>android:actionLayout="@layout/field_search"
</code></pre>

<p>of an item from a xml menu, as shown below.</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt;
    &lt;item
        android:id="@+id/autocomplete_menu_item"
        android:showAsAction="always|collapseActionView"
        android:actionLayout="@layout/field_search"
        android:icon="@drawable/ic_search"
    /&gt;
&lt;/menu&gt;
</code></pre>

<p>When the search icon is clicked, the keyboard is shown for the user. When the user chooses and item, the auto complete text view content is modified for the choosed item name, and the keyboard is hidden. Well, this part is working fine.</p>

<p>What isn't working is that the keyboard is been hidden only at auto complete click events. So, if I click at the search item and don't choose an item of the auto complete text view, and then click at the home button of the action bar (to close the current activity), the keyboard isn't closed. It remains opened.</p>

<p>I'm setting the home button (of the action bar) click through the method below:</p>

<pre><code>@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Action bar back button.
        case android.R.id.home:
            onBackPressed();
            return true;
        case R.id.autocomplete_menu_item:
            initializeAutoComplete();
            return true;
        // Default.
        default:
            return super.onOptionsItemSelected(item);
    }
}
</code></pre>

<p>So, the onBackPressed() method is been called. But the keyboard isn't being closed.</p>

<p>Then, I tried to close the keyboard at the onPause() method, as shown below:</p>

<pre><code>@Override
protected void onPause() {
    super.onPause();

    // Closes keyboard before exit.
    if (mKeyboardShown)
        hideKeyboard(mAutoComplete);
}
</code></pre>

<p>And again, the keyboard isn't being closed.</p>

<p>The methods that opens and closes the keyboard are shown below:</p>

<pre><code>/**
 * Shows the keyboard.
 * 
 * @param view
 */
public void showKeyboard(View view) {
    Context context = view.getContext();
    InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}

/**
 * Hides the keyboard.
 * 
 * @param view
 */
public void hideKeyboard(View view) {
    Context context = view.getContext();
    InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
</code></pre>

<p>The method that manages the auto complete is shown below.</p>

<pre><code>/**
 * Calls the auto complete text view.
 */
public boolean initializeAutoComplete() {
    if (mAutoComplete == null) return false;

    // Cleans text.
    mAutoComplete.setText("");

    // Invoke virtual keyboard.
    mAutoComplete.requestFocus();
    showKeyboard(mAutoComplete);
    mKeyboardShown = true;

    // Creates an array adapter to display the school units from the auto complete text view.
    final AutoCompleteAdapter adapter = new AutoCompleteAdapter(this, mList);

    // Sets the click listener of the auto complete text view, to show the keyboard when the auto complete
    // text view has shown the keyboard and this keyboard was closed (so the auto complete text view is been
    // shown, but the keyboard don't, so we need to show it).
    mAutoComplete.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showKeyboard(mAutoComplete);
            mKeyboardShown = true;
        }
    });

    // Sets the click listener of the auto complete text view, to set the adapter. 
    mAutoComplete.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // Populate list with our static array of titles.
            mAutoComplete.setAdapter(adapter);

            return false;
        }
    });

    // Sets the item click listener of the auto complete text view, to set the auto complete text view name,
    // hide the keyboard, and hide the auto complete drop down.
    mAutoComplete.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
            // Get id of the unit from the adapter tag.
            Integer nameId = (Integer)view.getTag();

            // Get clicked id of the auto complete text view.
            Data data = getDataById(nameId);
            if (data != null) mAutoComplete.setText(data.getName());
            else mAutoComplete.setText("Data not found.");

            // Hide keyboard and hide auto complete drop down.
            hideKeyboard(mAutoComplete);
            mKeyboardShown = false;
            mAutoComplete.setDropDownHeight(0);
        }
    });

    return true;
}
</code></pre>

<p>So, the keyboard is shown every time I call showKeyboard(), but only is being hidden when I call hideKeyboard() at the click event of the auto complete text view.</p>

<p>It seems like the hideKeyboard() only gets the "window token" when the click event is called. But I need to close the keyboard when I want, and not just at click event.</p>

<p>In this app there's two activities: MainActivity and AutoCompleteActivity. Below I show 1 image divided in 6 frames (from left to right / up to down), showing the following actions:</p>

<p><a href="http://i50.tinypic.com/wuqq0l.png" rel="nofollow">Image Link</a></p>

<ol>
<li>After enter the AutoCompleteActivity.</li>
<li>After click at the search icon (here the keyboard is called).</li>
<li>After type some text, the auto complete show what was found by the adapter.</li>
<li>After click into the items showed by the auto complete, the auto complete text content is updated with the name of the choosed item (in this case, with "Apple" in white colour).</li>
<li>After click at the action bar back button, the auto complete text view is hidden, but not the keyboard, even calling the onBackPressed() (when onOptionsItemSelected() is called).</li>
<li>Pressing the action bar back button again, we are back at the MainActivity. But the keyboard remains on the screen, even with the onPaused() of the AutoCompleteActivity being called. How can this happen?</li>
</ol>

<p>How call I solve this issue?</p>
