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 expected. Clicking the "X" in the SearchView box closes the SearchView as expected. However, when the SearchView is activated and I press the "back" button, my app is exited. This is the correct behavior, but what I am trying to do now is to capture back button press in my fragment and just have it close the SearchView (not my app) when the SearchView is visible. Is there a way to invoke the SearchView OnCloseListener() programmatically on a back button press? what I have now in My onCreate() in the fragment:
activity?.onBackPressedDispatcher?.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (!searchView.isIconified) {
hideKeyboard()
searchView.onActionViewCollapsed()
false
}
}
})
this work fine but I have 2 issue I have to double click the back button and the second one I can never exist the app even if I return to my recycler view list