Android JetPack导航组件 - 后面按钮关闭搜索视图,而不是导航到上一个屏幕
我使用的是Android JetPack导航组件来在碎片之间进行导航。在一个片段上,我的搜索视图以扩展的形式。当我按下后按钮(在搜索视图的左侧)时,以下情况发生:
- 搜索视图将首先关闭,而不是导航到上一个屏幕
- 我需要按下后面的按钮第二次导航到上一个屏幕
为什么?为什么它不直接导航到上一个屏幕?
在这里,您可以在处理搜索视图相关的代码的片段中查看代码:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
// add a search view to the menu
inflater.inflate(R.menu.search_menu, menu)
val searchItem = menu.findItem(R.id.search)
searchView = searchItem.actionView as SearchView
searchItem.expandActionView()
searchView.apply {
queryHint = "Search"
isIconified = false
/* this shows the query the user has typed (no submission) so that the user can see which search query was used */
setQuery(args.query, false)
doOnLayout {
this.clearFocus()
}
setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String): Boolean {
// do some stuff with the query ..
return false
}
override fun onQueryTextChange(newText: String): Boolean = false
})
}
}
在这里,您可以看到search_menu.xml
文件内容:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- SearchView -->
<item android:id="@+id/search"
android:title="@string/search"
android:icon="@drawable/search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
<item android:id="@+id/profile_icon"
android:title="@string/profile"
android:icon="@drawable/account"
app:showAsAction="collapseActionView|ifRoom"/>
</menu>
我需要添加什么当我按下后面按钮而无需首先关闭搜索视图时,请导航回上一个屏幕?
I am using the Android Jetpack Navigation Component for navigating between fragments back and forth. On one Fragment, I have a SearchView in expanded form. When I press the back button (to the left of the SearchView), then the following happens:
- the SearchView will be first closed instead of navigating to the previous screeen
- I need to press the back button a 2nd time to navigate to the previous screen
Why is that ? Why it does not navigate directly to the previous screen?
Here, you can see the code in the fragment handling the SearchView-related code:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
// add a search view to the menu
inflater.inflate(R.menu.search_menu, menu)
val searchItem = menu.findItem(R.id.search)
searchView = searchItem.actionView as SearchView
searchItem.expandActionView()
searchView.apply {
queryHint = "Search"
isIconified = false
/* this shows the query the user has typed (no submission) so that the user can see which search query was used */
setQuery(args.query, false)
doOnLayout {
this.clearFocus()
}
setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String): Boolean {
// do some stuff with the query ..
return false
}
override fun onQueryTextChange(newText: String): Boolean = false
})
}
}
Here, you can see the search_menu.xml
file content:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- SearchView -->
<item android:id="@+id/search"
android:title="@string/search"
android:icon="@drawable/search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
<item android:id="@+id/profile_icon"
android:title="@string/profile"
android:icon="@drawable/account"
app:showAsAction="collapseActionView|ifRoom"/>
</menu>
What do I need to add to my code so that the app directly navigates back to the previous screen when I press the back button without closing the SearchView first ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
app:showasaction =“ collapseactionView | ifroom”
。collapseactionview
含义 back按钮首先关闭搜索视图。您已经第二次按下后面按钮将导航到上一个屏幕。回答您的问题,从
app:showasaction
中删除collapseactionView
,您将获得所需的行为(按下后续按钮将导航到上一个屏幕)。但是您也可能想要 >,因为它在导航到上一个屏幕时保持打开状态。
The problem is
app:showAsAction="collapseActionView|ifRoom"
.collapseActionView
means that the back button first closes the search view. You have press the back button a second time to navigate to the previous screen.Answering your question, remove
collapseActionView
fromapp:showAsAction
and you'll get the behavior you want (pressing the back button will navigate to the previous screen).But then you might also want to programmatically close the keyboard, since it stays open when navigating to the previous screen.