Android 导航组件注销后如何清空堆栈?
How to clear backstack after logging out Android Navigation Component?
我的应用程序中有以下片段
- SignInFragment
- HomeFragment(在我的代码中命名为 AllCategoryFragment)
- SettingFragment(从BottomNavigationView打开,用于注销)
- 其他几个片段
app screenshot
导航到 HomeFragment 时,我正在使用 popUpTo 和 popUpToInclusive 从后台堆栈中删除 SignInFragment,它工作得很好。
SignInFragment 的代码片段 -> HomeFragment
<fragment
android:id="@+id/signInFragment"
android:label="Log In">
<action
android:id="@+id/action_signInFragment_to_allCategoryFragment"
app:destination="@id/allCategoryFragment"
app:popUpTo="@id/signInFragment"
app:popUpToInclusive="true" />
</fragment>
我尝试使用类似的逻辑在退出 SignInFragment 后从 SettingFragment 导航时清除后台堆栈。如果用户在 SignInFragment 上按下后退按钮,我想要的目标是退出应用程序。在 SignInFragment 上按后退按钮会返回到之前的 present/navigated 个片段。
SettingFragment 的代码片段 -> SignInFragment
<fragment
android:id="@+id/settingFragment"
android:label="App Settings">
<action
android:id="@+id/action_settingFragment_to_manageCategoriesFragment"
app:destination="@id/manageCategoriesFragment" />
<action
android:id="@+id/action_settingFragment_to_signInFragment"
app:destination="@id/signInFragment"
app:popUpTo="@id/allCategoryFragment"
app:popUpToInclusive="true" />
</fragment>
HomeFragment 设置为导航图中的起始标记。
还有一个重要的注意事项,XML 上面编写的用于在退出后清除后台堆栈的代码仅在后台堆栈上有一个 HomeFragment 实例时才有效,否则用户可以返回到之前的 present/navigated 片段。
这种情况下返回堆栈被清除,HomeFragment -> HistoryFragment -> SettingFragment
但如果我有这样的东西,HomeFragment -> HistoryFragment -> HomeFragment -> SettingFragment 就会失败。在这种情况下,注销后我的后台堆栈看起来像 HomeFragment -> HistoryFragment,但应该被清除。
我找到了解决方案。
以下 3 个要点说明了我的导航是如何设置的
- 登录片段是 startDestination。
- 您从 设置片段注销。
- 只有一张导航图。
解决方法
如果将 popUpTo 设置为导航图的 ID,并将 popUpToInclusive 设置为 true注销后从设置片段导航到登录片段时,后台堆栈将被完全清除。
我的应用程序中有以下片段
- SignInFragment
- HomeFragment(在我的代码中命名为 AllCategoryFragment)
- SettingFragment(从BottomNavigationView打开,用于注销)
- 其他几个片段
app screenshot
导航到 HomeFragment 时,我正在使用 popUpTo 和 popUpToInclusive 从后台堆栈中删除 SignInFragment,它工作得很好。
SignInFragment 的代码片段 -> HomeFragment
<fragment
android:id="@+id/signInFragment"
android:label="Log In">
<action
android:id="@+id/action_signInFragment_to_allCategoryFragment"
app:destination="@id/allCategoryFragment"
app:popUpTo="@id/signInFragment"
app:popUpToInclusive="true" />
</fragment>
我尝试使用类似的逻辑在退出 SignInFragment 后从 SettingFragment 导航时清除后台堆栈。如果用户在 SignInFragment 上按下后退按钮,我想要的目标是退出应用程序。在 SignInFragment 上按后退按钮会返回到之前的 present/navigated 个片段。
SettingFragment 的代码片段 -> SignInFragment
<fragment
android:id="@+id/settingFragment"
android:label="App Settings">
<action
android:id="@+id/action_settingFragment_to_manageCategoriesFragment"
app:destination="@id/manageCategoriesFragment" />
<action
android:id="@+id/action_settingFragment_to_signInFragment"
app:destination="@id/signInFragment"
app:popUpTo="@id/allCategoryFragment"
app:popUpToInclusive="true" />
</fragment>
HomeFragment 设置为导航图中的起始标记。
还有一个重要的注意事项,XML 上面编写的用于在退出后清除后台堆栈的代码仅在后台堆栈上有一个 HomeFragment 实例时才有效,否则用户可以返回到之前的 present/navigated 片段。
这种情况下返回堆栈被清除,HomeFragment -> HistoryFragment -> SettingFragment
但如果我有这样的东西,HomeFragment -> HistoryFragment -> HomeFragment -> SettingFragment 就会失败。在这种情况下,注销后我的后台堆栈看起来像 HomeFragment -> HistoryFragment,但应该被清除。
我找到了解决方案。
以下 3 个要点说明了我的导航是如何设置的
- 登录片段是 startDestination。
- 您从 设置片段注销。
- 只有一张导航图。
解决方法
如果将 popUpTo 设置为导航图的 ID,并将 popUpToInclusive 设置为 true注销后从设置片段导航到登录片段时,后台堆栈将被完全清除。