扩展SearchActionItem而不添加到backstack

时间:2013-02-28 22:38:58

标签: android

我有一个搜索结果页面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);
}

但是当用户点击后退或上移按钮时,搜索首先关闭,然后您必须再次按下以关闭活动。如何强制活动立即完成?

1 个答案:

答案 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}}