咱们以TextView控件为例:
/**
* Created by SunshineBoy on 2020/9/23.
*/
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的生命周期是:
- 首先创建带两个参数的构造函数;
- 然后从xml文件中inflate完成后调onFinishInflate方法;
- 然后在Activity执行onResume之后依附Activity窗口调onAttachedToWindow方法;
- 然后调窗口状态发生改变调onWindowVisibilityChanged方法;
- 然后调view的状态发生改变调onVisibilityChanged方法;
- 然后开始测量view尺寸调onMeasure方法;
- 然后测量结果与上次测量结果不一致时调onSizeChanged方法;
- 然后为子view分配位置调onLayout方法;
- 然后view渲染绘制内容细节调onDraw方法;
- 最后Activity销毁时调onDetachedFromWindow方法。
记得关注我➕更多原创干货分享随时更新,有问题评论留言讨论
源码请去博客预览网址链接如下
Android开发 View的生命周期结合代码详解blog.csdn.net