用导航组件从背面删除片段

发布于 2025-01-17 16:40:33 字数 1651 浏览 1 评论 0原文

我有一个带有 navHost 片段的 PinCreateActivity 和 2 个片段 PinSetup 和 PinCreate 片段。

当 Activity 启动时,PinSetup 是默认片段,之后通过单击按钮导航到 PinCreate 片段。我想要的是来自 PinCreate 片段,当用户单击后退按钮时,不要像 PinCreateActivity 那样转到 PinSetup 并导航到后退堆栈。所以我想当我从 PinSetup 导航到 PinCreate 片段时,我必须从 backStack 中删除 PinSetup。我怎样才能做到这一点?

navigation_graph.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/pin_create_nav_graph"
    app:startDestination="@id/pinSetupFragment">

    <fragment
        android:id="@+id/pinSetupFragment"
        android:name="com.example.ui.fragments.pin.PinSetupFragment"
        android:label="Create PIN"
        tools:layout="@layout/fragment_pin_setup" >
        <action
            android:id="@+id/action_pinSetupFragment_to_pinCreateFragment"
            app:destination="@id/pinCreateFragment" />
    </fragment>
    <fragment
        android:id="@+id/pinCreateFragment"
        android:name="com.example.ui.fragments.pin.PinCreateFragment"
        android:label="Your PIN"
        tools:layout="@layout/fragment_pin_create" />
</navigation>

PinCreateActivity

private lateinit var navController: NavController

 override fun onCreate(savedInstanceState: Bundle?) {
 
    ...
    navController = Navigation.findNavController(this, R.id.pin_create_host_fragment)


    // onClick
    navController.navigate(R.id.action_pinSetupFragment_to_pinCreateFragment)

 }

I have a PinCreateActivity with a navHost fragment with 2 Fragments PinSetup and PinCreate fragments.

When the Activity launches, the PinSetup is the default fragment and after that with a click button i navigate to the PinCreate fragment. What i want is from the PinCreate fragment, when the User clicks the back button NOT to go to the PinSetup and navigate to the backstack as would the PinCreateActivity would do. So i guess when i navigate from the PinSetup to the PinCreate fragment i have to remove the PinSetup from the backStack. How can i do that?

navigation_graph.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/pin_create_nav_graph"
    app:startDestination="@id/pinSetupFragment">

    <fragment
        android:id="@+id/pinSetupFragment"
        android:name="com.example.ui.fragments.pin.PinSetupFragment"
        android:label="Create PIN"
        tools:layout="@layout/fragment_pin_setup" >
        <action
            android:id="@+id/action_pinSetupFragment_to_pinCreateFragment"
            app:destination="@id/pinCreateFragment" />
    </fragment>
    <fragment
        android:id="@+id/pinCreateFragment"
        android:name="com.example.ui.fragments.pin.PinCreateFragment"
        android:label="Your PIN"
        tools:layout="@layout/fragment_pin_create" />
</navigation>

PinCreateActivity

private lateinit var navController: NavController

 override fun onCreate(savedInstanceState: Bundle?) {
 
    ...
    navController = Navigation.findNavController(this, R.id.pin_create_host_fragment)


    // onClick
    navController.navigate(R.id.action_pinSetupFragment_to_pinCreateFragment)

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中楼上月下 2025-01-24 16:40:33

如果您在弹出时需要一些额外的逻辑,则可以通过Kotlin代码进行编程执行操作。
但是,如果您只需要从背堆中删除pinsetupfragment,就可以在导航图XML文件上执行此操作。

因此,如果您只需弹出片段而没有任何其他额外的逻辑,那么从背堆中弹出pinsetupfragment的最佳方法将更新您的navigation_graph.graph.xml文件。

只需将这两行添加到您的操作中:

app:popUpTo="@id/pinSetupFragment"
app:popUpToInclusive="true"

因此,您的navigation_graph.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/pin_create_nav_graph"
    app:startDestination="@id/pinSetupFragment">

    <fragment
        android:id="@+id/pinSetupFragment"
        android:name="com.example.ui.fragments.pin.PinSetupFragment"
        android:label="Create PIN"
        tools:layout="@layout/fragment_pin_setup" >
        <action
            android:id="@+id/action_pinSetupFragment_to_pinCreateFragment"
            app:destination="@id/pinCreateFragment"
            app:popUpTo="@id/pinSetupFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/pinCreateFragment"
        android:name="com.example.ui.fragments.pin.PinCreateFragment"
        android:label="Your PIN"
        tools:layout="@layout/fragment_pin_create" />
</navigation>

You can do that programmatically from Kotlin code if you need some extra logic while doing pop up.
But if you just need to remove PinSetupFragment from your back stack, you can do that on your navigation graph xml file.

So, if you are just going to pop up your fragment without any other extra logic, best way to pop up PinSetupFragment from your back stack would be updating your navigation_graph.xml file.

Just add these two lines to your action:

app:popUpTo="@id/pinSetupFragment"
app:popUpToInclusive="true"

As a result, your navigation_graph.xml file will be like this:

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

    <fragment
        android:id="@+id/pinSetupFragment"
        android:name="com.example.ui.fragments.pin.PinSetupFragment"
        android:label="Create PIN"
        tools:layout="@layout/fragment_pin_setup" >
        <action
            android:id="@+id/action_pinSetupFragment_to_pinCreateFragment"
            app:destination="@id/pinCreateFragment"
            app:popUpTo="@id/pinSetupFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/pinCreateFragment"
        android:name="com.example.ui.fragments.pin.PinCreateFragment"
        android:label="Your PIN"
        tools:layout="@layout/fragment_pin_create" />
</navigation>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文