Android - activity 完成后会重新释放片段引用吗?
Android - Do activity realases fragment references when finished?
根据 Android 开发者网站,activity 与其片段通信的正确方式是通过监听器。
https://developer.android.com/training/basics/fragments/communicating
我的问题是,这个片段持有对 activity 的引用...当 activity 被销毁时,片段管理器是否会释放片段,从而收集片段并所以 activity?或者它们是否持有需要在 Fragment 的 onDestroy 中也被取消的强引用?
当包含片段的 activity 被销毁时,片段也会自动被销毁。检查 this
片段生命周期绑定到 Activity 之一。将 Activity 想象成宇宙,将碎片想象成行星/恒星。如果宇宙死亡,其中的恒星/行星也会死亡。同样,如果 Activity 被销毁,那么它的所有碎片也会被销毁。
official documentation(你一定要看看)解释得很好:
A fragment must always be hosted in an activity and the fragment's
lifecycle is directly affected by the host activity's lifecycle. For
example, when the activity is paused, so are all fragments in it, and
when the activity is destroyed, so are all fragments. However, while
an activity is running (it is in the resumed lifecycle state), you can
manipulate each fragment independently, such as add or remove them.
根据 Android 开发者网站,activity 与其片段通信的正确方式是通过监听器。
https://developer.android.com/training/basics/fragments/communicating
我的问题是,这个片段持有对 activity 的引用...当 activity 被销毁时,片段管理器是否会释放片段,从而收集片段并所以 activity?或者它们是否持有需要在 Fragment 的 onDestroy 中也被取消的强引用?
当包含片段的 activity 被销毁时,片段也会自动被销毁。检查 this
片段生命周期绑定到 Activity 之一。将 Activity 想象成宇宙,将碎片想象成行星/恒星。如果宇宙死亡,其中的恒星/行星也会死亡。同样,如果 Activity 被销毁,那么它的所有碎片也会被销毁。
official documentation(你一定要看看)解释得很好:
A fragment must always be hosted in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them.