我有一个活动Dashboard Activity
,其中有一个片段和一个底部导航菜单,该菜单在DashboardActivity.kt
中设置如下:
setContentView(R.layout.activity_dashboard)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
navView.setupWithNavController(navController)
底部的导航栏包含3个项目:@+id/navigation_scoreboard
,@+id/navigation_dashboard
和@+id/navigation_gamesettings
在我的@+id/navigation_dashboard
底部导航菜单项内-用户可以在输入字段中输入一些数据,然后按一下我进行API调用的按钮,然后导航到结果屏幕@+id/slipup_result_fragment
使用导航+实时数据观察器的API调用成功。
现在的问题是::当我离开结果屏幕@+id/slipup_result_fragment
时,例如说要转到底部导航菜单中的另一个项目,然后按{ {1}},我再次进入结果屏幕,而不是原始片段-@+id/navigation_dashboard
,似乎无法找到返回原始片段的方法。
我在做什么错了?
更新:@+id/navigation_dashboard
中片段的导航图如下:
Dashboard Activity
Update#2:底部菜单XML如下:
<?xml version="1.0" encoding="utf-8"?>
<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_scoreboard">
<fragment
android:id="@+id/navigation_scoreboard"
android:name="com.ezchange.mvp.ui.scoreboard.ScoreboardFragment"
android:label="@string/title_scoreboard"
tools:layout="@layout/fragment_scoreboard" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.ezchange.mvp.ui.slipup.SlipUpFragment"
android:label="@string/title_slipup"
tools:layout="@layout/fragment_slipup" >
<action
android:id="@+id/action_navigation_dashboard_to_slipUpResultFragment"
app:destination="@id/slipup_result_fragment"
app:popUpTo="@id/navigation_dashboard"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/navigation_gamesettings"
android:name="com.ezchange.mvp.ui.gamesettings.GameSettingsFragment"
android:label="@string/title_game_settings"
tools:layout="@layout/fragment_game_settings" />
<fragment
android:id="@+id/slipup_result_fragment"
android:name="com.ezchange.mvp.ui.slipup.SlipUpResultFragment"
android:label="fragment_slip_up_result"
tools:layout="@layout/fragment_slip_up_result" >
<action
android:id="@+id/action_slipUpResultFragment_to_navigation_scoreboard"
app:destination="@id/navigation_scoreboard" />
</fragment>
</navigation>
答案 0 :(得分:0)
使用action_navigation_dashboard_to_slipUpResultFragment
导航控制器导航时,请遵循以下信息-
SlipUpFragment-> SlipUpResultFragment
在导航后,它将带您到SlipUpFragment,没有任何问题。
导航到navigation_gamesettings
后,导航应该具有以下信息-
SlipUpFragment-> SlipUpResultFragment-> GameSettingsFragment
但是没有这样的action_slipUpResultFragment_to_navigation_gamesettings
,这就是为什么导航导航器时会丢失的原因。
因此SlipUpResultFragment必须采取以下措施-
<fragment
android:id="@+id/slipup_result_fragment"
android:label="fragment_slip_up_result"
android:name="com.ezchange.mvp.ui.slipup.SlipUpResultFragment"
tools:layout="@layout/fragment_slip_up_result">
<action
android:id="@+id/action_slipUpResultFragment_to_navigation_gamesettings"
app:destination="@id/navigation_gamesettings"
app:popUpTo="@+id/navigation_dashboard"
app:popUpToInclusive="true" />
<action
android:id="@+id/action_slipUpResultFragment_to_navigation_scoreboard"
app:destination="@id/navigation_scoreboard"
app:popUpTo="@+id/navigation_dashboard"
app:popUpToInclusive="true" />
</fragment>
添加app:popUpToInclusive="true"
和app:popUpTo="@+id/navigation_dashboard"
以确保在导航后,您将转到 SlipUpFragment ,而不再是 SlipUpResultFragment 。
编辑1
使用以下NavGraph可以达到预期的效果-PSR of result
<?xml version="1.0" encoding="utf-8"?>
<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_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.example.bottomnavigationtest.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.bottomnavigationtest.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" >
<action
android:id="@+id/action_navigation_dashboard_to_detailsFagment"
app:destination="@id/detailsFagment" />
</fragment>
<fragment
android:id="@+id/navigation_notifications"
android:name="com.example.bottomnavigationtest.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" >
</fragment>
<fragment
android:id="@+id/detailsFagment"
android:name="com.example.bottomnavigationtest.ui.dashboard.details.DetailsFagment"
android:label="fragment_details_fagment"
tools:layout="@layout/fragment_details_fagment" >
<action
android:id="@+id/action_detailsFagment_to_navigation_home"
app:destination="@id/navigation_home" />
</fragment>
</navigation>
快乐编码!