代码之家  ›  专栏  ›  技术社区  ›  TapanHP

导航架构:如何在不使用clearTask的情况下管理正确的导航,因为clearTask已被弃用

  •  1
  • TapanHP  · 技术社区  · 7 年前

    here , here clearTask已弃用。

    但背压应用程序应该关闭。

    只需将clearTask添加到两个操作中即可,如下所示。

    <?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/nv_auth_phase"
        app:startDestination="@id/fragment_login">
    
        <fragment
            android:id="@+id/fragment_login"
            android:name="com.jobhook.ui.auth.login.LoginFragment"
            android:label="LoginFragment"
            tools:layout="@layout/fragment_login">
            <action
                android:id="@+id/nv_action_login_to_registration"
                app:clearTask="true"
                app:destination="@id/fragment_registration" />
    
        </fragment>
    
    
        <fragment
            android:id="@+id/fragment_registration"
            android:name="com.jobhook.ui.auth.registration.RegistrationFragment"
            android:label="RegistrationFragment"
            tools:layout="@layout/fragment_registration">
    
    
            <action
                android:id="@+id/nv_action_registration_to_login"
                app:clearTask="true"
                app:destination="@id/fragment_login" />
        </fragment>
    </navigation>
    

    但由于不赞成,我尝试了其他解决方案,如添加 弹出窗口->导航图的Id ,制作 在两个操作中,launchSingleTop都为true . 在我的情况下似乎什么都不管用。

    我查过了 this question 但也没有找到解决办法。

    1 回复  |  直到 7 年前
        1
  •  6
  •   Eugene    7 年前

    你需要在你的行动中使用下一个代码

    app:popUpTo="@+id/fragment_login"
    app:popUpToInclusive="true"
    
        2
  •  0
  •   Alex Mofer    6 年前

    <fragment
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:defaultNavHost="false"
        app:navGraph="@navigation/nav_graph"
        ... />
    

    这个应用程序:defaultNavHost=“true”属性确保NavHostFragment拦截系统后退按钮。请注意,只有一个NavHost可以作为默认值。如果在同一布局中有多个主机(例如,两个窗格布局),请确保仅指定一个默认NavHost。

    推荐文章