首页 > 代码库 > Android自定义View探索(一)—生命周期
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探索(一)—生命周期
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。