Android view - onAttachedToWindow and onDetachedFromWindow - when are they called in the activity lifecycle?

I believe that onAttachedToWindow() is called when onCreate() setContentView(R.layout.myLayout.xml) . So, can I assume that in the life cycle of the action onDetachedFromWindow() is called when the action is destroyed? My question is, how do I relate these two intercepts to the life cycle of actions?

Can I say that onAttachedToWindow() bound to onCreate() and onDetachedFromWindow() bound to onDestroy() ?

+9
source share
2 answers

Technically speaking, onAttachedToWindow is called after onResume (and this happens only once perlifecycle). ActivityThread.handleResumeActivity will add a DecorView to the current WindowManger , which in turn will call WindowManagerGlobal.addView() , which will go through all the views and call onAttachedToWindow in each view.

onDetachedFromWindow contacts onDestroy

+12
source

I believe it is possible that onAttachedToWindow will be called when setContentView called.

When you use split screen on Android N and the configChanges activity value in AndroidManifest.xml should be set:

  "keyboardHidden|orientation|screenSize" 

onAttachedToWindow will be called in setContentView because the "mAttachInfo" variable in the window decor is not null when you call setContentView to add a rootView in the decorView , dispatchAttachedToWindow called in addViewInner() .

Finally, after the onResume() action, onAttachedToWindow() will not be called again.

+3
source

Source: https://habr.com/ru/post/1014821/


All Articles