Android 从嵌套导航图退出

Android 从嵌套导航图退出,android,android-architecture-components,Android,Android Architecture Components,我有3个图形:“Main”、“1”和“2” 应用程序开始“主”,我们检查用户是否已登录,然后根据登录状态转到“1”或“2” 这正如预期的那样有效 问题是: 当用户从“1”或“2”主屏幕单击“后退”时,应用程序将导航回“main” 以下是预期结果: 当用户从“1”或“2”主屏幕单击“后退”时,应用程序应退出 如何从嵌套图的顶层片段退出? 这是我的“主”导航图: <?xml version="1.0" encoding="utf-8"?> <navigation xmlns:and

我有3个图形:“Main”、“1”和“2”

应用程序开始“主”,我们检查用户是否已登录,然后根据登录状态转到“1”或“2”

这正如预期的那样有效

问题是:

当用户从“1”或“2”主屏幕单击“后退”时,应用程序将导航回“main”

以下是预期结果:

当用户从“1”或“2”主屏幕单击“后退”时,应用程序应退出

如何从嵌套图的顶层片段退出?

这是我的“主”导航图:

<?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/main_navigation"
    app:startDestination="@id/fragment_main">

    <fragment
        android:id="@+id/fragment_main"
        android:name="com.my.app.fragments.MainFragment"
        android:label="MainFragment"
        tools:layout="@layout/fragment_main">
        <action
            android:id="@+id/action_fragment_main_to_logged_out_navigation"
            app:destination="@id/logged_out_navigation"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"
            app:launchSingleTop="true"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim"
            app:popUpTo="@+id/main_navigation"
            app:popUpToInclusive="true" />
        <action
            android:id="@+id/action_fragment_main_to_logged_in_navigation"
            app:destination="@id/logged_in_navigation"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"
            app:launchSingleTop="true"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim"
            app:popUpTo="@+id/main_navigation"
            app:popUpToInclusive="true" />
    </fragment>
    <include app:graph="@navigation/logged_in_navigation" />
    <include app:graph="@navigation/logged_out_navigation" />
</navigation>

要在嵌套导航图目标中退出应用程序,只需使用
poputo
并将其设置为
main\u nav\u graph.xml

示例:

设计

XML

<?xml version="1.0" encoding="utf-8"?>
<navigation ...
    android:id="@+id/main_nav_graph.xml"
    app:startDestination="@id/start">

    <fragment
        android:id="@+id/start"
        android:name="com.example.navargs.StartFragment"
        android:label="Start"
        tools:layout="@layout/fragment_start" >
        ...
        <action
            android:id="@+id/action_start_to_navigation"
            app:destination="@id/login_nav_graph"
            app:popUpTo="@+id/main_nav_graph.xml" />

    </fragment>
    <fragment ... />
    <include app:graph="@navigation/login_nav_graph" />
</navigation>

...

在回购协议中。

我一直面临着同样的问题。通过谷歌搜索,我在谷歌问题追踪器上发现了一个问题:


它说,这是故意的行为,不会被纠正。根据,您不应该将起始目标从后堆栈中弹出。

为了补充答案,要从嵌套导航中退出应用程序,您必须配置popUpTo和popUpToInclusive

例如:

  • 下图的第14行和第15行
main_navigation.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/main_navigation.xml"
    app:startDestination="@id/startFragment">

    <fragment
        android:id="@+id/startFragment"
        android:name="com.fortatic.apps.nestednavigationexample.start.StartFragment"
        android:label="fragment_start"
        tools:layout="@layout/fragment_start">
        <action
            android:id="@+id/action_startFragment_to_navGraphOne"
            app:destination="@id/navGraphOne"
            app:popUpTo="@id/startFragment"
            app:popUpToInclusive="true" />
        <action
            android:id="@+id/action_startFragment_to_navGraphTwo"
            app:destination="@id/navGraphTwo"
            app:popUpTo="@id/startFragment"
            app:popUpToInclusive="true" />
    </fragment>

    <navigation
        android:id="@+id/navGraphOne"
        app:startDestination="@id/AFragment">
        <fragment
            android:id="@+id/AFragment"
            android:name="com.fortatic.apps.nestednavigationexample.one.AFragment"
            android:label="fragment_a"
            tools:layout="@layout/fragment_a">
            <action
                android:id="@+id/action_AFragment_to_BFragment"
                app:destination="@id/BFragment"
                app:popUpTo="@id/AFragment"
                app:popUpToInclusive="true" />
        </fragment>
        <fragment
            android:id="@+id/BFragment"
            android:name="com.fortatic.apps.nestednavigationexample.one.BFragment"
            android:label="fragment_b"
            tools:layout="@layout/fragment_b" />
    </navigation>

    <navigation
        android:id="@+id/navGraphTwo"
        app:startDestination="@id/CFragment">
        <fragment
            android:id="@+id/CFragment"
            android:name="com.fortatic.apps.nestednavigationexample.two.CFragment"
            android:label="fragment_c"
            tools:layout="@layout/fragment_c">
            <action
                android:id="@+id/action_CFragment_to_DFragment"
                app:destination="@id/DFragment" />
        </fragment>
        <fragment
            android:id="@+id/DFragment"
            android:name="com.fortatic.apps.nestednavigationexample.two.DFragment"
            android:label="fragment_d"
            tools:layout="@layout/fragment_d" />
    </navigation>

</navigation>


用户是否从
main
graph的主屏幕导航到
1
2
图形?如果需要,请返回主屏幕
main
。以下答案是否解决了您的问题?这是预期的行为,不会得到解决:@zoha131 nuts。把它作为一个答案贴出来,我会给你一个复选标记,我已经在做了,不幸的是,它没有做,尽管trickIt在回购协议中起作用。尝试删除该操作并重新创建。另外,请尝试将nav版本更改为最新。repo具有嵌套的导航元素,而不是“include”元素。我怀疑它对“include”元素不起作用,但希望我只是在做点什么wrong@Psest328如果您可以设置一个示例回购来重现问题。@user158回购对我不起作用。在名为First的片段中,如果单击向上导航按钮,则应用程序不会关闭。实际上,这里有两个分支,一个是我展示如何生成嵌套导航图(分支),另一个是我展示如何生成嵌套导航图(分支),但集成了ViewModels(分支)。你可以看到你需要的一个。你有1个而不是2个。您有2个导航图,但您根本不需要它们。正确,作为提出问题的用户,我只有1个navhostfragment,我有3个导航图,如果我需要它们,我需要它们与提出问题的用户处于相同的情况。正如问题所说,我有一个主图和两个嵌套图。