英文:
Navigating to Wrong Fragment On Back Pressed
问题
我有一个具有底部导航栏的主活动。这个底部导航栏包括3个目的地。
- 主页
- 我的路线
- 语言
当活动首次启动时,它会在主页上打开,显示主页片段(HomeFragment)。这个HomeFragment有一些按钮,可能会导航到不同的片段(仍然在主页内)。
假设我进入了HomeFragment -> TourFragment -> ReadyRoutesFragment(全部在主页内),然后按返回按钮。它可以正常工作。
但是,假设我进入了HomeFragment -> TourFragment -> ReadyRoutesFragment -> 在底部导航栏中按“我的路线”按钮 -> MyRoutesFragment(最后一个在“我的路线”中),然后按返回按钮,而不是返回到ReadyRoutesFragment,我回到了主页。
我使用导航组件来处理导航,没有覆盖任何返回按键功能。在同一个底部导航栏目的片段之间导航时,我使用了定义的操作。
然而,如果我做同样的操作(HomeFragment -> TourFragment -> ReadyRoutesFragment -> 在底部导航栏中按“我的路线”按钮 -> MyRoutesFragment),但是不按返回按钮,而是按主页按钮,然后会显示ReadyRoutesFragment。
我的主要导航图:
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_navigation"
app:startDestination="@id/homeFragmentStart">
<include app:graph="@navigation/home_navigation" />
<fragment
android:id="@id/myRoutesFragment"
android:name="com.example.extoryapp.Main.MyRoutesPage.MyRoutesFragment"
android:label="MyRoutesFragment">
</fragment>
<fragment
android:id="@id/languagesFragment"
android:name="com.example.extoryapp.Main.LanguagesPage.LanguagesFragment"
android:label="LanguagesFragment" />
</navigation>
home_navigation图:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@id/homeFragmentStart"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.extoryapp.Main.HomePage.HomeFragment"
android:label="HomeFragment">
<action
android:id="@+id/action_homeFragment_to_tourFragment"
app:destination="@id/tourFragment"/>
</fragment>
<fragment
android:id="@+id/tourFragment"
android:name="com.example.extoryapp.Main.InformationPages.TourFragment"
android:label="TourFragment">
<action
android:id="@+id/action_tourFragment_to_routeCreationFragment"
app:destination="@id/routeCreationFragmentMain"/>
<action
android:id="@+id/action_tourFragment_to_readyRoutesFragment"
app:destination="@id/readyRoutesFragment" />
<action
android:id="@+id/action_tourFragment_to_destinationFragment"
app:destination="@id/destinationFragment" />
</fragment>
<fragment
android:id="@+id/routeCreationFragmentMain"
android:name="com.example.extoryapp.Main.RouteCreationPages.RouteCreationFragment"
android:label="RouteCreationFragment" />
<fragment
android:id="@+id/readyRoutesFragment"
android:name="com.example.extoryapp.Main.InformationPages.ReadyRoutesFragment"
android:label="ReadyRoutesFragment">
<action
android:id="@+id/action_readyRoutesFragment_to_routeCreationFragment"
app:destination="@id/routeCreationFragmentMain" />
</fragment>
<fragment
android:id="@+id/destinationFragment"
android:name="com.example.extoryapp.Main.InformationPages.DestinationFragment"
android:label="DestinationFragment">
<action
android:id="@+id/action_destinationFragment_to_imageDetailFragment"
app:destination="@id/imageDetailFragment" />
</fragment>
<fragment
android:id="@+id/imageDetailFragment"
android:name="com.example.extoryapp.Main.InformationPages.ImageDetailFragment"
android:label="ImageDetailFragment" />
</navigation>
希望这有助于解释您的导航问题。
英文:
I have a Main Activity with a bottom navigation bar. This bottom navigation bar includes 3 destinations.
- Home
- My Routes
- Languages
When the activity first starts, it opens at Home with HomeFragment. This HomeFragment has some buttons that might navigate to different fragments. (Still inside Home)
Lets say I went to HomeFragment -> TourFragment -> ReadyRoutesFragment (all in Home)
then press back button. It works properly.
But lets say I went to
HomeFragment-> TourFragment -> ReadyRoutesFragment -> pressing My Routes button in bottom navigation bar -> MyRoutesFragment (the last one is in My Routes)
then press back button, instead of going back to ReadyRoutesFragment I get HomeFragment.
I use navigation component to handle the navigations, did not override any back press functions. I use the defined actions while navigating between fragments inside the same bottom navigation bar destination.
However, If I do the same thing (HomeFragment-> TourFragment -> ReadyRoutesFragment -> pressing My Routes button in bottom navigation bar -> MyRoutesFragment) but instead of pressing back button press Home button then it shows ReadyRoutesFragment.
My Main Nav Graph:
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_navigation"
app:startDestination="@id/homeFragmentStart">
<include app:graph="@navigation/home_navigation" />
<fragment
android:id="@id/myRoutesFragment"
android:name="com.example.extoryapp.Main.MyRoutesPage.MyRoutesFragment"
android:label="MyRoutesFragment" >
</fragment>
<fragment
android:id="@id/languagesFragment"
android:name="com.example.extoryapp.Main.LanguagesPage.LanguagesFragment"
android:label="LanguagesFragment" />
</navigation>
home_navigation graph:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@id/homeFragmentStart"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.extoryapp.Main.HomePage.HomeFragment"
android:label="HomeFragment" >
<action
android:id="@+id/action_homeFragment_to_tourFragment"
app:destination="@id/tourFragment"/>
</fragment>
<fragment
android:id="@+id/tourFragment"
android:name="com.example.extoryapp.Main.InformationPages.TourFragment"
android:label="TourFragment" >
<action
android:id="@+id/action_tourFragment_to_routeCreationFragment"
app:destination="@id/routeCreationFragmentMain"/>
<action
android:id="@+id/action_tourFragment_to_readyRoutesFragment"
app:destination="@id/readyRoutesFragment" />
<action
android:id="@+id/action_tourFragment_to_destinationFragment"
app:destination="@id/destinationFragment" />
</fragment>
<fragment
android:id="@+id/routeCreationFragmentMain"
android:name="com.example.extoryapp.Main.RouteCreationPages.RouteCreationFragment"
android:label="RouteCreationFragment" />
<fragment
android:id="@+id/readyRoutesFragment"
android:name="com.example.extoryapp.Main.InformationPages.ReadyRoutesFragment"
android:label="ReadyRoutesFragment" >
<action
android:id="@+id/action_readyRoutesFragment_to_routeCreationFragment"
app:destination="@id/routeCreationFragmentMain" />
</fragment>
<fragment
android:id="@+id/destinationFragment"
android:name="com.example.extoryapp.Main.InformationPages.DestinationFragment"
android:label="DestinationFragment" >
<action
android:id="@+id/action_destinationFragment_to_imageDetailFragment"
app:destination="@id/imageDetailFragment" />
</fragment>
<fragment
android:id="@+id/imageDetailFragment"
android:name="com.example.extoryapp.Main.InformationPages.ImageDetailFragment"
android:label="ImageDetailFragment" />
</navigation>
答案1
得分: 0
override fun onBackPressed() {
val mainNavigationBar = findViewById
val mainFragmentView = supportFragmentManager.findFragmentById(R.id.mainFragmentView) as NavHostFragment
val navController = mainFragmentView.navController
if (navController.currentDestination?.label == "MyRoutesFragment" || navController.currentDestination?.label == "LanguagesFragment")
mainNavigationBar.selectedItemId = R.id.homeFragmentStart
else
super.onBackPressed()
}
I added this code to my MainActivity. Instead of handling backstack, I just navigate to Home if the back button is pressed in My Routes or Languages.
This was a solution I thought about. But if there are any other suggestions, I will be happy to listen to them.
英文:
override fun onBackPressed() {
val mainNavigationBar=findViewById<BottomNavigationView>(R.id.mainNavigationBar)
val mainFragmentView = supportFragmentManager.findFragmentById(R.id.mainFragmentView) as NavHostFragment
val navController = mainFragmentView.navController
if(navController.currentDestination?.label=="MyRoutesFragment" || navController.currentDestination?.label=="LanguagesFragment")
mainNavigationBar.selectedItemId=R.id.homeFragmentStart
else
super.onBackPressed()
}
I added this code to my MainActivity. Instead of handling backstack, I just navigate to Home if back button is pressed in My Routes or Languages.
This was a a solution I thought about. But if there is any other suggestions, I will be happy to listen to them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论