Android开发 View的生命周期结合代码详解

   日期:2020-09-26     浏览:86    评论:0    
核心提示:咱们以TextView控件为例:public class TestTextView extends android.support.v7.widget.AppCompatTextView { public TestTextView(Context context) { super(context); Log.e("TestTextView","TestTextView

咱们以TextView控件为例:


public class TestTextView extends android.support.v7.widget.AppCompatTextView {
    public TestTextView(Context context) {
        super(context);
        Log.e("TestTextView","TestTextView(Context context)");
    }

    public TestTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        Log.e("TestTextView","TestTextView(Context context, @Nullable AttributeSet attrs)");
    }

    public TestTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        Log.e("TestTextView","onVisibilityChanged");
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Log.e("TestTextView","onFinishInflate");
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.e("TestTextView","onAttachedToWindow");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.e("TestTextView","onMeasure");
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.e("TestTextView","onSizeChanged");
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.e("TestTextView","onLayout");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("TestTextView","onDraw");
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        Log.e("TestTextView","onWindowVisibilityChanged="+visibility);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.e("TestTextView","onDetachedFromWindow");
    }
}

先看看默认状态下生命周期:

2020-09-23 17:34:40.882 15132-15132/com.freddy.chat E/TestTextView: TestTextView(Context context, @Nullable AttributeSet attrs)
2020-09-23 17:34:40.882 15132-15132/com.freddy.chat E/TestTextView: onFinishInflate
2020-09-23 17:34:40.927 15132-15132/com.freddy.chat E/TestTextView: onAttachedToWindow
2020-09-23 17:34:40.927 15132-15132/com.freddy.chat E/TestTextView: onWindowVisibilityChanged=0
2020-09-23 17:34:40.927 15132-15132/com.freddy.chat E/TestTextView: onVisibilityChanged
2020-09-23 17:34:40.928 15132-15132/com.freddy.chat E/TestTextView: onMeasure
2020-09-23 17:34:41.056 15132-15132/com.freddy.chat E/TestTextView: onMeasure
2020-09-23 17:34:41.056 15132-15132/com.freddy.chat E/TestTextView: onSizeChanged
2020-09-23 17:34:41.056 15132-15132/com.freddy.chat E/TestTextView: onLayout
2020-09-23 17:34:41.065 15132-15132/com.freddy.chat E/TestTextView: onDraw

接下来看看setVisibility(View.INVISIBLE)状态:

2020-09-23 17:37:14.191 15132-15132/com.freddy.chat E/TestTextView: onVisibilityChanged

再看看setVisibility(View.VISIBLE)状态:

2020-09-23 17:38:07.338 15132-15132/com.freddy.chat E/TestTextView: onVisibilityChanged
2020-09-23 17:38:07.535 15132-15132/com.freddy.chat E/TestTextView: onDraw

再看看离开Activity挂起状态:

2020-09-23 17:39:33.128 15132-15132/com.freddy.chat E/TestTextView: onWindowVisibilityChanged=8
2020-09-23 17:39:33.267 15132-15132/com.freddy.chat E/TestTextView: onVisibilityChanged

然后Activity挂起恢复:

2020-09-23 17:40:32.178 15132-15132/com.freddy.chat E/TestTextView: onWindowVisibilityChanged=4
2020-09-23 17:40:32.361 15132-15132/com.freddy.chat E/TestTextView: onVisibilityChanged
2020-09-23 17:40:32.414 15132-15132/com.freddy.chat E/TestTextView: onWindowVisibilityChanged=0
2020-09-23 17:40:32.551 15132-15132/com.freddy.chat E/TestTextView: onDraw

最后再看看Activity销毁状态:

2020-09-23 17:41:31.610 15132-15132/com.freddy.chat E/TestTextView: onWindowVisibilityChanged=8
2020-09-23 17:41:31.837 15132-15132/com.freddy.chat E/TestTextView: onDetachedFromWindow

由上可知View的生命周期是:

  1. 首先创建带两个参数的构造函数;
  2. 然后从xml文件中inflate完成后调onFinishInflate方法;
  3. 然后在Activity执行onResume之后依附Activity窗口调onAttachedToWindow方法;
  4. 然后调窗口状态发生改变调onWindowVisibilityChanged方法;
  5. 然后调view的状态发生改变调onVisibilityChanged方法;
  6. 然后开始测量view尺寸调onMeasure方法;
  7. 然后测量结果与上次测量结果不一致时调onSizeChanged方法;
  8. 然后为子view分配位置调onLayout方法;
  9. 然后view渲染绘制内容细节调onDraw方法;
  10. 最后Activity销毁时调onDetachedFromWindow方法。
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服