简体   繁体   English

从 D - 导航组件返回时防止破坏(或恢复状态)片段 B

[英]Prevent destroying (or restore state) of Fragment B when navigating back from D - Navigation Component

I have fragments: A, B, C, D. I navigate A -> B .我有片段: A, B, C, D. 我导航 A -> B 。 Fragment B gets and saves state from arguments.片段 B 从参数中获取并保存状态。 Then I navigate B -> C. And then C -> D. When I call two times findNavController().popBackStack() I get correct behavior: D -> B and B still has correct state.然后我导航 B -> C。然后 C -> D。当我调用两次findNavController().popBackStack()我得到了正确的行为:D -> B 和 B 仍然具有正确的状态。 It works because fragment B has never been destroyed, just its view.它有效是因为片段 B 从未被销毁,只是它的视图。 And then view is recreated when coming back.然后返回时重新创建视图。 But calling two times popBackStack() isn't recommended action.但是不推荐调用两次 popBackStack() 。 We should instead use the action with app:popUpTo and app:popUpToInclusive="true" :我们应该改为使用app:popUpToapp:popUpToInclusive="true"

<action
    android:id="@+id/action_fragmentD_to_fragmentB"
    app:destination="@id/fragmentB"
    app:popUpTo="@+id/fragmentB"
    app:popUpToInclusive="true" />

But it forces fragment B to be destroyed completely and then recreated.但它迫使片段 B 完全销毁,然后重新创建。 Bu with no previous state .没有以前的状态

In other words I want to achieve the same behavior as with Activities when used FLAG_ACTIVITY_CLEAR_TOP + FLAG_ACTIVITY_SINGLE_TOP : https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP换句话说,我想在使用FLAG_ACTIVITY_CLEAR_TOP + FLAG_ACTIVITY_SINGLE_TOP时实现与活动相同的行为: https : //developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

There's no requirement to have an app:destination="@id/fragmentB" on an action if you don't want to navigate to a new instance of fragmentB (since that's what app:destination does).如果您不想导航到fragmentB的新实例,则不需要在操作上使用app:destination="@id/fragmentB" (因为app:destination这样做的)。 Therefore you can use:因此,您可以使用:

<action
    android:id="@+id/action_fragmentD_to_fragmentB"
    app:popUpTo="@+id/fragmentB" />

This is identical to calling popBackStack(R.id.fragmentB, false) - ie, pop back to fragmentB , but don't pop fragmentB itself.这与调用popBackStack(R.id.fragmentB, false) - 即弹出回到fragmentB ,但不弹出fragmentB本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导航回片段时恢复回收器视图状态 - 使用 jetpacks 导航架构 - Restore recycler view state when navigating back to the fragment - using the jetpacks navigation architecture 从bottomNavigationView导航到片段B时保存片段A的状态 - Save state of fragment A when navigating from bottomNavigationView to fragment B 导航返回时的 RecyclerView(导航组件) - RecyclerView when navigating back (Navigation Component) 使用导航组件向后导航 - Navigating back with the Navigation Component 导航组件防止在回按时重新创建片段 - Navigation Component prevent to recreate fragment on back press Android:当用户在导航组件、单活动应用程序中点击片段 B 时更新片段 A - Android: Update Fragment A when user hits back on Fragment B in Navigation Component, Single activity app 如何在使用导航组件导航时保存片段 state - How to save fragment state while navigating with navigation component 向后移动/导航时如何存储/恢复活动状态 - How to store/restore activity state when moving/navigating back 如何在从片段 B 切换到片段 A 时恢复 ViewPager state - How to restore ViewPager state on switch from fragment B to fragment A 背面的新导航组件上是否有办法恢复recyclerview的状态 - Is there a way on the new navigation component on back pressed to restore the state on recyclerview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM