Android View的生命周期

大家都知道Activity的生命周期,那view的生命周期呢?除了常见的onMeasure,onLayout,onDraw,这些和Activity中的onCreate、onStart、onResume的执行先后顺序是什么呢?
View的生命周期从构造方法开始依次执行以下几个方法,该View的可见性为默认值时:

一、生命周期方法
(0)Constructors()

View在代码中被创建时调用第一种构造方法,View从layout中加载出来时会被调用第二种构造方法,其中XML中的属性也会被解析。

(1)onFinishInflate()

该方法当View及其子View从XML文件中加载完成后触发调用。通常是在Activity中的onCreate方法调用后调用。

(2)onAttachedToWindow()

当View被附着到一个窗口时触发。在Activity第一次执行完onResume方法后被调用。
(3)onWindowVisibilityChanged()

包含当前View的Window可见性改变时被调用。

(4)onVisibilityChanged()

该方法在当前View或其祖先的可见性改变时被调用。如果View状态不可见或者GONE,该方法会第一个被调用。

(5)onMeasure()

该方法确定View以及其子View尺寸大小时被调用。

(6)onSizeChanged()

该方法在Measure方法之后且测量大小与之前不一样的时候被调用。

(7)onLayout()

该方法在当前View需要为其子View分配尺寸和位置时会被调用。

(8)onDraw(Canvas)

该方法用于View渲染内容的细节。

(9)onWindowFocusChanged()

该方法也可能在绘制过程中被调用,具体是在包含当前View的Window获得或失去焦点时被调用。此时可以设置代码中定义的View的一些LayoutParameter。

如果View进入了销毁阶段,肯定是会被调用的。

(10)onDetachedFromWindow()

当View离开附着的窗口时触发,比如在Activity调用onDestroy方法时View就会离开窗口。和一开始的AttachedToWindow相对,都只会被调用一次。
代码如下:

package com.example.testviewlifecycle;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class CustomView extends View {
private static final String TAG = “CustomView=–>”;
private Paint mPaint;

public CustomView(Context context) {
    super(context);
    Log.d(TAG, "view Constructors 1");
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    Log.d(TAG, "view Constructors 2");
}

public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    Log.d(TAG, "view Constructors 3");
}

private void init() {
    mPaint = new Paint();
    mPaint.setColor(Color.RED);
    mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mPaint.setTextAlign(Paint.Align.CENTER);
    mPaint.setAntiAlias(true);
    mPaint.setTextSize(36);
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    Log.d(TAG, "onFinishInflate");
}

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

@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    Log.d(TAG, "onFocusChanged");
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    Log.d(TAG, "onAttachedToWindow");
}

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    Log.d(TAG, "onDetachedFromWindow");
}

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

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

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Log.d(TAG, "onDraw");
    canvas.drawText("view生命周期", 100, 400, mPaint);
}

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

@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
    super.onWindowFocusChanged(hasWindowFocus);
    Log.d(TAG, "onWindowFocusChanged");
}

@Override
protected void onWindowVisibilityChanged(int visibility) {
    super.onWindowVisibilityChanged(visibility);
    Log.d(TAG, "onWindowVisibilityChanged");
}

}


这里默认的customView是visible的,和Activity的生命周期结果如下
1)、android:visibility=“visible”
创建过程

2020-03-10 14:38:06.542 7961-7961/com.example.testviewlifecycle D/MainActivity=–>: onCreate 1
2020-03-10 14:38:06.564 7961-7961/com.example.testviewlifecycle D/CustomView=–>: view Constructors 2
2020-03-10 14:38:06.564 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onFinishInflate
2020-03-10 14:38:06.566 7961-7961/com.example.testviewlifecycle D/MainActivity=–>: onCreate 2
2020-03-10 14:38:06.569 7961-7961/com.example.testviewlifecycle D/MainActivity=–>: onStart
2020-03-10 14:38:06.572 7961-7961/com.example.testviewlifecycle D/MainActivity=–>: onResume
2020-03-10 14:38:06.589 7961-7961/com.example.testviewlifecycle D/MainActivity=–>: onAttachedToWindow
2020-03-10 14:38:06.590 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onAttachedToWindow
2020-03-10 14:38:06.590 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onWindowVisibilityChanged
2020-03-10 14:38:06.590 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onVisibilityChanged
2020-03-10 14:38:06.593 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onMeasure
2020-03-10 14:38:06.594 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onMeasure
2020-03-10 14:38:06.606 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onMeasure
2020-03-10 14:38:06.606 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onMeasure
2020-03-10 14:38:06.606 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onSizeChanged
2020-03-10 14:38:06.606 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onLayout
2020-03-10 14:38:06.608 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onDraw
2020-03-10 14:38:06.621 7961-7961/com.example.testviewlifecycle D/CustomView=–>: onWindowFocusChanged


销毁过程

2020-03-10 14:41:24.684 9188-9188/com.example.testviewlifecycle D/MainActivity=–>: onPause
2020-03-10 14:41:24.694 9188-9188/com.example.testviewlifecycle D/CustomView=–>: onWindowFocusChanged
2020-03-10 14:41:25.237 9188-9188/com.example.testviewlifecycle D/CustomView=–>: onWindowVisibilityChanged
2020-03-10 14:41:25.252 9188-9188/com.example.testviewlifecycle D/MainActivity=–>: onStop
2020-03-10 14:41:25.255 9188-9188/com.example.testviewlifecycle D/CustomView=–>: onVisibilityChanged
2020-03-10 14:41:25.258 9188-9188/com.example.testviewlifecycle D/MainActivity=–>: onDestroy
2020-03-10 14:41:25.259 9188-9188/com.example.testviewlifecycle D/CustomView=–>: onDetachedFromWindow
2020-03-10 14:41:25.260 9188-9188/com.example.testviewlifecycle D/MainActivity=–>: onDetachedFromWindow


2)、android:visibility=“invisible”不可见时
创建

2020-03-10 14:42:54.333 9649-9649/? D/MainActivity=–>: onCreate 1
2020-03-10 14:42:54.404 9649-9649/? D/CustomView=–>: view Constructors 2
2020-03-10 14:42:54.404 9649-9649/? D/CustomView=–>: onFinishInflate
2020-03-10 14:42:54.413 9649-9649/? D/MainActivity=–>: onCreate 2
2020-03-10 14:42:54.417 9649-9649/? D/MainActivity=–>: onStart
2020-03-10 14:42:54.422 9649-9649/? D/MainActivity=–>: onResume
2020-03-10 14:42:54.480 9649-9649/? D/MainActivity=–>: onAttachedToWindow
2020-03-10 14:42:54.480 9649-9649/? D/CustomView=–>: onAttachedToWindow
2020-03-10 14:42:54.480 9649-9649/? D/CustomView=–>: onWindowVisibilityChanged
2020-03-10 14:42:54.480 9649-9649/? D/CustomView=–>: onVisibilityChanged
2020-03-10 14:42:54.488 9649-9649/? D/CustomView=–>: onMeasure
2020-03-10 14:42:54.489 9649-9649/? D/CustomView=–>: onMeasure
2020-03-10 14:42:54.511 9649-9649/? D/CustomView=–>: onMeasure
2020-03-10 14:42:54.511 9649-9649/? D/CustomView=–>: onMeasure
2020-03-10 14:42:54.511 9649-9649/? D/CustomView=–>: onSizeChanged
2020-03-10 14:42:54.511 9649-9649/? D/CustomView=–>: onLayout
2020-03-10 14:42:54.638 9649-9649/? D/CustomView=–>: onWindowFocusChanged


销毁

2020-03-10 14:43:50.009 9649-9649/com.example.testviewlifecycle D/MainActivity=–>: onPause
2020-03-10 14:43:50.019 9649-9649/com.example.testviewlifecycle D/CustomView=–>: onWindowFocusChanged
2020-03-10 14:43:50.557 9649-9649/com.example.testviewlifecycle D/CustomView=–>: onWindowVisibilityChanged
2020-03-10 14:43:50.578 9649-9649/com.example.testviewlifecycle D/MainActivity=–>: onStop
2020-03-10 14:43:50.579 9649-9649/com.example.testviewlifecycle D/CustomView=–>: onVisibilityChanged
2020-03-10 14:43:50.582 9649-9649/com.example.testviewlifecycle D/MainActivity=–>: onDestroy
2020-03-10 14:43:50.583 9649-9649/com.example.testviewlifecycle D/CustomView=–>: onDetachedFromWindow
2020-03-10 14:43:50.584 9649-9649/com.example.testviewlifecycle D/MainActivity=–>: onDetachedFromWindow


3)、android:visibility=”gone”
创建

2020-03-10 14:45:03.585 9921-9921/com.example.testviewlifecycle D/MainActivity=–>: onCreate 1
2020-03-10 14:45:03.620 9921-9921/com.example.testviewlifecycle D/CustomView=–>: view Constructors 2
2020-03-10 14:45:03.620 9921-9921/com.example.testviewlifecycle D/CustomView=–>: onFinishInflate
2020-03-10 14:45:03.621 9921-9921/com.example.testviewlifecycle D/MainActivity=–>: onCreate 2
2020-03-10 14:45:03.623 9921-9921/com.example.testviewlifecycle D/MainActivity=–>: onStart
2020-03-10 14:45:03.626 9921-9921/com.example.testviewlifecycle D/MainActivity=–>: onResume
2020-03-10 14:45:03.647 9921-9921/com.example.testviewlifecycle D/MainActivity=–>: onAttachedToWindow
2020-03-10 14:45:03.648 9921-9921/com.example.testviewlifecycle D/CustomView=–>: onAttachedToWindow
2020-03-10 14:45:03.648 9921-9921/com.example.testviewlifecycle D/CustomView=–>: onWindowVisibilityChanged
2020-03-10 14:45:03.648 9921-9921/com.example.testviewlifecycle D/CustomView=–>: onVisibilityChanged
2020-03-10 14:45:03.680 9921-9921/com.example.testviewlifecycle D/CustomView=–>: onWindowFocusChanged


销毁

2020-03-10 14:45:34.421 9921-9921/com.example.testviewlifecycle D/MainActivity=–>: onPause
2020-03-10 14:45:34.431 9921-9921/com.example.testviewlifecycle D/CustomView=–>: onWindowFocusChanged
2020-03-10 14:45:34.978 9921-9921/com.example.testviewlifecycle D/CustomView=–>: onWindowVisibilityChanged
2020-03-10 14:45:34.997 9921-9921/com.example.testviewlifecycle D/MainActivity=–>: onStop
2020-03-10 14:45:34.998 9921-9921/com.example.testviewlifecycle D/CustomView=–>: onVisibilityChanged
2020-03-10 14:45:35.001 9921-9921/com.example.testviewlifecycle D/MainActivity=–>: onDestroy
2020-03-10 14:45:35.002 9921-9921/com.example.testviewlifecycle D/CustomView=–>: onDetachedFromWindow
2020-03-10 14:45:35.003 9921-9921/com.example.testviewlifecycle D/MainActivity=–>: onDetachedFromWindow


总结
1、view的创建过程是调用带有两个参数的构造函数,当然,如果该View不是在layout中定义的话,会调用一个参数的构造函数。
2、从XMl文件中inflate完成(onFinishInflate())。
3、将View加到window中(View是gone的,那么View创建生命周期也就结束)。
4、测量view的长宽(onMeasure())。
5、定位View 在父View中的位置(onLayout()),若View是invisible,则View的创建生命周期结束。
6、绘制View的content(onDraw()),只有可见的View才在window中绘制。
7、View的销毁流程和可见性没有关系。

在Activity的onCreate方法中加载View,View的onFinishInflate会被调用,继而Activity的生命周期执行到onResume方法之后View才被附着到窗口上,继而进行绘制工作,onMeasure、onSizeChanged 、onLayout、onDraw。这几个方法可能由于setVisible或onResume被调用多次,最后是Window失去焦点后的销毁阶段。

阅读全文
资源下载
下载价格免费
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.dandroid.cn/archives/16820,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?