退出嵌套导航图
Exit from nested Navigation Graph
我有 3 个图表:'Main'、“1”和“2”
应用程序开始 'Main',我们检查用户是否登录,然后根据登录状态转到“1”或“2”。
这按预期工作。
这是问题所在:
当用户从“1”或“2”主屏幕单击 'back' 时,应用会导航回 'Main'。
这是预期的结果:
当用户从“1”或“2”主屏幕点击 'back' 时,应用应退出。
如何退出嵌套图的顶级片段?
这是我的 '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/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>
为了在嵌套导航图目的地中退出应用程序,只需使用 popUpTo
并将其设置为 main_nav_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>
要查看存储库中的工作示例 login-flow branch。
我也遇到了同样的问题。 google 之后,我在 Google 问题跟踪器上发现了一个问题:https://issuetracker.google.com/issues/140124444
它说,这是有意为之的行为,不会被修复。并且根据 Principles of Navigation,您不应该从返回堆栈中弹出起始目的地。
只是为了补充 user158 答案,要从嵌套导航中退出应用程序,您必须配置 popUpTo 和 popUpToInclusive。
示例:
- 下图第14行和第15行main_navigation.xml
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
上查看源代码
我有 3 个图表:'Main'、“1”和“2”
应用程序开始 'Main',我们检查用户是否登录,然后根据登录状态转到“1”或“2”。
这按预期工作。
这是问题所在:
当用户从“1”或“2”主屏幕单击 'back' 时,应用会导航回 'Main'。
这是预期的结果:
当用户从“1”或“2”主屏幕点击 'back' 时,应用应退出。
如何退出嵌套图的顶级片段?
这是我的 '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/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>
为了在嵌套导航图目的地中退出应用程序,只需使用 popUpTo
并将其设置为 main_nav_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>
要查看存储库中的工作示例 login-flow branch。
我也遇到了同样的问题。 google 之后,我在 Google 问题跟踪器上发现了一个问题:https://issuetracker.google.com/issues/140124444
它说,这是有意为之的行为,不会被修复。并且根据 Principles of Navigation,您不应该从返回堆栈中弹出起始目的地。
只是为了补充 user158 答案,要从嵌套导航中退出应用程序,您必须配置 popUpTo 和 popUpToInclusive。
示例:
- 下图第14行和第15行main_navigation.xml
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
上查看源代码