Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. So can I assume then that in the activity lifecycle that onDetachedFromWindow() is called when the activity is destroyed? 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()?

like image 907
j2emanue Avatar asked Feb 09 '17 00:02

j2emanue


People also ask

When onPause is called in activity?

onPause() Called when the system is about to start resuming a previous activity. There are two possible lifecycle methods that will be called after OnPause() : OnResume() will be called if the Activity is to be returned to the foreground.

When in the activity lifecycle is onSaveInstanceState () called?

Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.

When onPause method is called in Android?

onPause. Called when the Activity is still partially visible, but the user is probably navigating away from your Activity entirely (in which case onStop will be called next). For example, when the user taps the Home button, the system calls onPause and onStop in quick succession on your Activity .

Which activity life cycle method gets called whenever a dialog opens on screen?

If a dialog is opened on the activity then the activity goes to pause state and calls onPause() method.


1 Answers

Technically speaking onAttachedToWindow is called after onResume(and it happens only once perlifecycle). 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.

onDetachedFromWindow is tied with onDestroy

like image 153
WenChao Avatar answered Sep 19 '22 16:09

WenChao