导航架构:如何在不使用 clearTask 的情况下管理正确的导航,因为它已被弃用
Navigation architecture: How to manage proper navigation without using clearTask as it is deprecated
从 here , here 开始使用导航架构时,不推荐使用 clearTask。
我的场景是这样的:有2个屏幕登录和注册,都有彼此的链接。因此,您可以从登录转到注册,也可以从注册转到登录。 但是在后面Press App应该是关闭的。
只需将 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>
但由于它已被弃用,我尝试了其他解决方案,例如添加 popUpTo -> 导航图的 Id,使 launchSingleTop 在两个操作中都为真.在我的场景中似乎没有任何效果。
我也检查了 但没有得到解决方案。
您需要在您的操作中使用下一个代码
app:popUpTo="@+id/fragment_login"
app:popUpToInclusive="true"
将您的 NavHostFragment defaultNavHost 值设置为 false,
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="false"
app:navGraph="@navigation/nav_graph"
... />
app:defaultNavHost="true" 属性确保您的 NavHostFragment 拦截系统后退按钮。请注意,只有一个 NavHost 可以是默认值。如果在同一布局(例如双窗格布局)中有多个主机,请确保仅指定一个默认 NavHost。
简单有效的解决方法:
fun signOut(activity: Activity) = activity.finish()
从 here , here 开始使用导航架构时,不推荐使用 clearTask。
我的场景是这样的:有2个屏幕登录和注册,都有彼此的链接。因此,您可以从登录转到注册,也可以从注册转到登录。 但是在后面Press App应该是关闭的。
只需将 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>
但由于它已被弃用,我尝试了其他解决方案,例如添加 popUpTo -> 导航图的 Id,使 launchSingleTop 在两个操作中都为真.在我的场景中似乎没有任何效果。
我也检查了
您需要在您的操作中使用下一个代码
app:popUpTo="@+id/fragment_login"
app:popUpToInclusive="true"
将您的 NavHostFragment defaultNavHost 值设置为 false,
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="false"
app:navGraph="@navigation/nav_graph"
... />
app:defaultNavHost="true" 属性确保您的 NavHostFragment 拦截系统后退按钮。请注意,只有一个 NavHost 可以是默认值。如果在同一布局(例如双窗格布局)中有多个主机,请确保仅指定一个默认 NavHost。
简单有效的解决方法:
fun signOut(activity: Activity) = activity.finish()