add back button to search

This commit is contained in:
Bnyro 2022-08-12 16:59:33 +02:00
parent 30cec4ab7c
commit 308060cea6
2 changed files with 17 additions and 2 deletions

View File

@ -214,7 +214,6 @@ class MainActivity : BaseActivity() {
// stuff for the search in the topBar
val searchItem = menu.findItem(R.id.action_search)
searchView = searchItem.actionView as SearchView
searchView.maxWidth = Integer.MAX_VALUE
val searchViewModel = ViewModelProvider(this)[SearchViewModel::class.java]
@ -245,6 +244,22 @@ class MainActivity : BaseActivity() {
}
})
searchItem.setOnActionExpandListener(
object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionExpand(p0: MenuItem?): Boolean {
return true
}
override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean {
val currentFragmentId = navController.currentDestination?.id
if (currentFragmentId == R.id.searchFragment || currentFragmentId == R.id.searchResultFragment) {
onBackPressed()
}
return true
}
}
)
searchView.setOnCloseListener {
if (navController.currentDestination?.id == R.id.searchFragment) {
searchViewModel.setQuery(null)

View File

@ -8,7 +8,7 @@
android:icon="@drawable/ic_search"
android:title="@string/search_hint"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="ifRoom" />
app:showAsAction="ifRoom|collapseActionView" />
<item
android:id="@+id/action_settings"