从嵌套导航图退出

时间:2019-08-22 18:12:14

标签: android android-architecture-components

我有3个图表:“ Main”,“ 1”和“ 2”

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

这按预期工作。

问题出在这里

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

这是预期的结果:

当用户在“ 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>

3 个答案:

答案 0 :(得分:0)

要在嵌套导航图目标中退出应用程序,只需使用popUpTo并将其设置为main_nav_graph.xml

示例:

设计

main nav graph

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>

要查看工作示例中的仓库中的login-flow branch结帐。

答案 1 :(得分:0)

我一直面临着同样的问题。经过一番Google搜索后,我在Google问题跟踪器上发现了一个问题:here

它说,这是预期的行为,不会被解决。而且根据https://issuetracker.google.com/issues/140124444,您不应将起始目标从堆栈中弹出。

答案 2 :(得分:0)

仅是对user158答案的补充,为了从嵌套导航中退出应用程序,您必须配置popUpTo和popUpToInclusive。

示例:

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>

您也可以在github上查看源代码