[英]Android view - onAttachedToWindow and onDetachedFromWindow - when are they called in the activity lifecycle?
I believe that onAttachedToWindow()
is called when the onCreate()
's setContentView(R.layout.myLayout.xml)
is called.我相信
onAttachedToWindow()
在调用onCreate()
的setContentView(R.layout.myLayout.xml)
时被调用。 So can I assume then that in the activity lifecycle that onDetachedFromWindow()
is called when the activity is destroyed?那么我可以假设在活动生命周期中当活动被销毁时调用
onDetachedFromWindow()
吗? My question is how do I tie these two call back hooks to the activities lifecycle?我的问题是如何将这两个回调挂钩绑定到活动生命周期?
Can I say that onAttachedToWindow()
is tied to onCreate()
and onDetachedFromWindow()
is tied to onDestroy()
?我可以说
onAttachedToWindow()
绑定到onCreate()
而onDetachedFromWindow()
绑定到onDestroy()
吗?
Technically speaking onAttachedToWindow
is called after onResume
(and it happens only once perlifecycle).从技术上讲,
onAttachedToWindow
在onResume
之后调用(并且每个生命周期只发生一次)。 ActivityThread.handleResumeActivity
call will add DecorView
to the current WindowManger
which will in turn call WindowManagerGlobal.addView()
which than traverse all the views and call onAttachedToWindow
on each view. ActivityThread.handleResumeActivity
调用会将DecorView
添加到当前WindowManger
,后者又会调用WindowManagerGlobal.addView()
,然后遍历所有视图并在每个视图上调用onAttachedToWindow
。
onDetachedFromWindow
is tied with onDestroy
onDetachedFromWindow
与onDestroy
绑定
This is not really an answer but an advice...这不是一个真正的答案,而是一个建议......
Many times, I've felt the urge to use this method (onDetachedFromWindow) to unregister observers and/or clear scopes...很多次,我都有使用这种方法 (onDetachedFromWindow) 取消注册观察者和/或清除作用域的冲动……
DO NOT DO THIS!!不要这样做!!
onDetachedFromWindow() does not equal Fragment's onDestroyView(). onDetachedFromWindow() 不等于 Fragment 的 onDestroyView()。
There is no inner method that gets called specifically when the view is destroyed (unfortunately).当视图被销毁(不幸的是)时,没有专门调用的内部方法。
onDetachFromWindow() will get called when changing the page in a ViewPager/ViewPager2, while the view is not really being destroyed.在 ViewPager/ViewPager2 中更改页面时将调用 onDetachFromWindow(),而视图并未真正被销毁。 If you use onDetachFromWindow() to clear scopes, you'll either get a NullPointerException, or the view will simply stop responding to updates when scrolling back to the page in question.
如果您使用 onDetachFromWindow() 来清除范围,您将得到一个 NullPointerException,或者视图将在滚动回有问题的页面时简单地停止响应更新。
The best and easiest thing you could do is use the onDestroyView() method to clear scopes.您可以做的最好和最简单的事情是使用 onDestroyView() 方法来清除范围。
The hardest/best way is to listen for the Fragment's LifeCycle (if you want a one time instantiated Adapter), and then dispatch an "destroyed" message through the Adapter to all views observing the Adapter and make them self-unregister themselves.... not even the DataSetObserver class is built to do this (when it should).最难/最好的方法是监听 Fragment 的生命周期(如果你想要一个一次性实例化的适配器),然后通过适配器向所有观察适配器的视图发送一个“销毁”消息,并让它们自行注销自己...... . 甚至没有构建 DataSetObserver 类来执行此操作(当它应该时)。
I find it's possible that onAttachedToWindow
will be called when setContentView
is called.我发现调用
setContentView
时可能会调用onAttachedToWindow
。
When you use split screen on Android N, and the value of configChanges
of activity in AndroidManifest.xml
to be set:当你在Android N上使用分屏时,要设置
AndroidManifest.xml
中activity的configChanges
的值:
"keyboardHidden|orientation|screenSize"
onAttachedToWindow
will be called in setContentView
, because the the variable "mAttachInfo" in the decorview of window is not null, when you call setContentView
to add rootView
to decorView
, dispatchAttachedToWindow
is called in addViewInner()
. onAttachedToWindow
会在setContentView
中调用,因为 window 的 decorview 中的变量“mAttachInfo”不为 null,当你调用setContentView
将rootView
添加到decorView
时, dispatchAttachedToWindow
会在addViewInner()
中调用。
Finally after activity onResume()
, onAttachedToWindow()
is not be called again.最后在活动
onResume()
之后,不会再次调用onAttachedToWindow()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.