![](/img/trans.png)
[英]How to set startDestination in Navigation Component based on the fragment/nav_graph I come from?
[英]How to set included graph as startDestination in Navigation Graph?
我的應用程序中有多個模塊,其中一些是 UI 模塊,UI 模塊有一個NavigationGraph
,它包含在應用程序中。
該應用程序同時具有BottomNavigationView
和Toolbar
。 所以我堅持一些過渡。 我將分享下面的代碼並詳細說明問題。
這是我的應用導航圖
<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/mobile_navigation"
app:startDestination="@+id/navigation_group">
<fragment
android:id="@+id/navigation_group"
android:name="com.example.group.view.fragments.GroupHostFragment"
android:label="@string/title_group"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_scene"
android:name="com.wisilica.scene.view.SceneHostFragment"
android:label="@string/title_scene"
tools:layout="@layout/fragment_dashboard" />
...
</navigation>
每個片段都是 UI 模塊中的一個宿主片段。
我的儀表板活動布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarLayout">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar" />
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavView"
.................
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
.................
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
儀表盤活動代碼
setupActionBarWithNavController(navController, appBarConfiguration)
toolbar.setupWithNavController(navController)
bottomNavView.setupWithNavController(navController)
單擊底部菜單項時,它運行良好並導航到不同的主機片段。 但問題是homeUp 按鈕只顯示點擊第二個主機片段,而不是第一個主機片段及其目的地。
Dashboard Graph
| |
v v
hostA | host B
| |
V V
dest1 (dest2) dest1(dest2)
最初,我們在hostA上(dest1會顯示),但是homeUp符號不會顯示(OK)然后點擊dest2,homeUp符號沒有顯示(為什么)
單擊底部視圖菜單項並移動到主機B,顯示homeUp符號(為什么),然后單擊按鈕
並搬到了hostA。
我試圖為hostA和hostB包含navGraph,然后它沒有響應BottomNavigationView
請幫我解決這個問題?
提前致謝
您需要對 Graph XML Fragments 使用相同的菜單項 ID。
其余的事情將被自動管理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.