无法移动到嵌套子导航组件中的父片段?

我在我的应用程序中使用导航组件,具有单个活动和多个片段的模式,我有一些片段占据整个设备屏幕,其他片段与底部导航组件共享它,并通过单个活动实现这一点,我使用了片段容器视图在我的主要活动和具有导航片段的主屏幕片段中,我使用了 RootHomeFragment,它具有另一个带有底部导航视图的片段容器视图。这里是完整的应用程序导航图

(nav_main)
-->MainActivity 有视图 (main_fragment)--> (SplashFragment - LoginFragment = RootHomeFragment)

(nav_home) --> RootHomeFragment 有视图 (home_fragment / 底部导航视图) --> (HomeFragment - SettingFragment)

问题出在 settingFragment,有一个注销操作将我重定向到登录屏幕,但我无法执行此操作,我尝试使用 SettingFragment 中的下一个代码执行此操作,但应用程序移动到 HomeFragment,这是默认的主目的地nav_home 的

   Navigation.findNavController(requireActivity(),R.id.fragment_main)
                    .navigate(R.id.actionGoToLoginFromHome)

这是我的 nav_main 的 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/nav_main"
    app:startDestination="@id/splashFragment">

    <action android:id="@+id/action_global_login"
        app:destination="@id/loginFragment"/>

    <fragment
        android:id="@+id/rootHomeFragment"
        android:name="package.main.RootHomeFragment"
        android:label="fragment_root_home"
        tools:layout="@layout/fragment_root_home">
        <action
            android:id="@+id/actionGoToLoginFromHome"
            app:destination="@id/loginFragment"
         />

    </fragment>
    
    <fragment
        android:id="@+id/splashFragment"
        android:name="package.splash.SplashFragment"
        android:label="fragment_splash"
        tools:layout="@layout/fragment_splash">
        <action
            android:id="@+id/actionGoToRootHome"
            app:destination="@id/rootHomeFragment"
            app:launchSingleTop="true"
            app:popUpTo="@id/splashFragment"
            app:popUpToInclusive="true" />
        <action
            android:id="@+id/actionGoToLogin"
            app:destination="@id/loginFragment"
            app:launchSingleTop="true"
            app:popUpTo="@id/splashFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/loginFragment"
        android:name="package.signinup.LoginFragment"
        android:label="fragment_login"
        tools:layout="@layout/fragment_login">
        <action
            android:id="@+id/actionGoToRootHomeFromLogin"
            app:destination="@id/rootHomeFragment"
            app:launchSingleTop="true"
            app:popUpTo="@id/splashFragment"
            app:popUpToInclusive="true" />
    </fragment>
</navigation>

nav_home 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/nav_home"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="package.main.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/ordersFragment"
        android:name="package.main.OrdersFragment"
        android:label="fragment_orders"
        tools:layout="@layout/fragment_orders" />
    <fragment
        android:id="@+id/settingFragment"
        android:name="package.main.SettingFragment"
        android:label="fragment_setting"
        tools:layout="@layout/fragment_setting" >

    </fragment>
</navigation>

我从许多答案中尝试了许多解决方案,即使在使用 liveData 并在 RootHomeFragment 中观察它并尝试从那里移动到 LoginFragment 它也会移动 RootHomeFramgnet 默认目标 HomeFragment 时,有效的解决方案总是将我重定向到 homeFragment?任何解决方案?

以上是无法移动到嵌套子导航组件中的父片段?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>