问题描述
和其他许多人一样,我正在为这个错误而苦苦挣扎:
Like many others, I'm struggling with this error:
此问题提供了一些有关如何处理此问题的想法.但是,这个问题没有任何解释,为什么片段首先从其活动中脱离出来?了解发生这种情况的原因是否有助于我设计应用程序,从而避免发生这种情况?
This question offers some ideas on how to deal with this. There are no explanations to the question however, why does a Fragment detach from their Activity in the first place? Does an understanding of why this happens help me to design my app in a way to avoid this happening?
在某些情况下,我可能没有异步任务,请在完成后调用 getResources()
;我在Fragment的 onCreate()
方法中调用 getResources()
.有时,当我快速浏览我的应用程序时,会出现错误提示.可以预料到,在其自己的 onCreate()
方法中,片段甚至不一定会附加到其Activity吗?
In may case I do not have some asynchronous task and call getResources()
when it completes; I call getResources()
in the onCreate()
method of the Fragment. And sometimes, when I'm navigating my app rather fast, said error surfaces. Is it to be expected that the Fragment is not even necessarily attached to its Activity during its own onCreate()
method?
其次,链接的问题中提供了解决方案(用 i Leicester()
和 getActivity()!= null
保护 getResources()
)不要帮我没有合理的方法来处理 getResources()
不可用.
Secondly, the solutions provided in the linked question (guarding getResources()
with isAdded()
and getActivity() != null
) don't help me. There is no reasonable way to deal with getResources()
not being available.
推荐答案
因为Android可能会销毁并重新创建Activity以适应配置更改.相反,碎片不是.
Because Android supposes that Activities may be destroyed and recreated to accommodate configuration changes. Fragments, in contrast, are not.
是的,它是预期的".IMO设计不良,但可预期.
Yes, it is "expected." Bad design IMO, but expected.
这篇关于为什么片段会与其活动分离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!