View生命週期與Activity生命週期的關係

風靈使發表於2018-11-06

一、View相關的生命週期

1、Creation(建立)

Constructors(建構函式)
onFinishInflate():該方法當View及其子View從XML檔案中載入完成後會被呼叫。

2、Layout(佈局)

onMeasure(int, int):該方法在計算當前View及其所有子View尺寸大小需求時會被呼叫。
onLayout(boolean, int, int, int, int):該方法在當前View需要為其子View分配尺寸和位置時會被呼叫。
onSizeChanged(int, int, int, int):該方法在當前View尺寸變化時被呼叫。

3、Drawing(繪製)

onDraw(android.graphics.Canvas):該方法在當前View需要呈現其內容時被呼叫。

4、Event processing(事件處理)

onKeyDown(int, KeyEvent):該方法在一個物理按鍵事件發生時被呼叫。
onKeyUp(int, KeyEvent):該方法在一個物理按鍵彈起事件發生時被呼叫。
onTrackballEvent(MotionEvent):該方法在一個軌跡球運動事件發生時被呼叫。
onTouchEvent(MotionEvent):該方法在一個觸控螢幕運動事件發生時被呼叫。

5、Focus(聚焦)

onFocusChanged(boolean, int, android.graphics.Rect):該方法在當前View獲得或失去焦點時被呼叫。
onWindowFocusChanged(boolean):該方法在包含當前View的window獲得或失去焦點時被呼叫。

6、Attaching(附上)

onAttachedToWindow():該方法在當前View被附到一個window上時被呼叫。
onDetachedFromWindow():該方法在當前View從一個window上分離時被呼叫。
onVisibilityChanged(View, int):該方法在當前View或其祖先的可見性改變時被呼叫。
onWindowVisibilityChanged(int):該方法在包含當前View的window可見性改變時被呼叫。

二、 View生命週期相關方法呼叫順序

1、android:visibility=visible

TestView(Context context, AttributeSet attrs)
onFinishInflate()
onVisibilityChanged(View changedView, int visibility) visibility = 4
onVisibilityChanged(View changedView, int visibility) visibility = 0
onAttachedToWindow()
onWindowVisibilityChanged(int visibility) visibility = 0
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onSizeChanged(int w, int h, int oldw, int oldh)
onLayout(boolean changed, int left, int top, int right, int bottom) changed = true
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onSizeChanged(int w, int h, int oldw, int oldh)
onLayout(boolean changed, int left, int top, int right, int bottom) changed = true
onDraw(Canvas canvas)
onWindowFocusChanged(boolean hasWindowFocus) hasWindowFocus = true
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onLayout(boolean changed, int left, int top, int right, int bottom) changed = false
onDraw(Canvas canvas)

2、android:visibility=invisible

onVisibilityChanged(View changedView, int visibility) visibility = 4
TestView(Context context, AttributeSet attrs)
onFinishInflate()
onVisibilityChanged(View changedView, int visibility) visibility = 4
onVisibilityChanged(View changedView, int visibility) visibility = 0
onAttachedToWindow()
onWindowVisibilityChanged(int visibility) visibility = 0
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onSizeChanged(int w, int h, int oldw, int oldh)
onLayout(boolean changed, int left, int top, int right, int bottom) changed = true
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onSizeChanged(int w, int h, int oldw, int oldh)
onLayout(boolean changed, int left, int top, int right, int bottom) changed = true
onWindowFocusChanged(boolean hasWindowFocus) hasWindowFocus = true
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
onLayout(boolean changed, int left, int top, int right, int bottom) changed = false

3、android:visibility=gone

onVisibilityChanged(View changedView, int visibility) visibility = 8
TestView(Context context, AttributeSet attrs)
onFinishInflate()
onVisibilityChanged(View changedView, int visibility) visibility = 4
onVisibilityChanged(View changedView, int visibility) visibility = 0
onAttachedToWindow()
onWindowVisibilityChanged(int visibility) visibility = 0
onWindowFocusChanged(boolean hasWindowFocus) hasWindowFocus = true

4、銷燬

onWindowFocusChanged(boolean hasWindowFocus) hasWindowFocus = false
onWindowVisibilityChanged(int visibility) visibility = 8
onDetachedFromWindow()

三、View的關鍵生命週期為:

--> 構造View() 
--> onFinishInflate() 
--> onAttachedToWindow() 
--> onMeasure() 
--> onSizeChanged() 
--> onLayout() 
--> onDraw() 
--> onWindowFocusChanged() 
--> onDetackedFromWindow()

四、View的關鍵生命週期與Activity生命週期關係

Activity --> onCreate()
View     --> 構造View()
View     --> onFinishInflate()
Activity --> onStart()
Activity --> onResum()
View     --> onAttachedToWindow() 
View     --> onMeasure() 
View     --> onSizeChanged() 
View     --> onLayout() 
View     --> onDraw() 
View     --> onWindowFocusChanged()  true
Activity --> onPause()
View     --> onWindowFocusChanged()  false
Activity --> onStop()
Activity --> onDestroy()
View     --> onDetackedFromWindow()

相關文章