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
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();
}
}
});