android - 导航组件 SingleTask 启动 fragment 导航

标签 android android-xml android-navigation android-launcher android-navigation-graph

我正在使用导航组件来构建我的应用程序。我想在 fragment 导航中实现 SingleTask 启动模式,如何实现?

为了更清楚起见,我将详细说明我的问题。

我有一个 HomeFragement(让 fragment A),并从中定义了内部屏幕的操作

Fragment A > Fragment B > Fragment C > Fragment D

我有一个 BaseFragment,它扩展了上述所有 fragment ,其中我使用 findNavController().popBackStack() 实现了后退单击及其操作

在 fragment D 中,当用户按预期单击后退按钮时,它会导航回 fragment C。当我调用目标 fragment 为 fragment A 的操作时,我的问题出现了。我在成功事件中调用此操作 fragment D,这个 Action 也效果很好。但是,当用户从 fragment A 按下后退按钮时,它会转到 fragment C,下一次返回单击会转到 fragment B,然后下一次返回单击会转到 fragment A。我应该在 fragment 中调用的成功事件上销毁 fragment B 和 fragment C D 并且应该恢复 fragment A。

我知道如果它是一个 Activity 而不是 Fragment,可以通过使用启动模式作为第一个屏幕(Fragment A)的 SingleTask 来实现此流程。

我的导航图 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/app_navigation"
    app:startDestination="@id/fragmentA">

    <fragment

        android:id="@+id/fragmentA"
        android:name="FragmentA"
        
        tools:layout="@layout/fragment_a">
        <action
            android:id="@+id/action_FragmentA_to_FragmentB"
            app:destination="@id/dashboardFragment"
            app:launchSingleTop="true"
            app:popUpTo="@+id/main_nav_graph"
            app:popUpToInclusive="true" />
    </fragment>
   <fragment

        android:id="@+id/fragmentB"
        android:name="FragmentB"
        
        tools:layout="@layout/fragment_b">
        <action
            android:id="@+id/action_FragmentB_to_FragmentC"
            app:destination="@id/dashboardFragment"
            app:launchSingleTop="true"
            app:popUpTo="@+id/main_nav_graph"
            app:popUpToInclusive="true" />
    </fragment>
<fragment

        android:id="@+id/fragmentC"
        android:name="FragmentC"
        
        tools:layout="@layout/fragment_c">
        <action
            android:id="@+id/action_fragemntC_to_fragementA"
            app:launchSingleTop="true"
            app:popUpToInclusive="true"
            app:destination="@id/deliveriesFragment" />
    </fragment>
   </navigation>

最佳答案

But when the user press the back button from Fragment A it goes to Fragment C, the next back click goes to Fragment B, then the next back click to FRagment A.

如果我理解正确的话,您的 fragment A是起始目的地,就像主页一样,并且您希望“每当我回到 fragment A时,我单击的下一个后退按钮应该退出应用程序”,对吗?

如果是这样,请尝试将任何 Fragment 中的 action 添加到 Fragment A 并设置:

app:popUpTo="@id/fragmentA"
app:popUpToInclusive="true" />

它应该看起来像这样:

enter image description here

enter image description here

演示:https://youtu.be/LNyk_FEkZoA

关于android - 导航组件 SingleTask 启动 fragment 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65705452/

相关文章:

java - android中使用OnTouchListener实现 slider 结构

java - 将字符串转换为 JSON 数组

android - 如何设置复选框默认选中复选框对话框

android - ImageView 没有保持最大高度和宽度

java - 如何打印动态更新 View 的当前 XML

android - 为什么我的 fragment 没有显示在导航 Controller 中?

java - 如何使用 SQLiteOpenHelper Android 在单个应用程序中创建多个数据库

java - 在 Java 文件而不是 xml 中定义颜色常量

android - 是否可以在 `FragmentContainerView` 中膨胀包含 `Fragment` 的 View ?

android - 尝试在空对象引用上调用虚拟方法 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()'