Activity七个基本的生命周期方法:onCreate、onStart、onResume、onPause、onStop、onDestory,这六个生命周期方法两两相互对应,外加一个onRestart方法。
自定义View时,常需要复写的方法:onMeasure、onLayout、onDraw以及onSizeChanged、onFinishInflate。
structure=>operation: 构造方法
onAttachToWin=>operation: onAttachToWindow
onMeasure1=>operation: onMeasure
onSizeChanged=>operation: onSizeChanged
onLayout1=>operation: onLayout
onMeasure2=>operation: onMeasure
onLayout2=>operation: onLayout
onDraw=>operation: onDraw
onDetachedFromWindow=>operation: onDetachedFromWindow
structure->onAttachToWin->onMeasure1->onSizeChanged->onLayout1->onMeasure2->onLayout2->onDraw->onDetachedFromWindow
先蛮测下呗,在MainActivity中给七个生命周期方法打log;自定义FrameLayout和TextView的子类,即CustomViewGroup和CustomView,仅仅给五个生命周期方法打log;这两个自定义View直接放到Activity的xml布局中:
生命周期的执行流程
启动Activity
| 顺序 |
MainActivity |
CustonViewGroup |
CustomView |
| 1 |
onCreate before setContentView |
|
|
| 2 |
|
CustonViewGroup(ctx, attrs) |
|
| 3 |
|
|
CustomView(ctx, attrs) |
| 4 |
|
|
onFinishInflate |
| 5 |
|
onFinishInflate |
|
| 6 |
onCreate after setContentView |
|
|
| 7 |
onStart |
|
|
| 8 |
onResume |
|
|
| 9 |
|
|
onMeasure |
| 10 |
|
onMeasure |
|
| 11 |
|
|
onMeasure |
| 12 |
|
onMeasure |
|
| 13 |
|
|
onMeasure |
| 14 |
|
onMeasure |
|
| 15 |
|
|
onMeasure |
| 16 |
|
onMeasure |
|
| 17 |
|
onSizeChanged |
|
| 18 |
|
|
onSizeChanged |
| 19 |
|
|
onLayout |
| 20 |
|
onLayout |
|
| 21 |
|
|
onMeasure |
| 22 |
|
onMeasure |
|
| 23 |
|
|
onMeasure |
| 24 |
|
onMeasure |
|
| 25 |
|
|
onLayout |
| 26 |
|
onLayout |
|
| 27 |
|
onDraw |
|
| 28 |
|
|
onDraw |
按下回退键
| 顺序 |
MainActivity |
CustonViewGroup |
CustomView |
| 1 |
onPause |
|
|
| 2 |
onStop |
|
|
| 3 |
onDestroy |
|
|
重新进入MainActivity并按下Home键
| 顺序 |
MainActivity |
CustonViewGroup |
CustomView |
| 1 |
onPause |
|
|
| 2 |
onStop |
|
|
回到MainActivity
| 顺序 |
MainActivity |
CustonViewGroup |
CustomView |
| 1 |
onRestart |
|
|
| 2 |
onStart |
|
|
| 3 |
onResume |
|
|
| 4 |
|
onDraw |
|
| 5 |
|
|
onDraw |
onFinishInflate
| 顺序 |
CustonViewGroup |
CustomView |
| 1 |
|
onFinishInflate |
| 2 |
onFinishInflate before |
|
| 3 |
onFinishInflate after |
|
onMeasure
| 顺序 |
CustonViewGroup |
CustomView |
| 1 |
onMeasure before |
|
| 2 |
|
onMeasure |
| 3 |
onMeasure after |
|
onSizeChanged和onLayout
| 顺序 |
CustonViewGroup |
CustomView |
| 1 |
onSizeChanged before |
|
| 2 |
onSizeChanged after |
|
| 3 |
onLayout before |
|
| 4 |
|
onSizeChanged |
| 5 |
|
onLayout |
| 6 |
onLayout after |
|
onDraw
| 顺序 |
CustonViewGroup |
CustomView |
| 1 |
onDraw before |
|
| 2 |
onDraw after |
|
| 3 |
|
onDraw |