【发布时间】:2022-11-15 12:00:14
【问题描述】:
我创建了一个导航栏,但我不明白如何为 grph 中的每个片段创建一个 ID。
导航图:
<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/nav_graph"
app:startDestination="@id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.mordechay.yemotapp.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
</fragment>
<fragment
android:id="@id/nav_explorer"
android:name="com.mordechay.yemotapp.filseExplorerFragment"
android:label="fragment_filse_explorer"
tools:layout="@layout/fragment_filse_explorer" />
</navigation>
在活动中:
<fragment
android:id="@+id/nvgv_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
/>
我在活动中写道:
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.nav_explorer);
结果是空的,我需要写什么来获取片段?
我试图通过写入在导航图中找到的片段的 ID 来获取片段。 但我得到空。
【问题讨论】:
标签: java android android-fragments android-navigation-graph