如何从 ViewPager2 的 NavHostFragment 内的片段导航回来? [英] How to navigate back from fragments inside NavHostFragment of ViewPager2?
问题描述
从 ViewPager2 的嵌套片段返回的正确方法是什么?
What is the proper way of navigating back from nested fragments of ViewPager2?
尽管使用 app:defaultNavHost=true"
和 FragmentContainerView
在页面的嵌套片段中按后退按钮调用 Activity 的后按而不是导航回上一个片段.
Despite using app:defaultNavHost="true"
with FragmentContainerView
pressing back button while in a nested fragment of a page calls Activity's back press instead of navigating back to previous fragment.
推荐答案
根据 创建NavHostFragment文档,app:defaultNavHost=true"
调用setPrimaryNavigationFragment()
首次添加 Fragment 时 - 它是 setPrimaryNavigationFragment()
自动将返回按钮按下事件路由到该片段.
As per the Create a NavHostFragment documentation, app:defaultNavHost="true"
calls setPrimaryNavigationFragment()
when the Fragment is first added - it is setPrimaryNavigationFragment()
that routes back button press events to that fragment automatically.
在 ViewPager2
中,负责创建和添加 Fragment 的是 ViewPager2.由于 Fragment 层次结构的每一层都需要为主导航片段,通过 XML 添加子片段仍然不能解决缺失的链接:ViewPager2 中的 Fragment 需要为主导航片段.
In a ViewPager2
though, it is the ViewPager2 that is responsible for creating and adding the Fragment. Since every level of the Fragment hierarchy needs to be the primary navigation fragment, adding a child fragment via XML still doesn't solve the missing link: that the Fragment in the ViewPager2 needs to be the primary navigation fragment.
因此,当 Fragment 成为活动 Fragment 时,您需要挂钩回调并调用 setPrimaryNavigationFragment()
.ViewPager2 1.1.0-alpha01 在FragmentTransactionCallback
,特别是onFragmentMaxLifecyclePreUpdated()
,每当 Fragment 的 Lifecycle 状态发生更改时都会调用它:当它更改为 RESUMED
时,该 Fragment 现在是活动片段,应该成为主要导航片段,作为 onPost
回调的一部分.
Therefore, you need to hook into the callbacks for when a Fragment is made the active Fragment and call setPrimaryNavigationFragment()
. ViewPager2 1.1.0-alpha01 adds exactly this API in the FragmentTransactionCallback
, specifically, the onFragmentMaxLifecyclePreUpdated()
, which is called whenever the Lifecycle state of a Fragment is changed: when it is changed to RESUMED
, that Fragment is now the active fragment and should become the primary navigation Fragment as part of the onPost
callback.
private class Adapter(parentFragment: Fragment) : FragmentStateAdapter(parentFragment) {
init {
// Add a FragmentTransactionCallback to handle changing
// the primary navigation fragment
registerFragmentTransactionCallback(object : FragmentTransactionCallback() {
override fun onFragmentMaxLifecyclePreUpdated(
fragment: Fragment,
maxLifecycleState: Lifecycle.State
) = if (maxLifecycleState == Lifecycle.State.RESUMED) {
// This fragment is becoming the active Fragment - set it to
// the primary navigation fragment in the OnPostEventListener
OnPostEventListener {
fragment.parentFragmentManager.commitNow {
setPrimaryNavigationFragment(fragment)
}
}
} else {
super.onFragmentMaxLifecyclePreUpdated(fragment, maxLifecycleState)
}
})
}
// The rest of your FragmentStateAdapter...
}
这篇关于如何从 ViewPager2 的 NavHostFragment 内的片段导航回来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!