导航组件 SingleTask 启动片段导航 [英] Navigation component SingleTask launching of fragment navigation

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

问题描述

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

I am using Navigation Component to build my application. I would like to implement SingleTask launch mode in the fragment navigation how can I achieve it?

为了更清楚,我将详细说明我的问题.

For more clarity, I will detail my problem.

我有一个 HomeFragement(Lets Fragment A),然后我定义了对内部屏幕的操作

I have a HomeFragement(Lets Fragment A) and from that I have defined actions to the inner screens

片段 A >片段 B >片段 C >片段D

Fragment A > Fragment B > Fragment C > Fragment D

我有一个 BaseFragment,它扩展了我使用 findNavController().popBackStack()

I have a BaseFragment which extends all the above fragments in which I have implemented back click and its action using findNavController().popBackStack()

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

In Fragment D when the user clicks the back button as expected, it is navigating back to Fragment C. My problem comes when I have called an action whose destination fragment is Fragment A. I am calling this action on a successful event in Fragment D, that action also works well. 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. I should destroy Fragment B and Fragment C on the successful event called in Fragment D and should resume Fragment A.

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

I know this flow can be achieved by using launch mode as SingleTask of the first screen(Fragment A) if it is an Activity instead of Fragment.

我的导航图 XML 文件

My navigation graph XML file

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

推荐答案

但是当用户从 Fragment A 按下后退按钮时,它会转到 Fragment C,下一次后退点击会转到 Fragment B,然后下一次后退按钮会转到 FRagment A.

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.

如果我理解正确,您的片段 A 是起始目的地,就像主页一样,并且您想要每当我回到 片段 A 时"的行为,我点击的下一个后退按钮应该退出应用程序",对吗?

If I understand correctly, your Fragment A is the start destination, like a main page, and you want the behavior of "whenever I come back to Fragment A, the next back button I click should exit the app", right?

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

If so, try to add action from any Fragment to Fragment A and set:

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

它应该是这样的:

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

这篇关于导航组件 SingleTask 启动片段导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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