我有一个搜索结果页面Activity在操作栏中有一个SearchView小部件。在onCreateOptionsMenu()中,我自动展开它并显示如下查询:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem searchMenuItem = (MenuItem) menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView)searchMenuItem.getActionView();
searchView.setOnQueryTextListener(this);
searchMenuItem.expandActionView(); // expand the search item
searchView.setQuery(query, false); // hide the keyboard
searchView.clearFocus();
return true;
}
主页按钮设置如下:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
但是当用户点击后退或上移按钮时,搜索首先关闭,然后您必须再次按下以关闭活动。如何强制活动立即完成?
答案 0 :(得分:2)
finish()
Activity
只要SearchView
因实施OnActionExpandListener
而崩溃,就可以做searchMenuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
finish();
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
});
:
{{1}}