返回错误的片段时导航回退。

huangapple go评论138阅读模式
英文:

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:

&lt;navigation
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:id=&quot;@+id/main_navigation&quot;
app:startDestination=&quot;@id/homeFragmentStart&quot;&gt;

&lt;include app:graph=&quot;@navigation/home_navigation&quot; /&gt;

&lt;fragment
    android:id=&quot;@id/myRoutesFragment&quot;
    android:name=&quot;com.example.extoryapp.Main.MyRoutesPage.MyRoutesFragment&quot;
    android:label=&quot;MyRoutesFragment&quot; &gt;
&lt;/fragment&gt;

&lt;fragment
    android:id=&quot;@id/languagesFragment&quot;
    android:name=&quot;com.example.extoryapp.Main.LanguagesPage.LanguagesFragment&quot;
    android:label=&quot;LanguagesFragment&quot; /&gt;

</navigation>

home_navigation graph:

&lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:id=&quot;@id/homeFragmentStart&quot;
app:startDestination=&quot;@id/homeFragment&quot;&gt;

&lt;fragment
    android:id=&quot;@+id/homeFragment&quot;
    android:name=&quot;com.example.extoryapp.Main.HomePage.HomeFragment&quot;
    android:label=&quot;HomeFragment&quot; &gt;
    &lt;action
        android:id=&quot;@+id/action_homeFragment_to_tourFragment&quot;
        app:destination=&quot;@id/tourFragment&quot;/&gt;
&lt;/fragment&gt;

&lt;fragment
    android:id=&quot;@+id/tourFragment&quot;
    android:name=&quot;com.example.extoryapp.Main.InformationPages.TourFragment&quot;
    android:label=&quot;TourFragment&quot; &gt;
    &lt;action
        android:id=&quot;@+id/action_tourFragment_to_routeCreationFragment&quot;
        app:destination=&quot;@id/routeCreationFragmentMain&quot;/&gt;
    &lt;action
        android:id=&quot;@+id/action_tourFragment_to_readyRoutesFragment&quot;
        app:destination=&quot;@id/readyRoutesFragment&quot; /&gt;
    &lt;action
        android:id=&quot;@+id/action_tourFragment_to_destinationFragment&quot;
        app:destination=&quot;@id/destinationFragment&quot; /&gt;
&lt;/fragment&gt;
&lt;fragment
    android:id=&quot;@+id/routeCreationFragmentMain&quot;
    android:name=&quot;com.example.extoryapp.Main.RouteCreationPages.RouteCreationFragment&quot;
    android:label=&quot;RouteCreationFragment&quot; /&gt;
&lt;fragment
    android:id=&quot;@+id/readyRoutesFragment&quot;
    android:name=&quot;com.example.extoryapp.Main.InformationPages.ReadyRoutesFragment&quot;
    android:label=&quot;ReadyRoutesFragment&quot; &gt;
    &lt;action
        android:id=&quot;@+id/action_readyRoutesFragment_to_routeCreationFragment&quot;
        app:destination=&quot;@id/routeCreationFragmentMain&quot; /&gt;
&lt;/fragment&gt;
&lt;fragment
    android:id=&quot;@+id/destinationFragment&quot;
    android:name=&quot;com.example.extoryapp.Main.InformationPages.DestinationFragment&quot;
    android:label=&quot;DestinationFragment&quot; &gt;
    &lt;action
        android:id=&quot;@+id/action_destinationFragment_to_imageDetailFragment&quot;
        app:destination=&quot;@id/imageDetailFragment&quot; /&gt;
&lt;/fragment&gt;
&lt;fragment
    android:id=&quot;@+id/imageDetailFragment&quot;
    android:name=&quot;com.example.extoryapp.Main.InformationPages.ImageDetailFragment&quot;
    android:label=&quot;ImageDetailFragment&quot; /&gt;

</navigation>

答案1

得分: 0

override fun onBackPressed() {
val mainNavigationBar = findViewById(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 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&lt;BottomNavigationView&gt;(R.id.mainNavigationBar)
    val mainFragmentView = supportFragmentManager.findFragmentById(R.id.mainFragmentView) as NavHostFragment
    val navController = mainFragmentView.navController

    if(navController.currentDestination?.label==&quot;MyRoutesFragment&quot; || navController.currentDestination?.label==&quot;LanguagesFragment&quot;)
        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.

huangapple
  • 本文由 发表于 2023年4月11日 00:13:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定