Android自定义View探索—生命周期
Posted 安卓笔记侠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android自定义View探索—生命周期相关的知识,希望对你有一定的参考价值。
Activity代码:
public class FiveActivity extends AppCompatActivity { private MyView myView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("log", "Activity生命周期:onCreate"); setContentView(R.layout.activity_five); initView(); } private void initView() { myView = (MyView) findViewById(R.id.my_view); } @Override protected void onStart() { super.onStart(); Log.e("log", "Activity生命周期:onStart"); } @Override protected void onResume() { super.onResume(); Log.e("log", "Activity生命周期:onResume"); } @Override protected void onRestart() { super.onRestart(); Log.e("log", "Activity生命周期:onRestart"); } @Override protected void onPause() { super.onPause(); Log.e("log", "Activity生命周期:onPause"); } @Override protected void onStop() { super.onStop(); Log.e("log", "Activity生命周期:onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.e("log", "Activity生命周期:onDestroy"); } }
自定义View代码:
public class MyView extends View { public MyView(Context context, AttributeSet attrs) { super(context, attrs); Log.e("log", "onCreate"); } @Override protected void onFinishInflate() { super.onFinishInflate(); Log.e("log", "onFinishInflate"); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); Log.e("log", "onAttachedToWindow"); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); Log.e("log", "onMeasure"); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); Log.e("log", "onSizeChanged"); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); Log.e("log", "onLayout"); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Log.e("log", "onDraw"); } @Override public void onWindowFocusChanged(boolean hasWindowFocus) { super.onWindowFocusChanged(hasWindowFocus); Log.e("log", "onWindowFocusChanged" + " " + hasWindowFocus); } }
以上是关于Android自定义View探索—生命周期的主要内容,如果未能解决你的问题,请参考以下文章