gpt4 book ai didi

android - 未使用导航组件将 fragment 添加到后台堆栈

转载 作者:行者123 更新时间:2023-12-02 13:52:59 24 4
gpt4 key购买 nike

信息:

我正在以编程方式为应用的每个功能插入一个 NavHostFragment。每个 NavHostFragment 都有自己的导航图。 Dagger 通过使用特定于每个功能的 FragmentFactory 来提供它们。这是一个采用 MVVM 架构的单 Activity 结构。

仓库:https://github.com/mitchtabian/DaggerMultiFeature/tree/nav-component-backstack-bug

检查分支“nav-component-backstack-bug”。

<小时/>

问题

导航到图表时, fragment 不会添加到返回堆栈中。添加的唯一 fragment 是最近访问过的 fragment 。 因此堆栈大小始终保持为 1。

最初我认为这是因为我没有将 FragmentFactory 设置为 ChildFragmentManager 但这并没有改变任何东西。相关代码请参阅下面的代码 fragment 。或者 checkout 项目并运行它。我有日志从 ChildFragmentManagerSupportFragmentManager 打印出当前在后台堆栈中的 fragment 。两者的大小均恒定为 1。

<小时/>

Feature1NavHostFragment.kt

这是自定义 NavHostFragment 之一。伴随对象中的 create() 函数是我创建它们的方式。


class Feature1NavHostFragment
@Inject
constructor(
private val feature1FragmentFactory: Feature1FragmentFactory
): NavHostFragment(){

override fun onAttach(context: Context) {
((activity?.application) as BaseApplication)
.getAppComponent()
.feature1Component()
.create()
.inject(this)
childFragmentManager.fragmentFactory = feature1FragmentFactory
super.onAttach(context)
}

companion object{

const val KEY_GRAPH_ID = "android-support-nav:fragment:graphId"

@JvmStatic
fun create(
feature1FragmentFactory: Feature1FragmentFactory,
@NavigationRes graphId: Int = 0
): Feature1NavHostFragment{
var bundle: Bundle? = null
if(graphId != 0){
bundle = Bundle()
bundle.putInt(KEY_GRAPH_ID, graphId)
}
val result = Feature1NavHostFragment(feature1FragmentFactory)
if(bundle != null){
result.arguments = bundle
}
return result
}
}

}
<小时/>

MainActivity.kt

这是我如何在 MainActivity 中创建 NavHostFragment 的示例。


val newNavHostFragment = Feature1NavHostFragment.create(
getFeature1FragmentFactory(),
graphId
)
supportFragmentManager.beginTransaction()
.replace(
R.id.main_nav_host_container,
newNavHostFragment,
getString(R.string.NavHostFragmentTag)
)
.setPrimaryNavigationFragment(newNavHostFragment)
.commit()

<小时/>

Feature1MainFragment.kt

这是我如何导航到图表中其他 fragment 的示例。


btn_go_next.setOnClickListener {
findNavController().navigate(R.id.action_feature1MainFragment_to_feature1NextFragment)
}

<小时/>

摘要

就像我说的,在每个 fragment 中,我都打印 ChildFragmentManagerSupportFragmentManager 的返回堆栈。两者的大小均恒定为 1。就好像当我导航到图表时 fragment 被替换而不是添加到堆栈中。

无论如何,感谢您阅读本文,如果您有任何见解,我将不胜感激。

最佳答案

看起来像是我的误解(也是我的错误)。

如果您循环遍历 childFragmentManager 中的 fragment ,由于某种原因它只会显示最顶层的 fragment 。

示例

val navHostFragment = supportFragmentManager
.findFragmentByTag(getString(R.string.NavHostFragmentTag)) as NavHostFragment
val fragments = navHostFragment.childFragmentManager.fragments
for(fragment in fragments){
// Only prints a single fragment, no matter the backstack size
}

但是,如果您像这样打印后栈大小,您将得到正确的答案。

val navHostFragment = supportFragmentManager
.findFragmentByTag(getString(R.string.NavHostFragmentTag)) as NavHostFragment
val backstackCount = navHostFragment.childFragmentManager.backStackEntryCount
println("backstack count: $backstackCount")
<小时/>

最终,这种误解让我相信 fragment 没有被添加到后台堆栈中。一切都很好。

关于android - 未使用导航组件将 fragment 添加到后台堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59775946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com