简叙 Activity 生命周期及android 返回按钮捕捉

  @Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onStart() {
System.out.println("---------------onStart---------------");
super.onStart(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onRestart() {
System.out.println("---------------onRestart---------------");
super.onRestart(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onResume() {
System.out.println("---------------onResume---------------");
super.onResume(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onPostResume() {
System.out.println("---------------onPostResume---------------");
super.onPostResume(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onPause() {
System.out.println("---------------onPause---------------");
super.onPause(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onStop() {
System.out.println("---------------onStop---------------");
super.onStop(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onDestroy() {
System.out.println("---------------onDestroy---------------");
super.onDestroy(); //To change body of overridden methods use File | Settings | File Templates.
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode== KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
Toast.makeText(_context,"点击了返回【onKeyDown】",Toast.LENGTH_LONG).show();
System.out.println("---------------onKeyDown---------------");
this.onBackPressed();
return true; //确认返回了
}
return false; //不允许返回
}

@Override
public void onBackPressed() {

Toast.makeText(_context,"点击了返回【onBackPressed】",Toast.LENGTH_LONG).show();
System.out.println("---------------onBackPressed---------------");
super.onBackPressed();
}

简单的说,

   当activity 启动的时候会启动以下方法,按先后顺序

onCreate->onStart->onResume->onPostResume

 

当按返回的时候,也就是结束这个Activity的时候,先后顺序是:

 

onPase->onStop->onDestroy

你可能感兴趣的:(Activity)