gpt4 book ai didi

android - FragmentManager.put/getFragment 与 findFragmentByTag

转载 作者:搜寻专家 更新时间:2023-11-01 08:47:03 25 4
gpt4 key购买 nike

更重要的是,在 onCreate/onCreateView 中,我已经调用 FragmentManager.findFragmentByTag() 来查找我的 fragment 的任何现有实例,它似乎找到了它。

那么putFragment/getFragment有什么意义呢?它会节省一些额外的东西还是会导致其他生命周期的事情发生?它只是做或多或少相同事情的 findFragmentByTag() 的替代方法吗?因为在我看来, fragment 会自动为我保存,而无需使用 FragmentManager.putFragment()。

最佳答案

So what is the point of putFragment/getFragment?

根据目前的实现,什么putFragment(Bundle bundle, String key, Fragment fragment) do 是将 fragment 的索引放入带有参数键的包中。然后getFragment(Bundle bundle, String key)获取同一索引处的 fragment ,可以使用相同的键从 bundle 中检索该 fragment 。 Fragment 只有在添加到 FragmentManager 后才会在 FragmentManager 中有其索引,因此只有在添加后才能在 Fragment 上调用 putFragment()。

Does it save something extra or cause additional lifecycle stuff to happen?

它只保存一个 fragment 的索引,没有其他东西,也不会导致任何额外的生命周期东西。

Is it just an alternative to findFragmentByTag() that does more or less the same thing?

是的,我认为是。

按照目前的实现,putFragment/getFragment的作用可以用findFragmentByTag()来实现也。但是 putFragment/getFragment 的功能非常有限,因为你不能在没有 bundle 参数的情况下使用它们,这意味着你必须调用 putFragment()onSaveInstanceState() .

关于android - FragmentManager.put/getFragment 与 findFragmentByTag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27240756/

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