【问题标题】:fragment lifecycle: when "ondestroy" and "ondestroyview" are not called?片段生命周期:何时不调用“ondestroy”和“ondestroyview”?
【发布时间】:2013-06-19 15:46:08
【问题描述】:

想象一下这种情况:我在Pager 中有一个Fragment。我尝试切换到其他应用程序,以便拥有我的寻呼机(和我的片段)的 Activity 最终将被停止并暂时销毁。

所以,当我回到我的 Activity 时,会调用 Fragment 的回调 onCreateoncreateview 等等。但是之前没有调用 Fragment 的 onDestroy 回调!似乎在“onStop”之后片段立即被销毁。这是正常行为吗?是不是因为 Activity 没有调用它的 onDestroy 就被销毁了?

【问题讨论】:

标签: android android-fragments android-lifecycle


【解决方案1】:

看看这个问题:Why implement onDestroy() if it is not guaranteed to be called?

基本上,onDestroy() 只有在你调用 finish() 时才会被保证被调用。否则,在系统认为有必要之前,可能不会调用 onDestroy()。您可能希望将“关闭”逻辑放在 onPause() 或 onStop() 中。

【讨论】:

  • 感谢您的链接!我已经意识到这一点,但我仍然想知道 FragmentonDestroy 是否因此没有被调用。因此,您确认FragmentonDestroy 未被调用,因为它的ActivityonDestroy 也未被调用。我做对了吗?
猜你喜欢
  • 1970-01-01
  • 2014-07-06
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多