How do I close a SearchView programmatically?

后端 未结 13 1637
误落风尘
误落风尘 2020-12-23 19:10

I currently have a SearchView in the action bar of my app. When I click the search icon, the SearchView expands and the keyboard pops up as expecte

相关标签:
13条回答
  • 2020-12-23 19:37

    The following worked for me. I am trying to close search-view from Fragment in Jetpack NavigationView when a user clicks the back button -

    requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) { @Override public void handleOnBackPressed() {

            try {
                DashboardActivity activity = (DashboardActivity) getActivity();
                if (activity!=null) {
                    if (!activity.mSearchView.isIconified()) {
                        activity.mSearchView.setQuery("",false);
                        activity.mSearchView.setIconified(true);
                    } else {
                        NavController navController = Navigation.findNavController(activity, R.id.nav_host_fragment);
                        navController.popBackStack();
                    }
                }
    
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题