I am using Android SearchWidget on SearchActivity for searches. It automatically gets focus and soft-input keyboard is displayed.
However, when user goes back with ActionBar (but not with back button), the soft-input stays on the screen even though activity has
android:windowSoftInputMode="stateHidden|adjustUnspecified"
as descripbed on https://developer.android.com/training/keyboard-input/visibility.html
It seems that it only works when going forward, and not coming back.
The problem: there may be many Activities calling SearchActivity and for them making stateAlwaysHidden
may be not appropriate. (In other words: I don't know behavior of all other Activities.)
UPDATE: Giving code, actually just actionBar.setDisplayHomeAsUpEnabled(true);
@Override
protected void onCreate(Bundle savedInstanceState) {
....
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// get the action bar
ActionBar actionBar = getActionBar();
if (null!=actionBar){
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
....
}
UDATE 2:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
Using Rod lead and SO answers :
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// fix bug when soft-input stays on the screen when navigating away with ActionBar home/back button
// https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard
//getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); had no affect
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//check if no view has focus:
View view = this.getCurrentFocus();
if(view != null){
//imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
return super.onOptionsItemSelected(item);
}