简体   繁体   English

Android 视图 - onAttachedToWindow 和 onDetachedFromWindow - 它们在活动生命周期中何时被调用?

[英]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).从技术上讲, onAttachedToWindowonResume之后调用(并且每个生命周期只发生一次)。 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 onDetachedFromWindowonDestroy绑定

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,当你调用setContentViewrootView添加到decorView时, dispatchAttachedToWindow会在addViewInner()中调用。

Finally after activity onResume() , onAttachedToWindow() is not be called again.最后在活动onResume()之后,不会再次调用onAttachedToWindow()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 详细的android活动生命周期(onAttachedToWindow()) - Detailed android activity lifecycle (onAttachedToWindow()) 颤抖相当于`View :: onAttachedToWindow`和`View :: onDetachedFromWindow` - Flutter equivalents to `View::onAttachedToWindow` and `View::onDetachedFromWindow` Android 活动生命周期 - 何时调用“onPause”? - Android Activity LifeCycle - when 'onPause' is called? onDetachedFromWindow永远不会调用视图 - onDetachedFromWindow Never get Called for a View Android:在Activity生命周期中何时调用onCreateOptionsMenu? - Android: When is onCreateOptionsMenu called during Activity lifecycle? 相当于 RecyclerView 中 ViewHolder 的 onAttachedToWindow()/onDetachedFromWindow() - Equivalent of onAttachedToWindow()/onDetachedFromWindow() for a ViewHolder in RecyclerView Android Activity Lifecycle在启动新Activity时调用两次 - Android Activity Lifecycle called twice when starting new Activity Android是否可以在调用onAttachedToWindow()时隐藏状态栏? - Android Can we hide status bar when onAttachedToWindow() is called? 何时在Layouting Process中调用onAttachedToWindow - When is onAttachedToWindow called in the Layouting Process Android活动生命周期-在onStop()之后和调用onDestory()时会发生什么 - Android activity lifecycle - what happens after onStop() and when onDestory() is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM