如何解决使用导航组件导航到自身时如何显示后退按钮?
我已经这样设置了导航栏
with(ActivityHolderBinding.inflate(layoutInflater)) {
setContentView(root)
val fragment = supportFragmentManager.findFragmentByTag("holder") as NavHostFragment
toolbar.setupWithNavController(fragment.navController,AppBarConfiguration(fragment.navController.graph))
setSupportActionBar(toolbar)
}
但这仅在我导航到另一个片段时显示一个后退按钮,而在我标记为开始目标的片段上导航到自身时则不显示。
解决方法
根据this issue:
setupActionBarWithNavController使用当前的目标ID来确定是否显示了“向上”按钮,因此您看到的行为按预期进行。
您可以有多个使用同一Fragment类的目标,因此只需为递归调用创建一个单独的目标即可:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/catalog_nav_graph"
app:startDestination="@id/categories">
<fragment
android:id="@+id/categories"
android:name="somepackage.categories.CategoryListFragment"
tools:layout="@layout/catalog_category_list_frag">
<action
android:id="@+id/action_category_to_category"
app:destination="@id/categoriesRecursive" />
<action
android:id="@+id/action_category_to_product_list"
app:destination="@id/products_frag" />
<argument
android:name="categoryId"
app:argType="integer"
android:defaultValue="0" />
</fragment>
<fragment
android:id="@+id/categoriesRecursive"
android:name="somepackage.categories.CategoryListFragment"
tools:layout="@layout/catalog_category_list_frag">
<action
android:id="@+id/action_category_to_category"
app:destination="@id/categoriesRecursive" />
<action
android:id="@+id/action_category_to_product_list"
app:destination="@id/products_frag" />
<argument
android:name="categoryId"
app:argType="integer"
android:defaultValue="0" />
</fragment>
....
</navigation>
因此,通过使用两个不同的ID,NavigationUI
可以区分您的第一级(不应显示向上箭头)和应该显示向上箭头的第一级。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。