首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

Android生命周期,方便重写的时候复制

2024-12-18 来源:花图问答

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);

    }
}
显示全文