View 生命周期分析

View 生命周期

View生命周期函数意义
onFinishInflateView中所有的子元素均被映射成xml时触发
onAttachedToWindow该方法在当前View被附到一个Window上时被调用
onMeasureView确定所有子元素的大小
onSizeChanged当View大小发生变化时触发
onLayout当View分配所有子元素的位置和大小时触发
onDrawView渲染内容细节
onWindowFocusChanged当窗口包含的view获取或失去焦点时触发
onDetachedFromWindow当View离开被附着的窗口时触发

View和Activity生命周期关系

// Activity A 启动:
A:onCreate
V:onFinishInflate
A:onStart
A:onResume
V:onAttachedToWindow
V:onMeasure
V:onSizeChanged
V:onLayout
V:onDraw
A:onWindowFocusChanged
V:onWindowFocusChanged

// Activity A 跳转 Activity B
A:onPause
A: onWindowFocusChanged
V:onWindowFocusChanged
A: onStop
V:onSaveInstanceState

// Activity B 返回 Activity A
A:onReStart
A: onStart
A:onResume
V : onDraw
V:onWindowFocusChanged

// Activity A 横竖屏切换
A: onConfigurationChanged
V:onConfigurationChanged
V: onMeasure
V:onMeasure
V: onLayout
V: onDraw

// Activity A 结束
A:onPause
A: onWindowFocusChanged
V:onWindowFocusChanged
A:onStop
A:onDestroy
V:onDetachedFromWindow
  • 注意:
    • Activity执行onCreate之后初始化View,View执行onFinishInflate
    • 在Activity A 执行onResume之后,View进行绘制并处于在进程的前台模式获取焦点onAttachedToWindow->onMeasure->onSizeChanged->onLayout->onDraw->onWindowFocusChanged
    • 当Activity A 跳转 Activity B 时, view 在onStop之后保存状态(Activity跳转的过程是在同一个线程内同步执行的,所以在onStop之后对View的状态进行保存避免因为保存状态时间过长导致切换到B时的卡顿),view由进程的前台模式转变为可见模式,丢失焦点执行onWindowFocusChanged
    • Activity B 返回A时。View会重新走onDraw、onWindowFocusChanged 绘制并由进程的背景模式转为可见模式再转为前台模式重新获取焦点,再次执行onWindowFocusChanged
    • Activity A 结束时,进程由前台模式变为背景模式,view丢失焦点,在Activity执行onDestroy()之后,view执行onDetachedFromWindow,离开窗口
    • View 的onAttachedToWindow与onDetachedFromWindow在view的创建与销毁过程中分别只会调
      用一次,而onMeasure,onSizeChanged,onLayout,onDraw,onWindowFocusChanged会随着当前Activity或者Fragment的生命周期的改变,影响进程的模式而会被多次调用

实践

@SuppressLint("AppCompatCustomView")
class CustomTextView : TextView {
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, @Nullable attrs: AttributeSet?) : super(context, attrs) {}


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        Log.d(TAG, "onMeasure")
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }


    override fun onDraw(canvas: Canvas?) {
        Log.d(TAG, "onDraw")
        super.onDraw(canvas)
    }


    override fun onSaveInstanceState(): Parcelable? {
        Log.d(TAG, "onSaveInstanceState")
        return super.onSaveInstanceState()
    }


    override fun dispatchSaveInstanceState(container: SparseArray<Parcelable>?) {
        Log.d(TAG, "dispatchSaveInstanceState")
        super.dispatchSaveInstanceState(container)
    }


    override fun dispatchRestoreInstanceState(container: SparseArray<Parcelable>?) {
        Log.d(TAG, "dispatchRestoreInstanceState")
        super.dispatchRestoreInstanceState(container)
    }


    override fun onRestoreInstanceState(state: Parcelable?) {
        Log.d(TAG, "onRestoreInstanceState")
        super.onRestoreInstanceState(state)
    }


    override fun onLayout(
        changed: Boolean,
        left: Int,
        top: Int,
        right: Int,
        bottom: Int
    ) {
        Log.d(TAG, "onLayout")
        super.onLayout(changed, left, top, right, bottom)
    }


    override fun onAttachedToWindow() {
        Log.d(TAG, "onAttachedToWindow")
        super.onAttachedToWindow()
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        Log.d(TAG, "onSizeChanged")
        super.onSizeChanged(w, h, oldw, oldh)
    }


    override fun onFinishInflate() {
        Log.d(TAG, "onFinishInflate")
        super.onFinishInflate()
    }
    
    override fun onConfigurationChanged(newConfig: Configuration?) {
        Log.d(TAG, "onConfigurationChanged")
        super.onConfigurationChanged(newConfig)
    }

    companion object {
        private const val TAG = "MainActivityWJXView"
    }
}
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d(TAG, "onCreate")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn1.setOnClickListener{
            startActivity(Intent(this, MainActivity2::class.java))
        }
    }


    override fun onRestart() {
        Log.d(TAG, "onReStart")
        super.onRestart()
    }
    override fun onStart() {
        Log.d(TAG, "onStart")
        super.onStart()
    }


    override fun onResume() {
        Log.d(TAG, "onResume")
        super.onResume()
    }


    override fun onWindowFocusChanged(hasFocus: Boolean) {
        Log.d(TAG, "onWindowFocusChanged")
        super.onWindowFocusChanged(hasFocus)
    }


    override fun onPause() {
        Log.d(TAG, "onPause")
        super.onPause()
    }


    override fun onStop() {
        Log.d(TAG, "onStop")
        super.onStop()
    }


    override fun onDestroy() {
        Log.d(TAG, "onDestroy")
        super.onDestroy()
    }


    override fun onDetachedFromWindow() {
        Log.d(TAG, "onDetachedFromWindow")
        super.onDetachedFromWindow()
    }
    
    override fun onConfigurationChanged(newConfig: Configuration?) {
        Log.d(TAG, "onConfigurationChanged")
        super.onConfigurationChanged(newConfig)
    }

    companion object {
        private const val TAG = "MainActivityWJX"
    }
}
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页