美文网首页
View生命周期与Activity生命周期的关系

View生命周期与Activity生命周期的关系

作者: dengzi_android | 来源:发表于2017-10-23 16:41 被阅读0次

    一、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()
    

    相关文章

      网友评论

          本文标题:View生命周期与Activity生命周期的关系

          本文链接:https://www.haomeiwen.com/subject/knlauxtx.html