片段生命周期:当“销毁"时和“ondestroyview"不叫? [英] fragment lifecycle: when "ondestroy" and "ondestroyview" are not called?

查看:47
本文介绍了片段生命周期:当“销毁"时和“ondestroyview"不叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Imagine this scenario: I have a Fragment in a Pager. I try to switch to other apps, so that the Activity owning my pager (and my fragment) will be stopped and temporarily destroyed, eventually.

所以,当我回到我的 Activity 时,会调用 Fragment 的回调 onCreateoncreateview 等等.但是之前没有调用过 Fragment 的 onDestroy 回调!似乎在onStop"之后片段立即被销毁.这是正常行为吗?是否可能是因为 Activity 在没有调用其 onDestroy 的情况下被销毁了?

So, when I come back to my Activity, the Fragment's callbacks onCreate, oncreateview and so forth are called. But none of the Fragment's onDestroy callbacks were called before! It seems that after "onStop" the fragment is destroyed at once. Is it a normal behavior? Is it maybe because the Activity is destroyed without a call to its onDestroy?

推荐答案

看看这个问题:如果不能保证被调用,为什么要实现onDestroy()?

基本上,只有在调用finish() 时才能保证调用onDestroy().否则,在系统认为有必要之前,可能不会调用 onDestroy().您可能需要考虑将关闭"逻辑放在 onPause() 或 onStop() 中.

Basically, onDestroy() is only guaranteed to be called if you call finish(). Otherwise, onDestroy() may not be called until the system deems it necessary. You might want to look at putting your "closing" logic in the onPause() or onStop() instead.

这篇关于片段生命周期:当“销毁"时和“ondestroyview"不叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆