我试图在使用getFragmentManager()?.beginTransaction()?.remove(MyFragment)?.commitAllowingStateLoss()
切换到同一个activity
上的另一个fragment
时销毁以前的fragment
,但在remove()
部分,我把我的片段的名称放在那里,它说**必需:碎片!找到:* * 我想这是因为我在这个fragment
中使用了伴随对象,因为我需要它来实现putExtra
函数。**如何销毁片段,即使其中有一个伴随对象?**谢谢。
1条答案
按热度按时间vnzz0bqm1#
remove()
接受一个 *Fragment示例 *,您只需将 *Fragment类 * 的名称传递给它即可。因为在Kotlin中,你可以将
MyFragment.Companion.someProperty
缩写为MyFragment.someProperty
,仅仅使用MyFragment
作为一个值(就像你在这里所做的--remove
需要一个 object)就意味着你实际上引用了MyFragment.Companion
。所以你在这里有一个伴随对象并不是问题所在,你只是做了一件错误的事情,试图将它传递给remove
。