[英]How to display back button when navigating to self with navigation component?
我已經像這樣設置了我的導航欄
with(ActivityHolderBinding.inflate(layoutInflater)) {
setContentView(root)
val fragment = supportFragmentManager.findFragmentByTag("holder") as NavHostFragment
toolbar.setupWithNavController(fragment.navController,AppBarConfiguration(fragment.navController.graph))
setSupportActionBar(toolbar)
}
但這僅在我導航到另一個片段時顯示后退按鈕,而不是在標記為開始目的地的片段上導航到 self 時。
根據這個問題:
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
可以區分您的第一級(不應顯示向上箭頭)與應顯示向上箭頭的第一級。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.