单击项目时,底部导航没有导航
我创建了两个作为片段的NavController的活动,我不确定这是正确的方法。
对于MyActivity
,底部导航工作正常并显示片段。但是对于mainActivityRelawan
,当我按下返回按钮后,单击项目时,主菜单没有导航,我不知道为什么显示片段。
main_activity
用于控制主活动片段,main_activity_relawan
用于控制主要活动Relawan片段。
这是我的mainActivityRelawan
val navController = findNavController(R.id.nav_host_fragment_activity_main_relawan)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home_relawan, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
,这是我的mainActivity
val navController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
,对于findnavcontroller
i的每个活动,我都使用不同的活动xml和不同的navgraph
这是我的mainActivity findNavController
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/mobile_navigation" />
,这是我的mainActivitivitivityRelawan navcontroller
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view_relawan"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment_activity_main_relawan"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view_relawan"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation_relawan" />
,我对每个纳维尔图都使用了不同的navgraph。这是navigationgraph
mainActivity
<fragment
android:id="@+id/navigation_home"
android:name="com.example.tes.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.tes.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_notifications"
android:name="com.example.tes.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
,这是navgraph
mainActivityRelawan
<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/navigation_relawan"
app:startDestination="@+id/navigation_home_relawan">
<fragment
android:id="@+id/navigation_home_relawan"
android:name="com.example.tes.ui.home_relawan.RelawanFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_main_relawan" >
<action
android:id="@+id/action_navigation_home_relawan_to_navigation_dashboard"
app:destination="@id/navigation_dashboard" />
</fragment>
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.tes.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" >
<action
android:id="@+id/action_navigation_dashboard_to_navigation_notifications"
app:destination="@id/navigation_notifications" />
</fragment>
<fragment
android:id="@+id/navigation_notifications"
android:name="com.example.tes.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" >
</fragment>
</navigation>
I created two activities as a navcontroller for the fragment, I am not sure if this is the right approach.
For MyActivity
, the bottom navigation is working well and showing the fragment. But for the MainActivityRelawan
the Home Menu didn't navigate when item is clicked and the fragment only is shown after I press back button, I don't know why.
The main_activity
is for controlling the Main Activity Fragment, and main_activity_relawan
is for controlling Main Activity Relawan Fragment.
This is my MainActivityRelawan
val navController = findNavController(R.id.nav_host_fragment_activity_main_relawan)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home_relawan, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
And this is my MainActivity
val navController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
and for each of findNavController
i use different activity XML and different navGraph
this is my MainActivity findNavController
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/mobile_navigation" />
and this is my MainActivityRelawan NavController
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view_relawan"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment_activity_main_relawan"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view_relawan"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation_relawan" />
and i using different navGraph for each. This is NavigationGraph
for MainActivity
<fragment
android:id="@+id/navigation_home"
android:name="com.example.tes.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.tes.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_notifications"
android:name="com.example.tes.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
and this is NavGraph
for the MainActivityRelawan
<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/navigation_relawan"
app:startDestination="@+id/navigation_home_relawan">
<fragment
android:id="@+id/navigation_home_relawan"
android:name="com.example.tes.ui.home_relawan.RelawanFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_main_relawan" >
<action
android:id="@+id/action_navigation_home_relawan_to_navigation_dashboard"
app:destination="@id/navigation_dashboard" />
</fragment>
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.tes.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" >
<action
android:id="@+id/action_navigation_dashboard_to_navigation_notifications"
app:destination="@id/navigation_notifications" />
</fragment>
<fragment
android:id="@+id/navigation_notifications"
android:name="com.example.tes.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" >
</fragment>
</navigation>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了,这是因为我为菜单底部导航制作了不同的ID
solved, this is happened just because i made different id for the menu bottom navigation
我在一项活动中使用了它。我不知道这是好的。因此,您可以在我的
I used it in one Activity. I doun't know it's good or not. So you can see solution in my git