无法移动到嵌套子导航组件中的父片段? [英] failed to move to parent fragment in nested child navigation component?

查看:26
本文介绍了无法移动到嵌套子导航组件中的父片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Navigation component in my app with the pattern of single activity and multiple fragments, I had some fragments takes the full device screen and other fragments share it with the bottom navigation component and to achieve that with single activity I used fragment container view in my main activity and for home screens fragments which has navigation fragments I used RootHomeFragment which has another fragment container view with bottom navigation view. here the full application navigation graph

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

(nav_main) -->MainActivity has view (main_fragment)--> (SplashFragment - LoginFragment = RootHomeFragment)

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

(nav_home) --> RootHomeFragment has views (home_fragment / bottom navigation view) --> (HomeFragment - SettingFragment)

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

the problem is in settingFragment, there is an action to logout which redirects me to login screen but I can't do this action, I try to do it with the next code in SettingFragment but the app moves to HomeFragment which is the defaul home destination of nav_home

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

这是我的 nav_main 的 xml

here is the xml of my nav_main

<?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

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?有什么解决办法吗?

I tried many solutions from many answers and the worked solution always redirects me to homeFragment even when use liveData and observing it in the RootHomeFragment and try to move from there to the LoginFragment it moves also the RootHomeFramgnet default destination HomeFragment? Any solutions?

推荐答案

在我看来,您导航到 LoginFragment 然后它立即导航回 HomeFragment.

This sounds to me, that you navigate to the LoginFragment then it immediately navigates back to the HomeFragment.

我们需要看看 LoginFragment/它的 viewModel 发生了什么.

We need to see what happens in the LoginFragment / it's viewModel.

这篇关于无法移动到嵌套子导航组件中的父片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆