SearchManager.OnDismissListener 从未打过电话

SearchManager.OnDismissListener never called

我的目标是复制 "play music" 应用搜索功能。据我所知,当我按下搜索按钮时,会创建一个新的 activity,并且会自动展开搜索视图。当按下箭头或后退按钮以关闭搜索小部件时,activity 将关闭。除了未调用 onDismiss 方法外,一切正常。任何帮助深表感谢。谢谢!

搜索 activity 是在按下搜索图标时通过意图启动的。您可以在下面看到用于在搜索 activity 中创建搜索小部件的代码,它模仿上述播放音乐。

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_option, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        searchMenuItem.expandActionView();

        searchManager.setOnDismissListener(new SearchManager.OnDismissListener() {
            @Override
            public void onDismiss() {
                //Method never called. Logs don't show up/
                Log.i("1234Dismiss", "onDismiss called");
                finish();
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

请改用这个。它适用于我所有的应用程序。

MenuItemCompat.setOnActionExpandListener(searchMenuItem,
                new MenuItemCompat.OnActionExpandListener() {
                    @Override
                    public boolean onMenuItemActionCollapse(MenuItem item) {

                        // Do what you want to do when the search is collapsed
                        return true; // Return true to collapse action view
                    }

                    @Override
                    public boolean onMenuItemActionExpand(MenuItem item) {

                        // Do what you want to do when the search is expanded
                        return true; // Return true to expand action view
                    }
                });